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.Core.Reports; using InABox.Wpf.Reports; using InABox.WPF; using InABox.Wpf; using System.Windows.Media.Imaging; using InABox.Configuration; using System.Collections.Generic; using System.Threading; using PRSDesktop; namespace PRSDesktop { public class InvoiceGridSettings : IUserConfigurationSettings { [Obsolete] private CoreFilterDefinition? _currentFilter; [Obsolete] public CoreFilterDefinition? CurrentFilter { get => _currentFilter; set { if (value is not null) { Filters = new DynamicGridSelectedFilterSettings(new List { value }, false, null); } } } public DynamicGridSelectedFilterSettings Filters { get; set; } = new(); } public class InvoiceGrid : DynamicDataGrid, IMasterDetailControl { private InvoiceGridSettings _settings; public Job? Master { get; set; } public Filter MasterDetailFilter => Master != null ? Master.ID != Guid.Empty ? new Filter(x => x.JobLink.ID).IsEqualTo(Master.ID) : new Filter().None() : new Filter().All(); protected override void Init() { base.Init(); _settings = new UserConfiguration().Load(); FilterComponent.SetSettings(_settings.Filters, false); FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected; AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2); AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2); HiddenColumns.Add(x => x.CustomerLink.ID); HiddenColumns.Add(x => x.JobLink.ID); HiddenColumns.Add(x => x.SellGL.ID); PostUtils.AddPostColumn(this); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.AddRows = true; options.DeleteRows = true; options.EditRows = true; options.SelectColumns = true; options.FilterRows = true; } protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { criteria.Add(MasterDetailFilter); base.Reload(criteria, columns, ref sort, token, action); } protected override void DoAdd(bool OpenEditorOnDirectEdit = false) { if(Master == null || Master.ID == Guid.Empty || Master.JobType == JobType.Service) { base.DoAdd(OpenEditorOnDirectEdit); } else { var window = new ProgressClaimWindow(Master.ID, Guid.Empty); if(window.ShowDialog() == true) { var invoice = new Invoice(); invoice.Type = InvoiceType.ProgressClaim; invoice.Description = $"Progress claim for {DateTime.Today:MMM yyyy}"; //invoice.Date = DateTime.Today; invoice.JobLink.CopyFrom(Master); if (Master.Account.ID != Guid.Empty) invoice.CustomerLink.CopyFrom(Master.Account); else invoice.CustomerLink.CopyFrom(Master.Customer); invoice.Retained = window.Retained; var lines = new List(); foreach(var item in window.Items.Where(x => !x.Cost.IsEffectivelyEqual(0.0))) { var line = new InvoiceLine(); line.Description = item.JobScope.Description; line.ExTax = item.Cost; line.TaxCode.CopyFrom(item.JobScope.TaxCode); line.Scope.CopyFrom(item.JobScope); line.SellGL.ID = item.GLCodeID; lines.Add(line); } Client.Save(invoice, "Progress claim created by user."); foreach(var line in lines) { line.InvoiceLink.ID = invoice.ID; } Client.Save(lines, "Progress claim created by user."); var results = Client.QueryMultiple( new KeyedQueryDef( new Filter(x => x.Invoice.ID).IsEqualTo(Guid.Empty) .And(x => x.Type).IsEqualTo(StockMovementType.Issue) .And(x => x.Job.ID).IsEqualTo(Master.ID), Columns.Required().Add(x => x.ID).Add(x => x.Invoice.ID)), new KeyedQueryDef( new Filter(x => x.JobLink.ID).IsEqualTo(Master.ID) .And(x => x.Invoice.ID).IsEqualTo(Guid.Empty), Columns.Required().Add(x => x.Invoice.ID))); var movements = results.GetObjects().ToList(); foreach(var mvt in movements) { mvt.Invoice.ID = invoice.ID; } Client.Save(movements, "Attached to new progress claim."); var assignments = results.GetObjects().ToList(); foreach(var ass in assignments) { ass.Invoice.ID = invoice.ID; } Client.Save(assignments, "Attached to new progress claim."); Refresh(false, true); } } } protected override void DoEdit() { if(Master == null || Master.ID == Guid.Empty || Master.JobType == JobType.Service) { base.DoEdit(); } else { Guid invoiceid = SelectedRows.FirstOrDefault()?.Get(x => x.ID) ?? Guid.Empty; var window = new ProgressClaimWindow(Master.ID, invoiceid); if(window.ShowDialog() == true) { var invoice = Client.Query( new Filter(x => x.ID).IsEqualTo(invoiceid), Columns.Required() ).ToObjects().FirstOrDefault(); invoice.Retained = window.Retained; Client.Save(invoice,"Progress claim updated by user."); var lines = Client.Query( new Filter(x => x.InvoiceLink.ID).IsEqualTo(invoiceid), Columns.None().Add(x=>x.ID)).ToList(); Client.Delete(lines,"Invoice Updated"); lines.Clear(); foreach(var item in window.Items.Where(x => !x.Cost.IsEffectivelyEqual(0.0))) { var line = new InvoiceLine(); line.InvoiceLink.ID = window.InvoiceID; line.Description = item.JobScope.Description; line.ExTax = item.Cost; line.TaxCode.CopyFrom(item.JobScope.TaxCode); line.Scope.CopyFrom(item.JobScope); lines.Add(line); } Client.Save(lines, "Progress claim updated by user."); Refresh(false, true); } } } protected override bool CanCreateItems() { return base.CanCreateItems() && (Master == null || Master.ID != Guid.Empty); } public override Invoice CreateItem() { var result = base.CreateItem(); result.JobLink.ID = Master?.ID ?? Guid.Empty; result.JobLink.Synchronise(Master ?? new Job()); 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 JobID = row.Get(x => x.JobLink.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) { if (item is not Invoice inv) return; 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); } private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings) { _settings.Filters = settings; new UserConfiguration().Save(_settings); } } }