Пример #1
0
 /// <summary>
 /// Dunning Level Consequences
 /// </summary>
 /// <param name="level"></param>
 /// <param name="entry"></param>
 private void DunningLevelConsequences(MDunningLevel level, MDunningRunEntry entry)
 {
     //	Update Business Partner based on Level
     if (level.IsSetCreditStop() || level.IsSetPaymentTerm())
     {
         MBPartner thisBPartner = new MBPartner(GetCtx(), entry.GetC_BPartner_ID(), Get_TrxName());
         if (level.IsSetCreditStop())
         {
             thisBPartner.SetSOCreditStatus(X_C_BPartner.SOCREDITSTATUS_CreditStop);
         }
         if (level.IsSetPaymentTerm() && level.GetC_PaymentTerm_ID() != 0)
         {
             thisBPartner.SetC_PaymentTerm_ID(level.GetC_PaymentTerm_ID());
         }
         thisBPartner.Save();
     }
     //	Update Invoices if not Statement (Statement is hardcoded -9999 see also MDunningLevel)
     if (!level.GetDaysAfterDue().Equals(new Decimal(-9999)) && level.GetInvoiceCollectionType() != null)
     {
         MDunningRunLine[] lines = entry.GetLines();
         for (int i = 0; i < lines.Length; i++)
         {
             MDunningRunLine line = lines[i];
             if (line.GetC_Invoice_ID() != 0 && line.IsActive())
             {
                 MInvoice invoice = new MInvoice(GetCtx(), line.GetC_Invoice_ID(), Get_TrxName());
                 invoice.SetInvoiceCollectionType(level.GetInvoiceCollectionType());
                 invoice.Save();
             }
         }
     }
 }
        /// <summary>
        ///     Process
        /// </summary>
        /// <returns>message</returns>
        protected override String DoIt()
        {
            log.Info("C_DunningRun_ID=" + _C_DunningRun_ID
                     + ", Dispute=" + _IncludeInDispute
                     + ", C_BP_Group_ID=" + _C_BP_Group_ID
                     + ", C_BPartner_ID=" + _C_BPartner_ID);
            _run = new MDunningRun(GetCtx(), _C_DunningRun_ID, Get_TrxName());
            if (_run.Get_ID() == 0)
            {
                throw new ArgumentException(Msg.GetMsg(GetCtx(), "NotFndDunRun"));
            }
            if (!_run.DeleteEntries(true))
            {
                throw new ArgumentException(Msg.GetMsg(GetCtx(), "NotDelExistEntry"));
            }
            if (_SalesRep_ID == 0)
            {
                //throw new ArgumentException("No SalesRep");
            }
            if (_C_Currency_ID == 0)
            {
                throw new ArgumentException(Msg.GetMsg(GetCtx(), "NoCurrency"));
            }

            // Pickup the Runlevel
            _level = _run.GetLevel();

            // add up all invoices
            int inv = AddInvoices();
            // add up all payments
            int pay = AddPayments();

            // If the level should charge a fee do it now...
            // Add charge line only if any invoice line is added on run line.
            if (inv > 0 && _level.IsChargeFee())
            {
                AddFees();
            }
            if (_level.IsChargeInterest())
            {
                AddFees();
            }

            // we need to check whether this is a statement or not and some other rules
            CheckDunningEntry();

            int         entries = 0;
            IDataReader idr     = null;

            try
            {
                idr = DataBase.DB.ExecuteReader("SELECT count(*) FROM C_DunningRunEntry WHERE C_DunningRun_ID=" + _run.Get_ID(), null, Get_TrxName());
                if (idr.Read())
                {
                    entries = Utility.Util.GetValueOfInt(idr[0]);//.getInt(1);
                }
                idr.Close();
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                log.Log(Level.SEVERE, "countResults", e);
            }

            return("@C_DunningRunEntry_ID@ #" + entries);
        }
Пример #3
0
        /// <summary>
        /// Process
        /// </summary>
        /// <returns>info</returns>
        protected override String DoIt()
        {
            log.Info("C_DunningRun_ID=" + _C_DunningRun_ID + ",R_MailText_ID=" + _R_MailText_ID
                     + ", EmailPDF=" + _EMailPDF + ",IsOnlyIfBPBalance=" + _IsOnlyIfBPBalance);

            //	Need to have Template
            if (_EMailPDF && _R_MailText_ID == 0)
            {
                throw new Exception("@NotFound@: @R_MailText_ID@");
            }
            String    subject = "";
            MMailText mText   = null;

            if (_EMailPDF)
            {
                mText = new MMailText(GetCtx(), _R_MailText_ID, Get_TrxName());
                if (_EMailPDF && mText.Get_ID() == 0)
                {
                    throw new Exception("@NotFound@: @R_MailText_ID@ - " + _R_MailText_ID);
                }
                subject = mText.GetMailHeader();
            }
            //
            MDunningRun run = new MDunningRun(GetCtx(), _C_DunningRun_ID, Get_TrxName());

            if (run.Get_ID() == 0)
            {
                throw new Exception("@NotFound@: @C_DunningRun_ID@ - " + _C_DunningRun_ID);
            }
            //	Print Format on Dunning Level
            MDunningLevel level = new MDunningLevel(GetCtx(), run.GetC_DunningLevel_ID(), Get_TrxName());
            //MPrintFormat format = MPrintFormat.Get(GetCtx(), level.GetDunning_PrintFormat_ID(), false);

            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];
                if (_IsOnlyIfBPBalance && Env.Signum(entry.GetAmt()) <= 0)
                {
                    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 (_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;
                    }
                }
                //	BP Language
                //Language language =Language.getLoginLanguage();		//	Base Language
                Language language = Env.GetLoginLanguage(GetCtx());

                String tableName = "C_Dunning_Header_v";
                if (client.IsMultiLingualDocument())
                {
                    tableName += "t";
                    String AD_Language = bp.GetAD_Language();
                    if (AD_Language != null)
                    {
                        //language =language.getLanguage(AD_Language);
                    }
                }
                // format.SetLanguage(language);
                // format.SetTranslationLanguage(language);
                //	query
                Query query = new Query(tableName);
                query.AddRestriction("C_DunningRunEntry_ID", Query.EQUAL,
                                     entry.GetC_DunningRunEntry_ID());

                //	Engine
                //PrintInfo info = new PrintInfo(
                //    bp.GetName(),
                //    X_C_DunningRunEntry.Table_ID,
                //    entry.GetC_DunningRunEntry_ID(),
                //    entry.GetC_BPartner_ID());
                //info.SetDescription(bp.GetName() + ", Amt=" + entry.GetAmt());
                //ReportEngine re = new ReportEngine(GetCtx(), format, query, info);
                bool printed = false;
                if (_EMailPDF)
                {
                    EMail email = client.CreateEMail(to.GetEMail(), to.GetName(), null, null);
                    if (email == null || !email.IsValid())
                    {
                        AddLog(entry.Get_ID(), null, null,
                               "@RequestActionEMailError@ Invalid EMail: " + to);
                        errors++;
                        continue;
                    }
                    mText.SetUser(to);  //	variable context
                    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);
                    }
                    //
                    //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.Save();
                    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
                {
                    //re.print();
                    count++;
                    printed = true;
                }
                if (printed)
                {
                    entry.SetProcessed(true);
                    entry.Save();
                    DunningLevelConsequences(level, entry);
                }
            }   //	for all dunning letters
            if (errors == 0)
            {
                run.SetProcessed(true);
                run.Save();
            }

            if (_EMailPDF)
            {
                return("@Sent@=" + count + " - @Errors@=" + errors);
            }
            return("@Printed@=" + count);
        }