⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 invoiceprint.java

📁 Java写的ERP系统
💻 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  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.process;

import java.sql.*;
import java.math.*;
import java.io.*;

import org.compiere.model.*;
import org.compiere.print.*;
import org.compiere.util.*;

/**
 *	Print Invoices on Paperor send PDFs
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: InvoicePrint.java,v 1.3 2003/04/24 06:10:48 jjanke Exp $
 */
public class InvoicePrint extends SvrProcess
{
	/**
	 * 	Constructor
	 */
	public InvoicePrint()
	{
		super();
	}	//	InvoicePrint

	private boolean		m_emailPDF = false;
	private int			m_R_MailText_ID = 0;
	private Timestamp	m_dateInvoiced_From = null;
	private Timestamp	m_dateInvoiced_To = null;
	private int			m_C_BPartner_ID = 0;
	private int			m_C_Invoice_ID = 0;
	private String		m_DocumentNo_From = null;
	private String		m_DocumentNo_To = null;

	/**
	 *  Prepare - e.g., get Parameters.
	 */
	protected void prepare()
	{
		Parameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
			String name = para[i].ParameterName;
			if (name.equals("DateInvoiced"))
			{
				m_dateInvoiced_From = ((Timestamp)para[i].Parameter);
				m_dateInvoiced_To = ((Timestamp)para[i].Parameter_To);
			}
			else if (para[i].Parameter == null)
				;
			else if (name.equals("EmailPDF"))
				m_emailPDF = "Y".equals(para[i].Parameter);
			else if (name.equals("R_MailText_ID"))
				m_R_MailText_ID = ((BigDecimal)para[i].Parameter).intValue();
			else if (name.equals("C_BPartner_ID"))
				m_C_BPartner_ID = ((BigDecimal)para[i].Parameter).intValue();
			else if (name.equals("C_Invoice_ID"))
				m_C_Invoice_ID = ((BigDecimal)para[i].Parameter).intValue();
			else if (name.equals("DocumentNo"))
			{
				m_DocumentNo_From = (String)para[i].Parameter;
				m_DocumentNo_To = (String)para[i].Parameter_To;
			}
			else
				log.error("prepare - Unknown Parameter: " + name);
		}
		if (m_DocumentNo_From != null && m_DocumentNo_From.length() == 0)
			m_DocumentNo_From = null;
		if (m_DocumentNo_To != null && m_DocumentNo_To.length() == 0)
			m_DocumentNo_To = null;
	}	//	prepare

	/**
	 *  Perrform process.
	 *  @return Message
	 *  @throws Exception
	 */
	protected String doIt() throws java.lang.Exception
	{
		//	Need to have Template
		if (m_emailPDF && m_R_MailText_ID == 0)
			throw new Exception ("EMail requires Mail Template");
		//	Too broad selection
		if (m_C_BPartner_ID == 0 && m_C_Invoice_ID == 0 && m_dateInvoiced_From == null && m_dateInvoiced_To == null
			&& m_DocumentNo_From == null && m_DocumentNo_To == null)
			throw new Exception ("@RestrictSelection@");

		//	Get Info
		StringBuffer sql = new StringBuffer (
			"SELECT i.C_Invoice_ID,bp.AD_Language,c.IsMultiLingualDocument,"		//	1..3
			+ " pf.Invoice_PrintFormat_ID, dt.DocumentCopies+bp.DocumentCopies,"	//	4..5
			+ " bpc.EMail, i.DocumentNo,"											//	6..7
			+ " c.SMTPHost,c.RequestEMail,c.RequestUser,RequestUserPW,"				//	8..11
			+ " mt.MailHeader, mt.MailText, c.DocumentDir "							//	12..14
			+ "FROM C_Invoice i"
			+ " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)"
			+ " LEFT OUTER JOIN C_BPartner_Contact bpc ON (i.C_BPartner_Contact_ID=bpc.C_BPartner_Contact_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)"
			+ " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)"
			+ " LEFT OUTER JOIN R_MailText mt ON (i.AD_Client_ID=mt.AD_Client_ID AND mt.R_MailText_ID=")
			.append(m_R_MailText_ID)
			.append(") WHERE ");
		boolean needAnd = false;
		if (m_C_Invoice_ID != 0)
			sql.append("i.C_Invoice_ID=").append(m_C_Invoice_ID);
		else
		{
			if (m_C_BPartner_ID != 0)
			{
				sql.append ("i.C_BPartner_ID=").append (m_C_BPartner_ID);
				needAnd = true;
			}
			if (m_dateInvoiced_From != null && m_dateInvoiced_To != null)
			{
				if (needAnd)
					sql.append(" AND ");
				sql.append("TRUNC(i.DateInvoiced) BETWEEN ")
					.append(DB.TO_DATE(m_dateInvoiced_From, true)).append(" AND ")
					.append(DB.TO_DATE(m_dateInvoiced_To, true));
				needAnd = true;
			}
			else if (m_dateInvoiced_From != null)
			{
				if (needAnd)
					sql.append(" AND ");
				sql.append("TRUNC(i.DateInvoiced) >= ")
					.append(DB.TO_DATE(m_dateInvoiced_From, true));
				needAnd = true;
			}
			else if (m_dateInvoiced_To != null)
			{
				if (needAnd)
					sql.append(" AND ");
				sql.append("TRUNC(i.DateInvoiced) <= ")
					.append(DB.TO_DATE(m_dateInvoiced_To, true));
				needAnd = true;
			}
			else if (m_DocumentNo_From != null && m_DocumentNo_To != null)
			{
				if (needAnd)
					sql.append(" AND ");
				sql.append("i.DocumentNo BETWEEN ")
					.append(DB.TO_STRING(m_DocumentNo_From)).append(" AND ")
					.append(DB.TO_STRING(m_DocumentNo_To));
			}
			else if (m_DocumentNo_From != null)
			{
				if (needAnd)
					sql.append(" AND ");
				if (m_DocumentNo_From.indexOf('%') == -1)
					sql.append("i.DocumentNo >= ")
						.append(DB.TO_STRING(m_DocumentNo_From));
				else
					sql.append("i.DocumentNo LIKE ")
						.append(DB.TO_STRING(m_DocumentNo_From));
			}
		}
		log.debug(sql.toString());

		MPrintFormat format = null;
		int old_AD_PrintFormat_ID = -1;
		int count = 0;
		int errors = 0;
		try
		{
			Statement stmt = DB.createStatement();
			ResultSet rs = stmt.executeQuery(sql.toString());
			while (rs.next())
			{
				int C_Invoice_ID = rs.getInt(1);
				//	Set Language when enabled
				Language language = Language.getLanguage();		//	Base Language
				String AD_Language = rs.getString(2);
				if (AD_Language != null && "Y".equals(rs.getString(3)))
					language = Language.getLanguage(AD_Language);
				//
				int AD_PrintFormat_ID = rs.getInt(4);
				int copies = rs.getInt(5);
				if (copies == 0)
					copies = 1;
				String to = rs.getString(6);
				String DocumentNo = rs.getString(7);
				//
				String smtpHost = rs.getString(8);
				String from = rs.getString(9);
				String uid = rs.getString(10);
				String pwd = rs.getString(11);
				String subject = rs.getString(12);
				String message = rs.getString(13);
				String documentDir = rs.getString(14);
				if (documentDir == null || documentDir.length() == 0)
					documentDir = ".";

				//
				if (m_emailPDF && to == null)
				{
					addLog (null, C_Invoice_ID, null, DocumentNo + " @RequestActionEMailNoTo@");
					errors++;
					continue;
				}
				if (AD_PrintFormat_ID == 0)
				{
					addLog (null, C_Invoice_ID, null, DocumentNo + " No Print Format");
					errors++;
					continue;
				}
				//	Get Format & Data
				if (AD_PrintFormat_ID != old_AD_PrintFormat_ID)
				{
					format = MPrintFormat.get (AD_PrintFormat_ID, false);
					old_AD_PrintFormat_ID = AD_PrintFormat_ID;
				}
				format.setLanguage(language);
				format.setTranslationLanguage(language);
				//	query
				MQuery query = new MQuery("C_Invoice_Header_v");
				query.addRestriction("C_Invoice_ID", MQuery.EQUAL, new Integer(C_Invoice_ID));

				//	Engine
				ReportEngine re = new ReportEngine(getCtx(), format, query);
				boolean printed = false;
				if (m_emailPDF)
				{
					EMail mail = new EMail (smtpHost, from, to);
					mail.setEMailUser(uid, pwd);
					mail.setMessageHTML(subject, message);
					//
					File attachment = re.getPDF(new File(MInvoice.getPDFFileName(documentDir, C_Invoice_ID)));
					log.debug("doIt - " + to + " - " + attachment);
					mail.addAttachment(attachment);
					//
					String msg = mail.send();
					if (msg.equals(EMail.SENT_OK))
					{
						addLog (null, C_Invoice_ID, null,
						  DocumentNo + " @RequestActionEMailOK@");
						count++;
						printed = true;
					}
					else
					{
						addLog (null, C_Invoice_ID, null,
						  DocumentNo + " @RequestActionEMailError@ " + msg);
						errors++;
					}
				}
				else
				{
					re.print (false, copies, false);
					count++;
					printed = true;
				}
				//	Print Confirm
				if (printed)
				{
					StringBuffer sb = new StringBuffer ("UPDATE C_Invoice "
						+ "SET DatePrinted=SysDate, IsPrinted='Y' WHERE C_Invoice_ID=")
						.append (C_Invoice_ID);
					int no = DB.executeUpdate(sb.toString());
				}
			}
			rs.close();
			stmt.close();
		}
		catch (Exception e)
		{
			log.error("doIt - " + sql, e);
			throw new Exception (e);
		}
		//
		if (m_emailPDF)
			return "@Sent@=" + count + " - @Errors@=" + errors;
		return "@Printed@=" + count;
	}	//	doIt

}	//	InvoicePrint

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -