123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Threading;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for ProductBinList.xaml
- /// </summary>
- public partial class StockLocationPanel : UserControl, IPanel<StockLocation>
- {
- public StockLocationPanel()
- {
- InitializeComponent();
- }
- public bool IsReady { get; set; }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- host.CreatePanelAction(new PanelAction
- { Caption = "Treament PO", Image = PRSDesktop.Resources.purchase, OnExecute = DoCreatePurchaseOrder });
- }
- public string SectionName => "Stock Locations";
- public DataModel DataModel(Selection selection)
- {
- var ids = Locations.ExtractValues(x => x.ID, selection).ToArray();
- return new BaseDataModel<StockLocation>(new Filter<StockLocation>(x => x.ID).InList(ids));
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Refresh()
- {
- Locations.Refresh(false, true);
- Holdings.Refresh(false, true);
- Movements.Refresh(false, true);
- }
- public Dictionary<string, object[]> Selected()
- {
- var result = new Dictionary<string, object[]>();
- result[typeof(StockLocation).EntityName()] = Locations.SelectedRows;
- return result;
- }
- public void Setup()
- {
- Locations.Refresh(true, false);
- Holdings.Refresh(true, false);
- Movements.Refresh(true, false);
- Movements.Location = new StockLocation();
- Locations.OnSelectItem += Locations_OnSelectItem;
- Movements.OnChanged += Movements_OnChanged;
- Holdings.OnChanged += Holdings_OnChanged;
- }
- public void Shutdown()
- {
- }
- #region Treatment PO
- private void DoCreatePurchaseOrder(PanelAction obj)
- {
- if (!Locations.SelectedRows.Any())
- return;
- var id = Locations.SelectedRows.First().Get<StockLocation, Guid>(c => c.ID);
- var window = new StockTreatmentWindow(id);
- if (window.ShowDialog() != true)
- return;
- //Progress.ShowModal("Creating Purchase Order", (progress) =>
- //{
- // PurchaseOrder order = new PurchaseOrder();
- // order.SupplierLink.ID = window.SupplierID;
- // order.SupplierLink.Name = window.SupplierName; //supplier != null ? supplier.Name : "Unknown Supplier";
- // order.Notes = String.Format("Materials Processing Request raised by {0} from Factory Floor", Employees[myID]);
- // order.RaisedBy.ID = myID;
- // order.IssuedBy.ID = myID;
- // order.IssuedDate = DateTime.Today;
- // order.DueDate = DateTime.Today.AddDays(7);
- // new Client<PurchaseOrder>().Save(order, "Created Treatment Purchase Order");
- //});
- //Supplier supplier = new Client<Supplier>().Query(
- // new Filter<Supplier>(x => x.ID).IsEqualTo(window.SupplierID),
- // new Columns<Supplier>(x=>x.ID
- //).Rows.FirstOrDefault()?.ToObject<Supplier>());
- //Progress.SetMessage("Creating Consignment");
- //Consignment consignment = new Consignment();
- //consignment.Number = String.Format("MFG PO {0}", order.PONumber);
- //consignment.Supplier.ID = order.SupplierLink.ID;
- //consignment.EstimatedDispatchDate = order.IssuedDate;
- //consignment.EstimatedWarehouseArrival = order.DueDate;
- //consignment.Description = String.Format("{0} #{1} - {2}", CurrentSection.Name, CurrentStation, Employees.ContainsKey(myID) ? Employees[myID] : "(unknown)");
- //new Client<Consignment>().Save(consignment, "Created by Factory Floor Purchase");
- //Progress.SetMessage("Processing Order");
- //List<PurchaseOrderItem> orderitems = new List<PurchaseOrderItem>();
- //List<ManufacturingPacket> packetupdates = new List<ManufacturingPacket>();
- //MultiQuery query = new MultiQuery();
- //query.Add<SupplierProduct>(
- // new Filter<SupplierProduct>(x => x.ProductLink.ID).IsEqualTo(window.ProductID).And(x => x.SupplierLink.ID).IsEqualTo(window.SupplierID),
- // new Columns<SupplierProduct>(x => x.Job.ID).Add(x => x.CostPrice).Add(x => x.ProductLink.NettCost)
- //);
- //query.Add<Product>(
- // new Filter<Product>(x => x.ID).IsEqualTo(window.ProductID),
- // new Columns<Product>(x => x.NettCost)
- //);
- //query.Query();
- //var supprods = query.Get<SupplierProduct>().Rows.Select(x => x.ToObject<SupplierProduct>()).ToArray();
- //var supprice = supprods.FirstOrDefault(x => Guid.Equals(x.Job.ID, Guid.Empty));
- //var prodprice = query.Get<Product>().Rows.FirstOrDefault()?.ToObject<Product>();
- //double stdcost = supprice != null
- // ? supprice.CostPrice
- // : prodprice != null
- // ? prodprice.NettCost
- // : 0.0F;
- //foreach (var row in window.Selected)
- //{
- // var treatment = row.ToObject<ManufacturingTreatment>();
- // var packet = Packets.Rows.First(p => p.Get<ManufacturingPacket, Guid>(c => c.ID).Equals(treatment.Packet.ID)).ToObject<ManufacturingPacket>();
- // packetupdates.Add(packet);
- // PurchaseOrderItem item = new PurchaseOrderItem();
- // item.PurchaseOrderLink.ID = order.ID;
- // item.Job.ID = treatment.Packet.SetoutLink.JobLink.ID;
- // item.Packet.ID = packet.ID;
- // item.Consignment.ID = consignment.ID;
- // item.ProductLink.ID = window.ProductID;
- // item.Qty = packet.BarcodeQty;
- // item.UnitSize = treatment.Parameter == 0.0F ? 1.0F : treatment.Parameter;
- // var jobprice = supprods.FirstOrDefault(x => x.Job.ID.Equals(item.Job.ID));
- // item.Cost = jobprice != null ? jobprice.CostPrice : stdcost;
- // List<String> description = new List<String>();
- // description.Add(String.Format("{0} x {1} - {2}", packet.BarcodeQty, packet.Serial, packet.Title));
- // List<String> dimensions = new List<string>();
- // if (packet.Height > 0.0F)
- // dimensions.Add(String.Format("H:{0:F2}mm", packet.Height));
- // if (packet.Width > 0.0F)
- // dimensions.Add(String.Format("W:{0:F2}mm", packet.Width));
- // if (packet.Length > 0.0F)
- // dimensions.Add(String.Format("L:{0:F2}mm", packet.Length));
- // dimensions.Add(String.Format("Param:{0:F4}", treatment.Parameter));
- // if (dimensions.Any())
- // description.Add(String.Format("Dimensions: {0}", String.Join(" ", dimensions)));
- // item.Description = String.Join("\n", description);
- // orderitems.Add(item);
- //}
- //new Client<PurchaseOrderItem>().Save(orderitems, "Created by Factory Floor Purchase");
- //Progress.SetMessage("Updating Packets");
- //foreach (var orderitem in orderitems)
- //{
- // var packet = packetupdates.FirstOrDefault(x => x.ID.Equals(orderitem.Packet.ID));
- // if (packet != null)
- // packet.OrderItem.ID = orderitem.ID;
- //}
- //new Client<ManufacturingPacket>().Save(packetupdates.Where(x => x.IsChanged()), "");
- //Progress.SetMessage("Creating Delivery");
- //Delivery delivery = new Delivery();
- //delivery.Date = DateTime.Today;
- //delivery.Due = DateTime.Today;
- //delivery.Employee.ID = myID;
- //StringBuilder sb = new StringBuilder();
- //sb.Append("Serial # ");
- //sb.AppendLine();
- //if (Employees.ContainsKey(myID))
- // sb.AppendLine();
- //delivery.Notes = String.Format("Delivery of Items for processing to {0}\nOrder #{1} (raised on {2:dd MMM yy} by {3})\nItems: {4}",
- // order.SupplierLink.Name,
- // order.PONumber,
- // delivery.Date,
- // myName,
- // String.Join(", ", orderitems.Select(x => String.Format("{0}{1}", x.Description.Split('\n').FirstOrDefault(), x.Qty > 1 ? " (x" + x.Qty.ToString("F0") + ")" : "")))
- //);
- //new Client<Delivery>().Save(delivery, "Created by Factory Floor Purchase");
- //Progress.Close();
- //PrintDeliveryBarcode(delivery, order, consignment);
- //PrintOrderItemBarcodes(delivery, order, consignment);
- //SendPurchaseNotification(order.PONumber);
- //MessageBox.Show("All Done");
- //new DeliveryBuilder(delivery.ID, order.ID).ShowDialog();
- //Refresh();
- }
- //private void SendPurchaseNotification(String PONumber)
- //{
- // List<Notification> updates = new List<Notification>();
- // CoreTable roles = new Client<EmployeeRole>().Query(new Filter<EmployeeRole>(x => x.RoleLink.Code).IsEqualTo("PURCHASES"));
- // foreach (var role in roles.Rows)
- // {
- // Notification notification = new Notification()
- // {
- // Title = String.Format("Treatment PO #{0} has been raised", PONumber),
- // Description = "The above Purchase Order has been created, and is ready to be checked and issued to the relevant supplier."
- // };
- // notification.Sender.ID = myID;
- // notification.Employee.ID = role.Get<EmployeeRole, Guid>(x => x.EmployeeLink.ID);
- // updates.Add(notification);
- // }
- // new Client<Notification>().Save(updates, "Sent Notification");
- //}
- //private void PrintDeliveryBarcode(Delivery delivery, PurchaseOrder order, Consignment consignment)
- //{
- // ManufacturingTreatmentDataModel model = new ManufacturingTreatmentDataModel(order, delivery, consignment);
- // String templatename = "Print Treatment Delivery Bar Code";
- // ReportTemplate template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.Name).IsEqualTo(templatename).And(x => x.Section).IsEqualTo(model.Name)).FirstOrDefault();
- // if (template == null)
- // {
- // template = new ReportTemplate()
- // {
- // Section = model.Name,
- // Name = templatename
- // };
- // new Client<ReportTemplate>().Save(template, "Auto Created Report Template");
- // }
- // ReportUtils.PreviewReport(template, model, !Security.IsAllowed<CanDesignReports>(), Security.IsAllowed<CanDesignReports>());
- //}
- //private void PrintOrderItemBarcodes(Delivery delivery, PurchaseOrder order, Consignment consignment)
- //{
- // ManufacturingTreatmentDataModel model = new ManufacturingTreatmentDataModel(order, delivery, consignment);
- // String templatename = "Print Treatment Item Bar Codes";
- // ReportTemplate template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.Name).IsEqualTo(templatename).And(x => x.Section).IsEqualTo(model.Name)).FirstOrDefault();
- // if (template == null)
- // {
- // template = new ReportTemplate()
- // {
- // Section = model.Name,
- // Name = templatename
- // };
- // new Client<ReportTemplate>().Save(template, "Auto Created Report Template");
- // }
- // ReportUtils.PreviewReport(template, model, !Security.IsAllowed<CanDesignReports>(), Security.IsAllowed<CanDesignReports>());
- //}
- //private CoreTable POItemTable(IEnumerable<PurchaseOrderItem> items)
- //{
- // var result = new CoreTable();
- // result.LoadColumns(typeof(PurchaseOrderItem));
- // result.LoadRows(items);
- // return result;
- //}
- #endregion
- public Dictionary<Type, CoreTable> DataEnvironment()
- {
- var result = new Dictionary<Type, CoreTable>();
- if (Locations.SelectedRows.Any())
- {
- var id = Locations.SelectedRows.FirstOrDefault().Get<StockLocation, Guid>(c => c.ID);
- result[typeof(StockLocation)] = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.ID).IsEqualTo(id));
- result[typeof(StockHolding)] = new Client<StockHolding>().Query(new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(id));
- result[typeof(StockMovement)] = new Client<StockMovement>().Query(new Filter<StockMovement>(x => x.Location.ID).IsEqualTo(id));
- var productids = new List<Guid>();
- foreach (var row in result[typeof(StockHolding)].Rows)
- {
- var productid = row.Get<StockHolding, Guid>(x => x.Product.ID);
- if (!productids.Contains(productid))
- productids.Add(productid);
- }
- result[typeof(Product)] = new Client<Product>().Query(new Filter<Product>(x => x.ID).InList(productids.ToArray()));
- var docids = new List<Guid>();
- foreach (var row in result[typeof(Product)].Rows)
- {
- var docid = row.EntityLinkID<Product, ImageDocumentLink>(x => x.Image) ?? Guid.Empty;
- if (docid != Guid.Empty && !docids.Contains(docid))
- docids.Add(docid);
- }
- result[typeof(Document)] = new Client<Document>().Query(new Filter<Document>(x => x.ID).InList(docids.ToArray()));
- }
- return result;
- }
- private void Holdings_OnChanged(IDynamicGrid sender)
- {
- Movements.Refresh(false, true);
- }
- private void Movements_OnChanged(IDynamicGrid sender)
- {
- Holdings.Refresh(false, true);
- }
- private void Locations_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
- {
- var row = e.Rows?.FirstOrDefault();
- var id = row != null ? row.Get<StockLocation, Guid>(c => c.ID) : CoreUtils.FullGuid;
- var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
- timer.Tick += (o, args) =>
- {
- timer.Stop();
- var location = row?.ToObject<StockLocation>();
- Holdings.Location = location;
- Holdings.Refresh(false, true);
- Movements.Location = location;
- Movements.Refresh(false, true);
- };
- timer.Start();
- }
- }
- }
|