InvoiceGrid.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.Core.Reports;
  10. using InABox.Wpf.Reports;
  11. using InABox.WPF;
  12. using InABox.Wpf;
  13. using System.Windows.Media.Imaging;
  14. using InABox.Configuration;
  15. using System.Collections.Generic;
  16. using System.Threading;
  17. namespace PRSDesktop
  18. {
  19. public class InvoiceGridSettings : IUserConfigurationSettings
  20. {
  21. [Obsolete]
  22. private CoreFilterDefinition? _currentFilter;
  23. [Obsolete]
  24. public CoreFilterDefinition? CurrentFilter
  25. {
  26. get => _currentFilter;
  27. set
  28. {
  29. if (value is not null)
  30. {
  31. Filters = new DynamicGridSelectedFilterSettings(new List<CoreFilterDefinition> { value }, false, null);
  32. }
  33. }
  34. }
  35. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  36. }
  37. public class InvoiceGrid : DynamicDataGrid<Invoice>, IMasterDetailControl<Job,Invoice>
  38. {
  39. private InvoiceGridSettings _settings;
  40. public Job? Master { get; set; }
  41. public Filter<Invoice> MasterDetailFilter => Master != null
  42. ? Master.ID != Guid.Empty
  43. ? new Filter<Invoice>(x => x.JobLink.ID).IsEqualTo(Master.ID)
  44. : new Filter<Invoice>().None()
  45. : new Filter<Invoice>().All();
  46. protected override void Init()
  47. {
  48. base.Init();
  49. _settings = new UserConfiguration<InvoiceGridSettings>().Load();
  50. FilterComponent.SetSettings(_settings.Filters, false);
  51. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  52. AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2);
  53. AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2);
  54. HiddenColumns.Add(x => x.CustomerLink.ID);
  55. HiddenColumns.Add(x => x.JobLink.ID);
  56. PostUtils.AddPostColumn(this);
  57. }
  58. protected override void DoReconfigure(DynamicGridOptions options)
  59. {
  60. base.DoReconfigure(options);
  61. options.RecordCount = true;
  62. options.AddRows = true;
  63. options.DeleteRows = true;
  64. options.EditRows = true;
  65. options.SelectColumns = true;
  66. options.FilterRows = true;
  67. }
  68. protected override void Reload(
  69. Filters<Invoice> criteria, Columns<Invoice> columns, ref SortOrder<Invoice>? sort,
  70. CancellationToken token, Action<CoreTable?, Exception?> action)
  71. {
  72. criteria.Add(MasterDetailFilter);
  73. base.Reload(criteria, columns, ref sort, token, action);
  74. }
  75. protected override bool CanCreateItems()
  76. {
  77. return base.CanCreateItems() && (Master == null || Master.ID != Guid.Empty);
  78. }
  79. public override Invoice CreateItem()
  80. {
  81. var result = base.CreateItem();
  82. result.JobLink.ID = Master?.ID ?? Guid.Empty;
  83. result.JobLink.Synchronise(Master ?? new Job());
  84. return result;
  85. }
  86. public override void LoadEditorButtons(Invoice item, DynamicEditorButtons buttons)
  87. {
  88. base.LoadEditorButtons(item, buttons);
  89. buttons.Add(new DynamicEditorButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), item, PrintInvoice));
  90. buttons.Add(new DynamicEditorButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), item, EmailInvoice));
  91. }
  92. //private Dictionary<Type, CoreTable> LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row)
  93. //{
  94. // Dictionary<Type, CoreTable> env = new Dictionary<Type, CoreTable>();
  95. // env[typeof(Invoice)] = new Client<Invoice>().Query(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  96. // env[typeof(Job)] = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid));
  97. // env[typeof(Customer)] = new Client<Customer>().Query(new Filter<Customer>(x => x.ID).IsEqualTo(customerid));
  98. // env[typeof(InvoiceLine)] = new Client<InvoiceLine>().Query(new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid));
  99. // return env;
  100. //}
  101. private ReportTemplate LoadTemplate(DataModel model, string templatename)
  102. {
  103. var sectionName = "InvoiceListGrid";
  104. var client = new Client<ReportTemplate>();
  105. var template = client.Load(
  106. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(sectionName)
  107. .And(x => x.DataModel).IsEqualTo(model.Name)
  108. .And(x => x.Name).IsEqualTo(templatename),
  109. new SortOrder<ReportTemplate>(x => x.Name)
  110. ).FirstOrDefault();
  111. if (template == null)
  112. template = new ReportTemplate { DataModel = model.Name, Section = sectionName, Name = templatename };
  113. return template;
  114. }
  115. private bool PrintInvoice2(Button btn, CoreRow[] rows)
  116. {
  117. if (rows.Length != 1)
  118. {
  119. MessageBox.Show("Please select one Invoice to print!");
  120. return false;
  121. }
  122. var row = rows.First();
  123. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  124. var JobID = row.Get<Invoice, Guid>(x => x.JobLink.ID);
  125. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  126. DoPrintInvoice(JobID, InvoiceID, CustomerID);
  127. return false;
  128. }
  129. private void PrintInvoice(object sender, object? item)
  130. {
  131. var inv = (Invoice)sender;
  132. DoPrintInvoice(inv.JobLink.ID, inv.ID, inv.CustomerLink.ID);
  133. }
  134. private void DoPrintInvoice(Guid jobid, Guid invoiceid, Guid customerid)
  135. {
  136. var model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  137. var template = LoadTemplate(model, "Invoice");
  138. ReportUtils.PreviewReport(template, model, false, Security.IsAllowed<CanDesignReports>());
  139. }
  140. private bool EmailInvoice2(Button btn, CoreRow[] rows)
  141. {
  142. if (rows.Length != 1)
  143. {
  144. MessageBox.Show("Please select an Invoice to print!");
  145. return false;
  146. }
  147. var row = rows.First();
  148. var InvoiceNumber = row.Get<Invoice, int>(x => x.Number);
  149. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  150. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  151. DoEmailInvoice(InvoiceID, InvoiceNumber, CustomerID);
  152. return false;
  153. }
  154. private void EmailInvoice(object sender, object? item)
  155. {
  156. if (item is not Invoice inv) return;
  157. DoEmailInvoice(inv.ID, inv.Number, inv.CustomerLink.ID);
  158. }
  159. private void DoEmailInvoice(Guid invoiceid, int invoicenumber, Guid customerid)
  160. {
  161. MessageBox.Show("PDF Functions broken in .NET6");
  162. // InvoiceDataModel model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  163. // model.Populate();
  164. //
  165. // var template = LoadTemplate(model, "Invoice");
  166. //
  167. // byte[] pdf = InABox.Reports.ReportUtils.ReportToPDF(template,model,false);
  168. // String filename = Path.Combine(Path.GetTempPath(), String.Format("Invoice {0}.pdf", invoicenumber));
  169. // File.WriteAllBytes(filename, pdf);
  170. //
  171. // //PdfDocument finalDoc = new PdfDocument();
  172. // //PdfDocument.Merge(finalDoc, filename);
  173. // //finalDoc.Save(filename);
  174. // //finalDoc.Close(true);
  175. //
  176. // Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
  177. //
  178. // var mailer = ClientFactory.CreateMailer();
  179. // var msg = mailer.CreateMessage();
  180. // msg.From = me.Email;
  181. //
  182. //
  183. // List<String> emails = new List<String>();
  184. // if (customerid != default(Guid))
  185. // {
  186. // 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));
  187. // emails.AddRange(contacts.Select(x => x.Contact.Email));
  188. // if (!emails.Any())
  189. // {
  190. // var customer = new Client<Customer>().Load(new Filter<Customer>(x => x.ID).IsEqualTo(customerid).And(x => x.Email).IsNotEqualTo("")).FirstOrDefault();
  191. // if (customer != null)
  192. // emails.Add(customer.Email);
  193. // }
  194. // }
  195. //
  196. // msg.To = emails;
  197. // msg.Attachments = new Tuple<String, byte[]>[] { new Tuple<String,byte[]>(filename, pdf) };
  198. // msg.Subject = "New Invoice Available";
  199. // msg.Body = "Please find your invoice attached";
  200. // mailer.SendMessage(msg);
  201. }
  202. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
  203. {
  204. _settings.Filters = settings;
  205. new UserConfiguration<InvoiceGridSettings>().Save(_settings);
  206. }
  207. }
  208. }