BillTimberlinePoster.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using Comal.Classes;
  2. using ControlzEx.Standard;
  3. using CsvHelper;
  4. using CsvHelper.Configuration.Attributes;
  5. using FastReport.DevComponents.WinForms.Drawing;
  6. using InABox.Core;
  7. using InABox.Core.Postable;
  8. using InABox.Poster.Timberline;
  9. using InABox.Scripting;
  10. using Microsoft.Win32;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  19. namespace PRS.Shared
  20. {
  21. public class BillTimberlineHeader
  22. {
  23. [Ignore]
  24. public List<BillTimberlineDistribution> Distributions { get; set; } = new();
  25. [Index(1)]
  26. public string RecordID { get; set; } = "APIF";
  27. [Index(2)]
  28. public string Vendor { get; set; }
  29. [Index(3)]
  30. public string Invoice { get; set; }
  31. [Index(4)]
  32. public string Description { get; set; }
  33. [Index(5)]
  34. public double Amount { get; set; }
  35. [Index(6)]
  36. public double Tax { get; set; }
  37. [Index(7)]
  38. public double DiscountOffered { get; set; }
  39. [Index(8)]
  40. public double MiscDeduction { get; set; }
  41. [Index(9)]
  42. public DateTime InvoiceDate { get; set; }
  43. [Index(10)]
  44. public DateTime DateReceived { get; set; }
  45. [Index(11)]
  46. public DateTime DiscountDate { get; set; }
  47. [Index(12)]
  48. public DateTime PaymentDate { get; set; }
  49. [Index(13)]
  50. public DateTime AccountingDate { get; set; }
  51. [Index(14)]
  52. public string InvoiceCode1 { get; set; }
  53. [Index(15)]
  54. public string InvoiceCode2 { get; set; }
  55. [Index(16)]
  56. public string SmryPayeeName { get; set; }
  57. [Index(17)]
  58. public string SmryPayeeAddress1 { get; set; }
  59. [Index(18)]
  60. public string SmryPayeeAddress2 { get; set; }
  61. [Index(19)]
  62. public string SmryPayeeCity { get; set; }
  63. [Index(20)]
  64. public string SmryPayeeState { get; set; }
  65. [Index(21)]
  66. public string SmryPayeeZip { get; set; }
  67. }
  68. public class BillTimberlineDistribution
  69. {
  70. [Index(1)]
  71. public string RecordID { get; set; } = "APDF";
  72. [Index(2)]
  73. public string Commitment { get; set; }
  74. [Index(3)]
  75. public int CommitmentLineItem { get; set; }
  76. [Index(4)]
  77. public string Equipment { get; set; }
  78. [Index(5)]
  79. public string EQCostCode { get; set; }
  80. [Index(6)]
  81. public string Job { get; set; }
  82. [Index(7)]
  83. public string Extra { get; set; }
  84. [Index(8)]
  85. public string CostCode { get; set; }
  86. [Index(9)]
  87. public string Category { get; set; }
  88. [Index(10)]
  89. public string BLStdItem { get; set; }
  90. [Index(11)]
  91. public string Reserved { get; set; }
  92. [Index(12)]
  93. public string ExpenseAccount { get; set; }
  94. [Index(13)]
  95. public string APAccount { get; set; }
  96. [Index(14)]
  97. public double TaxablePayments { get; set; }
  98. [Index(15)]
  99. public string TaxGroup { get; set; }
  100. [Index(16)]
  101. public double Units { get; set; }
  102. [Index(17)]
  103. public double UnitCost { get; set; }
  104. [Index(18)]
  105. public double Amount { get; set; }
  106. [Index(19)]
  107. public double Tax { get; set; }
  108. [Index(20)]
  109. public double TaxLiability { get; set; }
  110. [Index(21)]
  111. public double DiscountOffered { get; set; }
  112. [Index(22)]
  113. public double Retainage { get; set; }
  114. [Index(23)]
  115. public double MiscDeduction { get; set; }
  116. [Index(24)]
  117. [BooleanTrueValues("t", "T", "1", "Y", "y")]
  118. [BooleanFalseValues("f", "F", "0", "N", "n")]
  119. public bool TaxablePaymentsExempt { get; set; }
  120. [Index(25)]
  121. public string DistCode { get; set; }
  122. [Index(26)]
  123. public string MiscEntry1 { get; set; }
  124. [Index(27)]
  125. public double MiscEntryUnits1 { get; set; }
  126. [Index(28)]
  127. public string MiscEntry2 { get; set; }
  128. [Index(29)]
  129. public double MiscEntryUnits2 { get; set; }
  130. [Index(30)]
  131. public double MeterOdometer { get; set; }
  132. [Index(31)]
  133. public string Description { get; set; }
  134. [Index(32)]
  135. public string Authorization { get; set; }
  136. [Index(33)]
  137. public string JointPayee { get; set; }
  138. }
  139. public class BillTimberlineSettings : TimberlinePosterSettings<Bill>
  140. {
  141. protected override string DefaultScript()
  142. {
  143. return @"
  144. using PRS.Shared;
  145. using InABox.Core;
  146. using System.Collections.Generic;
  147. public class Module
  148. {
  149. public void BeforePost(IDataModel<Bill> model)
  150. {
  151. // Perform pre-processing
  152. }
  153. public void ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
  154. {
  155. // Do extra processing for a header line
  156. }
  157. public void ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
  158. {
  159. // Do extra processing for a distribution line
  160. }
  161. public void AfterPost(IDataModel<Bill> model)
  162. {
  163. // Perform post-processing
  164. }
  165. }";
  166. }
  167. }
  168. public class BillTimberlinePoster : ITimberlinePoster<Bill, BillTimberlineSettings>
  169. {
  170. public ScriptDocument? Script { get; set; }
  171. public event ITimberlinePoster<Bill, BillTimberlineSettings>.AddFragmentCallback? AddFragment;
  172. public bool BeforePost(IDataModel<Bill> model)
  173. {
  174. model.SetIsDefault<Document>(false, alias: "CompanyLogo");
  175. model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
  176. model.SetIsDefault<Employee>(false);
  177. model.SetColumns(new Columns<Bill>(x => x.ID)
  178. .Add(x => x.SupplierLink.Code)
  179. .Add(x => x.Number)
  180. .Add(x => x.IncTax)
  181. .Add(x => x.Tax)
  182. .Add(x => x.BillDate)
  183. .Add(x => x.AccountingDate)
  184. .Add(x => x.PaymentDate));
  185. model.SetColumns(new Columns<BillLine>(x => x.ID)
  186. .Add(x => x.BillLink.ID)
  187. .Add(x => x.TaxCode.Code)
  188. .Add(x => x.IncTax)
  189. .Add(x => x.Tax)
  190. .Add(x => x.Description)
  191. .Add(x => x.OrderItem.ID),
  192. alias: "Bill_BillLine");
  193. model.SetIsDefault<BillLine>(true, "Bill_BillLine");
  194. model.AddChildTable<BillLine, PurchaseOrderItem>(x => x.OrderItem.ID, x => x.ID, isdefault: true,
  195. parentalias: "Bill_BillLine", childalias: "POItem",
  196. columns: new Columns<PurchaseOrderItem>(x => x.ID)
  197. .Add(x => x.PONumber)
  198. .Add(x => x.Job.JobNumber)
  199. .Add(x => x.Qty)
  200. .Add(x => x.Cost));
  201. Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
  202. return true;
  203. }
  204. private void ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
  205. {
  206. Script?.Execute(methodname: "ProcessHeader", parameters: new object[] { model, bill, header });
  207. }
  208. private void ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
  209. {
  210. Script?.Execute(methodname: "ProcessLine", parameters: new object[] { model, bill, distribution });
  211. }
  212. private List<BillTimberlineHeader> DoProcess(IDataModel<Bill> model)
  213. {
  214. var apifs = new List<BillTimberlineHeader>();
  215. var lines = model.GetTable<BillLine>("Bill_BillLine").ToObjects<BillLine>()
  216. .GroupBy(x => x.BillLink.ID).ToDictionary(x => x.Key, x => x.ToList());
  217. var purchaseOrderItems = model.GetTable<PurchaseOrderItem>("POItem").ToObjects<PurchaseOrderItem>()
  218. .ToDictionary(x => x.ID, x => x);
  219. foreach (var bill in model.GetTable<Bill>().ToObjects<Bill>())
  220. {
  221. var apif = new BillTimberlineHeader
  222. {
  223. Vendor = bill.SupplierLink.Code,
  224. Invoice = bill.Number,
  225. Description = "",
  226. Amount = bill.IncTax,
  227. Tax = bill.Tax,
  228. // DiscountOffered
  229. // Misc. Deduction
  230. InvoiceDate = bill.BillDate,
  231. // DateReceived doesn't exist
  232. DiscountDate = bill.BillDate,
  233. PaymentDate = bill.PaymentDate,
  234. AccountingDate = bill.AccountingDate,
  235. // InvoiceCode1
  236. // InvoiceCode2
  237. // SmryPayeeName
  238. // SmryPayeeAddress1
  239. // SmryPayeeAddress2
  240. // SmryPayeeCity
  241. // SmryPayeeState
  242. // SmryPayeeZip
  243. };
  244. ProcessHeader(model, bill, apif);
  245. foreach (var billLine in lines.GetValueOrDefault(bill.ID) ?? Enumerable.Empty<BillLine>())
  246. {
  247. var apdf = new BillTimberlineDistribution
  248. {
  249. // Equipment
  250. // EQ Cost Code
  251. // Extra
  252. // Cost Code
  253. // Category
  254. /// BL STd Item
  255. // Reserved
  256. // Expense account
  257. // AP Account
  258. // Taxable Payments
  259. TaxGroup = billLine.TaxCode.Code,
  260. Amount = billLine.IncTax,
  261. Tax = billLine.Tax,
  262. // Tax Liability
  263. // Discount OFfered
  264. // Retainage
  265. // MIsc Deduction
  266. // Tax Payments Exempt
  267. // Dist Code
  268. // Misc Entry 1
  269. // Misc Units 1
  270. // Misc Entry 2
  271. // Misc Units 2
  272. // Meter
  273. Description = billLine.Description,
  274. // Authorization
  275. // Joint Payee
  276. };
  277. if(purchaseOrderItems.TryGetValue(billLine.OrderItem.ID, out var poItem))
  278. {
  279. apdf.Commitment = poItem.PONumber;
  280. apdf.Job = poItem.Job.JobNumber;
  281. if (int.TryParse(poItem.ReceivedReference, out var itemNumber))
  282. {
  283. apdf.CommitmentLineItem = itemNumber;
  284. billLine.PostedReference = poItem.ReceivedReference;
  285. }
  286. apdf.Units = poItem.Qty;
  287. apdf.UnitCost = poItem.Cost;
  288. }
  289. ProcessLine(model, billLine, apdf);
  290. apif.Distributions.Add(apdf);
  291. AddFragment?.Invoke(billLine);
  292. }
  293. apifs.Add(apif);
  294. }
  295. return apifs;
  296. }
  297. public bool Process(IDataModel<Bill> model)
  298. {
  299. var apifs = DoProcess(model);
  300. var dlg = new SaveFileDialog()
  301. {
  302. Filter = "CSV Files (*.csv)|*.csv"
  303. };
  304. if (dlg.ShowDialog() == true)
  305. {
  306. using var writer = new StreamWriter(dlg.FileName);
  307. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  308. foreach(var apif in apifs)
  309. {
  310. csv.WriteRecord(apif);
  311. csv.NextRecord();
  312. foreach(var apdf in apif.Distributions)
  313. {
  314. csv.WriteRecord(apdf);
  315. csv.NextRecord();
  316. }
  317. }
  318. return true;
  319. }
  320. else
  321. {
  322. throw new PostCancelledException();
  323. }
  324. }
  325. public void AfterPost(IDataModel<Bill> model)
  326. {
  327. Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
  328. }
  329. }
  330. public class BillTimberlinePosterEngine<T> : TimberlinePosterEngine<Bill, BillTimberlineSettings>
  331. {
  332. }
  333. }