BillTimberlinePoster.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 bool BeforePost(IDataModel<Bill> model)
  172. {
  173. model.SetIsDefault<Document>(false, alias: "CompanyLogo");
  174. model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
  175. model.SetIsDefault<Employee>(false);
  176. model.SetIsDefault<BillLine>(true, "Bill_BillLine");
  177. model.AddChildTable<BillLine, PurchaseOrderItem>(x => x.OrderItem.ID, x => x.ID, isdefault: true, parentalias: "Bill_BillLine", childalias: "POItem");
  178. Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
  179. return true;
  180. }
  181. private void ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
  182. {
  183. Script?.Execute(methodname: "ProcessHeader", parameters: new object[] { model, bill, header });
  184. }
  185. private void ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
  186. {
  187. Script?.Execute(methodname: "ProcessLine", parameters: new object[] { model, bill, distribution });
  188. }
  189. private List<BillTimberlineHeader> DoProcess(IDataModel<Bill> model)
  190. {
  191. var apifs = new List<BillTimberlineHeader>();
  192. foreach(var bill in model.GetTable<Bill>().ToObjects<Bill>())
  193. {
  194. var apif = new BillTimberlineHeader
  195. {
  196. Vendor = bill.SupplierLink.Code,
  197. Invoice = bill.Number,
  198. Description = "",
  199. Amount = bill.IncTax,
  200. Tax = bill.Tax,
  201. // DiscountOffered
  202. // Misc. Deduction
  203. InvoiceDate = bill.BillDate,
  204. // DateReceived doesn't exist
  205. DiscountDate = bill.BillDate,
  206. PaymentDate = bill.PaymentDate,
  207. AccountingDate = bill.AccountingDate,
  208. // InvoiceCode1
  209. // InvoiceCode2
  210. // SmryPayeeName
  211. // SmryPayeeAddress1
  212. // SmryPayeeAddress2
  213. // SmryPayeeCity
  214. // SmryPayeeState
  215. // SmryPayeeZip
  216. };
  217. ProcessHeader(model, bill, apif);
  218. var purchaseOrderItems = model.GetTable<PurchaseOrderItem>("POItem").ToObjects<PurchaseOrderItem>()
  219. .ToDictionary(x => x.ID, x => x);
  220. foreach (var billLine in model.GetTable<BillLine>("Bill_BillLine").ToObjects<BillLine>())
  221. {
  222. var apdf = new BillTimberlineDistribution
  223. {
  224. // Equipment
  225. // EQ Cost Code
  226. // Extra
  227. // Cost Code
  228. // Category
  229. /// BL STd Item
  230. // Reserved
  231. // Expense account
  232. // AP Account
  233. // Taxable Payments
  234. TaxGroup = billLine.TaxCode.Code,
  235. Amount = billLine.IncTax,
  236. Tax = billLine.Tax,
  237. // Tax Liability
  238. // Discount OFfered
  239. // Retainage
  240. // MIsc Deduction
  241. // Tax Payments Exempt
  242. // Dist Code
  243. // Misc Entry 1
  244. // Misc Units 1
  245. // Misc Entry 2
  246. // Misc Units 2
  247. // Meter
  248. Description = billLine.Description,
  249. // Authorization
  250. // Joint Payee
  251. };
  252. if(purchaseOrderItems.TryGetValue(billLine.OrderItem.ID, out var poItem))
  253. {
  254. apdf.Commitment = poItem.PONumber;
  255. apdf.Job = poItem.Job.JobNumber;
  256. // apdf.CommitmentLineItem : Leaving this blank
  257. apdf.Units = poItem.Qty;
  258. apdf.UnitCost = poItem.Cost;
  259. }
  260. ProcessLine(model, billLine, apdf);
  261. apif.Distributions.Add(apdf);
  262. }
  263. apifs.Add(apif);
  264. }
  265. return apifs;
  266. }
  267. public bool Process(IDataModel<Bill> model)
  268. {
  269. var apifs = DoProcess(model);
  270. var dlg = new SaveFileDialog()
  271. {
  272. Filter = "CSV Files (*.csv)|*.csv"
  273. };
  274. if (dlg.ShowDialog() == true)
  275. {
  276. using var writer = new StreamWriter(dlg.FileName);
  277. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  278. foreach(var apif in apifs)
  279. {
  280. csv.WriteRecord(apif);
  281. csv.NextRecord();
  282. foreach(var apdf in apif.Distributions)
  283. {
  284. csv.WriteRecord(apdf);
  285. csv.NextRecord();
  286. }
  287. }
  288. return true;
  289. }
  290. else
  291. {
  292. throw new PostCancelledException();
  293. }
  294. }
  295. public void AfterPost(IDataModel<Bill> model)
  296. {
  297. Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
  298. }
  299. }
  300. public class BillTimberlinePosterEngine<T> : TimberlinePosterEngine<Bill, BillTimberlineSettings>
  301. {
  302. }
  303. }