using System; using System.Linq; using System.Windows; using System.Windows.Controls; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.Reports; using InABox.Core.Reports; using InABox.Wpf.Reports; using InABox.WPF; namespace PRSDesktop { public class InvoiceListGrid : DynamicDataGrid { public InvoiceListGrid() { Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.EditRows, DynamicGridOption.SelectColumns); AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2); AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2); HiddenColumns.Add(x => x.CustomerLink.ID); } public Guid JobID { get; set; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { criteria.Add(new Filter(x => x.JobLink.ID).IsEqualTo(JobID)); base.Reload(criteria, columns, ref sort, action); } protected override Invoice CreateItem() { var result = base.CreateItem(); result.JobLink.ID = JobID; var job = new Client().Load(new Filter(x => x.ID).IsEqualTo(JobID)).FirstOrDefault(); if (job != null) { //if (job.Account.ID != null) // CoreUtils.DeepClone(job.Account, result.CustomerLink); //else // CoreUtils.DeepClone(job.Customer, result.CustomerLink); } return result; } public override void LoadEditorButtons(Invoice item, DynamicEditorButtons buttons) { base.LoadEditorButtons(item, buttons); buttons.Add(new DynamicEditorButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), item, PrintInvoice)); buttons.Add(new DynamicEditorButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), item, EmailInvoice)); } //private Dictionary LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row) //{ // Dictionary env = new Dictionary(); // env[typeof(Invoice)] = new Client().Query(new Filter(x => x.ID).IsEqualTo(invoiceid)); // env[typeof(Job)] = new Client().Query(new Filter(x => x.ID).IsEqualTo(jobid)); // env[typeof(Customer)] = new Client().Query(new Filter(x => x.ID).IsEqualTo(customerid)); // env[typeof(InvoiceLine)] = new Client().Query(new Filter(x => x.InvoiceLink.ID).IsEqualTo(invoiceid)); // return env; //} private ReportTemplate LoadTemplate(DataModel model, string templatename) { var sectionName = "InvoiceListGrid"; var client = new Client(); var template = client.Load( new Filter(x => x.Section).IsEqualTo(sectionName) .And(x => x.DataModel).IsEqualTo(model.Name) .And(x => x.Name).IsEqualTo(templatename), new SortOrder(x => x.Name) ).FirstOrDefault(); if (template == null) template = new ReportTemplate { DataModel = model.Name, Section = sectionName, Name = templatename }; return template; } private bool PrintInvoice2(Button btn, CoreRow[] rows) { if (rows.Length != 1) { MessageBox.Show("Please select one Invoice to print!"); return false; } var row = rows.First(); var InvoiceID = row.Get(x => x.ID); var CustomerID = row.Get(x => x.CustomerLink.ID); DoPrintInvoice(JobID, InvoiceID, CustomerID); return false; } private void PrintInvoice(object sender, object item) { var inv = (Invoice)sender; DoPrintInvoice(inv.JobLink.ID, inv.ID, inv.CustomerLink.ID); } private void DoPrintInvoice(Guid jobid, Guid invoiceid, Guid customerid) { var model = new InvoiceDataModel(new Filter(x => x.ID).IsEqualTo(invoiceid)); var template = LoadTemplate(model, "Invoice"); ReportUtils.PreviewReport(template, model, false, Security.IsAllowed()); } private bool EmailInvoice2(Button btn, CoreRow[] rows) { if (rows.Length != 1) { MessageBox.Show("Please select an Invoice to print!"); return false; } var row = rows.First(); var InvoiceNumber = row.Get(x => x.Number); var InvoiceID = row.Get(x => x.ID); var CustomerID = row.Get(x => x.CustomerLink.ID); DoEmailInvoice(InvoiceID, InvoiceNumber, CustomerID); return false; } private void EmailInvoice(object sender, object item) { var inv = (Invoice)item; DoEmailInvoice(inv.ID, inv.Number, inv.CustomerLink.ID); } private void DoEmailInvoice(Guid invoiceid, int invoicenumber, Guid customerid) { MessageBox.Show("PDF Functions broken in .NET6"); // InvoiceDataModel model = new InvoiceDataModel(new Filter(x => x.ID).IsEqualTo(invoiceid)); // model.Populate(); // // var template = LoadTemplate(model, "Invoice"); // // byte[] pdf = InABox.Reports.ReportUtils.ReportToPDF(template,model,false); // String filename = Path.Combine(Path.GetTempPath(), String.Format("Invoice {0}.pdf", invoicenumber)); // File.WriteAllBytes(filename, pdf); // // //PdfDocument finalDoc = new PdfDocument(); // //PdfDocument.Merge(finalDoc, filename); // //finalDoc.Save(filename); // //finalDoc.Close(true); // // Employee me = new Client().Load(new Filter(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault(); // // var mailer = ClientFactory.CreateMailer(); // var msg = mailer.CreateMessage(); // msg.From = me.Email; // // // List emails = new List(); // if (customerid != default(Guid)) // { // var contacts = new Client().Load(new Filter(x => x.Customer.ID).IsEqualTo(customerid).And(x => x.Contact.Email).IsNotEqualTo("").And(x => x.Type.AccountsPayable).IsEqualTo(true)); // emails.AddRange(contacts.Select(x => x.Contact.Email)); // if (!emails.Any()) // { // var customer = new Client().Load(new Filter(x => x.ID).IsEqualTo(customerid).And(x => x.Email).IsNotEqualTo("")).FirstOrDefault(); // if (customer != null) // emails.Add(customer.Email); // } // } // // msg.To = emails; // msg.Attachments = new Tuple[] { new Tuple(filename, pdf) }; // msg.Subject = "New Invoice Available"; // msg.Body = "Please find your invoice attached"; // mailer.SendMessage(msg); } } }