InvoiceGrid.cs 11 KB

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