InvoiceGrid.cs 11 KB

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