123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.Xaml.Behaviors.Core;
- using PRSDesktop.Panels.PurchaseOrders;
- using PurchaseOrder = MYOB.AccountRight.SDK.Contracts.Version2.Purchase.PurchaseOrder;
- using PurchaseOrderItemAllocation = Comal.Classes.PurchaseOrderItemAllocation;
- namespace PRSDesktop;
- public class ConsignmentItemGrid : DynamicDataGrid<PurchaseOrderItem>
- {
- private Button receiveall;
- private Button receiveselected;
- private Button allocateparcels;
- public Guid ConsignmentID { get; set; } = CoreUtils.FullGuid;
- public Guid ParcelID { get; set; } = Guid.Empty;
- public bool Completed { get; set; }
-
- public Func<CoreTable>? GetParcels { get; set; }
-
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x=>x.ConsignmentParcel.ID);
- HiddenColumns.Add(x=>x.ConsignmentParcel.Description);
- allocateparcels = AddButton("Parcels", null, ShowParcelMenu);
- allocateparcels.IsEnabled = false;
-
- receiveall = AddButton("Receive All", null, ReceiveAll);
- receiveall.IsEnabled = false;
-
- receiveselected = AddButton("Receive Selected", null, ReceiveSelected);
- receiveselected.IsEnabled = false;
-
- AddButton("Assign Location", null, SupplierPurchaseOrderItemOneToMany.AssignLocation);
- ActionColumns.Add(
- new DynamicMenuColumn(
- DoBuildMenu,
- row => row.Get<PurchaseOrderItem,DateTime>(x=>x.ReceivedDate).IsEmpty()
- ? DynamicMenuStatus.Enabled
- : DynamicMenuStatus.Disabled
- )
- );
- }
- private void DoBuildMenu(DynamicMenuColumn column, CoreRow? row)
- {
- if (row is null)
- return;
- column.AddItem("Split Line", PRSDesktop.Resources.split, SplitLine);
- var parcels = GetParcels?.Invoke().ToArray<ConsignmentParcel>() ?? [];
- if (parcels.Any())
- {
- column.AddSeparator();
- foreach (var parcel in parcels.Where(x=>x.ID != Guid.Empty && x.ID != CoreUtils.FullGuid))
- column.AddItem(parcel.Description, null, r => AllocateParcels([r], parcel));
- if (row.Get<PurchaseOrderItem, Guid>(x => x.ConsignmentParcel.ID) != Guid.Empty)
- {
- column.AddSeparator();
- column.AddItem("Clear Parcel", null, r => AllocateParcels([r], new ConsignmentParcel()));
- }
- }
- }
-
- private void SplitLine(CoreRow? row)
- {
- if (row is null || !row.Get<PurchaseOrderItem, DateTime>(x => x.ReceivedDate).IsEmpty())
- return;
- Guid itemid = row.Get<PurchaseOrderItem, Guid>(x => x.ID);
- var query = new MultiQuery();
- query.Add(
- new Filter<PurchaseOrderItem>(x => x.ID).IsEqualTo(itemid),
- Columns.Local<PurchaseOrderItem>()
- );
- query.Add(
- new Filter<PurchaseOrderItemAllocation>(x => x.Item.ID),
- Columns.Local<PurchaseOrderItemAllocation>()
- );
- query.Query();
- var item = query.Get<PurchaseOrderItem>().ToObjects<PurchaseOrderItem>().FirstOrDefault() ?? new PurchaseOrderItem();
- var allocations = query.Get<PurchaseOrderItemAllocation>().ToList<PurchaseOrderItemAllocation>();
- var splits = allocations.ToList(SupplierPurchaseOrderItemSplit.FromAllocation);
- if (SupplierPurchaseOrderItemSplitWindow.Execute(item.Qty, splits, out var value))
- {
- var newItem = item.Clone();
- newItem.ID = Guid.Empty;
- newItem.CommitChanges();
- newItem.Qty = item.Qty - value;
- item.Qty = value;
- foreach(var split in splits)
- {
- var allocation = allocations.FirstOrDefault(x => x.ID == split.AllocationID);
- if (allocation is null)
- continue;
-
- var newAllocation = new PurchaseOrderItemAllocation();
- newAllocation.Job.CopyFrom(allocation.Job);
- newAllocation.JobRequisitionItem.CopyFrom(allocation.JobRequisitionItem);
- newAllocation.Quantity = allocation.Quantity - split.SplitQuantity;
- allocations.Add(newAllocation);
-
- allocation.Quantity = split.SplitQuantity;
- }
- Progress.ShowModal("Saving items", progress =>
- {
- Client.Save([item,newItem],"Split Consignment Line");
- Client.Save(allocations,"Split Consignment Line");
- });
-
- Refresh(false,true);
- }
- }
-
- private bool ShowParcelMenu(Button button, CoreRow[] rows)
- {
- var parcels = GetParcels?.Invoke()?.ToArray<ConsignmentParcel>() ?? [];
- if (!parcels.Any())
- return false;
- ContextMenu menu = new ContextMenu();
- if (rows.Any(r => r.Get<PurchaseOrderItem,Guid>(c => c.ConsignmentParcel.ID) != Guid.Empty))
- {
- menu.Items.Add(
- new MenuItem()
- {
- Header = "Clear Allocation",
- Command = new ActionCommand(() => AllocateParcels(rows, new ConsignmentParcel()))
- });
- menu.Items.Add(new Separator());
- }
-
- foreach (var parcel in parcels.Where(p => p.ID != Guid.Empty && p.ID != CoreUtils.FullGuid))
- menu.Items.Add(
- new MenuItem()
- {
- Header = parcel.Description,
- Command = new ActionCommand(() => AllocateParcels(rows, parcel))
- }
- );
-
- menu.IsOpen = true;
- return false;
- }
-
- private void AllocateParcels(CoreRow[] rows, ConsignmentParcel parcel)
- {
- List<PurchaseOrderItem> updates = new();
- foreach (var row in rows)
- {
- var update = row.ToObject<PurchaseOrderItem>();
- update.ConsignmentParcel.CopyFrom(parcel);
- updates.Add(update);
-
- row.Set<PurchaseOrderItem,Guid>(x=>x.ConsignmentParcel.ID, parcel.ID);
- row.Set<PurchaseOrderItem,string>(x=>x.ConsignmentParcel.Description, parcel.Description);
- InvalidateRow(row);
-
- }
- Client.Save(updates,"Updated from Consignment Screen", (o,e) => { });
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.AddRows = true;
- options.DeleteRows = true;
- options.MultiSelect = true;
- options.FilterRows = true;
- options.HideDatabaseFilters = true;
- }
-
- protected override void SelectItems(CoreRow[]? rows)
- {
- var parcels = GetParcels?.Invoke()?.ToArray<ConsignmentParcel>() ?? [];
- allocateparcels.IsEnabled = parcels.Any() && rows != null && rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- receiveselected.IsEnabled = rows != null && rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- receiveall.IsEnabled = Data.Rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- base.SelectItems(rows);
- }
- private bool ReceiveAll(Button sender, CoreRow[] rows)
- {
- var unreceived = Data.Rows.Where(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- if (!unreceived.Any())
- {
- MessageBox.Show("No Items to Receive");
- return false;
- }
- var now = DateTime.Now;
- using (new WaitCursor())
- {
- var items = unreceived.Select(x => x.ToObject<PurchaseOrderItem>());
- foreach (var item in items)
- item.ReceivedDate = now;
- new Client<PurchaseOrderItem>().Save(items, "Consignment Items Received");
- }
- return true;
- }
- private bool ReceiveSelected(Button sender, CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageBox.Show("Please select a row first");
- return false;
- }
- var now = DateTime.Now;
- using (new WaitCursor())
- {
- var items = LoadItems(rows);
- foreach (var item in items)
- item.ReceivedDate = now;
- new Client<PurchaseOrderItem>().Save(items, "Consignment Items Received");
- }
- return true;
- }
- protected override void Reload(
- Filters<PurchaseOrderItem> criteria, Columns<PurchaseOrderItem> columns, ref SortOrder<PurchaseOrderItem>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(new Filter<PurchaseOrderItem>(x => x.Consignment.ID).IsEqualTo(
- ConsignmentID == Guid.Empty ? CoreUtils.FullGuid : ConsignmentID));
- if (ParcelID != CoreUtils.FullGuid)
- criteria.Add(new Filter<PurchaseOrderItem>(x=>x.ConsignmentParcel.ID).IsEqualTo(ParcelID));
- base.Reload(criteria, columns, ref sort, token, action);
- }
- protected override void DoAdd(bool openEditorOnDirectEdit = false)
- {
- if (ConsignmentID.Equals(Guid.Empty))
- {
- MessageBox.Show("Please select a Requisition first!");
- return;
- }
- if (Completed)
- {
- MessageBox.Show("Cannot Modify a Completed Requisition");
- return;
- }
- var dialog = new MultiSelectDialog<PurchaseOrderItem>(
- new Filter<PurchaseOrderItem>(x => x.Consignment.ID).IsEqualTo(Guid.Empty).And(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue),
- Columns.None<PurchaseOrderItem>().Add(x => x.PurchaseOrderLink.PONumber, x => x.Product.Code, x => x.Product.Name, x => x.Description,
- x => x.Qty)
- //new System.Linq.Expressions.Expression<Func<PurchaseOrderItem, object>>[] { x => x.PurchaseOrderLink.PONumber, x => x.ProductLink.Code, x=>x.ProductLink.Name, x=>x.Description, x=>x.Qty}
- );
- if (dialog.ShowDialog())
- {
- Progress.Show("Adding Order Items");
- var items = dialog.Items(Columns.Required<PurchaseOrderItem>().Add(x => x.ID).Add(x => x.Consignment.ID));
- foreach (var item in items)
- item.Consignment.ID = ConsignmentID;
- Progress.SetMessage("Updating Items");
- new Client<PurchaseOrderItem>().Save(items, "Added to Consignment");
- Refresh(false, true);
- Progress.Close();
- MessageBox.Show(string.Format("{0} order items added", items.Length));
- }
- }
- public override bool EditItems(PurchaseOrderItem[] items, Func<Type, CoreTable?>? PageDataHandler, bool PreloadPages = false)
- {
- if (ConsignmentID.Equals(Guid.Empty))
- {
- MessageWindow.ShowMessage("Please select a Requisition first!", "Invalid Action");
- return false;
- }
- if (Completed)
- {
- MessageWindow.ShowMessage("Cannot Modify a Completed Requisition", "Invalid Action");
- return false;
- }
- return base.EditItems(items, PageDataHandler, PreloadPages);
- }
- protected override bool CanDeleteItems(CoreRow[] rows)
- {
- if (ConsignmentID.Equals(Guid.Empty))
- {
- MessageBox.Show("Please select a Requisition first!");
- return false;
- }
- if (Completed)
- {
- MessageBox.Show("Cannot Modify a Closed Requisition");
- return false;
- }
- return base.CanDeleteItems(rows);
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- using (new WaitCursor())
- {
- var items = LoadItems(rows);
- foreach (var item in items)
- item.Consignment.ID = Guid.Empty;
- new Client<PurchaseOrderItem>().Save(items, "Removed from Consignment");
- }
- }
-
- }
|