123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using Comal.Classes;
- using CsvHelper.Configuration.Attributes;
- using CsvHelper.Configuration;
- using CsvHelper;
- using InABox.Core.Postable;
- using InABox.Core;
- using InABox.Poster.Timberline;
- using InABox.Scripting;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using Microsoft.Win32;
- using CsvHelper.TypeConversion;
- using NPOI.SS.Formula.Functions;
- using Columns = InABox.Core.Columns;
- namespace PRS.Shared;
- public interface IStockMovementTimberlineLine
- {
- DateTime TransactionDate { get; set; }
- DateTime AccountingDate { get; set; }
- string Description { get; set; }
- double Amount { get; set; }
- string DebitAccount { get; set; }
- string CreditAccount { get; set; }
- string Reference1 { get; set; }
- string Reference2 { get; set; }
- }
- public enum StockMovementTimberlineTransactionType
- {
- APCost = 1,
- JCCost = 2,
- PRCost = 3,
- EQCost = 4,
- IVCost = 5
- }
- public class StockMovementTimberlineTransactionTypeConverter : DefaultTypeConverter
- {
- public override object? ConvertFromString(string? text, IReaderRow row, MemberMapData memberMapData)
- {
- if (Enum.TryParse<StockMovementTimberlineTransactionType>(text, out var type))
- {
- return type;
- }
- return base.ConvertFromString(text, row, memberMapData);
- }
- public override string? ConvertToString(object? value, IWriterRow row, MemberMapData memberMapData)
- {
- if (value is StockMovementTimberlineTransactionType type)
- {
- return ((int)type).ToString();
- }
- return "";
- }
- }
- public class StockMovementTimberlineDirectCost : IStockMovementTimberlineLine
- {
- [Ignore]
- public StockMovementBatchType BatchType { get; set; }
- [Index(0)]
- public string RecordID { get; set; } = "DC";
- [Index(1)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Job { get; set; }
- [Index(2)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Extra { get; set; }
- [Index(3)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 12)]
- public string CostCode { get; set; }
- [Index(4)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 3)]
- public string Category { get; set; }
- [Index(5)]
- [TypeConverter(typeof(StockMovementTimberlineTransactionTypeConverter))]
- public StockMovementTimberlineTransactionType TransactionType { get; set; }
- [Index(6)]
- [TypeConverter(typeof(TimberlinePosterDateConverter))]
- public DateTime TransactionDate { get; set; }
- [Index(7)]
- [TypeConverter(typeof(TimberlinePosterDateConverter))]
- public DateTime AccountingDate { get; set; }
- [Index(8)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
- public string Description { get; set; }
- [Index(9)]
- public double Units { get; set; }
- [Index(10)]
- public double UnitCost { get; set; }
- [Index(11)]
- public double Amount { get; set; }
- [Index(12)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
- public string DebitAccount { get; set; }
- [Index(13)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
- public string CreditAccount { get; set; }
- [Index(14)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Reference1 { get; set; }
- [Index(15)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Reference2 { get; set; }
- [Index(16)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string StandardItem { get; set; }
- }
- public class StockMovementTimberlineGL : IStockMovementTimberlineLine
- {
- [Ignore]
- public StockMovementBatchType BatchType { get; set; }
- [Index(0)]
- public string RecordID { get; set; } = "GL";
- [Index(1)]
- [TypeConverter(typeof(TimberlinePosterDateConverter))]
- public DateTime TransactionDate { get; set; }
- [Index(2)]
- [TypeConverter(typeof(TimberlinePosterDateConverter))]
- public DateTime AccountingDate { get; set; }
- [Index(3)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 30)]
- public string Description { get; set; }
- [Index(4)]
- public double Amount { get; set; }
- [Index(5)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
- public string DebitAccount { get; set; }
- [Index(6)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 25)]
- public string CreditAccount { get; set; }
- [Index(7)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Reference1 { get; set; }
- [Index(8)]
- [TypeConverter(typeof(TimberlinePosterStringConverter), 10)]
- public string Reference2 { get; set; }
- }
- public class StockMovementTimberlineSettings : TimberlinePosterSettings<StockMovement>
- {
- [TextBoxEditor]
- public string StockTakeGL { get; set; }
- [CheckBoxEditor]
- public bool IgnoreNetZeroTransfers { get; set; }
- protected override string DefaultScript()
- {
- return @"
- using PRS.Shared;
- using InABox.Core;
- using System.Collections.Generic;
- public class Module
- {
- public void BeforePost(IDataModel<StockMovement> model)
- {
- // Perform pre-processing
- }
- public bool ProcessDirectCostLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineDirectCost line)
- {
- // Do extra processing for a direct cost line; return false to fail this movement
- return true;
- }
- public bool ProcessGLLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineGL line)
- {
- // Do extra processing for a GL line; return false to fail this movement
- return true;
- }
- public void AfterPost(IDataModel<StockMovement> model)
- {
- // Perform post-processing
- }
- }";
- }
- }
- public class StockMovementTimberlineResult : TimberlinePostResult<IStockMovementTimberlineLine, StockMovement>
- {
- }
- public class StockMovementTimberlinePoster : ITimberlinePoster<StockMovement, StockMovementTimberlineSettings>
- {
- public ScriptDocument? Script { get; set; }
- public StockMovementTimberlineSettings Settings { get; set; }
- public bool BeforePost(IDataModel<StockMovement> model)
- {
- model.SetIsDefault<Document>(false, alias: "CompanyLogo");
- model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
- model.SetIsDefault<Employee>(false);
- model.SetColumns(Columns.None<StockMovement>().Add(x => x.ID).Add(x => x.Transaction));
- model.AddChildTable<StockMovement, StockMovement>(x => x.Transaction, x => x.Transaction,
- parentalias: "StockMovement", childalias: "FullTransactions",
- isdefault: true,
- columns: Columns.None<StockMovement>().Add(x => x.ID)
- .Add(x => x.Transaction)
- .Add(x => x.Job.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.Type)
- .Add(x => x.Units)
- .Add(x => x.Cost)
- .Add(x => x.Value)
- .Add(x => x.Date)
- .Add(x => x.Batch.ID)
- );
- model.AddLookupTable<StockMovement, Product>(x => x.Product.ID, x => x.ID, sourcealias: "FullTransactions",
- isdefault: true,
- columns: Columns.None<Product>().Add(x => x.ID)
- .Add(x => x.Name)
- .Add(x => x.CostCentre.Code)
- .Add(x => x.PurchaseGL.Code));
- model.AddLookupTable<StockMovement, Job>(x => x.Job.ID, x => x.ID, sourcealias: "FullTransactions",
- isdefault: true,
- columns: Columns.None<Job>().Add(x => x.ID)
- .Add(x => x.JobNumber));
- Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
- return true;
- }
- private bool ProcessDirectCostLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineDirectCost line)
- {
- return Script?.Execute(methodname: "ProcessDirectCostLine", parameters: new object[] { model, stockMovement, line }) != false;
- }
- private bool ProcessGLLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineGL line)
- {
- return Script?.Execute(methodname: "ProcessGLLine", parameters: new object[] { model, stockMovement, line }) != false;
- }
- private StockMovementTimberlineResult DoProcess(IDataModel<StockMovement> model)
- {
- var result = new StockMovementTimberlineResult();
- var firstMovements = model.GetTable<StockMovement>();
- var full = model.GetTable<StockMovement>("FullTransactions")
- .ToObjects<StockMovement>().GroupBy(x => x.Transaction);
- var products = model.GetTable<Product>().ToObjects<Product>()
- .ToDictionary(x => x.ID, x => x);
- var jobs = model.GetTable<Job>().ToObjects<Job>()
- .ToDictionary(x => x.ID, x => x);
- StockMovementTimberlineDirectCost CreateDirectCost(StockMovement movement)
- {
- var job = jobs[movement.Job.ID];
- var product = products[movement.Product.ID];
- var directCost = new StockMovementTimberlineDirectCost
- {
- Job = job.JobNumber,
- Extra = "",
- CostCode = product.CostCentre.Code,
- Category = "",
- Units = Math.Round(movement.Units, 4),
- UnitCost = Math.Round(movement.Cost, 4),
- TransactionType = StockMovementTimberlineTransactionType.IVCost
- };
- return ModifyLine(directCost, movement);
- }
- T ModifyLine<T>(T line, StockMovement movement)
- where T : IStockMovementTimberlineLine
- {
- var product = products[movement.Product.ID];
- line.TransactionDate = movement.Date;
- line.AccountingDate = movement.Date;
- line.Description = product.Name;
- line.Amount = Math.Round(movement.Value, 4);
- line.CreditAccount = product.PurchaseGL.Code;
- return line;
- }
- foreach (var transaction in full)
- {
- var mvts = transaction.ToArray();
- var ignorableTransfer = Settings.IgnoreNetZeroTransfers
- && mvts.Length == 2
- && mvts.Any(x => x.Type == StockMovementType.TransferOut)
- && mvts.Any(x => x.Type == StockMovementType.TransferIn)
- && mvts[0].Job.ID == mvts[1].Job.ID
- && mvts.Sum(x => x.Qty).IsEffectivelyEqual(0)
- && mvts.Sum(x => x.Qty * x.Cost).IsEffectivelyEqual(0);
- // I think we will fail all the movements if any one movement in the transaction failed. All the successful ones,
- // rather than saving them with AddSuccess immediately, we will put here first, and only succeed them if every movement succeeded.
- var successful = new List<(StockMovement mvt, IStockMovementTimberlineLine? line)>();
- foreach(var mvt in mvts)
- {
- switch (mvt.Type)
- {
- case StockMovementType.Issue:
- if(mvt.Job.ID == Guid.Empty)
- {
- // Issue to General Stock
- var gl = new StockMovementTimberlineGL { };
- gl = ModifyLine(gl, mvt);
- var product = products[mvt.Product.ID];
- gl.DebitAccount = product.SellGL.Code.NotWhiteSpaceOr(Settings.StockTakeGL);
- if (ProcessGLLine(model, mvt, gl))
- {
- successful.Add((mvt, gl));
- }
- else
- {
- result.AddFailed(mvt, "Failed by script.");
- }
- }
- else
- {
- // Ignore issues to a job.
- successful.Add((mvt, null));
- }
- break;
- case StockMovementType.Receive:
- successful.Add((mvt, null));
- break;
- case StockMovementType.StockTake:
- if(mvt.Job.ID == Guid.Empty)
- {
- // StockTake in General Stock
- var gl = new StockMovementTimberlineGL { };
- gl = ModifyLine(gl, mvt);
- var product = products[mvt.Product.ID];
- gl.DebitAccount = product.SellGL.Code.NotWhiteSpaceOr(Settings.StockTakeGL);
- if (ProcessGLLine(model, mvt, gl))
- {
- successful.Add((mvt, gl));
- }
- else
- {
- result.AddFailed(mvt, "Failed by script.");
- }
- }
- else
- {
- // StockTake in Job Holding
- var dc = CreateDirectCost(mvt);
- if (ProcessDirectCostLine(model, mvt, dc))
- {
- successful.Add((mvt, dc));
- }
- else
- {
- result.AddFailed(mvt, "Failed by script.");
- }
- }
- break;
- case StockMovementType.TransferOut:
- case StockMovementType.TransferIn:
- if(mvt.Job.ID != Guid.Empty && !ignorableTransfer)
- {
- var directCost = CreateDirectCost(mvt);
- if(ProcessDirectCostLine(model, mvt, directCost))
- {
- successful.Add((mvt, directCost));
- }
- else
- {
- result.AddFailed(mvt, "Failed by script.");
- }
- }
- else
- {
- successful.Add((mvt, null));
- }
- break;
- }
- }
- if(successful.Count < mvts.Length)
- {
- foreach(var (mvt, _) in successful)
- {
- result.AddFailed(mvt, "Transaction was unsuccessful.");
- }
- }
- else
- {
- foreach(var (mvt, item) in successful)
- {
- result.AddSuccess(mvt, item);
- }
- }
- }
- return result;
- }
- public IPostResult<StockMovement> Process(IDataModel<StockMovement> model)
- {
- var result = DoProcess(model);
- var dlg = new SaveFileDialog()
- {
- Title = "Select Output File",
- Filter = "CSV Files (*.csv)|*.csv"
- };
- if (dlg.ShowDialog() == true)
- {
- using (var writer = new StreamWriter(dlg.FileName))
- {
- using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
- foreach (var line in result.Exports.Distinct())
- {
- // Write the record.
- if(line is StockMovementTimberlineDirectCost dc)
- {
- csv.WriteRecord(dc);
- }
- else if(line is StockMovementTimberlineGL gl)
- {
- csv.WriteRecord(gl);
- }
- csv.NextRecord();
- }
- }
- }
- else
- {
- throw new PostCancelledException();
- }
- return result;
- }
- public void AfterPost(IDataModel<StockMovement> model, IPostResult<StockMovement> result)
- {
- Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
- }
- }
- public class StockMovementTimberlinePosterEngine<T> : TimberlinePosterEngine<StockMovement, StockMovementTimberlineSettings>
- {
- }
|