use of org.compiere.model.MUserMail in project adempiere by adempiere.
the class DunningPrint method doIt.
// prepare
/**
* Process
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ",R_MailText_ID=" + p_R_MailText_ID + ", EmailPDF=" + p_EMailPDF + ",IsOnlyIfBPBalance=" + p_IsOnlyIfBPBalance + ",PrintUnprocessedOnly=" + p_PrintUnprocessedOnly);
// Need to have Template
if (p_EMailPDF && p_R_MailText_ID == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@");
// String subject = "";
MMailText mText = null;
if (p_EMailPDF) {
mText = new MMailText(getCtx(), p_R_MailText_ID, get_TrxName());
if (p_EMailPDF && mText.get_ID() == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
// subject = mText.getMailHeader();
}
//
MDunningRun run = new MDunningRun(getCtx(), p_C_DunningRun_ID, get_TrxName());
if (run.get_ID() == 0)
throw new AdempiereUserError("@NotFound@: @C_DunningRun_ID@ - " + p_C_DunningRun_ID);
MClient client = MClient.get(getCtx());
int count = 0;
int errors = 0;
MDunningRunEntry[] entries = run.getEntries(false);
for (int i = 0; i < entries.length; i++) {
MDunningRunEntry entry = entries[i];
// Print Format on Dunning Level
MDunningLevel level = new MDunningLevel(getCtx(), entry.getC_DunningLevel_ID(), get_TrxName());
MPrintFormat format = null;
if (level.getDunning_PrintFormat_ID() > 0)
format = MPrintFormat.get(getCtx(), level.getDunning_PrintFormat_ID(), false);
if (p_IsOnlyIfBPBalance && entry.getAmt().signum() <= 0)
continue;
if (p_PrintUnprocessedOnly && entry.isProcessed())
continue;
// To BPartner
MBPartner bp = new MBPartner(getCtx(), entry.getC_BPartner_ID(), get_TrxName());
if (bp.get_ID() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @C_BPartner_ID@ " + entry.getC_BPartner_ID());
errors++;
continue;
}
// To User
MUser to = new MUser(getCtx(), entry.getAD_User_ID(), get_TrxName());
if (p_EMailPDF) {
if (to.get_ID() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @AD_User_ID@ - " + bp.getName());
errors++;
continue;
} else if (to.getEMail() == null || to.getEMail().length() == 0) {
addLog(entry.get_ID(), null, null, "@NotFound@: @EMail@ - " + to.getName());
errors++;
continue;
}
}
// query
MQuery query = new MQuery("C_Dunning_Header_v");
query.addRestriction("C_DunningRunEntry_ID", MQuery.EQUAL, new Integer(entry.getC_DunningRunEntry_ID()));
// Engine
PrintInfo info = new PrintInfo(bp.getName(), MDunningRunEntry.Table_ID, entry.getC_DunningRunEntry_ID(), entry.getC_BPartner_ID());
info.setDescription(bp.getName() + ", Amt=" + entry.getAmt());
ReportEngine re = null;
if (format != null)
re = new ReportEngine(getCtx(), format, query, info);
boolean printed = false;
if (p_EMailPDF) {
EMail email = client.createEMail(to.getEMail(), null, null);
if (!email.isValid()) {
addLog(entry.get_ID(), null, null, "@RequestActionEMailError@ Invalid EMail: " + to);
errors++;
continue;
}
// variable context
mText.setUser(to);
mText.setBPartner(bp);
mText.setPO(entry);
String message = mText.getMailText(true);
if (mText.isHtml())
email.setMessageHTML(mText.getMailHeader(), message);
else {
email.setSubject(mText.getMailHeader());
email.setMessageText(message);
}
//
if (re != null) {
File attachment = re.getPDF(File.createTempFile("Dunning", ".pdf"));
log.fine(to + " - " + attachment);
email.addAttachment(attachment);
}
//
String msg = email.send();
MUserMail um = new MUserMail(mText, entry.getAD_User_ID(), email);
um.saveEx();
if (msg.equals(EMail.SENT_OK)) {
addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailOK@");
count++;
printed = true;
} else {
addLog(entry.get_ID(), null, null, bp.getName() + " @RequestActionEMailError@ " + msg);
errors++;
}
} else {
if (re != null) {
re.print();
count++;
printed = true;
}
}
if (printed) {
entry.setProcessed(true);
entry.save();
}
}
// for all dunning letters
if (errors == 0) {
run.setProcessed(true);
run.saveEx();
}
if (p_EMailPDF)
return "@Sent@=" + count + " - @Errors@=" + errors;
return "@Printed@=" + count;
}
use of org.compiere.model.MUserMail in project adempiere by adempiere.
the class WebUtil method sendEMail.
// deleteCookieWebUser
/**************************************************************************
* Send EMail
* @param request request
* @param to web user
* @param msgType see MMailMsg.MAILMSGTYPE_*
* @param parameter object array with parameters
* @return mail EMail.SENT_OK or error message
*/
public static String sendEMail(HttpServletRequest request, WebUser to, String msgType, Object[] parameter) {
WebSessionCtx wsc = WebSessionCtx.get(request);
MStore wStore = wsc.wstore;
MMailMsg mailMsg = wStore.getMailMsg(msgType);
//
StringBuffer subject = new StringBuffer(mailMsg.getSubject());
if (parameter.length > 0 && parameter[0] != null)
subject.append(parameter[0]);
//
StringBuffer message = new StringBuffer();
String hdr = wStore.getEMailFooter();
if (hdr != null && hdr.length() > 0)
message.append(hdr).append("\n");
message.append(mailMsg.getMessage());
if (parameter.length > 1 && parameter[1] != null)
message.append(parameter[1]);
if (mailMsg.getMessage2() != null) {
message.append("\n").append(mailMsg.getMessage2());
if (parameter.length > 2 && parameter[2] != null)
message.append(parameter[2]);
}
if (mailMsg.getMessage3() != null) {
message.append("\n").append(mailMsg.getMessage3());
if (parameter.length > 3 && parameter[3] != null)
message.append(parameter[3]);
}
message.append(MRequest.SEPARATOR).append("http://").append(request.getServerName()).append(request.getContextPath()).append("/ - ").append(wStore.getName()).append("\n").append("Request from: ").append(getFrom(request)).append("\n");
String ftr = wStore.getEMailFooter();
if (ftr != null && ftr.length() > 0)
message.append(ftr);
// Create Mail
EMail email = wStore.createEMail(to.getEmail(), subject.toString(), message.toString());
// CC Order
if (msgType.equals(MMailMsg.MAILMSGTYPE_OrderAcknowledgement)) {
String orderEMail = wStore.getWebOrderEMail();
String storeEMail = wStore.getWStoreEMail();
if (orderEMail != null && orderEMail.length() > 0 && // already Bcc
!orderEMail.equals(storeEMail))
email.addBcc(orderEMail);
}
// Send
String retValue = email.send();
// Log
MUserMail um = new MUserMail(mailMsg, to.getAD_User_ID(), email);
um.saveEx();
//
return retValue;
}
use of org.compiere.model.MUserMail in project adempiere by adempiere.
the class InvoicePrint method doIt.
// prepare
/**
* Perform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws java.lang.Exception {
// Need to have Template
if (p_EMailPDF && p_R_MailText_ID == 0)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@");
log.info("C_BPartner_ID=" + m_C_BPartner_ID + ", C_Invoice_ID=" + m_C_Invoice_ID + ", EmailPDF=" + p_EMailPDF + ",R_MailText_ID=" + p_R_MailText_ID + ", DateInvoiced=" + m_dateInvoiced_From + "-" + m_dateInvoiced_To + ", DocumentNo=" + m_DocumentNo_From + "-" + m_DocumentNo_To);
MMailText mText = null;
if (p_R_MailText_ID != 0) {
mText = new MMailText(getCtx(), p_R_MailText_ID, get_TrxName());
if (mText.get_ID() != p_R_MailText_ID)
throw new AdempiereUserError("@NotFound@: @R_MailText_ID@ - " + p_R_MailText_ID);
}
// 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 AdempiereUserError("@RestrictSelection@");
MClient client = MClient.get(getCtx());
// Get Info
StringBuffer sql = new StringBuffer(// 1..3
"SELECT i.C_Invoice_ID,bp.AD_Language,c.IsMultiLingualDocument," + // Prio: 1. BPartner 2. DocType, 3. PrintFormat (Org) // see ReportCtl+MInvoice
" COALESCE(bp.Invoice_PrintFormat_ID, dt.AD_PrintFormat_ID, pf.Invoice_PrintFormat_ID)," + // 4
" dt.DocumentCopies+bp.DocumentCopies," + // 5
" bpc.AD_User_ID, i.DocumentNo," + // 6..7
" bp.C_BPartner_ID " + // 8
"FROM C_Invoice i" + " INNER JOIN C_BPartner bp ON (i.C_BPartner_ID=bp.C_BPartner_ID)" + " LEFT OUTER JOIN AD_User bpc ON (i.AD_User_ID=bpc.AD_User_ID)" + " INNER JOIN AD_Client c ON (i.AD_Client_ID=c.AD_Client_ID)" + " INNER JOIN AD_PrintForm pf ON (i.AD_Client_ID=pf.AD_Client_ID)" + " INNER JOIN C_DocType dt ON (i.C_DocType_ID=dt.C_DocType_ID)" + " WHERE i.AD_Client_ID=? AND i.AD_Org_ID=? AND i.isSOTrx='Y' AND " + // more them 1 PF
" pf.AD_Org_ID IN (0,i.AD_Org_ID) AND ");
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, 'DD') 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, 'DD') >= ").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, 'DD') <= ").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));
}
if (p_EMailPDF) {
if (needAnd) {
sql.append(" AND ");
}
/* if emailed to customer only select COmpleted & CLosed invoices */
sql.append("i.DocStatus IN ('CO','CL') ");
}
}
// more than 1 PF record
sql.append(" ORDER BY i.C_Invoice_ID, pf.AD_Org_ID DESC");
log.fine(sql.toString());
MPrintFormat format = null;
int old_AD_PrintFormat_ID = -1;
int old_C_Invoice_ID = -1;
int C_BPartner_ID = 0;
int count = 0;
int errors = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
pstmt.setInt(2, Env.getAD_Org_ID(Env.getCtx()));
rs = pstmt.executeQuery();
while (rs.next()) {
int C_Invoice_ID = rs.getInt(1);
if (// multiple pf records
C_Invoice_ID == old_C_Invoice_ID)
continue;
old_C_Invoice_ID = C_Invoice_ID;
// Set Language when enabled
// Base Language
Language language = Language.getLoginLanguage();
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;
int AD_User_ID = rs.getInt(6);
MUser to = new MUser(getCtx(), AD_User_ID, get_TrxName());
String DocumentNo = rs.getString(7);
C_BPartner_ID = rs.getInt(8);
//
String documentDir = client.getDocumentDir();
if (documentDir == null || documentDir.length() == 0)
documentDir = ".";
//
if (p_EMailPDF && (to.get_ID() == 0 || to.getEMail() == null || to.getEMail().length() == 0)) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailNoTo@");
errors++;
continue;
}
if (AD_PrintFormat_ID == 0) {
addLog(C_Invoice_ID, null, null, DocumentNo + " No Print Format");
errors++;
continue;
}
// Get Format & Data
if (AD_PrintFormat_ID != old_AD_PrintFormat_ID) {
format = MPrintFormat.get(getCtx(), 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
PrintInfo info = new PrintInfo(DocumentNo, X_C_Invoice.Table_ID, C_Invoice_ID, C_BPartner_ID);
info.setCopies(copies);
ReportEngine re = new ReportEngine(getCtx(), format, query, info);
boolean printed = false;
if (p_EMailPDF) {
String subject = mText.getMailHeader() + " - " + DocumentNo;
EMail email = client.createEMail(to.getEMail(), subject, null);
if (!email.isValid()) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailError@ Invalid EMail: " + to);
errors++;
continue;
}
// Context
mText.setUser(to);
// Context
mText.setBPartner(C_BPartner_ID);
mText.setPO(new MInvoice(getCtx(), C_Invoice_ID, get_TrxName()));
String message = mText.getMailText(true);
if (mText.isHtml())
email.setMessageHTML(subject, message);
else {
email.setSubject(subject);
email.setMessageText(message);
}
//
File invoice = null;
if (!Ini.isClient())
invoice = new File(MInvoice.getPDFFileName(documentDir, C_Invoice_ID));
File attachment = re.getPDF(invoice);
log.fine(to + " - " + attachment);
email.addAttachment(attachment);
//
String msg = email.send();
MUserMail um = new MUserMail(mText, getAD_User_ID(), email);
um.saveEx();
if (msg.equals(EMail.SENT_OK)) {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailOK@ - " + to.getEMail());
count++;
printed = true;
} else {
addLog(C_Invoice_ID, null, null, DocumentNo + " @RequestActionEMailError@ " + msg + " - " + to.getEMail());
errors++;
}
} else {
ServerReportCtl.startDocumentPrint(ReportEngine.INVOICE, // No custom print format
null, C_Invoice_ID, // No custom printer
null, null);
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(), get_TrxName());
}
}
// for all entries
} catch (Exception e) {
log.log(Level.SEVERE, "doIt - " + sql, e);
throw new Exception(e);
} finally {
DB.close(rs, pstmt);
}
//
if (p_EMailPDF)
return "@Sent@=" + count + " - @Errors@=" + errors;
return "@Printed@=" + count;
}
use of org.compiere.model.MUserMail in project adempiere by adempiere.
the class AssetDelivery method sendNoGuaranteeMail.
// doIt
/**
* Send No Guarantee EMail
* @param A_Asset_ID asset
* @param R_MailText_ID mail to send
* @return message - delivery errors start with **
*/
private String sendNoGuaranteeMail(int A_Asset_ID, int R_MailText_ID, String trxName) {
MAsset asset = new MAsset(getCtx(), A_Asset_ID, trxName);
if (asset.getAD_User_ID() == 0)
return "** No Asset User";
MUser user = new MUser(getCtx(), asset.getAD_User_ID(), get_TrxName());
if (user.getEMail() == null || user.getEMail().length() == 0)
return "** No Asset User Email";
if (m_MailText == null || m_MailText.getR_MailText_ID() != R_MailText_ID)
m_MailText = new MMailText(getCtx(), R_MailText_ID, get_TrxName());
if (m_MailText.getMailHeader() == null || m_MailText.getMailHeader().length() == 0)
return "** No Subject";
// Create Mail
EMail email = m_client.createEMail(user.getEMail(), null, null);
m_MailText.setPO(user);
m_MailText.setPO(asset);
String message = m_MailText.getMailText(true);
if (m_MailText.isHtml())
email.setMessageHTML(m_MailText.getMailHeader(), message);
else {
email.setSubject(m_MailText.getMailHeader());
email.setMessageText(message);
}
String msg = email.send();
new MUserMail(m_MailText, asset.getAD_User_ID(), email).saveEx();
if (!EMail.SENT_OK.equals(msg))
return "** Not delivered: " + user.getEMail() + " - " + msg;
//
return user.getEMail();
}
use of org.compiere.model.MUserMail in project adempiere by adempiere.
the class AssetDelivery method deliverIt.
// sendNoGuaranteeMail
/**************************************************************************
* Deliver Asset
* @param A_Asset_ID asset
* @return message - delivery errors start with **
*/
private String deliverIt(int A_Asset_ID) {
log.fine("A_Asset_ID=" + A_Asset_ID);
long start = System.currentTimeMillis();
//
MAsset asset = new MAsset(getCtx(), A_Asset_ID, get_TrxName());
if (asset.getAD_User_ID() == 0)
return "** No Asset User";
MUser user = new MUser(getCtx(), asset.getAD_User_ID(), get_TrxName());
if (user.getEMail() == null || user.getEMail().length() == 0)
return "** No Asset User Email";
if (asset.getProductR_MailText_ID() == 0)
return "** Product Mail Text";
if (m_MailText == null || m_MailText.getR_MailText_ID() != asset.getProductR_MailText_ID())
m_MailText = new MMailText(getCtx(), asset.getProductR_MailText_ID(), get_TrxName());
if (m_MailText.getMailHeader() == null || m_MailText.getMailHeader().length() == 0)
return "** No Subject";
// Create Mail
EMail email = m_client.createEMail(user.getEMail(), null, null);
if (!email.isValid()) {
asset.setHelp(asset.getHelp() + " - Invalid EMail");
asset.setIsActive(false);
return "** Invalid EMail: " + user.getEMail();
}
if (m_client.isSmtpAuthorization())
email.createAuthenticator(m_client.getRequestUser(), m_client.getRequestUserPW());
m_MailText.setUser(user);
m_MailText.setPO(asset);
String message = m_MailText.getMailText(true);
if (m_MailText.isHtml() || m_AttachAsset)
email.setMessageHTML(m_MailText.getMailHeader(), message);
else {
email.setSubject(m_MailText.getMailHeader());
email.setMessageText(message);
}
if (m_AttachAsset) {
MProductDownload[] pdls = asset.getProductDownloads();
if (pdls != null) {
for (int i = 0; i < pdls.length; i++) {
URI url = pdls[i].getDownloadURL(m_client.getDocumentDir());
if (url != null)
email.addAttachment(url);
}
} else
log.warning("No DowloadURL for A_Asset_ID=" + A_Asset_ID);
}
String msg = email.send();
new MUserMail(m_MailText, asset.getAD_User_ID(), email).saveEx();
if (!EMail.SENT_OK.equals(msg))
return "** Not delivered: " + user.getEMail() + " - " + msg;
MAssetDelivery ad = confirmDelivery(asset, email, user.getAD_User_ID());
ad.saveEx();
asset.saveEx();
//
log.fine((System.currentTimeMillis() - start) + " ms");
// success
return user.getEMail() + " - " + asset.getProductVersionNo();
}
Aggregations