Search in sources :

Example 1 with MDunningRun

use of org.compiere.model.MDunningRun 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;
}
Also used : AdempiereUserError(org.compiere.util.AdempiereUserError) MMailText(org.compiere.model.MMailText) MUserMail(org.compiere.model.MUserMail) MQuery(org.compiere.model.MQuery) PrintInfo(org.compiere.model.PrintInfo) MDunningRun(org.compiere.model.MDunningRun) MBPartner(org.compiere.model.MBPartner) EMail(org.compiere.util.EMail) MClient(org.compiere.model.MClient) MDunningRunEntry(org.compiere.model.MDunningRunEntry) MPrintFormat(org.compiere.print.MPrintFormat) ReportEngine(org.compiere.print.ReportEngine) MUser(org.compiere.model.MUser) File(java.io.File) MDunningLevel(org.compiere.model.MDunningLevel)

Example 2 with MDunningRun

use of org.compiere.model.MDunningRun in project adempiere by adempiere.

the class DunningRunCreate method doIt.

//	prepare
/**
	 * 	Process
	 *	@return message
	 *	@throws Exception
	 */
protected String doIt() throws Exception {
    log.info("C_DunningRun_ID=" + p_C_DunningRun_ID + ", Dispute=" + p_IncludeInDispute + ", C_BP_Group_ID=" + p_C_BP_Group_ID + ", C_BPartner_ID=" + p_C_BPartner_ID);
    m_run = new MDunningRun(getCtx(), p_C_DunningRun_ID, get_TrxName());
    if (m_run.get_ID() == 0)
        throw new IllegalArgumentException("Not found MDunningRun");
    if (!m_run.deleteEntries(true))
        throw new IllegalArgumentException("Cannot delete existing entries");
    if (p_SalesRep_ID == 0)
        throw new IllegalArgumentException("No SalesRep");
    if (p_C_Currency_ID == 0)
        throw new IllegalArgumentException("No Currency");
    //
    for (MDunningLevel l_level : m_run.getLevels()) {
        addInvoices(l_level);
        addPayments(l_level);
        if (l_level.isChargeFee())
            addFees(l_level);
        // we need to check whether this is a statement or not and some other rules
        checkDunningEntry(l_level);
    }
    int entries = DB.getSQLValue(get_TrxName(), "SELECT COUNT(*) FROM C_DunningRunEntry WHERE C_DunningRun_ID=?", m_run.get_ID());
    return "@C_DunningRunEntry_ID@ #" + entries;
}
Also used : MDunningRun(org.compiere.model.MDunningRun) MDunningLevel(org.compiere.model.MDunningLevel)

Aggregations

MDunningLevel (org.compiere.model.MDunningLevel)2 MDunningRun (org.compiere.model.MDunningRun)2 File (java.io.File)1 MBPartner (org.compiere.model.MBPartner)1 MClient (org.compiere.model.MClient)1 MDunningRunEntry (org.compiere.model.MDunningRunEntry)1 MMailText (org.compiere.model.MMailText)1 MQuery (org.compiere.model.MQuery)1 MUser (org.compiere.model.MUser)1 MUserMail (org.compiere.model.MUserMail)1 PrintInfo (org.compiere.model.PrintInfo)1 MPrintFormat (org.compiere.print.MPrintFormat)1 ReportEngine (org.compiere.print.ReportEngine)1 AdempiereUserError (org.compiere.util.AdempiereUserError)1 EMail (org.compiere.util.EMail)1