public void ProcessStatements(long personId, long periodId, long?printerId, List <CDS.Client.DataAccessLayer.Types.Statement> statements) { BL.ApplicationDataContext.Instance.LoggedInUser = (DB.SEC_User)BL.SEC.SEC_User.LoadByPerson(personId, DataContext); BL.ApplicationDataContext.Instance.PopulateModuleAccess(); BL.ApplicationDataContext.Instance.PopulateValidationRestrictions(); Statements = statements; using (var uow = new DevExpress.Xpo.UnitOfWork()) { Statements.ForEach(s => { var period = uow.Query <XDB.SYS_Period>().SingleOrDefault(n => n.Id == periodId); var currentuser = uow.Query <XDB.SYS_Person>().SingleOrDefault(n => n.Id == BL.ApplicationDataContext.Instance.LoggedInUser.PersonId); var entity = uow.Query <XDB.SYS_Entity>().SingleOrDefault(n => n.Id == s.EntityId); XDB.GLX_Statement stmt = new XDB.GLX_Statement(uow) { EntityId = entity, CreatedBy = currentuser, PeriodId = period, ShouldEmail = s.ShouldEmail, ShouldPrint = s.ShouldPrint }; }); uow.CommitChanges(); } using (var uow = new DevExpress.Xpo.UnitOfWork()) { //System.Threading.Tasks.Parallel.ForEach( foreach (var s in uow.Query <XDB.GLX_Statement>().Where(n => n.ShouldEmail && n.HasMailed == null || n.ShouldPrint && n.HasPrinted == null) ) //, s => { using (var iuow = new DevExpress.Xpo.UnitOfWork()) { if (s.ShouldPrint) { Statements.SingleOrDefault(n => n.EntityId == s.EntityId.Id).HasPrinted = ProcessStatementPrint(s.Id, printerId); } //System.Threading.Thread.Sleep(2000); if (s.ShouldEmail) { Statements.SingleOrDefault(n => n.EntityId == s.EntityId.Id).HasMailed = ProcessStatementEmail(s.Id); } //System.Threading.Thread.Sleep(2000); iuow.CommitChanges(); } } //); } Statements = null; }
private bool ProcessStatementPrint(long statementId, long?printerId) { bool HasPrinted; if (!printerId.HasValue) { return(false); } using (var uow = new DevExpress.Xpo.UnitOfWork()) { XDB.GLX_Statement statement = uow.Query <XDB.GLX_Statement>().SingleOrDefault(n => n.Id == statementId); XDB.SYS_Printer printer = uow.Query <XDB.SYS_Printer>().SingleOrDefault(n => n.Id == printerId.Value); try { var account = uow.Query <XDB.GLX_Account>().Where(n => n.EntityId.Id == statement.EntityId.Id).Select(l => new { l.EntityId.CodeMain, l.EntityId.CodeSub }).FirstOrDefault(); if (account.CodeMain == BL.ApplicationDataContext.Instance.SiteAccounts.DebtorsEntity.CodeMain) { StatementPrinter sp = new StatementPrinter(); sp.PrintStatement(printer.Location, printer.PrinterModel, StatementCustomerPrintTemplate.OuterXml, statement.EntityId.Id, statement.PeriodId.Id, ConfigurationManager.ConnectionStrings["BaseConnection"].ConnectionString); //sp = new StatementPrinter(); //if (!System.IO.Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements")) // System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements"); // //sp.PrintStatementToFile(string.Format(@"{0}\STATEMENT_{1}.txt", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements", statement.EntityId.Id), printer.PrinterModel, StatementCustomerPrintTemplate.OuterXml, statement.EntityId.Id, statement.PeriodId.Id, ConfigurationManager.ConnectionStrings["BaseConnection"].ConnectionString); } else if (account.CodeMain == BL.ApplicationDataContext.Instance.SiteAccounts.CreditorsEntity.CodeMain) { StatementPrinter sp = new StatementPrinter(); sp.PrintStatement(printer.Location, printer.PrinterModel, StatementSupplierPrintTemplate.OuterXml, statement.EntityId.Id, statement.PeriodId.Id, ConfigurationManager.ConnectionStrings["BaseConnection"].ConnectionString); //sp = new StatementPrinter(); //if (!System.IO.Directory.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements")) // System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements"); // //sp.PrintStatementToFile(string.Format(@"{0}\STATEMENT_{1}.txt", System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Statements", statement.EntityId.Id), printer.PrinterModel, StatementSupplierPrintTemplate.OuterXml, statement.EntityId.Id, statement.PeriodId.Id, ConfigurationManager.ConnectionStrings["BaseConnection"].ConnectionString); } statement.HasPrinted = HasPrinted = true; } catch { statement.HasPrinted = HasPrinted = false; } uow.CommitChanges(); } return(HasPrinted); }
protected override void OnStart() { base.OnStart(); lblEmailProgress.Text = string.Format("Now Emailing 0 of {0}", statements.Count(n => n.ShouldEmail)); lblPrintProgress.Text = string.Format("Now Printing 0 of {0}", statements.Count(n => n.ShouldPrint)); using (var uow = new DevExpress.Xpo.UnitOfWork()) { DateTime date = BL.ApplicationDataContext.Instance.ServerDateTime.AddMonths(-1); ddlPeriod.EditValue = uow.Query <XDB.SYS_Period>().Where(n => date > n.StartDate && date < n.EndDate).Select(l => l.Id).FirstOrDefault(); } itmOk.Visibility = ViewOnly ? DevExpress.XtraLayout.Utils.LayoutVisibility.Never : DevExpress.XtraLayout.Utils.LayoutVisibility.Always; if (ViewOnly) { UpdateTimer.Start(); } }
private bool ProcessStatementEmail(long statementId) { bool HasMailed; using (var uow = new DevExpress.Xpo.UnitOfWork()) { XDB.GLX_Statement statement = uow.Query <XDB.GLX_Statement>().SingleOrDefault(n => n.Id == statementId); var account = uow.Query <XDB.GLX_Account>().SingleOrDefault(n => n.EntityId.Id == statement.EntityId.Id); XDB.ORG_Company company = null; if (account.EntityId.CodeMain == BL.ApplicationDataContext.Instance.SiteAccounts.DebtorsEntity.CodeMain) { company = uow.Query <XDB.ORG_Company>().SingleOrDefault(n => n.TypeId.Id == (byte)BL.ORG.ORG_Type.Customer && n.EntityId.EntityId.CodeSub == account.EntityId.CodeSub); } else if (account.EntityId.CodeMain == BL.ApplicationDataContext.Instance.SiteAccounts.CreditorsEntity.CodeMain) { company = uow.Query <XDB.ORG_Company>().SingleOrDefault(n => n.TypeId.Id == (byte)BL.ORG.ORG_Type.Supplier && n.EntityId.EntityId.CodeSub == account.EntityId.CodeSub); } if (!System.Text.RegularExpressions.Regex.IsMatch(company.AccountsContactEmail, @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$", System.Text.RegularExpressions.RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250))) { return(false); } try { DB.RPT_Report rptReport = BL.RPT.RPT_Report.LoadByName("Account Statement", DataContext); System.IO.Stream xmlstream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(rptReport.Data)); DevExpress.XtraReports.UI.XtraReport report = new DevExpress.XtraReports.UI.XtraReport(); report.LoadLayoutFromXml(xmlstream); if (report.DataSource is DevExpress.DataAccess.Sql.SqlDataSource) { ((DevExpress.DataAccess.Sql.SqlDataSource)(report.DataSource)).Connection.ConnectionString = BL.ApplicationDataContext.Instance.SqlConnectionString.ConnectionString; } else { throw new Exception("Data Source type not implemented in reports"); } DevExpress.XtraReports.Parameters.ParameterCollection Parameters = new DevExpress.XtraReports.Parameters.ParameterCollection(); Parameters.Add(new DevExpress.XtraReports.Parameters.Parameter() { Name = "Account", Value = statement.EntityId.Id }); Parameters.Add(new DevExpress.XtraReports.Parameters.Parameter() { Name = "Period", Value = statement.PeriodId.Id }); if (Parameters.Count != 0) { foreach (var extparam in Parameters) { foreach (var param in report.Parameters) { if (extparam.Name == param.Name) { param.Value = extparam.Value; break; } } } } // Create a new memory stream and export the report into it as PDF. System.IO.MemoryStream mem = new System.IO.MemoryStream(); report.ExportToPdf(mem); // Create a new attachment and put the PDF report into it. mem.Seek(0, System.IO.SeekOrigin.Begin); System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(mem, account.EntityId.CodeMain + ".pdf", "application/pdf"); // Create a new message and attach the PDF report to it. System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); mail.Attachments.Add(att); report.ExportOptions.Email.RecipientAddress = company.AccountsContactEmail; report.ExportOptions.Email.RecipientName = company.AccountsContactName; report.ExportOptions.Email.Subject = "Statement of Account for ACC# " + account.EntityId.CodeSub; report.ExportOptions.Email.Body = string.Format("Please see attached Statement of Account for ACC# {0} ({1:N2})", account.EntityId.CodeSub, account.EntityId.AccountBalance); // Specify sender and recipient options for the e-mail message. mail.From = new System.Net.Mail.MailAddress(BL.ApplicationDataContext.Instance.CompanySite.AccountEmailAddress, BL.ApplicationDataContext.Instance.LoggedInUser.DisplayName); mail.To.Add(new System.Net.Mail.MailAddress(report.ExportOptions.Email.RecipientAddress, report.ExportOptions.Email.RecipientName)); // Specify other e-mail options. mail.Subject = report.ExportOptions.Email.Subject; mail.Body = report.ExportOptions.Email.Body; // Send the e-mail message via the specified SMTP server. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(BL.ApplicationDataContext.Instance.CompanySite.SMTPServerLocation, 587); if (BL.ApplicationDataContext.Instance.CompanySite.AccountEmailPassword != null && BL.ApplicationDataContext.Instance.CompanySite.AccountEmailPassword != string.Empty) { System.Net.NetworkCredential credential = new System.Net.NetworkCredential(BL.ApplicationDataContext.Instance.CompanySite.AccountEmailUsername, BL.ApplicationDataContext.Instance.CompanySite.AccountEmailPassword); if (BL.ApplicationDataContext.Instance.CompanySite.AccountEmailDomain != string.Empty) { credential.Domain = BL.ApplicationDataContext.Instance.CompanySite.AccountEmailDomain; } smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = credential; smtpClient.EnableSsl = true; } smtpClient.Send(mail); // Close the memory stream. mem.Close(); mem.Flush(); statement.HasMailed = HasMailed = true; } catch (Exception ex) { statement.HasMailed = HasMailed = false; } uow.CommitChanges(); } return(HasMailed); }