InvoiceListGrid.cs 7.7 KB

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