www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > MInvoice.java
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.io.*;
import org.compiere.util.*;
import org.compiere.print.*;
/**
* Invoice Model
*
* @author Jorg Janke
* @version $Id: MInvoice.java,v 1.6 2003/04/28 04:18:58 jjanke Exp $
*/
public class MInvoice extends PO
{
/**
* Invoice Constructor
* @param ctx context
* @param C_Invoice_ID invoice or 0 for new
*/
public MInvoice (Properties ctx, int C_Invoice_ID)
{
super (ctx, C_Invoice_ID);
if (C_Invoice_ID == 0)
{
setDocStatus ("DR"); // Draft
setDocAction ("CO");
setPaymentRule("P"); // 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);
setPosted(false);
setProcessed (false);
}
} // MInvoice
/**
* Load Constructor
* @param ctx context
* @param rs result set record
*/
public MInvoice (Properties ctx, ResultSet rs)
{
super (ctx, rs);
} // MOrder
public static final String DocBaseType_ARI = "ARI";
public static final String DocBaseType_API = "API";
/** Open Amount */
private BigDecimal m_openAmt = null;
/** Invoice Lines */
private MInvoiceLine[] m_lines;
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
protected POInfo initPO (Properties ctx)
{
int AD_Table_ID = 318;
POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
return poi;
} // initPO
/**
* 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 = bp.getPaymentRule();
if (ss != null)
setPaymentRule(ss);
// Set Locations
MBPartner_Location[] locs = bp.getLocations();
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.error("setBPartner - Has no To Address: " + bp);
// Set Contact
MBPartner_Contact[] contacts = bp.getContacts();
if (contacts != null && contacts.length == 1)
setC_BPartner_Contact_ID(contacts[0].getC_BPartner_Contact_ID());
} // setBPartner
/**
* Set Target Document Type
* @param DocBaseType doc type
*/
public void setC_DocTypeTarget_ID (String DocBaseType)
{
int C_DocType_ID = 0;
//
String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType=? ORDER BY IsDefault DESC";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getAD_Org_ID());
pstmt.setString(2, DocBaseType);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
C_DocType_ID = rs.getInt(1);
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("setC_DocTypeTarget_ID", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
if (C_DocType_ID == 0)
log.error("setC_DocTypeTarget_ID - Not found for AC_Client_ID=" + getAD_Client_ID() + " - " + DocBaseType);
else
{
log.debug ("setC_DocTypeTarget_ID - " + DocBaseType);
setC_DocTypeTarget_ID (C_DocType_ID);
setIsSOTrx (DocBaseType_ARI.equals(DocBaseType));
}
} // setC_DocTypeTarget_ID
/**
* Set Defaults for mandatory values where not set yet
*/
private void setDefaults ()
{
log.debug("setDefaults");
int AD_Client_ID = getAD_Client_ID();
// No Partner Info - set Template
if (getC_BPartner_ID() == 0)
setBPartner(MBPartner.getTemplate(getCtx(), AD_Client_ID));
if (getC_BPartner_Location_ID() == 0)
setBPartner(new MBPartner(getCtx(), getC_BPartner_ID()));
// Price List
if (getM_PriceList_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#M_PriceList_ID");
if (ii != 0)
setM_PriceList_ID(ii);
else
{
String sql = "SLECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'";
ii = DB.getSQLValue (sql, AD_Client_ID);
if (ii != 0)
setM_PriceList_ID (ii);
}
}
// Currency
if (getC_Currency_ID() == 0)
{
String sql = "SLECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?";
int ii = DB.getSQLValue (sql, getM_PriceList_ID());
if (ii != 0)
setC_Currency_ID (ii);
else
setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID"));
}
// Sales Rep
if (getSalesRep_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#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() ? DocBaseType_ARI : DocBaseType_API);
// Payment Term
if (getC_PaymentTerm_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID");
if (ii != 0)
setC_PaymentTerm_ID (ii);
else
{
String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'";
ii = DB.getSQLValue(sql, AD_Client_ID);
if (ii != 0)
setC_PaymentTerm_ID (ii);
}
}
} // setDefaults
/**
* Get Invoice Lines
* @return lines
*/
public MInvoiceLine[] getLines()
{
if (m_lines == null || m_lines.length == 0)
;
else
return m_lines;
//
ArrayList list = new ArrayList();
String sql = "SELECT * FROM C_InvoiceLine WHERE C_Invoice_ID=? ORDER BY Line";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getC_Invoice_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
list.add(new MInvoiceLine(getCtx(), rs));
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("getLines", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
//
m_lines = new MInvoiceLine[list.size()];
list.toArray(m_lines);
return m_lines;
} // getLines
/*************************************************************************/
/**
* Save Invoice
* @return true if saved
*/
public boolean save ()
{
log.debug ("save");
setDefaults();
if (getDocumentNo() == null)
{
String DocumentNo = DB.getDocumentNo (getAD_Client_ID (), getC_DocTypeTarget_ID ());
setDocumentNo (DocumentNo);
}
return super.save ();
} // save
public String toString ()
{
StringBuffer sb = new StringBuffer ("MInvoice[")
.append(getID())
.append ("]");
return sb.toString ();
}
public void setC_Activity_ID (int C_Activity_ID)
{
setValue ("C_Activity_ID", new Integer (C_Activity_ID));
}
public int getC_Activity_ID ()
{
Integer ii = (Integer)getValue ("C_Activity_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setC_BPartner_Location_ID (int C_BPartner_Location_ID)
{
setValue ("C_BPartner_Location_ID", new Integer (C_BPartner_Location_ID));
}
public int getC_BPartner_Location_ID ()
{
Integer ii = (Integer)getValue ("C_BPartner_Location_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public int getC_Invoice_ID ()
{
return getID();
}
public void setC_BPartner_Contact_ID (int C_BPartner_Contact_ID)
{
setValue ("C_BPartner_Contact_ID", new Integer (C_BPartner_Contact_ID));
}
public int getC_BPartner_Contact_ID ()
{
Integer ii = (Integer)getValue ("C_BPartner_Contact_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setDateAcct (Timestamp DateAcct)
{
if (DateAcct == null)
throw new IllegalArgumentException ("DateAcct is mandatory");
setValue ("DateAcct", DateAcct);
}
public Timestamp getDateAcct ()
{
return (Timestamp)getValue ("DateAcct");
}
void setDateOrdered (Timestamp DateOrdered)
{
setValueNoCheck ("DateOrdered", DateOrdered);
}
public Timestamp getDateOrdered ()
{
return (Timestamp)getValue ("DateOrdered");
}
/**
* 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);
pstmt.setInt(1, M_PriceList_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
setValue ("M_PriceList_ID", new Integer(rs.getInt(1)));
setC_Currency_ID (rs.getInt(2));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("setM_PriceList_ID", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
}
public int getM_PriceList_ID ()
{
Integer ii = (Integer)getValue ("M_PriceList_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setC_Order_ID (int C_Order_ID)
{
setValueNoCheck ("C_Order_ID", new Integer (C_Order_ID));
}
public int getC_Order_ID ()
{
Integer ii = (Integer)getValue ("C_Order_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setC_Campaign_ID (int C_Campaign_ID)
{
setValue ("C_Campaign_ID", new Integer (C_Campaign_ID));
}
public int getC_Campaign_ID ()
{
Integer ii = (Integer)getValue ("C_Campaign_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setDocStatus (String DocStatus)
{
if (DocStatus.equals ("??") || DocStatus.equals ("AP")
|| DocStatus.equals ("CH") || DocStatus.equals ("CL")
|| DocStatus.equals ("CO") || DocStatus.equals ("DR")
|| DocStatus.equals ("IN") || DocStatus.equals ("IP")
|| DocStatus.equals ("NA") || DocStatus.equals ("PE")
|| DocStatus.equals ("PO") || DocStatus.equals ("PR")
|| DocStatus.equals ("RE") || DocStatus.equals ("TE")
|| DocStatus.equals ("TR") || DocStatus.equals ("VO")
|| DocStatus.equals ("WP") || DocStatus.equals ("XX"))
;
else
throw new IllegalArgumentException ("DocStatus Invalid value - Reference_ID=131 - ?? - AP - CH - CL - CO - DR - IN - IP - NA - PE - PO - PR - RE - TE - TR - VO - WP - XX");
if (DocStatus == null)
throw new IllegalArgumentException ("DocStatus is mandatory");
setValueNoCheck ("DocStatus", DocStatus);
}
public String getDocStatus ()
{
return (String)getValue ("DocStatus");
}
void setDocAction (String DocAction)
{
if (DocAction.equals ("--") || DocAction.equals ("AP")
|| DocAction.equals ("CL") || DocAction.equals ("CO")
|| DocAction.equals ("PO") || DocAction.equals ("PR")
|| DocAction.equals ("RA") || DocAction.equals ("RC")
|| DocAction.equals ("RE") || DocAction.equals ("RJ")
|| DocAction.equals ("TR") || DocAction.equals ("VO")
|| DocAction.equals ("XL"))
;
else
throw new IllegalArgumentException ("DocAction Invalid value - Reference_ID=135 - __ - AP - CL - CO - PO - PR - RA - RC - TE - RJ - VO - XL");
if (DocAction == null)
throw new IllegalArgumentException ("DocAction is mandatory");
setValueNoCheck ("DocAction", DocAction);
}
public String getDocAction ()
{
return (String)getValue ("DocAction");
}
void setGrandTotal (BigDecimal GrandTotal)
{
if (GrandTotal == null)
throw new IllegalArgumentException ("GrandTotal is mandatory");
setValueNoCheck ("GrandTotal", GrandTotal);
}
public BigDecimal getGrandTotal ()
{
BigDecimal bd = (BigDecimal)getValue ("GrandTotal");
if (bd == null)
return Env.ZERO;
return bd;
}
public void setDescription (String Description)
{
setValue ("Description", Description);
}
public String getDescription ()
{
return (String)getValue ("Description");
}
public void setC_Project_ID (int C_Project_ID)
{
setValue ("C_Project_ID", new Integer (C_Project_ID));
}
public int getC_Project_ID ()
{
Integer ii = (Integer)getValue ("C_Project_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setIsPrinted (boolean IsPrinted)
{
setValueNoCheck ("IsPrinted", new Boolean(IsPrinted));
}
public boolean isPrinted ()
{
Boolean bb = (Boolean)getValue ("IsPrinted");
if (bb != null)
return bb.booleanValue ();
return false;
}
void setIsSOTrx (boolean IsSOTrx)
{
setValueNoCheck ("IsSOTrx", new Boolean(IsSOTrx));
}
public boolean isSOTrx ()
{
Boolean bb = (Boolean)getValue ("IsSOTrx");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setSalesRep_ID (int SalesRep_ID)
{
setValue ("SalesRep_ID", new Integer (SalesRep_ID));
}
public int getSalesRep_ID ()
{
Integer ii = (Integer)getValue ("SalesRep_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setDateInvoiced (Timestamp DateInvoiced)
{
if (DateInvoiced == null)
throw new IllegalArgumentException ("DateInvoiced is mandatory");
setValue ("DateInvoiced", DateInvoiced);
}
public Timestamp getDateInvoiced ()
{
return (Timestamp)getValue ("DateInvoiced");
}
public void setDatePrinted (Timestamp DatePrinted)
{
setValue ("DatePrinted", DatePrinted);
}
public Timestamp getDatePrinted ()
{
return (Timestamp)getValue ("DatePrinted");
}
public void setC_Charge_ID (int C_Charge_ID)
{
setValue ("C_Charge_ID", new Integer (C_Charge_ID));
}
public int getC_Charge_ID ()
{
Integer ii = (Integer)getValue ("C_Charge_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setProcessed (boolean Processed)
{
setValueNoCheck ("Processed", new Boolean(Processed));
}
public boolean isProcessed ()
{
Boolean bb = (Boolean)getValue ("Processed");
if (bb != null)
return bb.booleanValue ();
return false;
}
void setPosted (boolean Posted)
{
setValueNoCheck ("Posted", new Boolean(Posted));
}
public boolean isPosted ()
{
Boolean bb = (Boolean)getValue ("Posted");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setC_Payment_ID (int C_Payment_ID)
{
setValue ("C_Payment_ID", new Integer (C_Payment_ID));
}
public int getC_Payment_ID ()
{
Integer ii = (Integer)getValue ("C_Payment_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setChargeAmt (BigDecimal ChargeAmt)
{
setValue ("ChargeAmt", ChargeAmt);
}
public BigDecimal getChargeAmt ()
{
BigDecimal bd = (BigDecimal)getValue ("ChargeAmt");
if (bd == null)
return Env.ZERO;
return bd;
}
public void setC_DocTypeTarget_ID (int C_DocTypeTarget_ID)
{
setValue ("C_DocTypeTarget_ID", new Integer (C_DocTypeTarget_ID));
}
public int getC_DocTypeTarget_ID ()
{
Integer ii = (Integer)getValue ("C_DocTypeTarget_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setTotalLines (BigDecimal TotalLines)
{
if (TotalLines == null)
throw new IllegalArgumentException ("TotalLines is mandatory");
setValueNoCheck ("TotalLines", TotalLines);
}
public BigDecimal getTotalLines ()
{
BigDecimal bd = (BigDecimal)getValue ("TotalLines");
if (bd == null)
return Env.ZERO;
return bd;
}
public void setIsPaid (boolean IsPaid)
{
setValue ("IsPaid", new Boolean(IsPaid));
}
public boolean isPaid ()
{
Boolean bb = (Boolean)getValue ("IsPaid");
if (bb != null)
return bb.booleanValue ();
return false;
}
/**
* Get Open Amount
* @return Open Amt
*/
public BigDecimal getOpenAmt ()
{
if (isPaid())
return Env.ZERO;
if (m_openAmt != null)
return m_openAmt;
//
String sql = "SELECT C_Invoice_Open(?) FROM DUAL";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getC_Invoice_ID());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
m_openAmt = rs.getBigDecimal(1);
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("getOpenAmt", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
//
return m_openAmt;
} // getOpenAmt
public void setSendEMail (boolean SendEMail)
{
setValue ("SendEMail", new Boolean(SendEMail));
}
public boolean isSendEMail ()
{
Boolean bb = (Boolean)getValue ("SendEMail");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setC_CashLine_ID (int C_CashLine_ID)
{
setValue ("C_CashLine_ID", new Integer (C_CashLine_ID));
}
public int getC_CashLine_ID ()
{
Integer ii = (Integer)getValue ("C_CashLine_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setIsDiscountPrinted (boolean IsDiscountPrinted)
{
setValue ("IsDiscountPrinted", new Boolean(IsDiscountPrinted));
}
public boolean isDiscountPrinted ()
{
Boolean bb = (Boolean)getValue ("IsDiscountPrinted");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setIsTaxIncluded (boolean IsTaxIncluded)
{
setValue ("IsTaxIncluded", new Boolean(IsTaxIncluded));
}
public boolean isTaxIncluded ()
{
Boolean bb = (Boolean)getValue ("IsTaxIncluded");
if (bb != null)
return bb.booleanValue ();
return false;
}
void setIsApproved (boolean IsApproved)
{
setValueNoCheck ("IsApproved", new Boolean(IsApproved));
}
public boolean isApproved ()
{
Boolean bb = (Boolean)getValue ("IsApproved");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setC_BPartner_ID (int C_BPartner_ID)
{
setValue ("C_BPartner_ID", new Integer (C_BPartner_ID));
}
public int getC_BPartner_ID ()
{
Integer ii = (Integer)getValue ("C_BPartner_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setC_PaymentTerm_ID (int C_PaymentTerm_ID)
{
setValue ("C_PaymentTerm_ID", new Integer (C_PaymentTerm_ID));
}
public int getC_PaymentTerm_ID ()
{
Integer ii = (Integer)getValue ("C_PaymentTerm_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setDocumentNo (String DocumentNo)
{
if (DocumentNo == null)
throw new IllegalArgumentException ("DocumentNo is mandatory");
setValueNoCheck ("DocumentNo", DocumentNo);
}
public String getDocumentNo ()
{
return (String)getValue ("DocumentNo");
}
void setC_DocType_ID (int C_DocType_ID)
{
setValueNoCheck ("C_DocType_ID", new Integer (C_DocType_ID));
}
public int getC_DocType_ID ()
{
Integer ii = (Integer)getValue ("C_DocType_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
void setIsTransferred (boolean IsTransferred)
{
setValueNoCheck ("IsTransferred", new Boolean(IsTransferred));
}
public boolean isTransferred ()
{
Boolean bb = (Boolean)getValue ("IsTransferred");
if (bb != null)
return bb.booleanValue ();
return false;
}
public void setC_Currency_ID (int C_Currency_ID)
{
setValue ("C_Currency_ID", new Integer (C_Currency_ID));
}
public int getC_Currency_ID ()
{
Integer ii = (Integer)getValue ("C_Currency_ID");
if (ii == null)
return 0;
return ii.intValue ();
}
public void setPOReference (String POReference)
{
setValue ("POReference", POReference);
}
public String getPOReference ()
{
return (String)getValue ("POReference");
}
public void setPaymentRule (String PaymentRule)
{
if (PaymentRule.equals ("B") || PaymentRule.equals ("K")
|| PaymentRule.equals ("P") || PaymentRule.equals ("S")
|| PaymentRule.equals ("T"))
;
else
throw new IllegalArgumentException (
"PaymentRule Invalid value - Reference_ID=195 - B - K - P - S - T");
setValue ("PaymentRule", PaymentRule);
}
public String getPaymentRule ()
{
return (String)getValue ("PaymentRule");
}
/*************************************************************************/
/**
* Create PDF
* @return true if success
*/
public File getPDF ()
{
return getPDF(null);
} // getPDF
/**
* Create PDF file
* @param file output file
* @return true if success
*/
public File getPDF (File file)
{
int AD_PrintFormat_ID = 0;
Language language = Language.getLanguage(); // Base Language;
String sql = "SELECT bp.AD_Language, c.IsMultiLingualDocument,"
+ " pf.Invoice_PrintFormat_ID, c.DocumentDir "
+ "FROM C_Invoice i"
+ " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)"
+ " INNER JOIN AD_Client c ON (i.AD_Client_ID=c.AD_Client_ID)"
+ " INNER JOIN AD_PrintForm pf ON (c.AD_Client_ID=pf.AD_Client_ID) "
+ "WHERE i.C_Invoice_ID=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getC_Invoice_ID());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
String AD_Language = rs.getString(1);
if (AD_Language != null && "Y".equals(rs.getString(2)))
language = Language.getLanguage(AD_Language);
//
AD_PrintFormat_ID = rs.getInt(3);
//
if (file == null)
file = new File (getPDFFileName(rs.getString(4)));
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
Log.error("getPDF", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
if (AD_PrintFormat_ID == 0)
{
log.error("getPDF - No PrintFormat found");
return null;
}
// Format
MPrintFormat format = MPrintFormat.get (AD_PrintFormat_ID, false);
format.setLanguage(language);
format.setTranslationLanguage(language);
// Query
MQuery query = new MQuery("C_Invoice_Header_v");
query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer(getC_Invoice_ID()));
// Engine
ReportEngine re = new ReportEngine(getCtx(), format, query);
return re.getPDF(file);
} // getPDF
/**
* Get PDF File Name
* @param documentDir directory
* @return file name
*/
public String getPDFFileName (String documentDir)
{
return getPDFFileName(documentDir, getC_Invoice_ID());
} // getPDFFileName
/**
* 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
} // MInvoice