Преглед на файлове

Improved RequiStore handling of transfers when not enough stock is present.

Kenric Nugteren преди 1 година
родител
ревизия
b8f0c68e6e
променени са 1 файла, в които са добавени 27 реда и са изтрити 17 реда
  1. 27 17
      prs.stores/RequisitionStore.cs

+ 27 - 17
prs.stores/RequisitionStore.cs

@@ -280,30 +280,40 @@ namespace Comal.Stores
 
                 var jobid = entity.JobLink.ID;
 
-                var holdings = Provider.Query<StockHolding>(
-                    new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(locationid)
-                        .And(x => x.Product.ID).IsEqualTo(productid)
-                        .And(x => x.Style.ID).IsEqualTo(styleid)
-                        .And(x => x.Dimensions.UnitSize).IsEqualTo(unitsize),
-                    new Columns<StockHolding>(x => x.Job.ID)
-                );
-                var holdingrow = holdings.Rows.FirstOrDefault(r => r.Get<StockHolding, Guid>(c => c.Job.ID).Equals(jobid))
-                    ?? holdings.Rows.FirstOrDefault(r => r.Get<StockHolding, Guid>(c => c.Job.ID).Equals(Guid.Empty))
-                    ?? holdings.Rows.FirstOrDefault();
-                Guid holdingjobid = holdingrow != null
-                    ? holdingrow.Get<StockHolding, Guid>(c => c.Job.ID)
-                    : Guid.Empty;
+                var holdingQty = 0.0;
+                if(jobid != Guid.Empty)
+                {
+                    var holdings = Provider.Query<StockHolding>(
+                        new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(locationid)
+                            .And(x => x.Product.ID).IsEqualTo(productid)
+                            .And(x => x.Style.ID).IsEqualTo(styleid)
+                            .And(x => x.Dimensions.UnitSize).IsEqualTo(unitsize)
+                            .And(x => x.Job.ID).IsEqualTo(jobid)
+                            .And(x => x.Qty).IsGreaterThan(0.0),
+                        new Columns<StockHolding>(x => x.Qty)
+                    );
+                    holdingQty = holdings.Rows.FirstOrDefault()?.Get<StockHolding, double>(x => x.Qty) ?? 0.0;
+                }
 
                 var qty = item.ActualQuantity;
                 var dimensions = item.Product.DefaultInstance.Dimensions;
 
                 var txnid = Guid.NewGuid();
-                if (jobid != holdingjobid)
+                if(holdingQty < qty)
                 {
-                    updates.Add(CreateStockMovement(entity.Employee.ID, DateTime.Now, batch.ID, productid, locationid, styleid, holdingjobid,
-                        -qty, dimensions, txnid, true, string.Format("Requisition #{0} Internal Transfer", entity.Number)));
-                    updates.Add(CreateStockMovement(entity.Employee.ID, DateTime.Now, batch.ID, productid, locationid, styleid, jobid, qty, dimensions, txnid, true,
+                    var extraRequired = qty - holdingQty;
+                    // We don't have enough stock in this case, so transfer the necessary stock from general. We don't check for quantity in general stock,
+                    // but instead will let general stock go negative if not enough. Obviously we have the stock, because its being sent to site. So if we do
+                    // get negatives, it means probably our number are wrong I think.
+
+                    // Transfer the necessary balance from General Stock...
+                    updates.Add(CreateStockMovement(entity.Employee.ID, DateTime.Now, batch.ID, productid, locationid, styleid, Guid.Empty,
+                        -extraRequired, dimensions, txnid, true, string.Format("Requisition #{0} Internal Transfer", entity.Number)));
+                    // ... to the job.
+                    updates.Add(CreateStockMovement(entity.Employee.ID, DateTime.Now, batch.ID, productid, locationid, styleid, jobid, extraRequired, dimensions, txnid, true,
                         string.Format("Requisition #{0} Internal Transfer", entity.Number)));
+
+                    // Now we have a full qty in the job holding, and we can issue to site.
                 }
 
                 updates.Add(CreateStockMovement(entity.Employee.ID, DateTime.Now, batch.ID, productid, locationid, styleid, jobid, 0.0F - qty, dimensions, txnid,