InvoiceGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
  39. private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
  40. private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
  41. private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
  42. private InvoiceGridSettings _settings;
  43. public Job? Master { get; set; }
  44. public Filter<Invoice> MasterDetailFilter => Master != null
  45. ? Master.ID != Guid.Empty
  46. ? new Filter<Invoice>(x => x.JobLink.ID).IsEqualTo(Master.ID)
  47. : new Filter<Invoice>().None()
  48. : new Filter<Invoice>().All();
  49. protected override void Init()
  50. {
  51. base.Init();
  52. _settings = new UserConfiguration<InvoiceGridSettings>().Load();
  53. FilterComponent.SetSettings(_settings.Filters, false);
  54. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  55. AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2);
  56. AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2);
  57. HiddenColumns.Add(x => x.CustomerLink.ID);
  58. HiddenColumns.Add(x => x.JobLink.ID);
  59. HiddenColumns.Add(x => x.PostedStatus);
  60. HiddenColumns.Add(x => x.PostedNote);
  61. ActionColumns.Add(new DynamicImageColumn(Posted_Image, Posted_Click)
  62. {
  63. ToolTip = Posted_ToolTip
  64. });
  65. }
  66. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  67. {
  68. base.DoReconfigure(options);
  69. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.EditRows,
  70. DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
  71. }
  72. private bool Posted_Click(CoreRow? arg)
  73. {
  74. return false;
  75. }
  76. private FrameworkElement? Posted_ToolTip(DynamicActionColumn column, CoreRow? row)
  77. {
  78. if (row is null)
  79. {
  80. return column.TextToolTip("Invoice Processed Status");
  81. }
  82. return column.TextToolTip(row.Get<Invoice, PostedStatus>(x => x.PostedStatus) switch
  83. {
  84. PostedStatus.PostFailed => "Post failed: " + row.Get<Invoice, string>(x => x.PostedNote),
  85. PostedStatus.RequiresRepost => "Repost required: " + row.Get<Invoice, string>(x => x.PostedNote),
  86. PostedStatus.Posted => "Processed",
  87. PostedStatus.NeverPosted or _ => "Not posted yet",
  88. });
  89. }
  90. private BitmapImage? Posted_Image(CoreRow? arg)
  91. {
  92. if(arg is null)
  93. return post;
  94. return arg.Get<Invoice, PostedStatus>(x => x.PostedStatus) switch
  95. {
  96. PostedStatus.PostFailed => warning,
  97. PostedStatus.Posted => tick,
  98. PostedStatus.RequiresRepost => refresh,
  99. PostedStatus.NeverPosted or _ => null,
  100. };
  101. }
  102. //public bool IncludePaidInvoices { get; set; }
  103. protected override void Reload(Filters<Invoice> criteria, Columns<Invoice> columns,
  104. ref SortOrder<Invoice>? sort,
  105. Action<CoreTable?, Exception?> action)
  106. {
  107. criteria.Add(MasterDetailFilter);
  108. base.Reload(criteria, columns, ref sort, action);
  109. }
  110. protected override bool CanCreateItems()
  111. {
  112. return base.CanCreateItems() && (Master == null || Master.ID != Guid.Empty);
  113. }
  114. public override Invoice CreateItem()
  115. {
  116. var result = base.CreateItem();
  117. result.JobLink.ID = Master?.ID ?? Guid.Empty;
  118. result.JobLink.Synchronise(Master ?? new Job());
  119. return result;
  120. }
  121. public override void LoadEditorButtons(Invoice item, DynamicEditorButtons buttons)
  122. {
  123. base.LoadEditorButtons(item, buttons);
  124. buttons.Add(new DynamicEditorButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), item, PrintInvoice));
  125. buttons.Add(new DynamicEditorButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), item, EmailInvoice));
  126. }
  127. //private Dictionary<Type, CoreTable> LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row)
  128. //{
  129. // Dictionary<Type, CoreTable> env = new Dictionary<Type, CoreTable>();
  130. // env[typeof(Invoice)] = new Client<Invoice>().Query(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  131. // env[typeof(Job)] = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid));
  132. // env[typeof(Customer)] = new Client<Customer>().Query(new Filter<Customer>(x => x.ID).IsEqualTo(customerid));
  133. // env[typeof(InvoiceLine)] = new Client<InvoiceLine>().Query(new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid));
  134. // return env;
  135. //}
  136. private ReportTemplate LoadTemplate(DataModel model, string templatename)
  137. {
  138. var sectionName = "InvoiceListGrid";
  139. var client = new Client<ReportTemplate>();
  140. var template = client.Load(
  141. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(sectionName)
  142. .And(x => x.DataModel).IsEqualTo(model.Name)
  143. .And(x => x.Name).IsEqualTo(templatename),
  144. new SortOrder<ReportTemplate>(x => x.Name)
  145. ).FirstOrDefault();
  146. if (template == null)
  147. template = new ReportTemplate { DataModel = model.Name, Section = sectionName, Name = templatename };
  148. return template;
  149. }
  150. private bool PrintInvoice2(Button btn, CoreRow[] rows)
  151. {
  152. if (rows.Length != 1)
  153. {
  154. MessageBox.Show("Please select one Invoice to print!");
  155. return false;
  156. }
  157. var row = rows.First();
  158. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  159. var JobID = row.Get<Invoice, Guid>(x => x.JobLink.ID);
  160. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  161. DoPrintInvoice(JobID, InvoiceID, CustomerID);
  162. return false;
  163. }
  164. private void PrintInvoice(object sender, object? item)
  165. {
  166. var inv = (Invoice)sender;
  167. DoPrintInvoice(inv.JobLink.ID, inv.ID, inv.CustomerLink.ID);
  168. }
  169. private void DoPrintInvoice(Guid jobid, Guid invoiceid, Guid customerid)
  170. {
  171. var model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  172. var template = LoadTemplate(model, "Invoice");
  173. ReportUtils.PreviewReport(template, model, false, Security.IsAllowed<CanDesignReports>());
  174. }
  175. private bool EmailInvoice2(Button btn, CoreRow[] rows)
  176. {
  177. if (rows.Length != 1)
  178. {
  179. MessageBox.Show("Please select an Invoice to print!");
  180. return false;
  181. }
  182. var row = rows.First();
  183. var InvoiceNumber = row.Get<Invoice, int>(x => x.Number);
  184. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  185. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  186. DoEmailInvoice(InvoiceID, InvoiceNumber, CustomerID);
  187. return false;
  188. }
  189. private void EmailInvoice(object sender, object? item)
  190. {
  191. if (item is not Invoice inv) return;
  192. DoEmailInvoice(inv.ID, inv.Number, inv.CustomerLink.ID);
  193. }
  194. private void DoEmailInvoice(Guid invoiceid, int invoicenumber, Guid customerid)
  195. {
  196. MessageBox.Show("PDF Functions broken in .NET6");
  197. // InvoiceDataModel model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  198. // model.Populate();
  199. //
  200. // var template = LoadTemplate(model, "Invoice");
  201. //
  202. // byte[] pdf = InABox.Reports.ReportUtils.ReportToPDF(template,model,false);
  203. // String filename = Path.Combine(Path.GetTempPath(), String.Format("Invoice {0}.pdf", invoicenumber));
  204. // File.WriteAllBytes(filename, pdf);
  205. //
  206. // //PdfDocument finalDoc = new PdfDocument();
  207. // //PdfDocument.Merge(finalDoc, filename);
  208. // //finalDoc.Save(filename);
  209. // //finalDoc.Close(true);
  210. //
  211. // Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
  212. //
  213. // var mailer = ClientFactory.CreateMailer();
  214. // var msg = mailer.CreateMessage();
  215. // msg.From = me.Email;
  216. //
  217. //
  218. // List<String> emails = new List<String>();
  219. // if (customerid != default(Guid))
  220. // {
  221. // 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));
  222. // emails.AddRange(contacts.Select(x => x.Contact.Email));
  223. // if (!emails.Any())
  224. // {
  225. // var customer = new Client<Customer>().Load(new Filter<Customer>(x => x.ID).IsEqualTo(customerid).And(x => x.Email).IsNotEqualTo("")).FirstOrDefault();
  226. // if (customer != null)
  227. // emails.Add(customer.Email);
  228. // }
  229. // }
  230. //
  231. // msg.To = emails;
  232. // msg.Attachments = new Tuple<String, byte[]>[] { new Tuple<String,byte[]>(filename, pdf) };
  233. // msg.Subject = "New Invoice Available";
  234. // msg.Body = "Please find your invoice attached";
  235. // mailer.SendMessage(msg);
  236. }
  237. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
  238. {
  239. _settings.Filters = settings;
  240. new UserConfiguration<InvoiceGridSettings>().Save(_settings);
  241. }
  242. }
  243. }