123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- 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;
- namespace PRSDesktop;
- public class SupplierPurchaseOrderItemOneToMany : DynamicOneToManyGrid<PurchaseOrder,PurchaseOrderItem>
- {
- private Button bill;
- private Button? createConsignment;
- //private Button? viewconsign;
- private Button receive;
- private Button assignLocation;
- private List<Tuple<PurchaseOrderItem, JobRequisitionItemPurchaseOrderItem>> JobRequisitionItems = 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.Job.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.TaxCode.ID);
- HiddenColumns.Add(x => x.TaxCode.Code);
- HiddenColumns.Add(x => x.TaxCode.Description);
- HiddenColumns.Add(x => x.TaxCode.Rate);
- foreach (var column in new Columns<PurchaseOrderItem>()
- .AddDimensionsColumns(x => x.Dimensions, Dimensions.ColumnsType.Local)
- .GetColumns())
- {
- 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();
- 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(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- if (!ReadOnly && Security.CanEdit<PurchaseOrderItem>())
- {
- options.Add(DynamicGridOption.DirectEdit);
- options.Add(DynamicGridOption.DragTarget);
- }
- //if (!IsDirectEditMode(options))
- //{
- options.Add(DynamicGridOption.FilterRows);
- //}
- }
- protected override void OnAfterRefresh()
- {
- base.OnAfterRefresh();
- JobRequisitionItems.RemoveAll(x => !Items.Contains(x.Item1));
- }
- public override void AfterSave(object item)
- {
- base.AfterSave(item);
- var toSave = new List<JobRequisitionItemPurchaseOrderItem>();
- foreach (var poItem in Items)
- {
- var jriPois = JobRequisitionItems.Where(x => x.Item1 == poItem).Select(x => x.Item2);
- foreach (var jriPoi in jriPois)
- {
- jriPoi.PurchaseOrderItem.ID = poItem.ID;
- }
- toSave.AddRange(jriPois);
- }
- 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 qty = row.Get<PurchaseOrderItem, double>(x => x.Qty);
- var value = qty / 2;
- if(DoubleEdit.Execute("Enter quantity to split on:", 0.0, qty, ref value))
- {
- var poi = LoadItem(row);
- IEnumerable<Guid> jobRequiItemIDs;
- if(poi.ID == Guid.Empty)
- {
- // If not saved yet, we will have any jriPOIs in the transient list.
- jobRequiItemIDs = JobRequisitionItems.Where(x => x.Item1 == poi).Select(x => x.Item2.JobRequisitionItem.ID).ToList();
- }
- else
- {
- // Otherwise, they'll all be in the database.
- jobRequiItemIDs = Client.Query(
- new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.PurchaseOrderItem.ID).IsEqualTo(poi.ID),
- new Columns<JobRequisitionItemPurchaseOrderItem>(x => x.JobRequisitionItem.ID))
- .ExtractValues<JobRequisitionItemPurchaseOrderItem, Guid>(x => x.JobRequisitionItem.ID);
- }
- var newLine = new PurchaseOrderItem
- {
- };
- newLine.BillLine.ID = poi.BillLine.ID;
- newLine.BillLine.Synchronise(poi.BillLine);
- newLine.StockLocation.ID = poi.StockLocation.ID;
- newLine.StockLocation.Synchronise(poi.StockLocation);
- newLine.Consignment.ID = poi.Consignment.ID;
- newLine.Consignment.Synchronise(poi.Consignment);
- newLine.PurchaseGL.ID = poi.PurchaseGL.ID;
- newLine.PurchaseGL.Synchronise(poi.PurchaseGL);
- newLine.CostCentre.ID = poi.CostCentre.ID;
- newLine.CostCentre.Synchronise(poi.CostCentre);
- newLine.Product.ID = poi.Product.ID;
- newLine.Product.Synchronise(poi.Product);
- newLine.Style.ID = poi.Style.ID;
- newLine.Style.Synchronise(poi.Style);
- newLine.TaxCode.ID = poi.TaxCode.ID;
- newLine.TaxCode.Synchronise(poi.TaxCode);
- newLine.PurchaseOrderLink.ID = poi.PurchaseOrderLink.ID;
- newLine.PurchaseOrderLink.Synchronise(poi.PurchaseOrderLink);
- newLine.Job.ID = poi.Job.ID;
- newLine.Job.Synchronise(poi.Job);
- newLine.Dimensions.CopyFrom(poi.Dimensions);
- 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;
- poi.Qty = value;
- newLine.Qty = qty - value;
- foreach(var jriID in jobRequiItemIDs)
- {
- // Add to a list to be saved later.
- var jriPoi = new JobRequisitionItemPurchaseOrderItem();
- jriPoi.JobRequisitionItem.ID = jriID;
- JobRequisitionItems.Add(new(newLine, jriPoi));
- }
- 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(new Columns<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 = now;
- new Client<PurchaseOrderItem>().Save(items, "Consignment Items Received");
- }
- 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)
- {
- if (createConsignment != null)
- {
- createConsignment.IsEnabled =
- rows != null
- && !rows.Any(r => Entity.IsEntityLinkValid<PurchaseOrderItem, ConsignmentLink>(x => x.Consignment, r))
- && !rows.Any(r => !r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty())
- && !ReadOnly && Security.CanEdit<Consignment>();
- }
- receive.IsEnabled =
- rows != null
- && !rows.Any(r => Entity.IsEntityLinkValid<PurchaseOrderItem, ConsignmentLink>(x => x.Consignment, r))
- && !rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty() == false)
- && !ReadOnly && Security.CanEdit<PurchaseOrderItem>();
- bill.IsEnabled =
- rows != null
- && !rows.Any(r => r.IsEntityLinkValid<PurchaseOrderItem, BillLineLink>(x => x.BillLine))
- && !ReadOnly && Security.CanEdit<PurchaseOrderItem>();
- assignLocation.IsEnabled =
- rows != null && !ReadOnly && Security.CanEdit<PurchaseOrderItem>();
- base.SelectItems(rows);
- }
- protected override void CustomiseEditor(PurchaseOrderItem[] items, DynamicGridColumn column, BaseEditor editor)
- {
- base.CustomiseEditor(items, column, editor);
- if(items.Any(x => x.ReceivedDate != DateTime.MinValue) && !new Column<PurchaseOrderItem>(x => x.ReceivedDate).IsEqualTo(column.ColumnName) && editor.Editable == Editable.Enabled)
- {
- editor.Editable = editor.Editable.Combine(Editable.Disabled);
- }
- }
- 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.Equals("Dimensions") || 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;
- }
- }
|