BillTimberlinePoster.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. using Comal.Classes;
  2. using ControlzEx.Standard;
  3. using CsvHelper;
  4. using CsvHelper.Configuration;
  5. using CsvHelper.Configuration.Attributes;
  6. using FastReport.Utils;
  7. using InABox.Core;
  8. using InABox.Core.Postable;
  9. using InABox.Poster.Timberline;
  10. using InABox.Scripting;
  11. using Microsoft.Win32;
  12. using NPOI.SS.Formula.Functions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  22. namespace PRS.Shared
  23. {
  24. public class BillTimberlineHeader
  25. {
  26. [Ignore]
  27. public List<BillTimberlineDistribution> Distributions { get; set; } = new();
  28. [Index(1)]
  29. public string RecordID { get; set; } = "APIF";
  30. [Index(2)]
  31. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  32. public string Vendor { get; set; } = "";
  33. [Index(3)]
  34. [TypeConverter(typeof(TimberlinePosterStringConverter), 15)]
  35. public string Invoice { get; set; } = "";
  36. [Index(4)]
  37. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  38. public string Description { get; set; } = "";
  39. [Index(5)]
  40. public double Amount { get; set; }
  41. [Index(6)]
  42. public double Tax { get; set; }
  43. [Index(7)]
  44. public double DiscountOffered { get; set; }
  45. [Index(8)]
  46. public double MiscDeduction { get; set; }
  47. [Index(9)]
  48. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  49. public DateTime InvoiceDate { get; set; }
  50. [Index(10)]
  51. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  52. public DateTime DateReceived { get; set; }
  53. [Index(11)]
  54. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  55. public DateTime DiscountDate { get; set; }
  56. [Index(12)]
  57. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  58. public DateTime PaymentDate { get; set; }
  59. [Index(13)]
  60. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  61. public DateTime AccountingDate { get; set; }
  62. [Index(14)]
  63. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  64. public string InvoiceCode1 { get; set; } = "";
  65. [Index(15)]
  66. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  67. public string InvoiceCode2 { get; set; } = "";
  68. [Index(16)]
  69. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  70. public string SmryPayeeName { get; set; } = "";
  71. [Index(17)]
  72. [TypeConverter(typeof(TimberlinePosterStringConverter), 33)]
  73. public string SmryPayeeAddress1 { get; set; } = "";
  74. [Index(18)]
  75. [TypeConverter(typeof(TimberlinePosterStringConverter), 33)]
  76. public string SmryPayeeAddress2 { get; set; } = "";
  77. [Index(19)]
  78. [TypeConverter(typeof(TimberlinePosterStringConverter), 15)]
  79. public string SmryPayeeCity { get; set; } = "";
  80. [Index(20)]
  81. [TypeConverter(typeof(TimberlinePosterStringConverter), 4)]
  82. public string SmryPayeeState { get; set; } = "";
  83. [Index(21)]
  84. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  85. public string SmryPayeeZip { get; set; } = "";
  86. }
  87. public class BillTimberlineDistribution
  88. {
  89. [Index(1)]
  90. public string RecordID { get; set; } = "APDF";
  91. [Index(2)]
  92. [TypeConverter(typeof(TimberlinePosterStringConverter), 12)]
  93. public string Commitment { get; set; } = "";
  94. [Index(3)]
  95. public int CommitmentLineItem { get; set; }
  96. [Index(4)]
  97. [TypeConverter(typeof(TimberlinePosterStringConverter), 7)]
  98. public string Equipment { get; set; } = "";
  99. [Index(5)]
  100. [TypeConverter(typeof(TimberlinePosterStringConverter), 6)]
  101. public string EQCostCode { get; set; } = "";
  102. [Index(6)]
  103. [TypeConverter(typeof(TimberlinePosterStringConverter), 6)]
  104. public string Job { get; set; } = "";
  105. [Index(7)]
  106. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  107. public string Extra { get; set; } = "";
  108. [Index(8)]
  109. [TypeConverter(typeof(TimberlinePosterStringConverter), 6)]
  110. public string CostCode { get; set; } = "";
  111. [Index(9)]
  112. [TypeConverter(typeof(TimberlinePosterStringConverter), 3)]
  113. public string Category { get; set; } = "";
  114. [Index(10)]
  115. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  116. public string BLStdItem { get; set; } = "";
  117. [Index(11)]
  118. [TypeConverter(typeof(TimberlinePosterStringConverter), 8)]
  119. public string Reserved { get; set; } = "";
  120. [Index(12)]
  121. [TypeConverter(typeof(TimberlinePosterStringConverter), 7)]
  122. public string ExpenseAccount { get; set; } = "";
  123. [Index(13)]
  124. [TypeConverter(typeof(TimberlinePosterStringConverter), 7)]
  125. public string APAccount { get; set; } = "";
  126. [Index(14)]
  127. public double TaxablePayments { get; set; }
  128. [Index(15)]
  129. [TypeConverter(typeof(TimberlinePosterStringConverter), 6)]
  130. public string TaxGroup { get; set; } = "";
  131. [Index(16)]
  132. public double Units { get; set; }
  133. [Index(17)]
  134. public double UnitCost { get; set; }
  135. [Index(18)]
  136. public double Amount { get; set; }
  137. [Index(19)]
  138. public double Tax { get; set; }
  139. [Index(20)]
  140. public double TaxLiability { get; set; }
  141. [Index(21)]
  142. public double DiscountOffered { get; set; }
  143. [Index(22)]
  144. public double Retainage { get; set; }
  145. [Index(23)]
  146. public double MiscDeduction { get; set; }
  147. [Index(24)]
  148. [BooleanTrueValues("t", "T", "1", "Y", "y")]
  149. [BooleanFalseValues("f", "F", "0", "N", "n")]
  150. public bool TaxablePaymentsExempt { get; set; }
  151. [Index(25)]
  152. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  153. public string DistCode { get; set; } = "";
  154. [Index(26)]
  155. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  156. public string MiscEntry1 { get; set; } = "";
  157. [Index(27)]
  158. public double MiscEntryUnits1 { get; set; }
  159. [Index(28)]
  160. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  161. public string MiscEntry2 { get; set; } = "";
  162. [Index(29)]
  163. public double MiscEntryUnits2 { get; set; }
  164. [Index(30)]
  165. public double MeterOdometer { get; set; }
  166. [Index(31)]
  167. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  168. public string Description { get; set; } = "";
  169. [Index(32)]
  170. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  171. public string Authorization { get; set; } = "";
  172. [Index(33)]
  173. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  174. public string JointPayee { get; set; } = "";
  175. }
  176. public class BillTimberlineSettings : TimberlinePosterSettings<Bill>
  177. {
  178. protected override string DefaultScript()
  179. {
  180. return @"
  181. using PRS.Shared;
  182. using InABox.Core;
  183. using System.Collections.Generic;
  184. public class Module
  185. {
  186. public void BeforePost(IDataModel<Bill> model)
  187. {
  188. // Perform pre-processing
  189. }
  190. public bool ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
  191. {
  192. // Do extra processing for a header line; return false to fail this header
  193. return true;
  194. }
  195. public bool ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
  196. {
  197. // Do extra processing for a distribution line; return false to fail this header
  198. return true;
  199. }
  200. public void AfterPost(IDataModel<Bill> model)
  201. {
  202. // Perform post-processing;
  203. }
  204. }";
  205. }
  206. }
  207. public class BillTimberlineResult : TimberlinePostResult<BillTimberlineHeader, Bill>
  208. {
  209. }
  210. public class BillTimberlinePoster : ITimberlinePoster<Bill, BillTimberlineSettings>
  211. {
  212. public ScriptDocument? Script { get; set; }
  213. public BillTimberlineSettings Settings { get; set; }
  214. public bool BeforePost(IDataModel<Bill> model)
  215. {
  216. model.SetIsDefault<Document>(false, alias: "CompanyLogo");
  217. model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
  218. model.SetIsDefault<Employee>(false);
  219. model.SetColumns(new Columns<Bill>(x => x.ID)
  220. .Add(x => x.SupplierLink.Code)
  221. .Add(x => x.Number)
  222. .Add(x => x.IncTax)
  223. .Add(x => x.Tax)
  224. .Add(x => x.BillDate)
  225. .Add(x => x.AccountingDate)
  226. .Add(x => x.PaymentDate));
  227. model.SetColumns(new Columns<BillLine>(x => x.ID)
  228. .Add(x => x.BillLink.ID)
  229. .Add(x => x.TaxCode.Code)
  230. .Add(x => x.IncTax)
  231. .Add(x => x.Tax)
  232. .Add(x => x.Description)
  233. .Add(x => x.OrderItem.ID),
  234. alias: "Bill_BillLine");
  235. model.SetIsDefault<BillLine>(true, "Bill_BillLine");
  236. model.AddChildTable<BillLine, PurchaseOrderItem>(x => x.OrderItem.ID, x => x.ID, isdefault: true,
  237. parentalias: "Bill_BillLine", childalias: "POItem",
  238. columns: new Columns<PurchaseOrderItem>(x => x.ID)
  239. .Add(x => x.PONumber)
  240. .Add(x => x.Job.JobNumber)
  241. .Add(x => x.Qty)
  242. .Add(x => x.Cost));
  243. Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
  244. return true;
  245. }
  246. private bool ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
  247. {
  248. return Script?.Execute(methodname: "ProcessHeader", parameters: new object[] { model, bill, header }) != false;
  249. }
  250. private bool ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
  251. {
  252. return Script?.Execute(methodname: "ProcessLine", parameters: new object[] { model, bill, distribution }) != false;
  253. }
  254. private BillTimberlineResult DoProcess(IDataModel<Bill> model)
  255. {
  256. var result = new BillTimberlineResult();
  257. var lines = model.GetTable<BillLine>("Bill_BillLine").ToObjects<BillLine>()
  258. .GroupBy(x => x.BillLink.ID).ToDictionary(x => x.Key, x => x.ToList());
  259. var purchaseOrderItems = model.GetTable<PurchaseOrderItem>("POItem").ToObjects<PurchaseOrderItem>()
  260. .ToDictionary(x => x.ID, x => x);
  261. foreach (var bill in model.GetTable<Bill>().ToObjects<Bill>())
  262. {
  263. var apif = new BillTimberlineHeader
  264. {
  265. Vendor = bill.SupplierLink.Code,
  266. Invoice = bill.Number,
  267. Description = "",
  268. Amount = bill.IncTax,
  269. Tax = bill.Tax,
  270. // DiscountOffered
  271. // Misc. Deduction
  272. InvoiceDate = bill.BillDate,
  273. // DateReceived doesn't exist
  274. DiscountDate = bill.BillDate,
  275. PaymentDate = bill.PaymentDate,
  276. AccountingDate = bill.AccountingDate,
  277. // InvoiceCode1
  278. // InvoiceCode2
  279. // SmryPayeeName
  280. // SmryPayeeAddress1
  281. // SmryPayeeAddress2
  282. // SmryPayeeCity
  283. // SmryPayeeState
  284. // SmryPayeeZip
  285. };
  286. if (!ProcessHeader(model, bill, apif))
  287. {
  288. result.AddFailed(bill, "Failed by script.");
  289. }
  290. else
  291. {
  292. var success = true;
  293. var billLines = lines.GetValueOrDefault(bill.ID) ?? new List<BillLine>();
  294. foreach (var billLine in billLines)
  295. {
  296. var apdf = new BillTimberlineDistribution
  297. {
  298. // Equipment
  299. // EQ Cost Code
  300. // Extra
  301. // Cost Code
  302. // Category
  303. /// BL STd Item
  304. // Reserved
  305. // Expense account
  306. // AP Account
  307. // Taxable Payments
  308. TaxGroup = billLine.TaxCode.Code,
  309. Amount = billLine.IncTax,
  310. Tax = billLine.Tax,
  311. // Tax Liability
  312. // Discount OFfered
  313. // Retainage
  314. // MIsc Deduction
  315. // Tax Payments Exempt
  316. // Dist Code
  317. // Misc Entry 1
  318. // Misc Units 1
  319. // Misc Entry 2
  320. // Misc Units 2
  321. // Meter
  322. Description = billLine.Description,
  323. // Authorization
  324. // Joint Payee
  325. };
  326. if (purchaseOrderItems.TryGetValue(billLine.OrderItem.ID, out var poItem))
  327. {
  328. apdf.Commitment = poItem.PONumber;
  329. apdf.Job = poItem.Job.JobNumber;
  330. if (int.TryParse(poItem.ReceivedReference, out var itemNumber))
  331. {
  332. apdf.CommitmentLineItem = itemNumber;
  333. billLine.PostedReference = poItem.ReceivedReference;
  334. }
  335. apdf.Units = poItem.Qty;
  336. apdf.UnitCost = poItem.Cost;
  337. }
  338. if (!ProcessLine(model, billLine, apdf))
  339. {
  340. success = false;
  341. break;
  342. }
  343. apif.Distributions.Add(apdf);
  344. }
  345. if (success)
  346. {
  347. foreach(var billLine in billLines)
  348. {
  349. result.AddFragment(billLine);
  350. }
  351. result.AddSuccess(bill, apif);
  352. }
  353. else
  354. {
  355. result.AddFailed(bill, "Failed by script.");
  356. }
  357. }
  358. }
  359. return result;
  360. }
  361. public IPostResult<Bill> Process(IDataModel<Bill> model)
  362. {
  363. var result = DoProcess(model);
  364. var dlg = new SaveFileDialog()
  365. {
  366. Filter = "CSV Files (*.csv)|*.csv"
  367. };
  368. if (dlg.ShowDialog() == true)
  369. {
  370. using (var writer = new StreamWriter(dlg.FileName))
  371. {
  372. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  373. foreach (var apif in result.Exports)
  374. {
  375. csv.WriteRecord(apif);
  376. csv.NextRecord();
  377. foreach (var apdf in apif.Distributions)
  378. {
  379. csv.WriteRecord(apdf);
  380. csv.NextRecord();
  381. }
  382. }
  383. }
  384. /*while (true)
  385. {
  386. var logDlg = new OpenFileDialog
  387. {
  388. InitialDirectory = Path.GetDirectoryName(dlg.FileName),
  389. FileName = "JCREJECT.JCC",
  390. Filter = "Rejected Item Files (*.jcc) | *.jcc;*.JCC | All Files (*.*) | *.*",
  391. Title = "Please select JCREJECT.JCC"
  392. };
  393. if (logDlg.ShowDialog() == true)
  394. {
  395. var rejectedHeaders = new List<BillTimberlineHeader?>();
  396. var rejectedLines = new List<BillTimberlineDistribution?>();
  397. BillTimberlineHeader? lastHeader = null;
  398. using (var reader = new StreamReader(logDlg.FileName))
  399. {
  400. using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
  401. {
  402. HasHeaderRecord = false
  403. });
  404. var i = 1;
  405. while (csv.Read())
  406. {
  407. var id = csv.GetField(0);
  408. if (id == "APIF")
  409. {
  410. var header = csv.GetRecord<BillTimberlineHeader>();
  411. if (header is not null)
  412. {
  413. var entry = result.Items.FirstOrDefault(x => x.Item2?.Invoice.Equals(header.Invoice) == true);
  414. if (entry is not null)
  415. {
  416. (entry.Item1 as IPostable).FailPost("");
  417. }
  418. lastHeader = header;
  419. }
  420. else
  421. {
  422. Logger.Send(LogType.Error, "", "Bill Timberline export: Unable to parse header from CSV line in rejection file.");
  423. MessageBox.Show($"Invalid line {i} in file; skipping.");
  424. }
  425. }
  426. else if (id == "APDF")
  427. {
  428. var line = csv.GetRecord<BillTimberlineDistribution>();
  429. if (line is not null)
  430. {
  431. var entry = result.Items.FirstOrDefault(x => x.Item2?.Invoice.Equals(line.Invoice) == true);
  432. if (entry is not null)
  433. {
  434. (entry.Item1 as IPostable).FailPost("");
  435. }
  436. }
  437. else
  438. {
  439. Logger.Send(LogType.Error, "", "Bill Timberline export: Unable to parse line from CSV line in rejection file.");
  440. MessageBox.Show("Invalid line in file; skipping.");
  441. }
  442. }
  443. ++i;
  444. }
  445. }
  446. return result;
  447. }
  448. else
  449. {
  450. if (MessageBox.Show("Do you wish to cancel the export?", "Cancel Export?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  451. {
  452. throw new PostCancelledException();
  453. }
  454. else if (MessageBox.Show("Did everything post successfully?", "Successful?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  455. {
  456. return result;
  457. }
  458. }
  459. }*/
  460. return result;
  461. }
  462. else
  463. {
  464. throw new PostCancelledException();
  465. }
  466. }
  467. public void AfterPost(IDataModel<Bill> model, IPostResult<Bill> result)
  468. {
  469. Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
  470. }
  471. }
  472. public class BillTimberlinePosterEngine<T> : TimberlinePosterEngine<Bill, BillTimberlineSettings>
  473. {
  474. }
  475. }