123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- 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;
- 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;
- 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
- {
- [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
- {
- [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>
- {
- 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 void 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 void 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(new Columns<StockMovement>(x => x.Transaction));
- model.AddChildTable<StockMovement, StockMovement>(x => x.Transaction, x => x.Transaction,
- parentalias: "StockMovement", childalias: "FullTransactions",
- isdefault: true,
- filter: new Filter<StockMovement>(x => x.IsTransfer).IsEqualTo(true),
- columns: new Columns<StockMovement>(x => x.ID)
- .Add(x => x.Transaction)
- .Add(x => x.Job.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.IsTransfer)
- );
- model.AddLookupTable<StockMovement, Product>(x => x.Product.ID, x => x.ID, sourcealias: "FullTransactions",
- columns: new Columns<Product>(x => x.ID)
- .Add(x => x.CostCentre.Code));
- model.AddLookupTable<StockMovement, Job>(x => x.Job.ID, x => x.ID, sourcealias: "FullTransactions",
- columns: new Columns<Job>(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<PurchaseOrder> model, StockMovement stockMovement, StockMovementTimberlineDirectCost 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>();
- 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);
- foreach (var movement in full)
- {
- IStockMovementTimberlineLine line;
- if(movement.Job.ID != Guid.Empty)
- {
- var job = jobs[movement.Job.ID];
- var dc = new StockMovementTimberlineDirectCost
- {
- Job = job.JobNumber,
- Extra = "",
- CostCode = products[movement.Product.ID].CostCentre.Code,
- Category = "",
- Units = movement.Units,
- UnitCost = movement.Cost
- };
- line = dc;
- }
- else
- {
- line = new StockMovementTimberlineGL
- {
- };
- }
- }
- /*var lines = model.GetTable<PurchaseOrderItem>("PurchaseOrder_PurchaseOrderItem").ToObjects<PurchaseOrderItem>()
- .GroupBy(x => x.PurchaseOrderLink.ID).ToDictionary(x => x.Key, x => x.ToList());
- foreach (var purchaseOrder in model.GetTable<PurchaseOrder>().ToObjects<PurchaseOrder>())
- {
- var c = new PurchaseOrderTimberlineHeader
- {
- CommitmentID = purchaseOrder.PONumber,
- CommitmentType = PurchaseOrderTimberlineCommitmentType.PO,
- Description = purchaseOrder.Description,
- VendorID = purchaseOrder.SupplierLink.Code,
- Date = purchaseOrder.IssuedDate,
- // RetainagePercent
- // Committed to JC
- Closed = purchaseOrder.ClosedDate != DateTime.MinValue,
- // Printed
- };
- if (!ProcessHeader(model, purchaseOrder, c))
- {
- cs.AddFailed(purchaseOrder, "Failed by script.");
- }
- else
- {
- // Dictionary from line number to POItem.
- var items = new Dictionary<int, PurchaseOrderItem>();
- var POItems = lines.GetValueOrDefault(purchaseOrder.ID)?.ToList() ?? new List<PurchaseOrderItem>();
- foreach (var purchaseOrderItem in POItems)
- {
- if (int.TryParse(purchaseOrderItem.PostedReference, out var itemNumber))
- {
- if (items.TryGetValue(itemNumber, out var oldItem))
- {
- // Theoretically shouldn't happen, but just in case.
- MessageBox.Show($"Warning: Multiple PurchaseOrder Items have the same line number for export; the line number for '{purchaseOrderItem.Description}' will be changed in the export.");
- Logger.Send(LogType.Error, "", $"Purchase Order Post: Multiple POItems with the same Line Number; changing line number of POItem {purchaseOrderItem.ID}");
- purchaseOrderItem.PostedReference = "";
- }
- else
- {
- items[itemNumber] = purchaseOrderItem;
- }
- }
- }
- var success = true;
- foreach (var purchaseOrderItem in POItems)
- {
- if (!int.TryParse(purchaseOrderItem.PostedReference, out var itemNumber))
- {
- itemNumber = 1;
- while (items.ContainsKey(itemNumber))
- {
- ++itemNumber;
- }
- items[itemNumber] = purchaseOrderItem;
- purchaseOrderItem.PostedReference = itemNumber.ToString();
- }
- var ci = new PurchaseOrderTimberlineLine
- {
- CommitmentID = purchaseOrder.PONumber,
- ItemNumber = itemNumber,
- Description = purchaseOrderItem.Description,
- // RetainagePercent = ,
- DeliveryDate = purchaseOrderItem.ReceivedDate,
- //ScopeOfWork
- Job = purchaseOrderItem.Job.JobNumber,
- //Extra = purchaseOrderItem.Job
- CostCode = purchaseOrderItem.CostCentre.Code,
- //Category = purchaseOrderItem.cat
- TaxGroup = purchaseOrderItem.TaxCode.Code,
- Units = purchaseOrderItem.Qty,
- UnitCost = purchaseOrderItem.Cost,
- UnitDescription = purchaseOrderItem.Dimensions.UnitSize,
- Amount = purchaseOrderItem.IncTax,
- // BoughtOut
- };
- if (!ProcessLine(model, purchaseOrderItem, ci))
- {
- success = false;
- break;
- }
- c.Lines.Add(ci);
- }
- if (success)
- {
- foreach (var item in POItems)
- {
- cs.AddFragment(item);
- }
- cs.AddSuccess(purchaseOrder, c);
- }
- else
- {
- cs.AddFailed(purchaseOrder, "Failed by script.");
- }
- }
- }*/
- return result;
- }
- public IPostResult<StockMovement> Process(IDataModel<StockMovement> model)
- {
- var result = DoProcess(model);
- var dlg = new SaveFileDialog()
- {
- 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)
- {
- // Write the record.
- if(line is StockMovementTimberlineDirectCost dc)
- {
- csv.WriteRecord(dc);
- }
- else if(line is StockMovementTimberlineGL gl)
- {
- csv.WriteRecord(gl);
- }
- }
- }
- }
- 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>
- {
- }
- }
|