123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using Comal.Classes;
- using Comal.Stores;
- using InABox.Core;
- using InABox.Database;
- using System.Collections.Generic;
- using System.Linq;
- namespace PRSStores;
- public enum JobRequisitionItemAction
- {
- Created,
- Updated,
- Deleted,
- None
- }
- public class JobRequisitionItemStore : BaseStore<JobRequisitionItem>
- {
- protected override void AfterSave(JobRequisitionItem entity)
- {
- base.AfterSave(entity);
- if(entity.HasOriginalValue(x => x.Cancelled))
- {
- CancelMovements(entity);
- }
- }
- private static IEnumerable<StockMovement> GetMovements(IStore store, Guid jriID, Filter<StockMovement>? filter, Columns<StockMovement> columns)
- {
- return store.Provider
- .Query(
- Filter<StockMovement>.And(
- new Filter<StockMovement>(x => x.JobRequisitionItem.ID).IsEqualTo(jriID),
- filter),
- columns)
- .ToObjects<StockMovement>();
- }
-
- private void CancelMovements(JobRequisitionItem entity)
- {
- // Here, we care about *all* movements into or out of this requi. If stuff has been issued, it must be included,
- // since we cannot return issued stock back to general stock for the job.
- var movements = GetMovements(this, entity.ID, null,
- Columns.None<StockMovement>().Add(
- x => x.Product.ID,
- x => x.Style.ID,
- x => x.Job.ID,
- x => x.Location.ID,
- x => x.Units,
- x => x.Cost,
- x => x.OrderItem.ID)
- .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local));
- var newMovements = new List<StockMovement>();
- foreach(var movement in movements)
- {
- var from = movement.CreateMovement();
- from.Date = entity.Cancelled;
- from.Cost = movement.Cost;
- from.System = true;
- from.JobRequisitionItem.ID = entity.ID;
- from.OrderItem.ID = movement.OrderItem.ID;
- from.Notes = "Requisition item cancelled";
- var to = movement.CreateMovement();
- to.Date = entity.Cancelled;
- to.Cost = movement.Cost;
- to.System = true;
- to.Notes = "Requisition item cancelled";
- to.OrderItem.ID = movement.OrderItem.ID;
- to.Transaction = from.Transaction;
- if(movement.Units > 0)
- {
- // If this movement was an increase to reservation allocation, we create a transfer out of the reservation.
- from.Issued = movement.Units;
- to.Received = movement.Units;
- from.Type = StockMovementType.TransferOut;
- to.Type = StockMovementType.TransferIn;
- }
- else if(movement.Units < 0)
- {
- // If this movement was a decrease to reservation allocation, we create a transfer into the reservation.
- from.Received = -movement.Units;
- to.Issued = -movement.Units;
- from.Type = StockMovementType.TransferIn;
- to.Type = StockMovementType.TransferOut;
- }
- newMovements.Add(from);
- newMovements.Add(to);
- }
- if(newMovements.Count > 0)
- {
- var batch = new StockMovementBatch
- {
- Notes = "Requisition item cancelled."
- };
- FindSubStore<StockMovementBatch>().Save(batch, "");
- foreach(var mvt in newMovements)
- {
- mvt.Batch.ID = batch.ID;
- }
- FindSubStore<StockMovement>().Save(newMovements, "Requisition item cancelled.");
- }
- }
-
- }
|