Browse Source

Basically wrote stock movement poster

Kenric Nugteren 1 year ago
parent
commit
0e98d02e09
1 changed files with 99 additions and 9 deletions
  1. 99 9
      prs.shared/Posters/Timberline/StockMovementTimberlinePoster.cs

+ 99 - 9
prs.shared/Posters/Timberline/StockMovementTimberlinePoster.cs

@@ -21,6 +21,8 @@ namespace PRS.Shared
 {
     public interface IStockMovementTimberlineLine
     {
+        StockMovementBatchType BatchType { get; set; }
+
         DateTime TransactionDate { get; set; }
 
         DateTime AccountingDate { get; set; }
@@ -70,6 +72,9 @@ namespace PRS.Shared
 
     public class StockMovementTimberlineDirectCost : IStockMovementTimberlineLine
     {
+        [Ignore]
+        public StockMovementBatchType BatchType { get; set; }
+
         [Index(0)]
         public string RecordID { get; set; } = "DC";
 
@@ -137,6 +142,9 @@ namespace PRS.Shared
 
     public class StockMovementTimberlineGL : IStockMovementTimberlineLine
     {
+        [Ignore]
+        public StockMovementBatchType BatchType { get; set; }
+
         [Index(0)]
         public string RecordID { get; set; } = "GL";
 
@@ -223,27 +231,38 @@ public class Module
             model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
             model.SetIsDefault<Employee>(false);
 
-            model.SetColumns(new Columns<StockMovement>(x => x.Transaction));
+            model.SetColumns(new Columns<StockMovement>(x => x.ID).Add(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)
+                    .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: new Columns<Product>(x => x.ID)
+                    .Add(x => x.Name)
                     .Add(x => x.CostCentre.Code));
 
             model.AddLookupTable<StockMovement, Job>(x => x.Job.ID, x => x.ID, sourcealias: "FullTransactions",
+                isdefault: true,
                 columns: new Columns<Job>(x => x.ID)
                     .Add(x => x.JobNumber));
 
+            model.AddLookupTable<StockMovement, StockMovementBatch>(x => x.Batch.ID, x => x.ID, sourcealias: "FullTransactions",
+                isdefault: true,
+                columns: new Columns<StockMovementBatch>(x => x.ID).Add(x => x.Type));
+
             Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
             return true;
         }
@@ -252,7 +271,7 @@ public class Module
         {
             return Script?.Execute(methodname: "ProcessDirectCostLine", parameters: new object[] { model, stockMovement, line }) != false;
         }
-        private bool ProcessGLLine(IDataModel<PurchaseOrder> model, StockMovement stockMovement, StockMovementTimberlineDirectCost line)
+        private bool ProcessGLLine(IDataModel<StockMovement> model, StockMovement stockMovement, StockMovementTimberlineGL line)
         {
             return Script?.Execute(methodname: "ProcessGLLine", parameters: new object[] { model, stockMovement, line }) != false;
         }
@@ -268,23 +287,40 @@ public class Module
                 .ToDictionary(x => x.ID, x => x);
             var jobs = model.GetTable<Job>().ToObjects<Job>()
                 .ToDictionary(x => x.ID, x => x);
+            var batches = model.GetTable<StockMovementBatch>().ToObjects<StockMovementBatch>()
+                .ToDictionary(x => x.ID, x => x);
 
             foreach (var movement in full)
             {
+                if (!movement.IsTransfer)
+                {
+                    result.AddSuccess(movement, null);
+                    continue;
+                }
+
+                var batch = batches.GetValueOrDefault(movement.Batch.ID);
+                if (batch is not null && batch.Type == StockMovementBatchType.Receipt)
+                {
+                    result.AddSuccess(movement, null);
+                    continue;
+                }
+
                 IStockMovementTimberlineLine line;
-                if(movement.Job.ID != Guid.Empty)
+                var product = products[movement.Product.ID];
+                if (movement.Job.ID != Guid.Empty)
                 {
                     var job = jobs[movement.Job.ID];
-                    var dc = new StockMovementTimberlineDirectCost
+                    var directCost = new StockMovementTimberlineDirectCost
                     {
                         Job = job.JobNumber,
                         Extra = "",
-                        CostCode = products[movement.Product.ID].CostCentre.Code,
+                        CostCode = product.CostCentre.Code,
                         Category = "",
                         Units = movement.Units,
-                        UnitCost = movement.Cost
+                        UnitCost = movement.Cost,
+                        // TransactionType = ???
                     };
-                    line = dc;
+                    line = directCost;
                 }
                 else
                 {
@@ -292,6 +328,28 @@ public class Module
                     {
                     };
                 }
+                line.TransactionDate = movement.Date;
+                line.Description = product.Name;
+                line.Amount = movement.Value;
+                line.BatchType = batch?.Type ?? StockMovementBatchType.Transfer;
+
+                bool success = true;
+                if (line is StockMovementTimberlineDirectCost dc)
+                {
+                    success = ProcessDirectCostLine(model, movement, dc);
+                }
+                else if (line is StockMovementTimberlineGL gl)
+                {
+                    success = ProcessGLLine(model, movement, gl);
+                }
+                if (success)
+                {
+                    result.AddSuccess(movement, line);
+                }
+                else
+                {
+                    result.AddFailed(movement, "Failed by script.");
+                }
             }
 
             /*var lines = model.GetTable<PurchaseOrderItem>("PurchaseOrder_PurchaseOrderItem").ToObjects<PurchaseOrderItem>()
@@ -401,6 +459,7 @@ public class Module
 
             var dlg = new SaveFileDialog()
             {
+                Title = "Select Consume Direct Cost File",
                 Filter = "CSV Files (*.csv)|*.csv"
             };
 
@@ -409,7 +468,7 @@ public class Module
                 using (var writer = new StreamWriter(dlg.FileName))
                 {
                     using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
-                    foreach (var line in result.Exports)
+                    foreach (var line in result.Exports.Where(x => x.BatchType != StockMovementBatchType.Stocktake))
                     {
                         // Write the record.
                         if(line is StockMovementTimberlineDirectCost dc)
@@ -420,6 +479,37 @@ public class Module
                         {
                             csv.WriteRecord(gl);
                         }
+                        csv.NextRecord();
+                    }
+                }
+            }
+            else
+            {
+                throw new PostCancelledException();
+            }
+
+            dlg = new SaveFileDialog()
+            {
+                Title = "Select Adjustments 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.Where(x => x.BatchType == StockMovementBatchType.Stocktake))
+                    {
+                        // Write the record.
+                        if (line is StockMovementTimberlineDirectCost dc)
+                        {
+                            csv.WriteRecord(dc);
+                        }
+                        else if (line is StockMovementTimberlineGL gl)
+                        {
+                            csv.WriteRecord(gl);
+                        }
                     }
                 }
             }