123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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 PRSDesktop.Panels.PurchaseOrders;
- using Syncfusion.Windows.Tools.Controls;
- namespace PRSDesktop;
- public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseOrder,PurchaseOrderItem>
- {
- private Button bill;
- private Button? createConsignment;
- //private Button? viewconsign;
- private Button receive;
- private Button assignLocation;
- private readonly List<Tuple<PurchaseOrderItem, PurchaseOrderItemAllocation>> Allocations = new();
- public SupplierPurchaseOrderItemOneToMany() : base()
- {
- HiddenColumns.Add(x => x.ID);
- HiddenColumns.Add(x => x.Description);
- HiddenColumns.Add(x => x.TaxRate);
- HiddenColumns.Add(x => x.ExTax);
- HiddenColumns.Add(x => x.Tax);
- HiddenColumns.Add(x => x.IncTax);
- HiddenColumns.Add(x => x.ReceivedDate);
- HiddenColumns.Add(x => x.Qty);
- HiddenColumns.Add(x => x.Balance);
- HiddenColumns.Add(x => x.PORevision);
- HiddenColumns.Add(x => x.DueDate);
- HiddenColumns.Add(x => x.SupplierCode);
- HiddenColumns.Add(x => x.BillLine.ID);
- HiddenColumns.Add(x => x.Consignment.ID);
- HiddenColumns.Add(x => x.StockLocation.ID);
- HiddenColumns.Add(x => x.PurchaseGL.ID);
- HiddenColumns.Add(x => x.CostCentre.ID);
- 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.Job.ID);
- HiddenColumns.Add(x => x.TaxCode.ID);
- HiddenColumns.Add(x => x.TaxCode.Code);
- HiddenColumns.Add(x => x.TaxCode.Description);
- HiddenColumns.Add(x => x.TaxCode.Rate);
- HiddenColumns.Add(x => x.PostedReference);
- foreach (var column in Columns.None<PurchaseOrderItem>()
- .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local))
- {
- HiddenColumns.Add(column);
- }
- HiddenColumns.Add(x => x.PurchaseOrderLink.SupplierLink.ID);
- HiddenColumns.Add(x => x.PurchaseOrderLink.Category.ID);
- HiddenColumns.Add(x => x.Product.DigitalForm.ID);
- HiddenColumns.Add(x => x.Product.DigitalForm.Description);
- HiddenColumns.Add(x => x.Product.Image.ID);
- HiddenColumns.Add(x => x.Product.Image.FileName);
- ActionColumns.Add(new DynamicImageManagerColumn<PurchaseOrderItem>(this, x => x.Product.Image, true)
- { Position = DynamicActionColumnPosition.Start });
- HiddenColumns.Add(x => x.FormCount);
- HiddenColumns.Add(x => x.OpenForms);
- ActionColumns.Add(new DynamicMenuColumn(BuildFormsMenu) { Position = DynamicActionColumnPosition.End });
- ActionColumns.Add(new DynamicImageColumn(FormsImage) { Position = DynamicActionColumnPosition.Start, ToolTip = FormsToolTip });
- }
- protected override void Init()
- {
- base.Init();
-
- AddButton("Catalog", PRSDesktop.Resources.product.AsBitmapImage(), SearchProducts);
- if (Security.IsAllowed<CanViewConsignmentModule>())
- {
- createConsignment = AddButton("Add to Consignment", null, AddToConsignment);
- createConsignment.IsEnabled = false;
- }
- receive = AddButton("Receive Items", null, ReceiveItems);
- receive.IsEnabled = false;
- bill = AddButton("Enter Bill", null, EnterBill);
- bill.IsEnabled = false;
- assignLocation = AddButton("Assign Location", null, AssignLocation);
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- if (!ReadOnly && Security.CanEdit<PurchaseOrderItem>())
- {
- options.DirectEdit = true;
- options.DragTarget = true;
- }
- //if (!IsDirectEditMode(options))
- //{
- options.FilterRows = true;
- //}
- }
- protected override void OnAfterRefresh()
- {
- base.OnAfterRefresh();
- Allocations.RemoveAll(x => !Items.Contains(x.Item1));
- }
- public override void AfterSave(object item)
- {
- base.AfterSave(item);
- var toSave = new List<PurchaseOrderItemAllocation>();
- foreach (var poItem in Items)
- {
- var allocations = Allocations.Where(x => x.Item1 == poItem).Select(x => x.Item2);
- foreach (var allocation in allocations)
- {
- allocation.Item.ID = poItem.ID;
- }
- toSave.AddRange(allocations);
- }
- Client.Save(toSave, "");
- }
- private void BuildFormsMenu(DynamicMenuColumn column, CoreRow? row)
- {
- if (row == null) return;
- if (Security.CanEdit<PurchaseOrderItem>())
- {
- column.AddItem("Split Line", PRSDesktop.Resources.split, SplitLine, enabled: row.Get<PurchaseOrderItem, DateTime>(x => x.ReceivedDate).IsEmpty());
- }
- if(row.Get<PurchaseOrderItem, Guid>(x => x.Consignment.ID) != Guid.Empty)
- {
- column.AddItem("View Consignment", null, ViewConsignment);
- }
- var formsItem = column.AddItem("Digital Forms", PRSDesktop.Resources.kanban, null);
- DynamicGridUtils.PopulateFormMenu<PurchaseOrderItemForm, PurchaseOrderItem, PurchaseOrderItemLink>(
- formsItem,
- row.Get<PurchaseOrderItem, Guid>(x => x.ID),
- row.ToObject<PurchaseOrderItem>);
- }
- private void SplitLine(CoreRow? row)
- {
- if (row is null)
- return;
- var grid = new SupplierPurchaseOrderItemSplitGrid();
- var poi = LoadItem(row);
- var _allocations = Allocations.Where(x => x.Item1 == poi).Select(x => x.Item2).ToList();
- if(poi.ID != Guid.Empty)
- {
- // Otherwise, they'll all be in the database.
- _allocations.AddRange(Client.Query(
- new Filter<PurchaseOrderItemAllocation>(x => x.Item.ID).IsEqualTo(poi.ID)
- .And(x => x.ID).NotInList(_allocations.ToArray(x => x.ID)),
- Columns.None<PurchaseOrderItemAllocation>()
- .Add(x => x.ID)
- .Add(x => x.Job.ID).Add(x => x.JobRequisitionItem.ID).Add(x => x.Quantity))
- .ToObjects<PurchaseOrderItemAllocation>());
- }
- var items = _allocations.ToList(SupplierPurchaseOrderItemSplit.FromAllocation);
- if (SupplierPurchaseOrderItemSplitWindow.Execute(poi.Qty, items, out var value))
- {
- var newLine = CreateItem();
- newLine.BillLine.CopyFrom(poi.BillLine);
- newLine.StockLocation.CopyFrom(poi.StockLocation);
- newLine.Consignment.CopyFrom(poi.Consignment);
- newLine.PurchaseGL.CopyFrom(poi.PurchaseGL);
- newLine.CostCentre.CopyFrom(poi.CostCentre);
- newLine.Product.CopyFrom(poi.Product);
- newLine.Style.CopyFrom(poi.Style);
- newLine.Job.CopyFrom(poi.Job);
- newLine.TaxCode.CopyFrom(poi.TaxCode);
- newLine.PurchaseOrderLink.CopyFrom(poi.PurchaseOrderLink);
- newLine.Dimensions.CopyFrom(poi.Dimensions);
- // Copying across the posted reference; this would be a problem if we were to try to sync via a Poster two purchase order items with the
- // original posted single line; however, after discussing with Frank, we can't imagine a place where we would do this; we would generally replace
- // the posted lines. Hence, we can copy the posted reference.
- newLine.PostedReference = poi.PostedReference;
- newLine.Description = poi.Description;
- newLine.TaxRate = poi.TaxRate;
- newLine.ExTax = poi.ExTax;
- newLine.Tax = poi.Tax;
- newLine.IncTax = poi.IncTax;
- newLine.Cost = poi.Cost;
- newLine.Balance = poi.Balance;
- newLine.PORevision = poi.PORevision;
- newLine.DueDate = poi.DueDate;
- newLine.SupplierCode = poi.SupplierCode;
- newLine.Qty = poi.Qty - value;
- poi.Qty = value;
- foreach(var item in items)
- {
- var allocation = _allocations.FirstOrDefault(x => x.ID == item.AllocationID);
- if (allocation is null) continue;
- // Add to a list to be saved later.
- var newAllocation = new PurchaseOrderItemAllocation();
- newAllocation.Job.CopyFrom(allocation.Job);
- newAllocation.JobRequisitionItem.CopyFrom(allocation.JobRequisitionItem);
- newAllocation.Quantity = allocation.Quantity - item.SplitQuantity;
- allocation.Quantity = item.SplitQuantity;
- // Save both allocations.
- if(!Allocations.Any(x => x.Item2 == allocation))
- {
- Allocations.Add(new(poi, allocation));
- }
- Allocations.Add(new(newLine, newAllocation));
- }
- SaveItem(poi);
- SaveItem(newLine);
- Refresh(false, true);
- DoChanged();
- }
- }
- private void ViewConsignment(CoreRow? row)
- {
- if (row is null) return;
- var consignmentID = row.Get<PurchaseOrderItem, Guid>(x => x.Consignment.ID);
- var consignments = Client.Query(
- new Filter<Consignment>(x => x.ID).IsEqualTo(consignmentID),
- DynamicGridUtils.LoadEditorColumns(Columns.None<Consignment>()))
- .ToObjects<Consignment>().ToArray();
- DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(Consignment)).EditItems(consignments);
- }
- private FrameworkElement? FormsToolTip(DynamicActionColumn arg1, CoreRow? arg2)
- {
- var text = arg2 == null || arg2.Get<PurchaseOrderItem, Guid>(x => x.Product.DigitalForm.ID) == Guid.Empty
- ? ""
- : arg2.Get<PurchaseOrderItem, int>(c => c.FormCount) == 0
- ? "No forms found for this item"
- : arg2.Get<PurchaseOrderItem, int>(c => c.OpenForms) > 0
- ? "Incomplete forms found"
- : "All forms completed";
- return string.IsNullOrWhiteSpace(text) ? null : arg1.TextToolTip(text);
- }
- private BitmapImage? FormsImage(CoreRow? arg)
- {
- if (arg == null)
- return PRSDesktop.Resources.quality.AsBitmapImage();
- return arg.Get<PurchaseOrderItem, Guid>(x => x.Product.DigitalForm.ID) == Guid.Empty
- ? null
- : arg.Get<PurchaseOrderItem, int>(c => c.FormCount) == 0
- ? PRSDesktop.Resources.warning.AsBitmapImage()
- : arg.Get<PurchaseOrderItem, int>(c => c.OpenForms) > 0
- ? PRSDesktop.Resources.warning.AsBitmapImage()
- : PRSDesktop.Resources.quality.AsBitmapImage();
- }
- private bool AddToConsignment(Button sender, CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageBox.Show("Please select a row first");
- return false;
- }
- var poItems = LoadItems(rows);
- if(poItems.Any(x => x.ID == Guid.Empty))
- {
- MessageBox.Show("Please save this purchase order first.");
- return false;
- }
- var menu = new ContextMenu();
- menu.AddItem("New Consignment", null, () =>
- {
- var consign = new Consignment();
- consign.Supplier.ID = rows.First().Get<PurchaseOrderItem, Guid>(x => x.PurchaseOrderLink.SupplierLink.ID);
- consign.Category.ID = rows.First().Get<PurchaseOrderItem, Guid>(x => x.PurchaseOrderLink.Category.ID);
- if (new DynamicDataGrid<Consignment>().EditItems(new[] { consign }, LoadConsignmentLines, true))
- {
- foreach (var item in poItems)
- {
- item.Consignment.ID = consign.ID;
- }
- new Client<PurchaseOrderItem>().Save(poItems, "Added to new consignment");
- Refresh(false, true);
- }
- });
- menu.AddItem("Existing Consignment", null, () =>
- {
- var popupList = new PopupList(typeof(Consignment), Guid.Empty, Array.Empty<string>());
- popupList.OnDefineFilter += type =>
- {
- return new Filter<Consignment>(x => x.Closed).IsNotEqualTo(DateTime.MinValue);
- };
- if (popupList.ShowDialog() == true)
- {
- foreach (var item in poItems)
- {
- item.Consignment.ID = popupList.ID;
- }
- new Client<PurchaseOrderItem>().Save(poItems, "Added to existing consignment");
- Refresh(false, true);
- }
- });
- menu.IsOpen = true;
- return false;
- }
- private CoreTable? LoadConsignmentLines(Type type)
- {
- if (type == typeof(PurchaseOrderItem))
- {
- var result = new CoreTable();
- result.LoadColumns(typeof(PurchaseOrderItem));
- result.LoadRows(SelectedRows);
- return result;
- }
- else
- {
- return null;
- }
- }
- private static bool EnterBill(Button sender, CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageBox.Show("Please select a row first");
- return false;
- }
- var bill = new Bill();
- bill.SupplierLink.ID = rows.First()
- .Get<PurchaseOrderItem, Guid>(x => x.PurchaseOrderLink.SupplierLink.ID);
- bill.BillDate = DateTime.Today;
- return new DynamicDataGrid<Bill>().EditItems(new[] { bill }, (type) =>
- {
- return LoadBillLines(type, rows);
- }, true);
- }
- private static CoreTable LoadBillLines(Type type, CoreRow[] rows)
- {
- var result = new CoreTable();
- result.LoadColumns(typeof(BillLine));
- foreach (var row in rows)
- {
- var billrow = result.NewRow();
- billrow.Set<BillLine, Guid>(x => x.OrderItem.ID, row.Get<PurchaseOrderItem, Guid>(x => x.ID));
- var description = new List<string>();
- if (row.Get<PurchaseOrderItem, Guid>(x => x.Product.ID) != Guid.Empty)
- description.Add(string.Format("{0} : {1}", row.Get<PurchaseOrderItem, string>(x => x.Product.Code),
- row.Get<PurchaseOrderItem, string>(x => x.Product.Name)));
- var Description = row.Get<PurchaseOrderItem, string>(x => x.Description);
- if (!string.IsNullOrEmpty(Description))
- description.Add(Description);
- billrow.Set<BillLine, string>(x => x.Description, string.Join("\n", description));
- billrow.Set<BillLine, Guid>(x => x.TaxCode.ID, row.Get<PurchaseOrderItem, Guid>(x => x.TaxCode.ID));
- billrow.Set<BillLine, string>(x => x.TaxCode.Code, row.Get<PurchaseOrderItem, string>(x => x.TaxCode.Code));
- billrow.Set<BillLine, string>(x => x.TaxCode.Description, row.Get<PurchaseOrderItem, string>(x => x.TaxCode.Description));
- billrow.Set<BillLine, double>(x => x.TaxCode.Rate, row.Get<PurchaseOrderItem, double>(x => x.TaxCode.Rate));
- billrow.Set<BillLine, double>(x => x.TaxRate, row.Get<PurchaseOrderItem, double>(x => x.TaxRate));
- billrow.Set<BillLine, double>(x => x.ExTax, row.Get<PurchaseOrderItem, double>(x => x.ExTax));
- billrow.Set<BillLine, double>(x => x.Tax, row.Get<PurchaseOrderItem, double>(x => x.Tax));
- billrow.Set<BillLine, double>(x => x.IncTax, row.Get<PurchaseOrderItem, double>(x => x.IncTax));
- result.Rows.Add(billrow);
- }
- return result;
- }
- private bool ReceiveItems(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 = item.ReceivedDate.IsEmpty() ? now : DateTime.MinValue;
- now = items.Select(x => x.ReceivedDate).FirstOrDefault();
- new Client<PurchaseOrderItem>().Save(items, now.IsEmpty() ? "Cleared Received Date" : $"Updated Received Date to {now:g}");
- }
- return true;
- }
- public static bool AssignLocation(Button btn, CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageBox.Show("Please select at least one row to assign");
- return false;
- }
- var menu = new ContextMenu();
- menu.AddItem("Create New Location", null, () =>
- {
- var grid = new StockLocationGrid();
- var location = new StockLocation();
- if (grid.EditItems(new StockLocation[] { location }))
- AssignLocationToItems(location.ID, rows);
- });
- menu.AddItem("Choose Existing", null, () =>
- {
- var popup = new PopupList(typeof(StockLocation), Guid.Empty, new string[] { });
- if (popup.ShowDialog() == true)
- AssignLocationToItems(popup.ID, rows);
- });
- menu.IsOpen = true;
- return true;
- }
- private static void AssignLocationToItems(Guid locationID, CoreRow[] rows)
- {
- var items = new List<PurchaseOrderItem>();
- foreach (CoreRow row in rows)
- {
- var item = row.ToObject<PurchaseOrderItem>();
- item.StockLocation.ID = locationID;
- items.Add(item);
- }
- Client.Save(items, "Added stock location from PurchaseOrderItem Grid");
- }
- /*private StockLocation[] QueryLocations()
- {
- CoreTable table = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.Active).IsEqualTo(true), new Columns<StockLocation>(x => x.ID));
- List<StockLocation> locations = new List<StockLocation>();
- foreach (CoreRow row in table.Rows)
- locations.Add(row.ToObject<StockLocation>());
- return locations.ToArray();
- }*/
- protected override void SelectItems(CoreRow[]? rows)
- {
- // Check if we can actually edit the selected lines
- var _editable = !ReadOnly && Security.CanEdit<Consignment>();
-
- // Check to ensure NO selected lines are received
- var _allunreceived = rows?.All(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty()) == true;
-
- // Check to ensure ALL select lines are received
- var _allreceived = rows?.All(r => !r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty()) == true;
-
- // Check to ensure NO selected lines are already linked to a PO
- var _anyconsigmments = rows?.Any(r => r.Get<PurchaseOrderItem,Guid>(c=>c.Consignment.ID) != Guid.Empty) == true;
-
- // Check to Ensure NO selected lines are already linked to a Bill
- var _anybills = rows?.Any(r => r.Get<PurchaseOrderItem,Guid>(c=>c.BillLine.ID) != Guid.Empty) == true;
- if (createConsignment != null)
- {
- createConsignment.IsEnabled =
- _anyconsigmments == false
- && _allunreceived
- && _editable;
- }
- receive.Content = _allreceived
- ? "Un-Receive Items"
- : "Receive Items";
-
- receive.IsEnabled =
- _anyconsigmments == false
- && (_allreceived || _allunreceived)
- && _editable;
- bill.IsEnabled =
- _anybills == false
- && _editable;
- assignLocation.IsEnabled =
- rows != null && !ReadOnly && Security.CanEdit<PurchaseOrderItem>();
- base.SelectItems(rows);
- }
-
- private bool SearchProducts(Button button, CoreRow[] rows)
- {
- var dlg = new MultiSelectDialog<ProductInstance>(
- new Filter<ProductInstance>().All(),
- Columns.None<ProductInstance>()
- .Add(x => x.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.Product.Code)
- .Add(x => x.Product.Name)
- .Add(x => x.Product.TaxCode.ID)
- .Add(x => x.Product.TaxCode.Code)
- .Add(x => x.Product.TaxCode.Description)
- .Add(x => x.Product.TaxCode.Rate)
- .Add(x => x.Product.PurchaseGL.ID)
- .Add(x => x.Product.PurchaseGL.Code)
- .Add(x => x.Product.PurchaseGL.Description)
- .Add(x => x.Product.CostCentre.ID)
- .Add(x => x.Product.CostCentre.Code)
- .Add(x => x.Product.CostCentre.Description)
- .Add(x => x.Style.ID)
- .Add(x => x.Style.Code)
- .Add(x => x.Style.Description)
- .AddDimensionsColumns(x => x.Dimensions)
- .Add(x => x.NettCost),
- false
- );
- if (dlg.ShowDialog() == true)
- {
- CreateItems(() =>
- {
- var result = new List<PurchaseOrderItem>();
- var pi = dlg.Data().Rows.FirstOrDefault()?.ToObject<ProductInstance>();
- if (pi == null)
- return result;
-
- var sp = new Client<SupplierProduct>().Query(
- new Filter<SupplierProduct>(x => x.Product.ID).IsEqualTo(pi.Product.ID)
- .And(x=>x.Style.ID).IsEqualTo(pi.Style.ID)
- .And(x=>x.Dimensions).DimensionEquals(pi.Dimensions),
- Columns.None<SupplierProduct>().Add(x => x.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.Product.Code)
- .Add(x => x.Product.Name)
- .Add(x => x.Product.TaxCode.ID)
- .Add(x => x.Product.TaxCode.Code)
- .Add(x => x.Product.TaxCode.Description)
- .Add(x => x.Product.TaxCode.Rate)
- .Add(x => x.Product.PurchaseGL.ID)
- .Add(x => x.Product.PurchaseGL.Code)
- .Add(x => x.Product.PurchaseGL.Description)
- .Add(x => x.Product.CostCentre.ID)
- .Add(x => x.Product.CostCentre.Code)
- .Add(x => x.Product.CostCentre.Description)
- .Add(x => x.Style.ID)
- .Add(x => x.Style.Code)
- .Add(x => x.Style.Description)
- .Add(x=>x.Job.ID)
- .Add(x=>x.Job.JobNumber)
- .Add(x=>x.Job.Name)
- .AddDimensionsColumns(x => x.Dimensions)
- .Add(x => x.CostPrice)
- .Add(x => x.ForeignCurrencyPrice)
- ).Rows.FirstOrDefault()?.ToObject<SupplierProduct>();
- if (sp != null)
- {
- var poi = CreateItem();
- poi.Product.CopyFrom(sp.Product);
- poi.Style.CopyFrom(sp.Style);
- poi.Dimensions.CopyFrom(sp.Dimensions);
- poi.ForeignCurrencyCost = sp.ForeignCurrencyPrice;
- poi.Cost = sp.CostPrice;
- poi.CostCentre.CopyFrom(sp.Product.CostCentre);
- poi.PurchaseGL.CopyFrom(sp.Product.PurchaseGL);
- poi.Description = sp.Product.Name;
- poi.Job.CopyFrom(sp.Job);
- result.Add(poi);
- }
- else if (pi != null)
- {
- var poi = CreateItem();
- poi.Product.CopyFrom(pi.Product);
- poi.Style.CopyFrom(pi.Style);
- poi.Dimensions.CopyFrom(pi.Dimensions);
- poi.Cost = pi.NettCost;
- poi.CostCentre.CopyFrom(pi.Product.CostCentre);
- poi.PurchaseGL.CopyFrom(pi.Product.PurchaseGL);
- poi.Description = pi.Product.Name;
- result.Add(poi);
- }
- return result;
- });
- }
- return false;
- }
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- var dlg = new MultiSelectDialog<SupplierProduct>(
- new Filter<SupplierProduct>(x => x.SupplierLink.ID).IsEqualTo(Item.SupplierLink.ID),
- Columns.None<SupplierProduct>().Add(x => x.ID)
- .Add(x => x.Product.ID)
- .Add(x => x.Product.Code)
- .Add(x => x.Product.Name)
- .Add(x => x.Product.TaxCode.ID)
- .Add(x => x.Product.TaxCode.Code)
- .Add(x => x.Product.TaxCode.Description)
- .Add(x => x.Product.TaxCode.Rate)
- .Add(x => x.Product.PurchaseGL.ID)
- .Add(x => x.Product.PurchaseGL.Code)
- .Add(x => x.Product.PurchaseGL.Description)
- .Add(x => x.Product.CostCentre.ID)
- .Add(x => x.Product.CostCentre.Code)
- .Add(x => x.Product.CostCentre.Description)
- .Add(x => x.Style.ID)
- .Add(x => x.Style.Code)
- .Add(x => x.Style.Description)
- .Add(x=>x.Job.ID)
- .Add(x=>x.Job.JobNumber)
- .Add(x=>x.Job.Name)
- .AddDimensionsColumns(x => x.Dimensions)
- .Add(x => x.CostPrice)
- .Add(x => x.ForeignCurrencyPrice),
- false
- );
- if (dlg.ShowDialog() == true)
- {
- CreateItems(() =>
- {
- var result = new List<PurchaseOrderItem>();
- var sp = dlg.Data().Rows.FirstOrDefault()?.ToObject<SupplierProduct>();
- if (sp != null)
- {
- var poi = CreateItem();
- poi.Product.ID = sp.Product.ID;
- poi.Product.Synchronise(sp.Product);
- poi.Style.ID = sp.Style.ID;
- poi.Style.Synchronise(sp.Style);
- poi.Dimensions.CopyFrom(sp.Dimensions);
- poi.ForeignCurrencyCost = sp.ForeignCurrencyPrice;
- poi.Cost = sp.CostPrice;
- poi.CostCentre.CopyFrom(sp.Product.CostCentre);
- poi.PurchaseGL.CopyFrom(sp.Product.PurchaseGL);
- poi.Job.CopyFrom(sp.Job);
- poi.Description = sp.Product.Name;
- result.Add(poi);
- }
- return result;
- });
- }
- }
-
- protected override void ConfigureColumns(DynamicGridColumns columns)
- {
- base.ConfigureColumns(columns);
- var editor = columns
- .FirstOrDefault(x => String.Equals(x.ColumnName, nameof(BillLine.ForeignCurrencyCost)))?.Editor as CurrencyEditor;
- if (editor != null)
- editor.CurrencySymbol = Item.SupplierLink.Currency.Symbol;
- }
- private readonly Column<PurchaseOrderItem> receivedDateColumn = new Column<PurchaseOrderItem>(x => x.ReceivedDate);
- protected override void CustomiseEditor(PurchaseOrderItem[] items, DynamicGridColumn column, BaseEditor editor)
- {
- base.CustomiseEditor(items, column, editor);
- if(items.Any(x => x.ReceivedDate != DateTime.MinValue) && !receivedDateColumn.IsEqualTo(column.ColumnName) && editor.Editable == Editable.Enabled)
- {
- editor.Editable = editor.Editable.Combine(Editable.Disabled);
- }
- }
-
- protected override void DoReconfigureEditors(DynamicEditorGrid grid, PurchaseOrderItem[] items)
- {
- base.DoReconfigureEditors(grid, items);
- if(!items.Any(x => x.ReceivedDate != DateTime.MinValue))
- {
- if (grid.FindEditor(nameof(PurchaseOrderItem.ForeignCurrencyCost)) is CurrencyEditorControl fcp)
- {
- fcp.SetEnabled(items.All(x => x.PurchaseOrderLink.SupplierLink.Currency.ID != Guid.Empty));
- fcp.CurrencySymbol = items.First().PurchaseOrderLink.SupplierLink.Currency.Symbol;
- }
- var tp = grid.FindEditor(nameof(PurchaseOrderItem.Cost));
- tp?.SetEnabled(items.All(x=>x.PurchaseOrderLink.SupplierLink.Currency.ID == Guid.Empty));
- }
- }
-
- protected override void OnAfterEditorValueChanged(DynamicEditorGrid? grid, PurchaseOrderItem[] items, AfterEditorValueChangedArgs args, Dictionary<string, object?> changes)
- {
- base.OnAfterEditorValueChanged(grid, items, args, changes);
- if (args.ColumnName.Equals("Product.ID") || args.ColumnName.Equals("Job.ID") || args.ColumnName.StartsWith("Dimensions.") || args.ColumnName.Equals("Style.ID"))
- {
- PurchaseOrder.UpdateCosts(
- items,
- Item.SupplierLink.ID,
- changes
- );
- }
- }
- protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, PurchaseOrderItem[] items, string name,
- object value)
- {
- var results = base.EditorValueChanged(editor, items, name, value);
- if (name.Equals("ProductLink.TaxCode.ID"))
- DynamicGridUtils.UpdateEditorValue(items, "TaxCode.ID", (Guid)value, results);
- return results;
- }
- }
|