using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using jdk.nashorn.@internal.ir.debug; using NPOI.SS.Formula.Functions; using org.omg.PortableInterceptor; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PRSDesktop { public delegate void StockReserved(); public class JobRequiItemAvailableStockGrid : DynamicDataGrid { public event StockReserved OnStockReserved; public Guid ProductID = Guid.Empty; public JobRequisitionItem Item = new JobRequisitionItem(); public Guid empID = new Guid(); public JobRequiItemAvailableStockGrid() { Options.AddRange( DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows, DynamicGridOption.RecordCount, DynamicGridOption.EditRows ); HiddenColumns.Add(x => x.Qty); AddButton("Reserve For Job", null, ReserveStock); } private bool ReserveStock(Button btn, CoreRow[] rows) { if (!rows.Any()) { MessageBox.Show("Please select at least one holding to reserve"); return false; } var holding = rows[0].ToObject(); int units = 0; if (Item.Qty <= holding.Units) units = Convert.ToInt32(Item.Qty); else units = Convert.ToInt32(holding.Units); if (NumberEdit.Execute("Enter amount to reserve", 1, units, ref units)) { StockMovementBatch batch = new StockMovementBatch(); batch.Type = StockMovementBatchType.Transfer; batch.Notes = "Reserving Stock for Requi: " + Item.Requisition.Number; new Client().Save(batch, "Created from Job Requi Item Reserve Stock Function"); List movements = new List { CreateMovement(StockMovementType.Issue, units, holding, batch), CreateMovement(StockMovementType.Receive, units, holding, batch) }; new Client().Save(movements, "Created from Job Requi Item Reserve Stock Function"); Item.Status = JobRequisitionItemStatus.Reserved; new Client().Save(Item, "Updated from Job Requi Item Reserve Stock Function"); MessageBox.Show("Success - " + Item.Product.Name + " stock reserved for Requisition " + Item.Requisition.Number); OnStockReserved?.Invoke(); } return true; } enum StockMovementType { Issue, Receive } private StockMovement CreateMovement(StockMovementType type, double units, StockHolding holding, StockMovementBatch batch) { StockMovement mvt = new StockMovement(); mvt.System = true; mvt.Batch.ID = batch.ID; mvt.Batch.Type = StockMovementBatchType.Transfer; mvt.Dimensions.CopyFrom(holding.Dimensions); mvt.Style.ID = holding.Style.ID; mvt.Location.ID = holding.Location.ID; mvt.Product.ID = holding.Product.ID; mvt.Date = DateTime.Now; mvt.IsTransfer = true; mvt.Notes = "Reserving Stock for Requi: " + Item.Requisition.Number; mvt.JobRequisitionItem.ID = Item.ID; mvt.Employee.ID = empID; if (type == StockMovementType.Issue) { mvt.Issued = units; mvt.Job.ID = holding.Job.ID; } else { mvt.Received = units; mvt.Job.ID = Item.Job.ID; } return mvt; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder sort, Action action) { if (ProductID != Guid.Empty) criteria.Add(new Filter(x => x.Product.ID).IsEqualTo(ProductID)); else criteria.Add(new Filter().None()); base.Reload(criteria, columns, ref sort, action); } protected override bool FilterRecord(CoreRow row) { var result = base.FilterRecord(row); if (result) { var qty = row.Get(x => x.Qty); var iszero = CoreUtils.IsEffectivelyEqual(qty, 0.0F); result = !iszero; } return result; } } }