| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- 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<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);
- 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<Invoice> criteria, Columns<Invoice> columns, ref SortOrder<Invoice>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> 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<InvoiceLine>();
- 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<StockMovement>(
- new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Guid.Empty)
- .And(x => x.Type).IsEqualTo(StockMovementType.Issue)
- .And(x => x.Job.ID).IsEqualTo(Master.ID),
- Columns.Required<StockMovement>().Add(x => x.ID).Add(x => x.Invoice.ID)),
- new KeyedQueryDef<Assignment>(
- new Filter<Assignment>(x => x.JobLink.ID).IsEqualTo(Master.ID)
- .And(x => x.Invoice.ID).IsEqualTo(Guid.Empty),
- Columns.Required<Assignment>().Add(x => x.Invoice.ID)));
- var movements = results.GetObjects<StockMovement>().ToList();
- foreach(var mvt in movements)
- {
- mvt.Invoice.ID = invoice.ID;
- }
- Client.Save(movements, "Attached to new progress claim.");
- var assignments = results.GetObjects<Assignment>().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<Invoice, Guid>(x => x.ID) ?? Guid.Empty;
- var window = new ProgressClaimWindow(Master.ID, invoiceid);
- if(window.ShowDialog() == true)
- {
- var invoice = Client.Query(
- new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid),
- Columns.Required<Invoice>()
- ).ToObjects<Invoice>().FirstOrDefault();
- invoice.Retained = window.Retained;
- Client.Save(invoice,"Progress claim updated by user.");
-
- var lines = Client.Query(
- new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid),
- Columns.None<InvoiceLine>().Add(x=>x.ID)).ToList<InvoiceLine>();
-
- 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<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);
- }
- }
- }
|