InvoiceListGrid.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Reports;
  10. using InABox.Reports.Common;
  11. using InABox.WPF;
  12. namespace PRSDesktop
  13. {
  14. public class InvoiceListGrid : DynamicDataGrid<Invoice>
  15. {
  16. public InvoiceListGrid()
  17. {
  18. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.EditRows,
  19. DynamicGridOption.SelectColumns);
  20. AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2);
  21. AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2);
  22. HiddenColumns.Add(x => x.CustomerLink.ID);
  23. }
  24. public Guid JobID { get; set; }
  25. protected override void Reload(Filters<Invoice> criteria, Columns<Invoice> columns, ref SortOrder<Invoice> sort,
  26. Action<CoreTable, Exception> action)
  27. {
  28. criteria.Add(new Filter<Invoice>(x => x.JobLink.ID).IsEqualTo(JobID));
  29. base.Reload(criteria, columns, ref sort, action);
  30. }
  31. protected override Invoice CreateItem()
  32. {
  33. var result = base.CreateItem();
  34. result.JobLink.ID = JobID;
  35. var job = new Client<Job>().Load(new Filter<Job>(x => x.ID).IsEqualTo(JobID)).FirstOrDefault();
  36. if (job != null)
  37. {
  38. //if (job.Account.ID != null)
  39. // CoreUtils.DeepClone(job.Account, result.CustomerLink);
  40. //else
  41. // CoreUtils.DeepClone(job.Customer, result.CustomerLink);
  42. }
  43. return result;
  44. }
  45. public override void LoadEditorButtons(Invoice item, DynamicEditorButtons buttons)
  46. {
  47. base.LoadEditorButtons(item, buttons);
  48. buttons.Add(new DynamicEditorButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), item, PrintInvoice));
  49. buttons.Add(new DynamicEditorButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), item, EmailInvoice));
  50. }
  51. //private Dictionary<Type, CoreTable> LoadReportData(Guid jobid, Guid invoiceid, Guid customerid) // CoreRow row)
  52. //{
  53. // Dictionary<Type, CoreTable> env = new Dictionary<Type, CoreTable>();
  54. // env[typeof(Invoice)] = new Client<Invoice>().Query(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  55. // env[typeof(Job)] = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid));
  56. // env[typeof(Customer)] = new Client<Customer>().Query(new Filter<Customer>(x => x.ID).IsEqualTo(customerid));
  57. // env[typeof(InvoiceLine)] = new Client<InvoiceLine>().Query(new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoiceid));
  58. // return env;
  59. //}
  60. private ReportTemplate LoadTemplate(DataModel model, string templatename)
  61. {
  62. var sectionName = "InvoiceListGrid";
  63. var client = new Client<ReportTemplate>();
  64. var template = client.Load(
  65. new Filter<ReportTemplate>(x => x.Section).IsEqualTo(sectionName)
  66. .And(x => x.DataModel).IsEqualTo(model.Name)
  67. .And(x => x.Name).IsEqualTo(templatename),
  68. new SortOrder<ReportTemplate>(x => x.Name)
  69. ).FirstOrDefault();
  70. if (template == null)
  71. template = new ReportTemplate { DataModel = model.Name, Section = sectionName, Name = templatename };
  72. return template;
  73. }
  74. private bool PrintInvoice2(Button btn, CoreRow[] rows)
  75. {
  76. if (rows.Length != 1)
  77. {
  78. MessageBox.Show("Please select one Invoice to print!");
  79. return false;
  80. }
  81. var row = rows.First();
  82. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  83. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  84. DoPrintInvoice(JobID, InvoiceID, CustomerID);
  85. return false;
  86. }
  87. private void PrintInvoice(object sender, object item)
  88. {
  89. var inv = (Invoice)sender;
  90. DoPrintInvoice(inv.JobLink.ID, inv.ID, inv.CustomerLink.ID);
  91. }
  92. private void DoPrintInvoice(Guid jobid, Guid invoiceid, Guid customerid)
  93. {
  94. var model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  95. var template = LoadTemplate(model, "Invoice");
  96. ReportUtils.PreviewReport(template, model, false, Security.IsAllowed<CanDesignReports>());
  97. }
  98. private bool EmailInvoice2(Button btn, CoreRow[] rows)
  99. {
  100. if (rows.Length != 1)
  101. {
  102. MessageBox.Show("Please select an Invoice to print!");
  103. return false;
  104. }
  105. var row = rows.First();
  106. var InvoiceNumber = row.Get<Invoice, int>(x => x.Number);
  107. var InvoiceID = row.Get<Invoice, Guid>(x => x.ID);
  108. var CustomerID = row.Get<Invoice, Guid>(x => x.CustomerLink.ID);
  109. DoEmailInvoice(InvoiceID, InvoiceNumber, CustomerID);
  110. return false;
  111. }
  112. private void EmailInvoice(object sender, object item)
  113. {
  114. var inv = (Invoice)item;
  115. DoEmailInvoice(inv.ID, inv.Number, inv.CustomerLink.ID);
  116. }
  117. private void DoEmailInvoice(Guid invoiceid, int invoicenumber, Guid customerid)
  118. {
  119. MessageBox.Show("PDF Functions broken in .NET6");
  120. // InvoiceDataModel model = new InvoiceDataModel(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid));
  121. // model.Populate();
  122. //
  123. // var template = LoadTemplate(model, "Invoice");
  124. //
  125. // byte[] pdf = InABox.Reports.ReportUtils.ReportToPDF(template,model,false);
  126. // String filename = Path.Combine(Path.GetTempPath(), String.Format("Invoice {0}.pdf", invoicenumber));
  127. // File.WriteAllBytes(filename, pdf);
  128. //
  129. // //PdfDocument finalDoc = new PdfDocument();
  130. // //PdfDocument.Merge(finalDoc, filename);
  131. // //finalDoc.Save(filename);
  132. // //finalDoc.Close(true);
  133. //
  134. // Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).FirstOrDefault();
  135. //
  136. // var mailer = ClientFactory.CreateMailer();
  137. // var msg = mailer.CreateMessage();
  138. // msg.From = me.Email;
  139. //
  140. //
  141. // List<String> emails = new List<String>();
  142. // if (customerid != default(Guid))
  143. // {
  144. // 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));
  145. // emails.AddRange(contacts.Select(x => x.Contact.Email));
  146. // if (!emails.Any())
  147. // {
  148. // var customer = new Client<Customer>().Load(new Filter<Customer>(x => x.ID).IsEqualTo(customerid).And(x => x.Email).IsNotEqualTo("")).FirstOrDefault();
  149. // if (customer != null)
  150. // emails.Add(customer.Email);
  151. // }
  152. // }
  153. //
  154. // msg.To = emails;
  155. // msg.Attachments = new Tuple<String, byte[]>[] { new Tuple<String,byte[]>(filename, pdf) };
  156. // msg.Subject = "New Invoice Available";
  157. // msg.Body = "Please find your invoice attached";
  158. // mailer.SendMessage(msg);
  159. }
  160. }
  161. }