using com.sun.corba.se.spi.orbutil.threadpool; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; using InABox.Wpf; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using PRSDesktop.Panels.Jobs.Summary; namespace PRSDesktop; internal class JobSummaryGrid : DynamicDataGrid, IMasterDetailControl, IDataModelSource { Guid empID = Guid.Empty; string empName = ""; private List DetailsColumns; private Job? _master; private Button _cancelRequisitions; private Button _releaseStock; public Job? Master { get => _master; set { _master = value; _cancelRequisitions.IsEnabled = Master?.JobStatus.Active == false; } } public Filter MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty ? new Filter(x => x.Job.ID).IsEqualTo(Master.ID) .And(x => x.Product.ID).IsNotEqualTo(Guid.Empty) : new Filter().None(); public bool IncludeReserves { get; set; } public bool ShowIssues { get; set; } public JobSummaryGrid() : base() { ColumnsTag = nameof(JobSummaryGrid); OnCellDoubleClick += JobSummaryGrid_OnCellDoubleClick; SetupDetailsColumns(); _cancelRequisitions = AddButton("Cancel Requisitions", PRSDesktop.Resources.archive.AsBitmapImage(), CancelRequisitions); _releaseStock = AddButton("Release Stock", PRSDesktop.Resources.archive.AsBitmapImage(), ReleaseStock); } public Dictionary> GetHoldings(CoreRow[] rows) { var result = new Dictionary>(); foreach (var row in rows) { var material = row.ToObject(); var filter = new Filter(x => x.Product.ID).IsEqualTo(material.Product.ID); if (StyleColumnVisible()) filter = filter.And(x => x.Style.ID).IsEqualTo(material.Style.ID); if(DimensionsColumnVisible()) filter = filter.And(x => x.Dimensions).DimensionEquals(material.Dimensions); var columns = Columns.None().Add(x => x.Location.ID) .Add(x => x.Product.ID) .Add(x => x.Style.ID) .AddDimensionsColumns(x=>x.Dimensions); foreach (var holding in new Client().Query(filter, columns).ToObjects()) result[holding] = holding.LoadRequisitionItems(true); } return result; } private bool ReleaseStock(System.Windows.Controls.Button btn, CoreRow[] rows) { if (rows?.Any() != true) return false; if(MessageWindow.ShowYesNoCancel("This will release any stock holdings for the selected items!\n\nAre you sure you wish to do this? ", "Confirm") != MessageWindowResult.Yes) return false; var updates = new List(); Progress.ShowModal("Loading Holdings", progress => { var holdings = GetHoldings(rows); foreach (var (holding, items) in holdings) { foreach (var item in items) { var from = holding.CreateMovement(); //from.OrderItem.ID = group.Key.OrderItem; from.JobRequisitionItem.ID = item.ID; from.Cost = StockRelease == StockReleaseWriteDownMethod.AverageCost ? holding.AverageValue : 0.0; from.Type = StockMovementType.TransferOut; from.Job.ID = Master.ID; from.Issued = item.Qty; var to = holding.CreateMovement(); to.Cost = StockRelease == StockReleaseWriteDownMethod.AverageCost ? holding.AverageValue : 0.0; //to.OrderItem.ID = group.Key.OrderItem; //to.JobRequisitionItem.ID = item.ID; to.Job.ID = Guid.Empty; to.Type = StockMovementType.TransferIn; to.Transaction = from.Transaction; to.Received = item.Qty; to.Notes = $"Released from job {Master.JobNumber}"; updates.Add(from); updates.Add(to); } } progress.Report("Saving Movements"); if (updates.Any()) Client.Save(updates, $"Stock Released from {Master.JobNumber} on Job Summary Screen"); }); MessageWindow.ShowMessage("The selected stock holdings have been cancelled.","Done"); return updates.Any(); } private bool CancelRequisitions(System.Windows.Controls.Button btn, CoreRow[] rows) { if (rows?.Any() != true) return false; if(MessageWindow.ShowYesNoCancel("This will cancel all outstanding requisitions for the selected items!\n\nAre you sure you wish to do this? ", "Confirm") != MessageWindowResult.Yes) return false; var updates = new List(); Progress.ShowModal("Loading Holdings", progress => { var holdings = GetHoldings(rows); foreach (var holding in holdings) { var items = holding.Value.Where( x => x.ID != Guid.Empty && x.Status != JobRequisitionItemStatus.Cancelled && x.Status != JobRequisitionItemStatus.Issued && x.Status != JobRequisitionItemStatus.Archived ); foreach (var item in items) { item.Cancelled = DateTime.Now; updates.Add(item); } progress.Report("Cancelling Requisitions"); Client.Save(updates, $"Requsition cancelled for {Master.JobNumber} from Job Summary Screen"); } }); MessageWindow.ShowMessage("The selected requisitions have been cancelled.","Done"); return updates.Any(); } private class UIComponent : DynamicGridGridUIComponent { private Column _jobshortage = new Column(x => x.JobShortage); private Column _stockshortage = new Column(x => x.FreeStockShortage); protected override Brush? GetCellBackground(CoreRow row, DynamicColumnBase column) { if (column is DynamicGridColumn col) { if (_jobshortage.IsEqualTo(col.ColumnName)) return row.Get(x => x.JobShortage) > 0.0F ? new SolidColorBrush(Colors.LightSalmon) { Opacity = 0.5 } : null; if (_stockshortage.IsEqualTo(col.ColumnName)) return row.Get(x => x.FreeStockShortage) > 0.0F ? new SolidColorBrush(Colors.LightSalmon) { Opacity = 0.5 } : null; } return null; } } protected override IDynamicGridUIComponent CreateUIComponent() { return new UIComponent() { Parent = this }; } protected override void Init() { base.Init(); HiddenColumns.Add(x => x.Product.ID); HiddenColumns.Add(x => x.Style.ID); HiddenColumns.Add(x => x.Dimensions.UnitSize); HiddenColumns.Add(x => x.BillOfMaterials); HiddenColumns.Add(x => x.Requisitions); HiddenColumns.Add(x => x.PickingLists); HiddenColumns.Add(x => x.Issued); HiddenColumns.Add(x => x.ReservedStock); HiddenColumns.Add(x => x.OnOrder); HiddenColumns.Add(x => x.JobShortage); HiddenColumns.Add(x => x.FreeOnHand); HiddenColumns.Add(x => x.FreeOnOrder); HiddenColumns.Add(x => x.FreeStockTotal); HiddenColumns.Add(x => x.FreeStockShortage); HiddenColumns.Add(x => x.Product.Image.ID); HiddenColumns.Add(x => x.Product.Image.FileName); ActionColumns.Add(new DynamicImageManagerColumn(this, x => x.Product.Image, false) { Position = DynamicActionColumnPosition.Start }); ActionColumns.Add(new DynamicMenuColumn(BuildMenu)); AddButton("Create PO", null, CreatePO); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.SelectColumns = true; options.FilterRows = true; options.ExportData = true; options.MultiSelect = true; } private bool StyleColumnVisible() { var styleColumn = CoreUtils.GetFullPropertyName(x => x.Style, "."); return VisibleColumns.Any(x => x.ColumnName.StartsWith(styleColumn)); } private bool DimensionsColumnVisible() { var dimColumn = CoreUtils.GetFullPropertyName(x => x.Dimensions, "."); return VisibleColumns.Any(x => x.ColumnName.StartsWith(dimColumn)); } public override DynamicGridColumns GenerateColumns() { var columns = new DynamicGridColumns(); columns.Add(x => x.Product.Group.Code, 120, "Group Code", "", Alignment.MiddleCenter); columns.Add(x => x.Product.Code, 200, "Product Code", "", Alignment.MiddleCenter); columns.Add(x => x.Product.Name, 0, "Product Name", "", Alignment.MiddleCenter); columns.Add(x => x.Dimensions.UnitSize, 120, "UOM", "", Alignment.MiddleCenter); columns.Add(x => x.BillOfMaterials, 80, "BOM", "", Alignment.MiddleCenter); columns.Add(x => x.Requisitions, 80, "Req.", "", Alignment.MiddleCenter); columns.Add(x => x.PickingLists, 80, "P/L", "", Alignment.MiddleCenter); columns.Add(x => x.Issued, 80, "Issued", "", Alignment.MiddleCenter); columns.Add(x => x.ReservedStock, 80, "Reserved", "", Alignment.MiddleCenter); columns.Add(x => x.OnOrder, 80, "Ordered", "", Alignment.MiddleCenter); columns.Add(x => x.JobShortage, 80, "Job Short", "", Alignment.MiddleCenter); columns.Add(x => x.FreeStockTotal, 80, "Free Stock", "", Alignment.MiddleCenter); columns.Add(x => x.FreeStockShortage, 80, "Stk Short", "", Alignment.MiddleCenter); return columns; } private void BuildMenu(DynamicMenuColumn column, CoreRow? row) { if (row is null) return; var menu = column.GetMenu(); menu.AddItem("View Stock Movements", PRSDesktop.Resources.forklift, row, ViewStockMovements_Click); foreach(var col in DetailsColumns) { menu.AddItem(col.MenuText, null, row, col.Action); } } private void ViewStockMovements_Click(CoreRow row) { var item = row.ToObject(); var productID = item.Product.ID; Guid? styleID = StyleColumnVisible() ? item.Style.ID : null; var dimensions = DimensionsColumnVisible() ? item.Dimensions : null; /*ShowDetailGrid( args.Column.ColumnName, x => x.Product.ID, productid, x => x.Style.ID, styleid, x => x.Dimensions.UnitSize, unitsize, x => x.Job.ID, new Filter(x => x.IsTransfer).IsEqualTo(false).And(x => x.Issued).IsNotEqualTo(0.0F), null ); var movements = Client.Query( );*/ var grid = (Activator.CreateInstance(typeof(DynamicDataGrid<>).MakeGenericType(typeof(StockMovement))) as DynamicDataGrid); if (grid == null) { MessageBox.Show($"Cannot create Grid for [{typeof(StockMovement).Name}]"); return; } grid.ColumnsTag = $"{ColumnsTag}.Transactions"; grid.Reconfigure(options => { options.Clear(); options.FilterRows = true; options.SelectColumns = true; }); grid.OnReload += (object sender, Filters criteria, Columns columns, ref SortOrder? sortby) => { var filter = criteria.Combine(); criteria.Clear(); criteria.Add(new Filter(x => x.Transaction).InQuery(filter, x => x.Transaction)); }; grid.OnDefineFilter += t => { var filter = new Filter(x => x.Product.ID).IsEqualTo(productID); if(dimensions is not null) filter = filter.And(x => x.Dimensions).DimensionEquals(dimensions); if (styleID.HasValue) filter = filter.And(x => x.Style.ID).IsEqualTo(styleID); filter = filter.And(x => x.Job.ID).IsEqualTo(Master?.ID ?? Guid.Empty); return filter; }; var window = DynamicGridUtils.CreateGridWindow($"Stock Movements", grid); window.ShowDialog(); } private void ShowDetailGrid( String columnname, Expression> productcol, Guid productid, Expression> stylecol, Guid? styleid, Expression> dimcol, IDimensions? dimensions, Expression>? jobcol, Filter? extrafilter, Func? rowfilter ) { var grid = (Activator.CreateInstance(typeof(DynamicDataGrid<>).MakeGenericType(typeof(TEntity))) as IDynamicDataGrid); if (grid == null) { MessageBox.Show($"Cannot create Grid for [{typeof(TEntity).Name}]"); return; } grid.ColumnsTag = $"{ColumnsTag}.{columnname}"; grid.Reconfigure(options => { options.Clear(); options.FilterRows = true; options.SelectColumns = true; }); grid.OnDefineFilter += t => { var filter = new Filter(productcol).IsEqualTo(productid); if(dimensions is not null) filter = filter.And(CoreUtils.GetFullPropertyName(dimcol, ".")).DimensionEquals(dimensions); if (styleid.HasValue) filter = filter.And(stylecol).IsEqualTo(styleid); if (jobcol != null) filter = filter.And(jobcol).IsEqualTo(Master?.ID ?? Guid.Empty); if (extrafilter != null) filter = filter.And(extrafilter); return filter; }; grid.OnFilterRecord += row => rowfilter?.Invoke(row) ?? true; var window = DynamicGridUtils.CreateGridWindow($"Viewing {CoreUtils.Neatify(columnname)} Calculation", grid); window.ShowDialog(); } private static readonly Column BOMColumn = new Column(x => x.BillOfMaterials); private static readonly Column RequisitionsColumn = new Column(x => x.Requisitions); private static readonly Column PickingListsColumn = new Column(x => x.PickingLists); private static readonly Column IssuedColumn = new Column(x => x.Issued); private static readonly Column ReservedStockColumn = new Column(x => x.ReservedStock); private static readonly Column OnOrderColumn = new Column(x => x.OnOrder); private static readonly Column FreeOnHandColumn = new Column(x => x.FreeOnHand); private static readonly Column FreeOnOrderColumn = new Column(x => x.FreeOnOrder); private void ViewBillOfMaterials(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( BOMColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, x => x.Job.ID, new Filter(x => x.BillOfMaterials.Approved).IsNotEqualTo(DateTime.MinValue), null ); } private void ViewRequisitions(CoreRow row) { var item = row.ToObject(); Guid? styleID = StyleColumnVisible() ? item.Style.ID : null; var dimensions = DimensionsColumnVisible() ? item.Dimensions : null; var grid = new JobRequisitionItemSummaryGrid(); grid.OnDefineFilter += t => { var filter = new Filter(x => x.Product.ID).IsEqualTo(item.Product.ID) .And(x => x.Requisition.Approved).IsNotEqualTo(DateTime.MinValue) .And(x => x.Cancelled).IsEqualTo(DateTime.MinValue); if(dimensions is not null) { filter = filter.And(x => x.Dimensions).DimensionEquals(item.Dimensions); } if (styleID.HasValue) { filter = filter.And(x => x.Style.ID).IsEqualTo(styleID.Value); } filter = filter.And(x => x.Requisition.Job.ID).IsEqualTo(Master?.ID ?? Guid.Empty); return filter; }; var window = DynamicGridUtils.CreateGridWindow($"Viewing Requisition Calculation", grid); window.ShowDialog(); } private void ViewPickingLists(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( PickingListsColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, x => x.JobLink.ID, new Filter(x => x.RequisitionLink.Filled).IsEqualTo(DateTime.MinValue), null ); } private void ViewIssued(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( IssuedColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, x => x.Job.ID, new Filter(x => x.Type).IsEqualTo(StockMovementType.Issue), null ); } private void ViewReservedStock(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( ReservedStockColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, x => x.Job.ID, new Filter(x => x.Units).IsGreaterThan(0.1), null ); } private void ViewOnOrder(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( OnOrderColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, x => x.Job.ID, null, null ); } private void ViewFreeOnHand(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( FreeOnHandColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, null, new Filter(x => x.Units).IsNotEqualTo(0.0F) .And( IncludeReserves ? new Filter(x => x.Job.ID).IsNotEqualTo(Master?.ID ?? Guid.Empty) : new Filter(x => x.Job.JobStatus.Active).IsEqualTo(false) ), null ); } private void ViewFreeOnOrder(CoreRow row) { var item = row.ToObject(); ShowDetailGrid( FreeOnOrderColumn.Property, x => x.Product.ID, item.Product.ID, x => x.Style.ID, StyleColumnVisible() ? item.Style.ID : null, x => x.Dimensions, DimensionsColumnVisible() ? item.Dimensions : null, null, IncludeReserves ? new Filter(x => x.Job.ID).IsNotEqualTo(Master?.ID ?? Guid.Empty) : new Filter(x => x.Job.JobStatus.Active).IsEqualTo(false), null ); } [MemberNotNull(nameof(DetailsColumns))] private void SetupDetailsColumns() { DetailsColumns = new List { new(BOMColumn, ViewBillOfMaterials, "View bill of materials"), new(RequisitionsColumn, ViewRequisitions, "View requisitions"), new(PickingListsColumn, ViewPickingLists, "View picking lists"), new(IssuedColumn, ViewIssued, "View issued"), new(ReservedStockColumn, ViewReservedStock, "View reserved stock"), new(OnOrderColumn, ViewOnOrder, "View on order"), new(FreeOnHandColumn, ViewFreeOnHand, "View free on hand"), new(FreeOnOrderColumn, ViewFreeOnOrder, "View free on order"), }; } private class DetailsColumn { public Column Column { get; set; } public Action Action { get; set; } public string MenuText { get; set; } public DetailsColumn(Column column, Action action, string menuText) { Column = column; Action = action; MenuText = menuText; } } private void JobSummaryGrid_OnCellDoubleClick(object sender, DynamicGridCellClickEventArgs args) { if (args?.Column is DynamicGridColumn col) { foreach (var column in DetailsColumns) { if (column.Column.IsEqualTo(col.ColumnName)) { column.Action(args.Row); break; } } } } public event DataModelUpdateEvent? OnUpdateDataModel; public string SectionName => "Job Summary"; public StockReleaseWriteDownMethod StockRelease { get; set; } public DataModel DataModel(Selection selection) { return new AutoDataModel(MasterDetailFilter); } public override JobMaterial CreateItem() { var result = base.CreateItem(); result.Job.ID = Master?.ID ?? Guid.Empty; result.Job.Synchronise(Master ?? new Job()); return result; } private class Key(Guid jobID, Guid productID, Guid? styleID, IDimensions? dimensions) { public Guid JobID { get; set; } = jobID; public Guid ProductID { get; set; } = productID; public Guid? StyleID { get; set; } = styleID; public IDimensions? Dimensions { get; set; } = dimensions; public override bool Equals(object? obj) { return obj is Key key && JobID == key.JobID && ProductID == key.ProductID && StyleID == key.StyleID && Equals(Dimensions, key.Dimensions); } public override int GetHashCode() { return HashCode.Combine( JobID, ProductID, StyleID, Dimensions); } } private Key[] GetKeys(IEnumerable rows, Columns columns, bool hasstyle, bool hasDimensions) { int jobcol = columns.IndexOf(x => x.Job.ID); int productcol = columns.IndexOf(x => x.Product.ID); int stylecol = hasstyle ? columns.IndexOf(x => x.Style.ID) : -1; var dimCols = hasDimensions ? Dimensions.GetFilterColumnIndices(columns, x => x.Dimensions) : null; var result = rows.Select(r => new Key( (Guid)(r.Values[jobcol] ?? Guid.Empty), (Guid)(r.Values[productcol] ?? Guid.Empty), (stylecol != -1) ? (Guid)(r.Values[stylecol] ?? Guid.Empty) : null, dimCols is not null ? r.ToDimensions(dimCols) : null) ).Distinct().ToArray(); return result; } private CoreRow[] GetRows(IEnumerable rows, Columns columns, Guid? jobID, Key key, Func? extrafilter = null) where TSource : IJobMaterial { int jobcol = columns.IndexOf(x => x.Job.ID); int productcol = columns.IndexOf(x => x.Product.ID); int stylecol = key.StyleID.HasValue ? columns.IndexOf(x => x.Style.ID) : -1; int unitcol = columns.IndexOf(x => x.Dimensions.UnitSize); var dimCols = Dimensions.GetFilterColumnIndices(columns, x => x.Dimensions); var subset = rows .Where(r => (!jobID.HasValue || Guid.Equals(jobID, r.Values[jobcol])) && Guid.Equals(key.ProductID, r.Values[productcol]) && (!key.StyleID.HasValue || Guid.Equals(key.StyleID, r.Values[stylecol])) && (key.Dimensions is null || key.Dimensions.Equals(r.ToDimensions(dimCols))) && ((extrafilter == null) || extrafilter(r)) ); return subset.ToArray(); } private double Aggregate(IEnumerable rows, Columns columns, bool hasstyle, bool hasjob, Expression> source, CoreRow target, Expression> aggregate) { int srcol = columns.IndexOf(source); if (srcol == -1) return 0.00; var total = rows.Aggregate(0d, (value, row) => value + (double)(row.Values[srcol] ?? 0.0d)); target.Set(aggregate, total); return total; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, Action action) { criteria.Add(MasterDetailFilter); criteria.Add(FilterComponent.GetFilter()); var orderby = sort; Progress.ShowModal("Loading Data", (progress) => { var table = new CoreTable(); table.LoadColumns(columns); var data = new Client().Query(criteria.Combine(), columns, orderby); var pids = data.ExtractValues(x => x.Product.ID).ToArray(); if (pids.Any()) { var results = Client.QueryMultiple( new KeyedQueryDef( new Filter(x => x.Product.ID).InList(pids) .And(x => x.Units).IsNotEqualTo(0.0F) .And(new Filter(x => x.Job.ID).IsEqualTo(Guid.Empty).Or(x => x.Job.ID).IsNotEqualTo(Master?.ID ?? Guid.Empty)), Columns.None().Add(x => x.Product.ID) .Add(x => x.Style.ID) .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local) .Add(x => x.Units) .Add(x => x.Job.ID) .Add(x => x.Job.JobStatus.Active)), new KeyedQueryDef( new Filter(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue) .And(x => x.Product.ID).InList(pids) .And(new Filter(x => x.Job.ID).IsEqualTo(Guid.Empty).Or(x => x.Job.ID).IsNotEqualTo(Master?.ID ?? Guid.Empty)), Columns.None().Add(x => x.Product.ID) .Add(x => x.Style.ID) .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local) .Add(x => x.Qty) .Add(x => x.Job.ID) .Add(x => x.Job.JobStatus.Active))); var freestock = results.Get(); var freestockcolumns = Columns.None().Add(freestock.Columns.Select(x => x.ColumnName)); var freeorders = results.Get(); var freeordercolumns = Columns.None().Add(freeorders.Columns.Select(x => x.ColumnName)); var hasStyle = StyleColumnVisible(); var hasDimensions = DimensionsColumnVisible(); var keys = GetKeys(data.Rows, columns, hasStyle, hasDimensions); foreach (var key in keys) { var rows = GetRows(data.Rows, columns, key.JobID, key); if (rows.Any()) { CoreRow newrow = table.NewRow(); newrow.LoadValues(rows.First().Values); var bom = Aggregate(rows, columns, hasStyle, true, x => x.BillOfMaterials, newrow, x => x.BillOfMaterials); var requi = Aggregate(rows, columns, hasStyle, true, x => x.Requisitions, newrow, x => x.Requisitions); var picklist = Aggregate(rows, columns, hasStyle, true, x => x.PickingLists, newrow, x => x.PickingLists); var issued = Aggregate(rows, columns, hasStyle, true, x => x.Issued, newrow, x => x.Issued); var reserved = Aggregate(rows, columns, hasStyle, true, x => x.ReservedStock, newrow, x => x.ReservedStock); var ordered = Aggregate(rows, columns, hasStyle, true, x => x.OnOrder, newrow, x => x.OnOrder); var shortage = Math.Max(0, Math.Max(0, (requi - issued)) - (reserved + ordered)); newrow.Set(x => x.JobShortage, shortage); var freestockrows = GetRows(freestock.Rows, freestockcolumns, null, key, IncludeReserves ? null : (r) => !r.Get(x => x.Job.JobStatus.Active)); var freeonhand = Aggregate(freestockrows, freestockcolumns, hasStyle, false, x => x.Units, newrow, x => x.FreeOnHand); newrow.Set(x => x.FreeOnHand, freeonhand); var freeorderrows = GetRows(freeorders.Rows, freeordercolumns, null, key, IncludeReserves ? null : (r) => !r.Get(x => x.Job.JobStatus.Active)); var freeonorder = Aggregate(freeorderrows, freeordercolumns, hasStyle, false, x => x.Qty, newrow, x => x.FreeOnOrder); newrow.Set(x => x.FreeOnOrder, freeonorder); newrow.Set(x => x.FreeStockTotal, freeonhand + freeonorder); newrow.Set(x => x.FreeStockShortage, Math.Max(0, shortage - (freeonhand + freeonorder))); table.Rows.Add(newrow); } } } action?.Invoke(table, null); } ); } protected override bool FilterRecord(CoreRow row) { var result = base.FilterRecord(row) && ( row.Get(x => x.BillOfMaterials) != 0.0F || row.Get(x => x.Requisitions) != 0.0F || row.Get(x => x.PickingLists) != 0.0F || row.Get(x => x.Issued) != 0.0F || row.Get(x => x.ReservedStock) != 0.0F || row.Get(x => x.OnOrder) != 0.0F ); if (ShowIssues) { result = result && ( row.Get(x => x.JobShortage) > 0.0F || row.Get(x => x.FreeStockShortage) > 0.0F); } return result; } #region Create PO private bool CreatePO(System.Windows.Controls.Button btn, CoreRow[] rows) { if (!rows.Any()) { MessageBox.Show("Please select at least one row to add to PO"); return false; } PurchaseOrder purchaseOrder = new PurchaseOrder(); purchaseOrder.Description = "Created from Job Summary Screen" + System.Environment.NewLine; purchaseOrder.RaisedBy.ID = empID; var page = new SupplierPurchaseOrders(); page.OnAfterSave += (form, items) => { MessageBox.Show("Success - New Purchase Order Created (" + purchaseOrder.PONumber + ")"); }; return page.EditItems(new[] { purchaseOrder }, LoadPurchaseOrderItems, true); } private CoreTable LoadPurchaseOrderItems(Type arg) { Progress.Show("Working"); var result = new CoreTable(); result.LoadColumns(typeof(PurchaseOrderItem)); List items = new List(); foreach (CoreRow row in SelectedRows) { JobMaterial material = row.ToObject(); PurchaseOrderItem POItem = new PurchaseOrderItem(); POItem.Product.ID = material.Product.ID; POItem.Product.Code = material.Product.Code; POItem.Product.Name = material.Product.Name; POItem.Description = material.Product.Name; POItem.Qty = 0; POItem.Dimensions.CopyFrom(material.Dimensions); POItem.Style.ID = material.Style.ID; POItem.Style.Code = material.Style.Code; POItem.Style.Description = material.Style.Description; POItem.Job.ID = material.Job.ID; POItem.Dimensions.UnitSize = material.Dimensions.UnitSize; items.Add(POItem); } result.LoadRows(items); Progress.Close(); return result; } #endregion }