123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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;
- 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<CoreFilterDefinition> { value }, false, null);
- }
- }
- }
- public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
- }
-
- public class InvoiceGrid : DynamicDataGrid<Invoice>, IMasterDetailControl<Job,Invoice>
- {
- private InvoiceGridSettings _settings;
-
- public Job? Master { get; set; }
- public Filter<Invoice> MasterDetailFilter => Master != null
- ? Master.ID != Guid.Empty
- ? new Filter<Invoice>(x => x.JobLink.ID).IsEqualTo(Master.ID)
- : new Filter<Invoice>().None()
- : new Filter<Invoice>().All();
-
- protected override void Init()
- {
- base.Init();
-
- _settings = new UserConfiguration<InvoiceGridSettings>().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);
- 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<Invoice> criteria, Columns<Invoice> columns,
- ref SortOrder<Invoice>? sort,
- Action<CoreTable?, Exception?> action)
- {
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, action);
- }
- 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<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 JobID = row.Get<Invoice, Guid>(x => x.JobLink.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)
- {
- 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<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);
- }
-
- private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
- {
- _settings.Filters = settings;
- new UserConfiguration<InvoiceGridSettings>().Save(_settings);
- }
- }
- }
|