|
@@ -5,24 +5,13 @@ using Comal.Classes;
|
|
|
using InABox.Core;
|
|
|
using PRSStores;
|
|
|
using System;
|
|
|
+using NPOI.SS.Formula.Functions;
|
|
|
|
|
|
namespace Comal.Stores;
|
|
|
|
|
|
internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
{
|
|
|
- private Columns<PurchaseOrderItem> RequiredColumns()
|
|
|
- {
|
|
|
- return new Columns<PurchaseOrderItem>(x => x.ID)
|
|
|
- .Add(x => x.Product.ID)
|
|
|
- .Add(x => x.Qty)
|
|
|
- .Add(x => x.Cost)
|
|
|
- .Add(x => x.StockLocation.ID)
|
|
|
- .Add(x => x.Style.ID)
|
|
|
- .Add(x => x.Job.ID)
|
|
|
- .Add(x => x.ReceivedDate)
|
|
|
- .Add(x => x.PurchaseOrderLink.PONumber)
|
|
|
- .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Data);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
private void UpdateStockMovements(PurchaseOrderItem entity)
|
|
|
{
|
|
@@ -67,6 +56,20 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
.ToObjects<JobRequisitionItem>();
|
|
|
});
|
|
|
|
|
|
+ var consigntask = Task<double>.Run(() =>
|
|
|
+ {
|
|
|
+ if (entity.Consignment.ID != Guid.Empty && !entity.Consignment.ExTax.IsEffectivelyEqual(0.0))
|
|
|
+ {
|
|
|
+ var values = Provider.Query(
|
|
|
+ new Filter<PurchaseOrderItem>(x => x.Consignment.ID).IsEqualTo(entity.Consignment.ID),
|
|
|
+ new Columns<PurchaseOrderItem>(x => x.ExTax)
|
|
|
+ ).Rows.Select(r => r.Get<PurchaseOrderItem, double>(c => c.ExTax));
|
|
|
+ return values.Sum(x => x);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0.0;
|
|
|
+ });
|
|
|
+
|
|
|
var instancetask = new Task<CoreRow?>(() =>
|
|
|
{
|
|
|
return Provider.Query(
|
|
@@ -144,7 +147,7 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
});
|
|
|
locationtask.Start();
|
|
|
|
|
|
- Task.WaitAll(producttask, locationtask, instancetask, jriTask);
|
|
|
+ Task.WaitAll(producttask, locationtask, instancetask, jriTask, consigntask);
|
|
|
|
|
|
var instancerow = instancetask.Result;
|
|
|
var productrow = producttask.Result;
|
|
@@ -242,7 +245,10 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
|
|
|
var poqty = entity.Qty * (Math.Abs(entity.Dimensions.Value) > 0.0001F ? entity.Dimensions.Value : 1.0F);
|
|
|
var pocost = entity.Cost * poqty;
|
|
|
-
|
|
|
+
|
|
|
+ if (!consigntask.Result.IsEffectivelyEqual(0.0) && !entity.Qty.IsEffectivelyEqual(0.0))
|
|
|
+ pocost += entity.Qty * entity.Cost * entity.Consignment.ExTax / consigntask.Result;
|
|
|
+
|
|
|
var totalqty = freeqty + poqty;
|
|
|
var totalcost = freecost + pocost;
|
|
|
|
|
@@ -269,13 +275,19 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
var movements = new List<StockMovement>();
|
|
|
|
|
|
var _pototal = entity.Qty;
|
|
|
+ var poCost = entity.Cost;
|
|
|
+ if (!consigntask.Result.IsEffectivelyEqual(0.0)
|
|
|
+ && !entity.Qty.IsEffectivelyEqual(0.0))
|
|
|
+ {
|
|
|
+ poCost += entity.Cost * entity.Consignment.ExTax / consigntask.Result;
|
|
|
+ }
|
|
|
foreach (var jri in jris)
|
|
|
{
|
|
|
- CreateMovement(entity, locationid, movements, jri, jri.Qty);
|
|
|
+ CreateMovement(entity, locationid, movements, jri, jri.Qty, poCost);
|
|
|
_pototal -= jri.Qty;
|
|
|
}
|
|
|
if (!_pototal.IsEffectivelyEqual(0.0F))
|
|
|
- CreateMovement(entity, locationid, movements, null, _pototal);
|
|
|
+ CreateMovement(entity, locationid, movements, null, _pototal, poCost);
|
|
|
|
|
|
FindSubStore<StockMovementBatch>().Save(batch, "Received on PO");
|
|
|
foreach(var mvt in movements)
|
|
@@ -286,7 +298,7 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
FindSubStore<StockMovement>().Save(movements, "Updated by Purchase Order Modification");
|
|
|
}
|
|
|
|
|
|
- private static void CreateMovement(PurchaseOrderItem entity, Guid locationid, List<StockMovement> movements, JobRequisitionItem jri, double qty)
|
|
|
+ private static void CreateMovement(PurchaseOrderItem entity, Guid locationid, List<StockMovement> movements, JobRequisitionItem jri, double qty, double cost)
|
|
|
{
|
|
|
var movement = new StockMovement();
|
|
|
movement.Product.ID = entity.Product.ID;
|
|
@@ -300,7 +312,7 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
movement.Employee.ID = Guid.Empty;
|
|
|
movement.OrderItem.ID = entity.ID;
|
|
|
movement.Notes = string.Format("Received on PO {0}", entity.PurchaseOrderLink.PONumber);
|
|
|
- movement.Cost = entity.Cost;
|
|
|
+ movement.Cost = cost;
|
|
|
movement.Type = StockMovementType.Receive;
|
|
|
movements.Add(movement);
|
|
|
|
|
@@ -336,8 +348,7 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
private void DeleteStockMovements(PurchaseOrderItem entity)
|
|
|
{
|
|
|
var movements = Provider.Query(
|
|
@@ -368,7 +379,7 @@ internal class PurchaseOrderItemStore : BaseStore<PurchaseOrderItem>
|
|
|
{
|
|
|
var item = Provider.Query(
|
|
|
new Filter<PurchaseOrderItem>(x => x.ID).IsEqualTo(entity.ID),
|
|
|
- RequiredColumns())
|
|
|
+ LookupFactory.RequiredColumns<PurchaseOrderItem>())
|
|
|
.ToObjects<PurchaseOrderItem>().FirstOrDefault();
|
|
|
if(item is not null)
|
|
|
{
|