StockMovementTimberlinePoster.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. using Comal.Classes;
  2. using CsvHelper.Configuration.Attributes;
  3. using CsvHelper.Configuration;
  4. using CsvHelper;
  5. using InABox.Core.Postable;
  6. using InABox.Core;
  7. using InABox.Poster.Timberline;
  8. using InABox.Scripting;
  9. using System.Collections.Generic;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using Microsoft.Win32;
  17. using CsvHelper.TypeConversion;
  18. using NPOI.SS.Formula.Functions;
  19. using Columns = InABox.Core.Columns;
  20. namespace PRS.Shared;
  21. public interface IStockMovementTimberlineLine
  22. {
  23. DateTime TransactionDate { get; set; }
  24. DateTime AccountingDate { get; set; }
  25. string Description { get; set; }
  26. double Amount { get; set; }
  27. string DebitAccount { get; set; }
  28. string CreditAccount { get; set; }
  29. string Reference1 { get; set; }
  30. string Reference2 { get; set; }
  31. }
  32. public enum StockMovementTimberlineTransactionType
  33. {
  34. APCost = 1,
  35. JCCost = 2,
  36. PRCost = 3,
  37. EQCost = 4,
  38. IVCost = 5
  39. }
  40. public class StockMovementTimberlineTransactionTypeConverter : DefaultTypeConverter
  41. {
  42. public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
  43. {
  44. if (Enum.TryParse<StockMovementTimberlineTransactionType>(text, out var type))
  45. {
  46. return type;
  47. }
  48. return base.ConvertFromString(text, row, memberMapData);
  49. }
  50. public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
  51. {
  52. if (value is StockMovementTimberlineTransactionType type)
  53. {
  54. return ((int)type).ToString();
  55. }
  56. return "";
  57. }
  58. }
  59. public class StockMovementTimberlineDirectCost : IStockMovementTimberlineLine
  60. {
  61. [Ignore]
  62. public StockMovementBatchType BatchType { get; set; }
  63. [Index(0)]
  64. public string RecordID { get; set; } = "DC";
  65. [Index(1)]
  66. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  67. public string Job { get; set; }
  68. [Index(2)]
  69. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  70. public string Extra { get; set; }
  71. [Index(3)]
  72. [TypeConverter(typeof(TimberlinePosterStringConverter), 12)]
  73. public string CostCode { get; set; }
  74. [Index(4)]
  75. [TypeConverter(typeof(TimberlinePosterStringConverter), 3)]
  76. public string Category { get; set; }
  77. [Index(5)]
  78. [TypeConverter(typeof(StockMovementTimberlineTransactionTypeConverter))]
  79. public StockMovementTimberlineTransactionType TransactionType { get; set; }
  80. [Index(6)]
  81. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  82. public DateTime TransactionDate { get; set; }
  83. [Index(7)]
  84. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  85. public DateTime AccountingDate { get; set; }
  86. [Index(8)]
  87. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  88. public string Description { get; set; }
  89. [Index(9)]
  90. public double Units { get; set; }
  91. [Index(10)]
  92. public double UnitCost { get; set; }
  93. [Index(11)]
  94. public double Amount { get; set; }
  95. [Index(12)]
  96. [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
  97. public string DebitAccount { get; set; }
  98. [Index(13)]
  99. [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
  100. public string CreditAccount { get; set; }
  101. [Index(14)]
  102. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  103. public string Reference1 { get; set; }
  104. [Index(15)]
  105. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  106. public string Reference2 { get; set; }
  107. [Index(16)]
  108. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  109. public string StandardItem { get; set; }
  110. }
  111. public class StockMovementTimberlineGL : IStockMovementTimberlineLine
  112. {
  113. [Ignore]
  114. public StockMovementBatchType BatchType { get; set; }
  115. [Index(0)]
  116. public string RecordID { get; set; } = "GL";
  117. [Index(1)]
  118. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  119. public DateTime TransactionDate { get; set; }
  120. [Index(2)]
  121. [TypeConverter(typeof(TimberlinePosterDateConverter))]
  122. public DateTime AccountingDate { get; set; }
  123. [Index(3)]
  124. [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
  125. public string Description { get; set; }
  126. [Index(4)]
  127. public double Amount { get; set; }
  128. [Index(5)]
  129. [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
  130. public string DebitAccount { get; set; }
  131. [Index(6)]
  132. [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
  133. public string CreditAccount { get; set; }
  134. [Index(7)]
  135. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  136. public string Reference1 { get; set; }
  137. [Index(8)]
  138. [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
  139. public string Reference2 { get; set; }
  140. }
  141. public class StockMovementTimberlineSettings : TimberlinePosterSettings<StockMovement>
  142. {
  143. [TextBoxEditor]
  144. public string StockTakeGL { get; set; }
  145. protected override string DefaultScript()
  146. {
  147. return @"
  148. using PRS.Shared;
  149. using InABox.Core;
  150. using System.Collections.Generic;
  151. public class Module
  152. {
  153. public void BeforePost(IDataModel<StockMovement> model)
  154. {
  155. // Perform pre-processing
  156. }
  157. public bool ProcessDirectCostLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineDirectCost line)
  158. {
  159. // Do extra processing for a direct cost line; return false to fail this movement
  160. return true;
  161. }
  162. public bool ProcessGLLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineGL line)
  163. {
  164. // Do extra processing for a GL line; return false to fail this movement
  165. return true;
  166. }
  167. public void AfterPost(IDataModel<StockMovement> model)
  168. {
  169. // Perform post-processing
  170. }
  171. }";
  172. }
  173. }
  174. public class StockMovementTimberlineResult : TimberlinePostResult<IStockMovementTimberlineLine, StockMovement>
  175. {
  176. }
  177. public class StockMovementTimberlinePoster : ITimberlinePoster<StockMovement, StockMovementTimberlineSettings>
  178. {
  179. public ScriptDocument? Script { get; set; }
  180. public StockMovementTimberlineSettings Settings { get; set; }
  181. public bool BeforePost(IDataModel<StockMovement> model)
  182. {
  183. model.SetIsDefault<Document>(false, alias: "CompanyLogo");
  184. model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
  185. model.SetIsDefault<Employee>(false);
  186. model.SetColumns(Columns.None<StockMovement>().Add(x => x.ID).Add(x => x.Transaction));
  187. model.AddChildTable<StockMovement, StockMovement>(x => x.Transaction, x => x.Transaction,
  188. parentalias: "StockMovement", childalias: "FullTransactions",
  189. isdefault: true,
  190. columns: Columns.None<StockMovement>().Add(x => x.ID)
  191. .Add(x => x.Transaction)
  192. .Add(x => x.Job.ID)
  193. .Add(x => x.Product.ID)
  194. .Add(x => x.Type)
  195. .Add(x => x.Units)
  196. .Add(x => x.Cost)
  197. .Add(x => x.Value)
  198. .Add(x => x.Date)
  199. .Add(x => x.Batch.ID)
  200. );
  201. model.AddLookupTable<StockMovement, Product>(x => x.Product.ID, x => x.ID, sourcealias: "FullTransactions",
  202. isdefault: true,
  203. columns: Columns.None<Product>().Add(x => x.ID)
  204. .Add(x => x.Name)
  205. .Add(x => x.CostCentre.Code)
  206. .Add(x => x.PurchaseGL.Code));
  207. model.AddLookupTable<StockMovement, Job>(x => x.Job.ID, x => x.ID, sourcealias: "FullTransactions",
  208. isdefault: true,
  209. columns: Columns.None<Job>().Add(x => x.ID)
  210. .Add(x => x.JobNumber));
  211. Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
  212. return true;
  213. }
  214. private bool ProcessDirectCostLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineDirectCost line)
  215. {
  216. return Script?.Execute(methodname: "ProcessDirectCostLine", parameters: new object[] { model, stockMovement, line }) != false;
  217. }
  218. private bool ProcessGLLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineGL line)
  219. {
  220. return Script?.Execute(methodname: "ProcessGLLine", parameters: new object[] { model, stockMovement, line }) != false;
  221. }
  222. private StockMovementTimberlineResult DoProcess(IDataModel<StockMovement> model)
  223. {
  224. var result = new StockMovementTimberlineResult();
  225. var firstMovements = model.GetTable<StockMovement>();
  226. var full = model.GetTable<StockMovement>("FullTransactions")
  227. .ToObjects<StockMovement>().GroupBy(x => x.Transaction);
  228. var products = model.GetTable<Product>().ToObjects<Product>()
  229. .ToDictionary(x => x.ID, x => x);
  230. var jobs = model.GetTable<Job>().ToObjects<Job>()
  231. .ToDictionary(x => x.ID, x => x);
  232. StockMovementTimberlineDirectCost CreateDirectCost(StockMovement movement)
  233. {
  234. var job = jobs[movement.Job.ID];
  235. var product = products[movement.Product.ID];
  236. var directCost = new StockMovementTimberlineDirectCost
  237. {
  238. Job = job.JobNumber,
  239. Extra = "",
  240. CostCode = product.CostCentre.Code,
  241. Category = "",
  242. Units = Math.Round(movement.Units, 4),
  243. UnitCost = Math.Round(movement.Cost, 4),
  244. TransactionType = StockMovementTimberlineTransactionType.IVCost
  245. };
  246. return ModifyLine(directCost, movement);
  247. }
  248. T ModifyLine<T>(T line, StockMovement movement)
  249. where T : IStockMovementTimberlineLine
  250. {
  251. var product = products[movement.Product.ID];
  252. line.TransactionDate = movement.Date;
  253. line.AccountingDate = movement.Date;
  254. line.Description = product.Name;
  255. line.Amount = Math.Round(movement.Value, 4);
  256. line.CreditAccount = product.PurchaseGL.Code;
  257. line.DebitAccount = product.SellGL.Code;
  258. return line;
  259. }
  260. foreach (var transaction in full)
  261. {
  262. var mvts = transaction.ToArray();
  263. // I think we will fail all the movements if any one movement in the transaction failed. All the successful ones,
  264. // rather than saving them with AddSuccess immediately, we will put here first, and only succeed them if every movement succeeded.
  265. var successful = new List<(StockMovement mvt, IStockMovementTimberlineLine? line)>();
  266. foreach(var mvt in mvts)
  267. {
  268. switch (mvt.Type)
  269. {
  270. case StockMovementType.Issue:
  271. if(mvt.Job.ID == Guid.Empty)
  272. {
  273. // Issue to General Stock
  274. var gl = new StockMovementTimberlineGL { };
  275. gl = ModifyLine(gl, mvt);
  276. gl.DebitAccount = Settings.StockTakeGL;
  277. if (ProcessGLLine(model, mvt, gl))
  278. {
  279. successful.Add((mvt, gl));
  280. }
  281. else
  282. {
  283. result.AddFailed(mvt, "Failed by script.");
  284. }
  285. }
  286. else
  287. {
  288. // Ignore issues to a job.
  289. successful.Add((mvt, null));
  290. }
  291. break;
  292. case StockMovementType.Receive:
  293. successful.Add((mvt, null));
  294. break;
  295. case StockMovementType.StockTake:
  296. if(mvt.Job.ID == Guid.Empty)
  297. {
  298. // StockTake in General Stock
  299. var gl = new StockMovementTimberlineGL { };
  300. gl = ModifyLine(gl, mvt);
  301. gl.DebitAccount = Settings.StockTakeGL;
  302. if (ProcessGLLine(model, mvt, gl))
  303. {
  304. successful.Add((mvt, gl));
  305. }
  306. else
  307. {
  308. result.AddFailed(mvt, "Failed by script.");
  309. }
  310. }
  311. else
  312. {
  313. // StockTake in Job Holding
  314. var dc = CreateDirectCost(mvt);
  315. if (ProcessDirectCostLine(model, mvt, dc))
  316. {
  317. successful.Add((mvt, dc));
  318. }
  319. else
  320. {
  321. result.AddFailed(mvt, "Failed by script.");
  322. }
  323. }
  324. break;
  325. case StockMovementType.TransferOut:
  326. case StockMovementType.TransferIn:
  327. if(mvt.Job.ID != Guid.Empty)
  328. {
  329. var directCost = CreateDirectCost(mvt);
  330. if(ProcessDirectCostLine(model, mvt, directCost))
  331. {
  332. successful.Add((mvt, directCost));
  333. }
  334. else
  335. {
  336. result.AddFailed(mvt, "Failed by script.");
  337. }
  338. }
  339. break;
  340. }
  341. }
  342. if(successful.Count < mvts.Length)
  343. {
  344. foreach(var (mvt, _) in successful)
  345. {
  346. result.AddFailed(mvt, "Transaction was unsuccessful.");
  347. }
  348. }
  349. else
  350. {
  351. foreach(var (mvt, item) in successful)
  352. {
  353. result.AddSuccess(mvt, item);
  354. }
  355. }
  356. }
  357. return result;
  358. }
  359. public IPostResult<StockMovement> Process(IDataModel<StockMovement> model)
  360. {
  361. var result = DoProcess(model);
  362. var dlg = new SaveFileDialog()
  363. {
  364. Title = "Select Output File",
  365. Filter = "CSV Files (*.csv)|*.csv"
  366. };
  367. if (dlg.ShowDialog() == true)
  368. {
  369. using (var writer = new StreamWriter(dlg.FileName))
  370. {
  371. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  372. foreach (var line in result.Exports.Distinct())
  373. {
  374. // Write the record.
  375. if(line is StockMovementTimberlineDirectCost dc)
  376. {
  377. csv.WriteRecord(dc);
  378. }
  379. else if(line is StockMovementTimberlineGL gl)
  380. {
  381. csv.WriteRecord(gl);
  382. }
  383. csv.NextRecord();
  384. }
  385. }
  386. }
  387. else
  388. {
  389. throw new PostCancelledException();
  390. }
  391. return result;
  392. }
  393. public void AfterPost(IDataModel<StockMovement> model, IPostResult<StockMovement> result)
  394. {
  395. Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
  396. }
  397. }
  398. public class StockMovementTimberlinePosterEngine<T> : TimberlinePosterEngine<StockMovement, StockMovementTimberlineSettings>
  399. {
  400. }