123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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 sun.misc;
- namespace PRSDesktop
- {
- public class SupplierPurchaseOrders : DynamicDataGrid<PurchaseOrder>
- {
- private readonly Button close;
- public SupplierPurchaseOrders()
- {
- Options.AddRange(
- DynamicGridOption.RecordCount,
- DynamicGridOption.FilterRows,
- DynamicGridOption.SelectColumns
- );
- OnEditorValueChanged += SupplierPurchaseOrders_OnEditorValueChanged;
-
- HiddenColumns.Add(x => x.ClosedDate);
- HiddenColumns.Add(x => x.Balance);
- close = AddButton("Close Order", null, CloseOrder);
- close.IsEnabled = false;
- }
- private Dictionary<string, object> SupplierPurchaseOrders_OnEditorValueChanged(object sender, string name, object value)
- {
- Dictionary<string, object> result = new Dictionary<string, object>();
- var form = sender as DynamicEditorForm;
- var itemspage = form?.Pages.FirstOrDefault(x => x is DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>) as DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>;
- if ((itemspage == null) || (itemspage.Items.Count == 0))
- return result;
-
- if (name.Equals("DueDate") && (MessageBox.Show("Update Due Date on existing items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
- {
- foreach (var item in itemspage.Items)
- item.DueDate = Convert.ToDateTime(value);
- itemspage.Refresh(false, true);
- }
- else if (name.Equals("SupplierLink.ID") &&
- (MessageBox.Show("Update Supplier Pricing to existihg items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
- {
- PurchaseOrder.UpdateCosts(itemspage.Items, (Guid)value, null);
- // var productids = itemspage.Items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
- // MultiQuery query = new MultiQuery();
- // query.Add(
- // new Filter<SupplierProduct>(x=>x.SupplierLink.ID).IsEqualTo(value).And(x=>x.ProductLink.ID).InList(productids),
- // new Columns<SupplierProduct>(x=>x.ProductLink.ID).Add(x=>x.Job.ID).Add(x=>x.CostPrice)
- // );
- // query.Add(
- // new Filter<Product>(x=>x.ID).InList(productids),
- // new Columns<Product>(x=>x.ID).Add(x=>x.NettCost)
- // );
- // query.Query();
- //
- // foreach (var item in itemspage.Items)
- // {
- // CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
- // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
- // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)
- // );
- // if (row == null)
- // row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
- // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
- // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)
- // );
- // if (row != null)
- // item.Cost = row.Get<SupplierProduct, double>(c => c.CostPrice);
- // else
- // {
- // row = query.Get<Product>()?.Rows.FirstOrDefault(r =>
- // (r.Get<Product, Guid>(c => c.ID) == item.Product.ID)
- // );
- // if (row != null)
- // item.Cost = row.Get<Product, double>(c => c.NettCost);
- // }
- //
- // }
-
- itemspage.Refresh(false, true);
- }
- return result;
-
- }
- // private List<SupplierProduct> GetSupplierProducts(object value)
- // {
- // List<SupplierProduct> supplierProducts = new List<SupplierProduct>();
- // CoreTable supplierProductstable = new Client<SupplierProduct>().Query(new Filter<SupplierProduct>(x => x.SupplierLink.ID).IsEqualTo(Guid.Parse(value.ToString())),
- // new Columns<SupplierProduct>(
- // x => x.ID,
- // x => x.SupplierLink.ID,
- // x => x.ProductLink.ID,
- // x => x.Job.ID,
- // x => x.TradePrice));
- // if (supplierProductstable.Rows.Any())
- // {
- // foreach (CoreRow row in supplierProductstable.Rows)
- // {
- // SupplierProduct supplierProduct = row.ToObject<SupplierProduct>();
- // supplierProducts.Add(supplierProduct);
- // }
- // }
- // return supplierProducts;
- // }
- public bool ShowAll { get; set; }
- public Guid SelectedCategory { get; set; }
- protected override void Reload(Filters<PurchaseOrder> criteria, Columns<PurchaseOrder> columns, ref SortOrder<PurchaseOrder> sort,
- Action<CoreTable, Exception> action)
- {
- if (!ShowAll)
- criteria.Add(new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue));
- if (SelectedCategory != CoreUtils.FullGuid)
- criteria.Add(new Filter<PurchaseOrder>(x => x.Category.ID).IsEqualTo(SelectedCategory));
-
- base.Reload(criteria, columns, ref sort, action);
- }
- protected override void SelectItems(CoreRow[] rows)
- {
- close.IsEnabled = rows != null && rows.Any(r =>
- r.Get<PurchaseOrder, DateTime>(c => c.ClosedDate).IsEmpty() && r.Get<PurchaseOrder, double>(x => x.Balance).Equals(0.0F));
- base.SelectItems(rows);
- }
- private bool CloseOrder(Button arg1, CoreRow[] arg2)
- {
- var orders = new List<PurchaseOrder>();
- foreach (var row in arg2)
- {
- var order = row.ToObject<PurchaseOrder>();
- order.ClosedDate = DateTime.Now;
- orders.Add(order);
- }
- new Client<PurchaseOrder>().Save(orders, "Marking Order as Closed");
- return true;
- }
- }
- }
|