use of org.compiere.model.MDunningRunEntry 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.MDunningRunEntry in project adempiere by adempiere.
the class DunningRunCreate method addFees.
// createPaymentLine
/**
* Add Fees for every line
*/
private void addFees(MDunningLevel level) {
// Only add a fee if it contains InvoiceLines and is not a statement
boolean onlyInvoices = level.isStatement();
MDunningRunEntry[] entries = m_run.getEntries(true, onlyInvoices);
if (entries != null && entries.length > 0) {
for (MDunningRunEntry element : entries) {
if (level.isShowAllDue() && level.isShowNotDue() && element.getAmt().compareTo(Env.ZERO) < 0)
// showing all the invoices and the amount of the entry is negative - don't generate a fee
continue;
MDunningRunLine line = new MDunningRunLine(element);
line.setFee(p_C_Currency_ID, level.getFeeAmt());
if (!line.save())
throw new IllegalStateException("Cannot save MDunningRunLine");
element.setQty(element.getQty().subtract(new BigDecimal(1)));
}
}
}
use of org.compiere.model.MDunningRunEntry in project adempiere by adempiere.
the class DunningRunCreate method checkDunningEntry.
// addFees
/**
* Check the dunning run
* 1) Check for following Rule: ShowAll should produce only a record if at least one new line is found
*/
private void checkDunningEntry(MDunningLevel level) {
// Check rule 1)
if (level.isShowAllDue()) {
MDunningRunEntry[] entries = m_run.getEntries(true);
if (entries != null && entries.length > 0) {
for (MDunningRunEntry element : entries) {
// We start with saying we delete this entry as long as we don't find something new
boolean entryDelete = true;
MDunningRunLine[] lines = element.getLines(true);
for (int j = 0; j < lines.length; j++) {
if (lines[j].getTimesDunned() < 0) {
// We clean up the *-1 from line 255
lines[j].setTimesDunned(lines[j].getTimesDunned() * -1);
if (!lines[j].save())
throw new IllegalStateException("Cannot save MDunningRunLine");
} else {
// We found something new, so we would not save anything...
entryDelete = false;
}
}
if (entryDelete)
element.delete(false);
}
}
}
}
use of org.compiere.model.MDunningRunEntry in project adempiere by adempiere.
the class DunningRunCreate method createPaymentLine.
// addPayments
/**
* Create Payment Line
* @param C_Payment_ID
* @param C_Currency_ID
* @param PayAmt
* @param OpenAmt
* @param C_BPartner_ID
* @param c_DunningLevel_ID
*/
private boolean createPaymentLine(int C_Payment_ID, int C_Currency_ID, BigDecimal PayAmt, BigDecimal OpenAmt, int C_BPartner_ID, int c_DunningLevel_ID) {
MDunningRunEntry entry = null;
try {
entry = m_run.getEntry(C_BPartner_ID, p_C_Currency_ID, p_SalesRep_ID, c_DunningLevel_ID);
} catch (BPartnerNoAddressException e) {
MPayment payment = new MPayment(getCtx(), C_Payment_ID, null);
String msg = "@Skip@ @C_Payment_ID@ " + payment.getDocumentInfo() + ", @C_BPartner_ID@ " + MBPartner.get(getCtx(), C_BPartner_ID).getName() + " @No@ @IsActive@ @C_BPartner_Location_ID@";
getProcessInfo().addLog(getProcessInfo().getAD_PInstance_ID(), null, null, msg);
return false;
}
if (entry.get_ID() == 0)
if (!entry.save())
throw new IllegalStateException("Cannot save MDunningRunEntry");
//
MDunningRunLine line = new MDunningRunLine(entry);
line.setPayment(C_Payment_ID, C_Currency_ID, PayAmt, OpenAmt);
if (!line.save())
throw new IllegalStateException("Cannot save MDunningRunLine");
return true;
}
use of org.compiere.model.MDunningRunEntry in project adempiere by adempiere.
the class DunningRunCreate method createInvoiceLine.
// addInvoices
/**
* Create Invoice Line
* @param C_Invoice_ID
* @param C_Currency_ID
* @param GrandTotal
* @param Open
* @param DaysDue
* @param IsInDispute
* @param C_BPartner_ID
* @param TimesDunned
* @param DaysAfterLast
* @param c_DunningLevel_ID
*/
private boolean createInvoiceLine(int C_Invoice_ID, int C_InvoicePaySchedule_ID, int C_Currency_ID, BigDecimal GrandTotal, BigDecimal Open, int DaysDue, boolean IsInDispute, int C_BPartner_ID, int TimesDunned, int DaysAfterLast, int c_DunningLevel_ID) {
MDunningRunEntry entry = null;
try {
entry = m_run.getEntry(C_BPartner_ID, p_C_Currency_ID, p_SalesRep_ID, c_DunningLevel_ID);
} catch (BPartnerNoAddressException e) {
String msg = "@Skip@ @C_Invoice_ID@ " + MInvoice.get(getCtx(), C_Invoice_ID).getDocumentInfo() + ", @C_BPartner_ID@ " + MBPartner.get(getCtx(), C_BPartner_ID).getName() + " @No@ @IsActive@ @C_BPartner_Location_ID@";
getProcessInfo().addLog(getProcessInfo().getAD_PInstance_ID(), null, null, msg);
return false;
}
if (entry.get_ID() == 0) {
if (!entry.save())
throw new IllegalStateException("Cannot save MDunningRunEntry");
}
//
MDunningRunLine line = new MDunningRunLine(entry);
line.setInvoice(C_Invoice_ID, C_Currency_ID, GrandTotal, Open, new BigDecimal(0), DaysDue, IsInDispute, TimesDunned, DaysAfterLast);
line.setC_InvoicePaySchedule_ID(C_InvoicePaySchedule_ID);
if (!line.save())
throw new IllegalStateException("Cannot save MDunningRunLine");
return true;
}
Aggregations