|
@@ -4,6 +4,7 @@ using InABox.Core;
|
|
|
using InABox.DynamicGrid;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
using System.Windows.Controls;
|
|
@@ -13,84 +14,117 @@ namespace PRSDesktop;
|
|
|
/// <summary>
|
|
|
/// Interaction logic for JobRequisitionPurchasing.xaml
|
|
|
/// </summary>
|
|
|
-public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynamicEditorHost
|
|
|
+public partial class JobRequisitionPurchasing : UserControl
|
|
|
{
|
|
|
public delegate void PurchaseOrderSaved();
|
|
|
- public event PurchaseOrderSaved OnPurchaseOrderSaved;
|
|
|
+ public event PurchaseOrderSaved? OnPurchaseOrderSaved;
|
|
|
|
|
|
- public List<JobRequisitionItem> JobRequiItems;
|
|
|
+ private PurchaseOrder Order { get; set; } = new PurchaseOrder();
|
|
|
|
|
|
- public event DataModelUpdateEvent? OnUpdateDataModel;
|
|
|
+ private List<JobRequiItemLinking> JobRequiItems = new List<JobRequiItemLinking>();
|
|
|
|
|
|
- public PurchaseOrder Order { get; set; } = new PurchaseOrder();
|
|
|
-
|
|
|
- bool bLoaded = false;
|
|
|
- public void LoadFromRequiLine()
|
|
|
+ private class JobRequiItemLinking
|
|
|
{
|
|
|
- if (bLoaded || JobRequiItems.Count == 0)
|
|
|
- return;
|
|
|
+ public JobRequisitionItem JobRequiItem { get; set; }
|
|
|
|
|
|
- var poid = JobRequiItems[0].PurchaseOrderItem.PurchaseOrderLink.ID;
|
|
|
- if (poid != Guid.Empty && poid != Order.ID)
|
|
|
- LoadPO(poid);
|
|
|
- else if (poid == Guid.Empty)
|
|
|
- ClearEditor();
|
|
|
- }
|
|
|
+ public JobRequisitionItemPurchaseOrderItem? JobRequisitionItemPurchaseOrderItem { get; set; }
|
|
|
|
|
|
- private void LoadPO(Guid poid)
|
|
|
- {
|
|
|
- var po = new Client<PurchaseOrder>().Query(
|
|
|
- new Filter<PurchaseOrder>(x => x.ID).IsEqualTo(poid),
|
|
|
- new Columns<PurchaseOrder>
|
|
|
- (
|
|
|
- x => x.ID,
|
|
|
- x => x.SupplierLink.ID,
|
|
|
- x => x.SupplierLink.Code,
|
|
|
- x => x.SupplierLink.Name,
|
|
|
- x => x.Description,
|
|
|
- x => x.Category.ID,
|
|
|
- x => x.Category.Code,
|
|
|
- x => x.Category.Description,
|
|
|
- x => x.RaisedBy.ID,
|
|
|
- x => x.RaisedBy.Code,
|
|
|
- x => x.RaisedBy.Name,
|
|
|
- x => x.DueDate,
|
|
|
- x => x.IssuedBy.ID,
|
|
|
- x => x.IssuedBy.Code,
|
|
|
- x => x.IssuedBy.Name,
|
|
|
- x => x.IssuedDate,
|
|
|
- x => x.ClosedDate,
|
|
|
- x => x.PONumber
|
|
|
- ))
|
|
|
- .Rows.FirstOrDefault()?.ToObject<PurchaseOrder>();
|
|
|
- if (po != null)
|
|
|
+ public PurchaseOrderItem? PurchaseOrderItem { get; set; }
|
|
|
+
|
|
|
+ public JobRequiItemLinking(JobRequisitionItem jobRequiItem, PurchaseOrderItem purchaseOrderItem)
|
|
|
{
|
|
|
- Order = po;
|
|
|
- CreatePOEditor(new BaseObject[] { po });
|
|
|
+ JobRequiItem = jobRequiItem;
|
|
|
+ PurchaseOrderItem = purchaseOrderItem;
|
|
|
+ JobRequisitionItemPurchaseOrderItem = null;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- public IEnumerable<DynamicGridColumn> Columns => throw new NotImplementedException();
|
|
|
-
|
|
|
- public bool IsReady { get; set; }
|
|
|
+ public JobRequiItemLinking(JobRequisitionItem jobRequiItem, JobRequisitionItemPurchaseOrderItem jobRequisitionItemPurchaseOrderItem)
|
|
|
+ {
|
|
|
+ JobRequiItem = jobRequiItem;
|
|
|
+ PurchaseOrderItem = null;
|
|
|
+ JobRequisitionItemPurchaseOrderItem = jobRequisitionItemPurchaseOrderItem;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- public string SectionName { get; set; }
|
|
|
+ bool bLoaded = false;
|
|
|
+ public bool EditorChanged { get; private set; }
|
|
|
|
|
|
public JobRequisitionPurchasing()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
- empName = new Client<Employee>().Query(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid)).Rows.FirstOrDefault().Get<Employee, string>(x => x.Name);
|
|
|
CreatePOEditor();
|
|
|
- //JobRequiItems = new List<JobRequisitionItem>();
|
|
|
}
|
|
|
|
|
|
- private string CheckDate(DateTime date)
|
|
|
+ private void ReloadJobRequiItems(Guid orderID)
|
|
|
{
|
|
|
- return date.IsEmpty() ? "" : date.ToShortDateString();
|
|
|
+ JobRequiItems.Clear();
|
|
|
+
|
|
|
+ if(orderID != Guid.Empty)
|
|
|
+ {
|
|
|
+ var poItems = Client.Query(
|
|
|
+ new Filter<JobRequisitionItemPurchaseOrderItem>(x => x.PurchaseOrderItem.PurchaseOrderLink.ID)
|
|
|
+ .IsEqualTo(orderID),
|
|
|
+ new Columns<JobRequisitionItemPurchaseOrderItem>(x => x.ID).Add(x => x.JobRequisitionItem.ID))
|
|
|
+ .ToObjects<JobRequisitionItemPurchaseOrderItem>();
|
|
|
+ foreach (var jriPoi in poItems)
|
|
|
+ {
|
|
|
+ var jri = new JobRequisitionItem
|
|
|
+ {
|
|
|
+ ID = jriPoi.JobRequisitionItem.ID
|
|
|
+ };
|
|
|
+ JobRequiItems.Add(new JobRequiItemLinking(jri, jriPoi));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public bool EditorChanged {get; private set; }
|
|
|
- private string empName = "";
|
|
|
+ public void LoadFromRequiLine(Guid orderID)
|
|
|
+ {
|
|
|
+ if (bLoaded)
|
|
|
+ return;
|
|
|
+
|
|
|
+ ReloadJobRequiItems(orderID);
|
|
|
+
|
|
|
+ if (orderID != Guid.Empty)
|
|
|
+ {
|
|
|
+ if (orderID != Order.ID)
|
|
|
+ {
|
|
|
+ LoadPO(orderID);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ ClearEditor();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void LoadPO(Guid orderID)
|
|
|
+ {
|
|
|
+ var po = Client.Query(
|
|
|
+ new Filter<PurchaseOrder>(x => x.ID).IsEqualTo(orderID),
|
|
|
+ new Columns<PurchaseOrder>(
|
|
|
+ x => x.ID,
|
|
|
+ x => x.SupplierLink.ID,
|
|
|
+ x => x.SupplierLink.Code,
|
|
|
+ x => x.SupplierLink.Name,
|
|
|
+ x => x.Description,
|
|
|
+ x => x.Category.ID,
|
|
|
+ x => x.Category.Code,
|
|
|
+ x => x.Category.Description,
|
|
|
+ x => x.RaisedBy.ID,
|
|
|
+ x => x.RaisedBy.Code,
|
|
|
+ x => x.RaisedBy.Name,
|
|
|
+ x => x.DueDate,
|
|
|
+ x => x.IssuedBy.ID,
|
|
|
+ x => x.IssuedBy.Code,
|
|
|
+ x => x.IssuedBy.Name,
|
|
|
+ x => x.IssuedDate,
|
|
|
+ x => x.ClosedDate,
|
|
|
+ x => x.PONumber))
|
|
|
+ .ToObjects<PurchaseOrder>().FirstOrDefault();
|
|
|
+ if (po is not null)
|
|
|
+ {
|
|
|
+ Order = po;
|
|
|
+ CreatePOEditor(new BaseObject[] { po });
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void CreatePOEditor(BaseObject[]? items = null)
|
|
|
{
|
|
@@ -118,7 +152,9 @@ public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynami
|
|
|
var cancel = new System.ComponentModel.CancelEventArgs();
|
|
|
Editor.SaveItem(cancel);
|
|
|
Editor.HideButtons = true;
|
|
|
- UpdateJobRequiItems();
|
|
|
+
|
|
|
+ SaveJRIPOIs();
|
|
|
+
|
|
|
OnPurchaseOrderSaved?.Invoke();
|
|
|
EditorChanged = false;
|
|
|
bLoaded = false;
|
|
@@ -127,7 +163,14 @@ public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynami
|
|
|
{
|
|
|
Editor.HideButtons = true;
|
|
|
bLoaded = false;
|
|
|
- ClearEditor();
|
|
|
+
|
|
|
+ var orderID = Order.ID;
|
|
|
+
|
|
|
+ Order = new PurchaseOrder();
|
|
|
+ JobRequiItems.Clear();
|
|
|
+
|
|
|
+ LoadFromRequiLine(orderID);
|
|
|
+
|
|
|
EditorChanged = false;
|
|
|
};
|
|
|
Editor.OnChanged += (sender, args) =>
|
|
@@ -139,7 +182,7 @@ public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynami
|
|
|
DetailBorder.Child = Editor;
|
|
|
|
|
|
var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), typeof(PurchaseOrder));
|
|
|
- grid.InitialiseEditorForm(Editor, items ?? new object[] { Activator.CreateInstance(typeof(PurchaseOrder))! });
|
|
|
+ grid.InitialiseEditorForm(Editor, items ?? new object[] { new PurchaseOrder() });
|
|
|
|
|
|
var loadPO = new Button
|
|
|
{
|
|
@@ -154,7 +197,7 @@ public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynami
|
|
|
|
|
|
private void LoadPO_Click(object sender, System.Windows.RoutedEventArgs e)
|
|
|
{
|
|
|
- var popup = new PopupList(typeof(PurchaseOrder), Guid.Empty, new string[0]);
|
|
|
+ var popup = new PopupList(typeof(PurchaseOrder), Guid.Empty, Array.Empty<string>());
|
|
|
if(popup.ShowDialog() == true)
|
|
|
{
|
|
|
bLoaded = true;
|
|
@@ -162,176 +205,77 @@ public partial class JobRequisitionPurchasing : UserControl, IBasePanel, IDynami
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void UpdateJobRequiItems()
|
|
|
- {
|
|
|
- var page = Editor.Pages.Find(x => x.GetType() == typeof(SupplierPurchaseOrderItemOneToMany)) as SupplierPurchaseOrderItemOneToMany;
|
|
|
- if (page.Items.Count != 0)
|
|
|
- MatchRequiItems(page.Items);
|
|
|
- }
|
|
|
-
|
|
|
- private void MatchRequiItems(List<PurchaseOrderItem> poItems)
|
|
|
+ private void SaveJRIPOIs()
|
|
|
{
|
|
|
- List<JobRequisitionItem> toSave = new List<JobRequisitionItem>();
|
|
|
- foreach (var JobReqItem in JobRequiItems)
|
|
|
+ var toSave = new List<JobRequisitionItemPurchaseOrderItem>();
|
|
|
+ foreach(var item in JobRequiItems)
|
|
|
{
|
|
|
- if (JobReqItem.PurchaseOrderItem.ID == Guid.Empty)
|
|
|
- foreach (var item in poItems)
|
|
|
- if (MatchReqItemToPOItem(JobReqItem, item))
|
|
|
- toSave.Add(UpdateJobReqItemWithPODetails(JobReqItem, item));
|
|
|
+ if(item.PurchaseOrderItem is not null)
|
|
|
+ {
|
|
|
+ var jriPoi = new JobRequisitionItemPurchaseOrderItem();
|
|
|
+ jriPoi.JobRequisitionItem.ID = item.JobRequiItem.ID;
|
|
|
+ jriPoi.PurchaseOrderItem.ID = item.PurchaseOrderItem.ID;
|
|
|
+ toSave.Add(jriPoi);
|
|
|
+ item.JobRequisitionItemPurchaseOrderItem = jriPoi;
|
|
|
+ }
|
|
|
}
|
|
|
- if (toSave.Count > 0)
|
|
|
- new Client<JobRequisitionItem>().Save(toSave, "Updated from Job Requi Review Create Purchase Order");
|
|
|
- }
|
|
|
|
|
|
- private bool MatchReqItemToPOItem(JobRequisitionItem JobReqItem, PurchaseOrderItem item)
|
|
|
- {
|
|
|
- if (JobReqItem.Product.ID == item.Product.ID &&
|
|
|
- JobReqItem.Dimensions.UnitSize == item.Dimensions.UnitSize &&
|
|
|
- JobReqItem.Requisition.Job.ID == item.Job.ID)
|
|
|
- return true;
|
|
|
- else
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private JobRequisitionItem UpdateJobReqItemWithPODetails(JobRequisitionItem JobReqItem, PurchaseOrderItem poItem)
|
|
|
- {
|
|
|
- JobReqItem.PurchaseOrderItem.ID = poItem.ID;
|
|
|
- JobReqItem.PurchaseOrderItem.DueDate = poItem.DueDate;
|
|
|
- if (JobReqItem.Status != JobRequisitionItemStatus.OnOrder)
|
|
|
- {
|
|
|
- JobReqItem.Notes = JobReqItem.Notes + Environment.NewLine + "Line marked as On Order by " + empName + " on " + DateTime.Now.ToString("dd MMM yy");
|
|
|
- JobReqItem.Ordered = poItem.Created;
|
|
|
- }
|
|
|
- return JobReqItem;
|
|
|
+ Client.Save(
|
|
|
+ toSave,
|
|
|
+ "Updated from Job Requi Review Create Purchase Order");
|
|
|
}
|
|
|
|
|
|
private void ClearEditor()
|
|
|
{
|
|
|
Order = new PurchaseOrder();
|
|
|
CreatePOEditor();
|
|
|
-
|
|
|
- //if (Popup is not null)
|
|
|
- //{
|
|
|
- // Popup.Value = Guid.Empty;
|
|
|
- //}
|
|
|
}
|
|
|
|
|
|
- public void DropItems(CoreRow[] rows)
|
|
|
+ public void DropItems(IEnumerable<CoreRow> rows)
|
|
|
{
|
|
|
var page = Editor.Pages.OfType<SupplierPurchaseOrderItemOneToMany>().First();
|
|
|
- if (page.Items.Count == 0)
|
|
|
- page.LoadItems(CreatePOItemsFromRequiItems(rows).ToArray());
|
|
|
- else
|
|
|
- page.LoadItems(CombineItems(rows, page.Items));
|
|
|
- }
|
|
|
|
|
|
- private PurchaseOrderItem[] CombineItems(CoreRow[] selected, List<PurchaseOrderItem> existingItems)
|
|
|
- {
|
|
|
- List<PurchaseOrderItem> items = new List<PurchaseOrderItem>();
|
|
|
- items.AddRange(existingItems);
|
|
|
- items.AddRange(CreatePOItemsFromRequiItems(selected, existingItems));
|
|
|
- return items.ToArray();
|
|
|
+ if(CreatePOItemsFromRequiItems(rows.ToObjects<JobRequisitionItem>(), page.Items))
|
|
|
+ {
|
|
|
+ page.Refresh(false, true);
|
|
|
+ page.DoChanged();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private List<PurchaseOrderItem> CreatePOItemsFromRequiItems(CoreRow[] selected, List<PurchaseOrderItem>? comparison = null)
|
|
|
+ private bool CreatePOItemsFromRequiItems(IEnumerable<JobRequisitionItem> selected, List<PurchaseOrderItem> items)
|
|
|
{
|
|
|
- List<PurchaseOrderItem> items = new List<PurchaseOrderItem>();
|
|
|
- foreach (CoreRow row in selected)
|
|
|
+ var changed = false;
|
|
|
+ foreach (var jobRequisitionItem in selected)
|
|
|
{
|
|
|
- JobRequisitionItem JobReqItem = row.ToObject<JobRequisitionItem>();
|
|
|
- JobRequiItems.Add(JobReqItem);
|
|
|
- if (JobReqItem.PurchaseOrderItem.ID != Guid.Empty)
|
|
|
+ // We only want not checked or order required.
|
|
|
+ if(jobRequisitionItem.Status != JobRequisitionItemStatus.NotChecked &&
|
|
|
+ jobRequisitionItem.Status != JobRequisitionItemStatus.OrderRequired)
|
|
|
+ {
|
|
|
continue;
|
|
|
-
|
|
|
- PurchaseOrderItem POItem = new PurchaseOrderItem();
|
|
|
- POItem.Product.ID = JobReqItem.Product.ID;
|
|
|
- POItem.Product.Synchronise(JobReqItem.Product);
|
|
|
- POItem.Description = JobReqItem.Product.Name;
|
|
|
- POItem.Qty = JobReqItem.Qty;
|
|
|
- POItem.Dimensions.CopyFrom(JobReqItem.Dimensions);
|
|
|
- POItem.Style.ID = JobReqItem.Style.ID;
|
|
|
- POItem.Style.Code = JobReqItem.Style.Code;
|
|
|
- POItem.Style.Description = JobReqItem.Style.Description;
|
|
|
- POItem.Job.ID = JobReqItem.Requisition.Job.ID;
|
|
|
- POItem.Job.JobNumber = JobReqItem.Requisition.Job.JobNumber;
|
|
|
- POItem.Job.Name = JobReqItem.Requisition.Job.Name;
|
|
|
- POItem.Dimensions.UnitSize = JobReqItem.Dimensions.UnitSize;
|
|
|
- if (comparison != null && !Duplicated(POItem, comparison))
|
|
|
- items.Add(POItem);
|
|
|
- else if (comparison == null)
|
|
|
- items.Add(POItem);
|
|
|
+ }
|
|
|
+ // Filter out ones we've already got.
|
|
|
+ if(JobRequiItems.Any(x => x.JobRequiItem.ID == jobRequisitionItem.ID))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ var poItem = new PurchaseOrderItem
|
|
|
+ {
|
|
|
+ Description = jobRequisitionItem.Product.Name,
|
|
|
+ Qty = jobRequisitionItem.Qty
|
|
|
+ };
|
|
|
+ poItem.Product.ID = jobRequisitionItem.Product.ID;
|
|
|
+ poItem.Product.Synchronise(jobRequisitionItem.Product);
|
|
|
+ poItem.Dimensions.CopyFrom(jobRequisitionItem.Dimensions);
|
|
|
+ poItem.Style.ID = jobRequisitionItem.Style.ID;
|
|
|
+ poItem.Style.Synchronise(jobRequisitionItem.Style);
|
|
|
+ poItem.Job.ID = jobRequisitionItem.Requisition.Job.ID;
|
|
|
+ poItem.Job.Synchronise(jobRequisitionItem.Requisition.Job);
|
|
|
+ items.Add(poItem);
|
|
|
+
|
|
|
+ JobRequiItems.Add(new JobRequiItemLinking(jobRequisitionItem, poItem));
|
|
|
+ changed = true;
|
|
|
}
|
|
|
- return items;
|
|
|
- }
|
|
|
-
|
|
|
- private bool Duplicated(PurchaseOrderItem newItem, List<PurchaseOrderItem> existing)
|
|
|
- {
|
|
|
- if (existing.Find(x =>
|
|
|
- x.Product.ID == newItem.Product.ID
|
|
|
- && x.Qty == newItem.Qty
|
|
|
- && x.Style.ID == newItem.Style.ID
|
|
|
- && x.Job.ID == newItem.Job.ID
|
|
|
- && x.Dimensions.UnitSize == newItem.Dimensions.UnitSize
|
|
|
- )
|
|
|
- != null)
|
|
|
- return true;
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public void LoadColumns(string column, Dictionary<string, string> columns)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(type);
|
|
|
-
|
|
|
- public void LoadLookups(ILookupEditorControl sender)
|
|
|
- {
|
|
|
- var editor = sender.EditorDefinition as ILookupEditor;
|
|
|
- var colname = sender.ColumnName;
|
|
|
-
|
|
|
- var values = editor.Values(colname, Editor.Items);
|
|
|
- sender.LoadLookups(values);
|
|
|
- }
|
|
|
-
|
|
|
- object?[] IDynamicEditorHost.GetItems() => Editor.Items;
|
|
|
-
|
|
|
- public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
|
|
|
-
|
|
|
- public void CreateToolbarButtons(IPanelHost host)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public Dictionary<string, object[]> Selected()
|
|
|
- {
|
|
|
- return new Dictionary<string, object[]>();
|
|
|
- }
|
|
|
-
|
|
|
- public void Heartbeat(TimeSpan time)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void Setup()
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void Shutdown(CancelEventArgs? cancel)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public void Refresh()
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- public DataModel DataModel(Selection selection)
|
|
|
- {
|
|
|
- return new EmptyDataModel();
|
|
|
+ return changed;
|
|
|
}
|
|
|
}
|