InvoiceGrid.cs 9.4 KB

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