BillTimberlinePoster.cs 13 KB

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