123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- namespace PRSDesktop;
- public class JobBillOfMaterialsItemGrid : DynamicDataGrid<JobBillOfMaterialsItem>, IMasterDetailControl<JobBillOfMaterials, JobBillOfMaterialsItem>
- {
- public Job? Job { get; set; }
- private JobBillOfMaterials? _master;
- public JobBillOfMaterials? Master
- {
- get => _master;
- set
- {
- _master = value;
- Reconfigure();
- CheckVisibility();
- }
- }
-
- public Filter<JobBillOfMaterialsItem> MasterDetailFilter => Master != null
- ? Master.ID != Guid.Empty
- ? new Filter<JobBillOfMaterialsItem>(x => x.BillOfMaterials.ID).IsEqualTo(Master.ID)
- : new Filter<JobBillOfMaterialsItem>().None()
- : Job is not null && Job.ID != Guid.Empty
- ? new Filter<JobBillOfMaterialsItem>(x => x.BillOfMaterials.Job.ID).IsEqualTo(Job.ID)
- : new Filter<JobBillOfMaterialsItem>().None();
- private Button CreateRequiButton;
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- options.AddRange(
- DynamicGridOption.RecordCount,
- DynamicGridOption.SelectColumns,
- DynamicGridOption.ExportData,
- DynamicGridOption.FilterRows,
- DynamicGridOption.MultiSelect,
- DynamicGridOption.EditRows,
- DynamicGridOption.DeleteRows
- );
- if(Master is not null)
- {
- options.Add(DynamicGridOption.AddRows);
- }
- else
- {
- options.Remove(DynamicGridOption.AddRows);
- }
- }
- private void CheckVisibility()
- {
- CreateRequiButton.Visibility = Master is not null && Security.CanEdit<JobRequisition>()
- ? Visibility.Visible
- : Visibility.Collapsed;
- }
- protected override void Init()
- {
- base.Init();
- foreach(var column in new Columns<JobBillOfMaterialsItem>()
- .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Data)
- .GetColumns())
- {
- HiddenColumns.Add(column);
- }
- HiddenColumns.Add(x => x.Product.ID);
- HiddenColumns.Add(x => x.Product.Code);
- HiddenColumns.Add(x => x.Product.Name);
- HiddenColumns.Add(x => x.Style.ID);
- HiddenColumns.Add(x => x.Style.Code);
- HiddenColumns.Add(x => x.Style.Description);
- HiddenColumns.Add(x => x.BillOfMaterials.ID);
- HiddenColumns.Add(x => x.BillOfMaterials.Description);
- HiddenColumns.Add(x => x.Job.ID);
- HiddenColumns.Add(x => x.PurchaseOrderItem.ID);
- HiddenColumns.Add(x => x.PurchaseOrderItem.PurchaseOrderLink.PONumber);
- HiddenColumns.Add(x => x.PurchaseOrderItem.ReceivedDate);
- HiddenColumns.Add(x => x.Packet.ID);
- CreateRequiButton = AddButton("Create Requi", null, CreateRequi);
- AddButton("Create PO", null, CreatePO);
- CheckVisibility();
- }
- private bool CreateRequi(System.Windows.Controls.Button btn, CoreRow[] rows)
- {
- if (Master is null) return false;
- if (!rows.Any())
- {
- MessageBox.Show("Please select at least one row to add to Requi");
- return false;
- }
- var jobRequi = new JobRequisition();
- jobRequi.Job.ID = Master.Job.ID;
- jobRequi.Job.Synchronise(Master.Job);
- jobRequi.Description = "New Requisition";
- var page = new JobRequisitionGrid();
- page.OnAfterSave += (form, items) => { MessageBox.Show("Success - Job Requisition Raised."); };
- return page.EditItems(new[] { jobRequi }, LoadJobRequiItems, true);
- }
- private CoreTable? LoadJobRequiItems(Type arg)
- {
- if (Master is null || arg != typeof(JobRequisitionItem)) return null;
- var result = new CoreTable();
- result.LoadColumns(typeof(JobRequisitionItem));
- var requiItems = new List<JobRequisitionItem>();
- foreach (var BOMitem in SelectedRows.ToObjects<JobBillOfMaterialsItem>())
- {
- var requiItem = new JobRequisitionItem();
- requiItem.Qty = BOMitem.Quantity;
- requiItem.UnitCost = BOMitem.UnitCost;
- requiItem.Product.ID = BOMitem.Product.ID;
- requiItem.Product.Code = BOMitem.Product.Code;
- requiItem.Product.Name = BOMitem.Product.Name;
- requiItem.Style.ID = BOMitem.Style.ID;
- requiItem.Style.Code = BOMitem.Style.Code;
- requiItem.Style.Description = BOMitem.Style.Description;
- requiItem.Supplier.ID = BOMitem.Supplier.ID;
- requiItem.Supplier.Code = BOMitem.Supplier.Code;
- requiItem.Supplier.Name = BOMitem.Supplier.Name;
- requiItem.Dimensions.CopyFrom(BOMitem.Dimensions);
- requiItem.Job.ID = Master.Job.ID;
- requiItem.Job.Synchronise(Master.Job);
- requiItem.Supplier.ID = BOMitem.Supplier.ID;
- requiItem.Notes = "Create from BOM " + BOMitem.BillOfMaterials.Description + " on " + DateTime.Now.ToString("hh:mm t dd-MMM-yy") + " by " + App.EmployeeName;
- requiItems.Add(requiItem);
- }
- result.LoadRows(requiItems);
- return result;
- }
- public event DataModelUpdateEvent? OnUpdateDataModel;
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- if (Master is null || Master.ID == Guid.Empty || Master.ID == CoreUtils.FullGuid)
- MessageWindow.ShowMessage("Please select a bill of materials to cancel.", "Select BOM");
- else
- base.DoAdd();
- }
- protected override JobBillOfMaterialsItem CreateItem()
- {
- var result = base.CreateItem();
- if(Master is not null)
- {
- result.BillOfMaterials.ID = Master.ID;
- result.BillOfMaterials.Synchronise(Master);
- result.Job.ID = Master.Job.ID;
- result.Job.Synchronise(Master.Job);
- }
- result.Quantity = 1;
- return result;
- }
- protected override void Reload(Filters<JobBillOfMaterialsItem> criteria, Columns<JobBillOfMaterialsItem> columns,
- ref SortOrder<JobBillOfMaterialsItem>? sort, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, action);
- }
- protected override void OnAfterEditorValueChanged(DynamicEditorGrid? grid, JobBillOfMaterialsItem[] items, AfterEditorValueChangedArgs args, Dictionary<string, object?> changes)
- {
- base.OnAfterEditorValueChanged(grid, items, args, changes);
- if (args.ColumnName.Equals("Product.ID") || args.ColumnName.Equals("Dimensions") || args.ColumnName.StartsWith("Dimensions.") || args.ColumnName.Equals("Style.ID") || args.ColumnName.Equals("Supplier.ID"))
- {
- JobBillOfMaterialsItem.UpdateCosts(
- items,
- changes
- );
- }
- }
- #region Create PO
- private bool CreatePO(System.Windows.Controls.Button btn, CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageWindow.ShowMessage("Please select at least one row to add to PO.", "Select items");
- return false;
- }
- var purchaseOrder = new PurchaseOrder();
- purchaseOrder.Description = "Created from Job BOM Review Screen" + System.Environment.NewLine;
- purchaseOrder.RaisedBy.ID = App.EmployeeID;
- var page = new SupplierPurchaseOrders();
- page.OnAfterSave += (form, items) =>
- {
- PurchaseOrderOnSave(form, items.Cast<PurchaseOrder>().ToArray());
- MessageBox.Show("Success - New Purchase Order Created (" + purchaseOrder.PONumber + ")");
- };
- return page.EditItems(new[] { purchaseOrder }, LoadPurchaseOrderItems, true);
- }
- private void PurchaseOrderOnSave(IDynamicEditorForm form, PurchaseOrder[] items)
- {
- Progress.Show("Working");
- Guid POID = items[0].ID;
- CoreTable table = new Client<PurchaseOrderItem>().Query(new Filter<PurchaseOrderItem>(x => x.PurchaseOrderLink.ID).IsEqualTo(POID),
- new Columns<PurchaseOrderItem>(x => x.ID, x => x.Product.ID, x => x.Qty, x => x.Dimensions.UnitSize, x => x.Dimensions.Value, x => x.Style.ID, x => x.DueDate, x => x.Job.ID));
- if (table.Rows.Any())
- {
- var poItems = AddPOItems(table, new List<PurchaseOrderItem>());
- var BOMItems = MatchBOMItems(poItems, new List<JobBillOfMaterialsItem>());
- if (BOMItems.Count > 0)
- SaveAndRefreshScreen(BOMItems);
- }
- Progress.Close();
- }
- private void SaveAndRefreshScreen(List<JobBillOfMaterialsItem> bomItems)
- {
- new Client<JobBillOfMaterialsItem>().Save(bomItems, "Updated on Create Purchase Order from BOM Dashboard");
- Dispatcher.BeginInvoke(() =>
- {
- Refresh(false, true);
- });
- }
- private List<PurchaseOrderItem> AddPOItems(CoreTable table, List<PurchaseOrderItem> poItems)
- {
- foreach (CoreRow row in table.Rows)
- {
- PurchaseOrderItem poItem = row.ToObject<PurchaseOrderItem>();
- poItems.Add(poItem);
- }
- return poItems;
- }
- private List<JobBillOfMaterialsItem> MatchBOMItems(List<PurchaseOrderItem> poItems, List<JobBillOfMaterialsItem> bomItems)
- {
- foreach (CoreRow row in SelectedRows)
- {
- JobBillOfMaterialsItem bomItem = row.ToObject<JobBillOfMaterialsItem>();
- foreach (var item in poItems)
- {
- if (MatchBOMItemToPOItem(bomItem, item))
- bomItems.Add(UpdateBOMItemWithPODetails(bomItem, item));
- }
- }
- return bomItems;
- }
- private JobBillOfMaterialsItem UpdateBOMItemWithPODetails(JobBillOfMaterialsItem bomItem, PurchaseOrderItem item)
- {
- bomItem.PurchaseOrderItem.ID = item.ID;
- bomItem.PurchaseOrderItem.DueDate = item.DueDate;
- return bomItem;
- }
- private bool MatchBOMItemToPOItem(JobBillOfMaterialsItem bomItem, PurchaseOrderItem item)
- {
- if (bomItem.Product.ID == item.Product.ID &&
- bomItem.Dimensions.UnitSize == item.Dimensions.UnitSize &&
- bomItem.Dimensions.Value == item.Dimensions.Value &&
- bomItem.Style.ID == item.Style.ID &&
- bomItem.Quantity == item.Qty &&
- bomItem.Job.ID == item.Job.ID)
- return true;
- else
- return false;
- }
- private CoreTable LoadPurchaseOrderItems(Type arg)
- {
- Progress.Show("Working");
- var result = new CoreTable();
- result.LoadColumns(typeof(PurchaseOrderItem));
- List<PurchaseOrderItem> items = new List<PurchaseOrderItem>();
- foreach (CoreRow row in SelectedRows)
- {
- JobBillOfMaterialsItem BOMItem = row.ToObject<JobBillOfMaterialsItem>();
- PurchaseOrderItem POItem = new PurchaseOrderItem();
- POItem.Product.ID = BOMItem.Product.ID;
- POItem.Product.Code = BOMItem.Product.Code;
- POItem.Product.Name = BOMItem.Product.Name;
- POItem.Qty = BOMItem.Quantity;
- POItem.Dimensions.CopyFrom(BOMItem.Dimensions);
- POItem.Dimensions.Value = BOMItem.Dimensions.Value;
- POItem.Style.ID = BOMItem.Style.ID;
- POItem.Style.Code = BOMItem.Style.Code;
- POItem.Style.Description = BOMItem.Style.Description;
- POItem.Job.ID = BOMItem.Job.ID;
- POItem.Dimensions.UnitSize = BOMItem.Dimensions.UnitSize;
- POItem.Description = BOMItem.Product.Name + " (" + BOMItem.Dimensions.ToString() + ")";
- items.Add(POItem);
- }
- result.LoadRows(items);
- Progress.Close();
- return result;
- }
- #endregion
- }
|