123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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<Invoice>
- {
- 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<Invoice> criteria, Columns<Invoice> columns, ref SortOrder<Invoice> sort,
- Action<CoreTable, Exception> action)
- {
- criteria.Add(new Filter<Invoice>(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<Job>().Load(new Filter<Job>(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<Type, CoreTable> LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row)
- //{
- // Dictionary<Type, CoreTable> env = new Dictionary<Type, CoreTable>();
- // env[typeof(Invoice)] = new Client<Invoice>().Query(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
- // env[typeof(Job)] = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid));
- // env[typeof(Customer)] = new Client<Customer>().Query(new Filter<Customer>(x => x.ID).IsEqualTo(customerid));
- // env[typeof(InvoiceLine)] = new Client<InvoiceLine>().Query(new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid));
- // return env;
- //}
- private ReportTemplate LoadTemplate(DataModel model, string templatename)
- {
- var sectionName = "InvoiceListGrid";
- var client = new Client<ReportTemplate>();
- var template = client.Load(
- new Filter<ReportTemplate>(x => x.Section).IsEqualTo(sectionName)
- .And(x => x.DataModel).IsEqualTo(model.Name)
- .And(x => x.Name).IsEqualTo(templatename),
- new SortOrder<ReportTemplate>(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<Invoice, Guid>(x => x.ID);
- var CustomerID = row.Get<Invoice, Guid>(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<Invoice>(x => x.ID).IsEqualTo(invoiceid));
- var template = LoadTemplate(model, "Invoice");
- ReportUtils.PreviewReport(template, model, false, Security.IsAllowed<CanDesignReports>());
- }
- 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<Invoice, int>(x => x.Number);
- var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
- var CustomerID = row.Get<Invoice, Guid>(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<Invoice>(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<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
- //
- // var mailer = ClientFactory.CreateMailer();
- // var msg = mailer.CreateMessage();
- // msg.From = me.Email;
- //
- //
- // List<String> emails = new List<String>();
- // if (customerid != default(Guid))
- // {
- // var contacts = new Client<CustomerContact>().Load(new Filter<CustomerContact>(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<Customer>().Load(new Filter<Customer>(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<String, byte[]>[] { new Tuple<String,byte[]>(filename, pdf) };
- // msg.Subject = "New Invoice Available";
- // msg.Body = "Please find your invoice attached";
- // mailer.SendMessage(msg);
- }
- }
- }
|