InvoiceGrid.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. using PRSDesktop;
  18. namespace PRSDesktop
  19. {
  20. public class InvoiceGridSettings : IUserConfigurationSettings
  21. {
  22. [Obsolete]
  23. private CoreFilterDefinition? _currentFilter;
  24. [Obsolete]
  25. public CoreFilterDefinition? CurrentFilter
  26. {
  27. get => _currentFilter;
  28. set
  29. {
  30. if (value is not null)
  31. {
  32. Filters = new DynamicGridSelectedFilterSettings(new List<CoreFilterDefinition> { value }, false, null);
  33. }
  34. }
  35. }
  36. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  37. }
  38. public class InvoiceGrid : DynamicDataGrid<Invoice>, IMasterDetailControl<Job,Invoice>
  39. {
  40. private InvoiceGridSettings _settings;
  41. public Job? Master { get; set; }
  42. public Filter<Invoice> MasterDetailFilter => Master != null
  43. ? Master.ID != Guid.Empty
  44. ? new Filter<Invoice>(x => x.JobLink.ID).IsEqualTo(Master.ID)
  45. : new Filter<Invoice>().None()
  46. : new Filter<Invoice>().All();
  47. protected override void Init()
  48. {
  49. base.Init();
  50. _settings = new UserConfiguration<InvoiceGridSettings>().Load();
  51. FilterComponent.SetSettings(_settings.Filters, false);
  52. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  53. AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2);
  54. AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2);
  55. HiddenColumns.Add(x => x.CustomerLink.ID);
  56. HiddenColumns.Add(x => x.JobLink.ID);
  57. HiddenColumns.Add(x => x.SellGL.ID);
  58. PostUtils.AddPostColumn(this);
  59. }
  60. protected override void DoReconfigure(DynamicGridOptions options)
  61. {
  62. base.DoReconfigure(options);
  63. options.RecordCount = true;
  64. options.AddRows = true;
  65. options.DeleteRows = true;
  66. options.EditRows = true;
  67. options.SelectColumns = true;
  68. options.FilterRows = true;
  69. }
  70. protected override void Reload(
  71. Filters<Invoice> criteria, Columns<Invoice> columns, ref SortOrder<Invoice>? sort,
  72. CancellationToken token, Action<CoreTable?, Exception?> action)
  73. {
  74. criteria.Add(MasterDetailFilter);
  75. base.Reload(criteria, columns, ref sort, token, action);
  76. }
  77. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  78. {
  79. if(Master == null || Master.ID == Guid.Empty || Master.JobType == JobType.Service)
  80. {
  81. base.DoAdd(OpenEditorOnDirectEdit);
  82. }
  83. else
  84. {
  85. var window = new ProgressClaimWindow(Master.ID, Guid.Empty);
  86. if(window.ShowDialog() == true)
  87. {
  88. var invoice = new Invoice();
  89. invoice.Type = InvoiceType.ProgressClaim;
  90. invoice.Description = $"Progress claim for {DateTime.Today:MMM yyyy}";
  91. //invoice.Date = DateTime.Today;
  92. invoice.JobLink.CopyFrom(Master);
  93. if (Master.Account.ID != Guid.Empty)
  94. invoice.CustomerLink.CopyFrom(Master.Account);
  95. else
  96. invoice.CustomerLink.CopyFrom(Master.Customer);
  97. invoice.Retained = window.Retained;
  98. var lines = new List<InvoiceLine>();
  99. foreach(var item in window.Items.Where(x => !x.Cost.IsEffectivelyEqual(0.0)))
  100. {
  101. var line = new InvoiceLine();
  102. line.Description = item.JobScope.Description;
  103. line.ExTax = item.Cost;
  104. line.TaxCode.CopyFrom(item.JobScope.TaxCode);
  105. line.Scope.CopyFrom(item.JobScope);
  106. line.SellGL.ID = item.GLCodeID;
  107. lines.Add(line);
  108. }
  109. Client.Save(invoice, "Progress claim created by user.");
  110. foreach(var line in lines)
  111. {
  112. line.InvoiceLink.ID = invoice.ID;
  113. }
  114. Client.Save(lines, "Progress claim created by user.");
  115. var results = Client.QueryMultiple(
  116. new KeyedQueryDef<StockMovement>(
  117. new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Guid.Empty)
  118. .And(x => x.Type).IsEqualTo(StockMovementType.Issue)
  119. .And(x => x.Job.ID).IsEqualTo(Master.ID),
  120. Columns.Required<StockMovement>().Add(x => x.ID).Add(x => x.Invoice.ID)),
  121. new KeyedQueryDef<Assignment>(
  122. new Filter<Assignment>(x => x.JobLink.ID).IsEqualTo(Master.ID)
  123. .And(x => x.Invoice.ID).IsEqualTo(Guid.Empty),
  124. Columns.Required<Assignment>().Add(x => x.Invoice.ID)));
  125. var movements = results.GetObjects<StockMovement>().ToList();
  126. foreach(var mvt in movements)
  127. {
  128. mvt.Invoice.ID = invoice.ID;
  129. }
  130. Client.Save(movements, "Attached to new progress claim.");
  131. var assignments = results.GetObjects<Assignment>().ToList();
  132. foreach(var ass in assignments)
  133. {
  134. ass.Invoice.ID = invoice.ID;
  135. }
  136. Client.Save(assignments, "Attached to new progress claim.");
  137. Refresh(false, true);
  138. }
  139. }
  140. }
  141. protected override void DoEdit()
  142. {
  143. if(Master == null || Master.ID == Guid.Empty || Master.JobType == JobType.Service)
  144. {
  145. base.DoEdit();
  146. }
  147. else
  148. {
  149. Guid invoiceid = SelectedRows.FirstOrDefault()?.Get<Invoice, Guid>(x => x.ID) ?? Guid.Empty;
  150. var window = new ProgressClaimWindow(Master.ID, invoiceid);
  151. if(window.ShowDialog() == true)
  152. {
  153. var invoice = Client.Query(
  154. new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid),
  155. Columns.Required<Invoice>()
  156. ).ToObjects<Invoice>().FirstOrDefault();
  157. invoice.Retained = window.Retained;
  158. Client.Save(invoice,"Progress claim updated by user.");
  159. var lines = Client.Query(
  160. new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid),
  161. Columns.None<InvoiceLine>().Add(x=>x.ID)).ToList<InvoiceLine>();
  162. Client.Delete(lines,"Invoice Updated");
  163. lines.Clear();
  164. foreach(var item in window.Items.Where(x => !x.Cost.IsEffectivelyEqual(0.0)))
  165. {
  166. var line = new InvoiceLine();
  167. line.InvoiceLink.ID = window.InvoiceID;
  168. line.Description = item.JobScope.Description;
  169. line.ExTax = item.Cost;
  170. line.TaxCode.CopyFrom(item.JobScope.TaxCode);
  171. line.Scope.CopyFrom(item.JobScope);
  172. lines.Add(line);
  173. }
  174. Client.Save(lines, "Progress claim updated by user.");
  175. Refresh(false, true);
  176. }
  177. }
  178. }
  179. protected override bool CanCreateItems()
  180. {
  181. return base.CanCreateItems() && (Master == null || Master.ID != Guid.Empty);
  182. }
  183. public override Invoice CreateItem()
  184. {
  185. var result = base.CreateItem();
  186. result.JobLink.ID = Master?.ID ?? Guid.Empty;
  187. result.JobLink.Synchronise(Master ?? new Job());
  188. return result;
  189. }
  190. public override void LoadEditorButtons(Invoice item, DynamicEditorButtons buttons)
  191. {
  192. base.LoadEditorButtons(item, buttons);
  193. buttons.Add(new DynamicEditorButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), item, PrintInvoice));
  194. buttons.Add(new DynamicEditorButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), item, EmailInvoice));
  195. }
  196. //private Dictionary<Type, CoreTable> LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row)
  197. //{
  198. // Dictionary<Type, CoreTable> env = new Dictionary<Type, CoreTable>();
  199. // env[typeof(Invoice)] = new Client<Invoice>().Query(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  200. // env[typeof(Job)] = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid));
  201. // env[typeof(Customer)] = new Client<Customer>().Query(new Filter<Customer>(x => x.ID).IsEqualTo(customerid));
  202. // env[typeof(InvoiceLine)] = new Client<InvoiceLine>().Query(new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid));
  203. // return env;
  204. //}
  205. private ReportTemplate LoadTemplate(DataModel model, string templatename)
  206. {
  207. var sectionName = "InvoiceListGrid";
  208. var client = new Client<ReportTemplate>();
  209. var template = client.Load(
  210. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(sectionName)
  211. .And(x => x.DataModel).IsEqualTo(model.Name)
  212. .And(x => x.Name).IsEqualTo(templatename),
  213. new SortOrder<ReportTemplate>(x => x.Name)
  214. ).FirstOrDefault();
  215. if (template == null)
  216. template = new ReportTemplate { DataModel = model.Name, Section = sectionName, Name = templatename };
  217. return template;
  218. }
  219. private bool PrintInvoice2(Button btn, CoreRow[] rows)
  220. {
  221. if (rows.Length != 1)
  222. {
  223. MessageBox.Show("Please select one Invoice to print!");
  224. return false;
  225. }
  226. var row = rows.First();
  227. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  228. var JobID = row.Get<Invoice, Guid>(x => x.JobLink.ID);
  229. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  230. DoPrintInvoice(JobID, InvoiceID, CustomerID);
  231. return false;
  232. }
  233. private void PrintInvoice(object sender, object? item)
  234. {
  235. var inv = (Invoice)sender;
  236. DoPrintInvoice(inv.JobLink.ID, inv.ID, inv.CustomerLink.ID);
  237. }
  238. private void DoPrintInvoice(Guid jobid, Guid invoiceid, Guid customerid)
  239. {
  240. var model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  241. var template = LoadTemplate(model, "Invoice");
  242. ReportUtils.PreviewReport(template, model, false, Security.IsAllowed<CanDesignReports>());
  243. }
  244. private bool EmailInvoice2(Button btn, CoreRow[] rows)
  245. {
  246. if (rows.Length != 1)
  247. {
  248. MessageBox.Show("Please select an Invoice to print!");
  249. return false;
  250. }
  251. var row = rows.First();
  252. var InvoiceNumber = row.Get<Invoice, int>(x => x.Number);
  253. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  254. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  255. DoEmailInvoice(InvoiceID, InvoiceNumber, CustomerID);
  256. return false;
  257. }
  258. private void EmailInvoice(object sender, object? item)
  259. {
  260. if (item is not Invoice inv) return;
  261. DoEmailInvoice(inv.ID, inv.Number, inv.CustomerLink.ID);
  262. }
  263. private void DoEmailInvoice(Guid invoiceid, int invoicenumber, Guid customerid)
  264. {
  265. MessageBox.Show("PDF Functions broken in .NET6");
  266. // InvoiceDataModel model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  267. // model.Populate();
  268. //
  269. // var template = LoadTemplate(model, "Invoice");
  270. //
  271. // byte[] pdf = InABox.Reports.ReportUtils.ReportToPDF(template,model,false);
  272. // String filename = Path.Combine(Path.GetTempPath(), String.Format("Invoice {0}.pdf", invoicenumber));
  273. // File.WriteAllBytes(filename, pdf);
  274. //
  275. // //PdfDocument finalDoc = new PdfDocument();
  276. // //PdfDocument.Merge(finalDoc, filename);
  277. // //finalDoc.Save(filename);
  278. // //finalDoc.Close(true);
  279. //
  280. // Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
  281. //
  282. // var mailer = ClientFactory.CreateMailer();
  283. // var msg = mailer.CreateMessage();
  284. // msg.From = me.Email;
  285. //
  286. //
  287. // List<String> emails = new List<String>();
  288. // if (customerid != default(Guid))
  289. // {
  290. // 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));
  291. // emails.AddRange(contacts.Select(x => x.Contact.Email));
  292. // if (!emails.Any())
  293. // {
  294. // var customer = new Client<Customer>().Load(new Filter<Customer>(x => x.ID).IsEqualTo(customerid).And(x => x.Email).IsNotEqualTo("")).FirstOrDefault();
  295. // if (customer != null)
  296. // emails.Add(customer.Email);
  297. // }
  298. // }
  299. //
  300. // msg.To = emails;
  301. // msg.Attachments = new Tuple<String, byte[]>[] { new Tuple<String,byte[]>(filename, pdf) };
  302. // msg.Subject = "New Invoice Available";
  303. // msg.Body = "Please find your invoice attached";
  304. // mailer.SendMessage(msg);
  305. }
  306. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
  307. {
  308. _settings.Filters = settings;
  309. new UserConfiguration<InvoiceGridSettings>().Save(_settings);
  310. }
  311. }
  312. }