StockLocationPanel.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using System.Windows.Threading;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. namespace PRSDesktop
  11. {
  12. /// <summary>
  13. /// Interaction logic for ProductBinList.xaml
  14. /// </summary>
  15. public partial class StockLocationPanel : UserControl, IPanel<StockLocation>
  16. {
  17. public StockLocationPanel()
  18. {
  19. InitializeComponent();
  20. }
  21. public bool IsReady { get; set; }
  22. public event DataModelUpdateEvent OnUpdateDataModel;
  23. public void CreateToolbarButtons(IPanelHost host)
  24. {
  25. host.CreatePanelAction(new PanelAction
  26. { Caption = "Treament PO", Image = PRSDesktop.Resources.purchase, OnExecute = DoCreatePurchaseOrder });
  27. }
  28. public string SectionName => "Stock Locations";
  29. public DataModel DataModel(Selection selection)
  30. {
  31. var ids = Locations.ExtractValues(x => x.ID, selection).ToArray();
  32. return new BaseDataModel<StockLocation>(new Filter<StockLocation>(x => x.ID).InList(ids));
  33. }
  34. public void Heartbeat(TimeSpan time)
  35. {
  36. }
  37. public void Refresh()
  38. {
  39. Locations.Refresh(false, true);
  40. Holdings.Refresh(false, true);
  41. Movements.Refresh(false, true);
  42. }
  43. public Dictionary<string, object[]> Selected()
  44. {
  45. var result = new Dictionary<string, object[]>();
  46. result[typeof(StockLocation).EntityName()] = Locations.SelectedRows;
  47. return result;
  48. }
  49. public void Setup()
  50. {
  51. Locations.Refresh(true, false);
  52. Holdings.Refresh(true, false);
  53. Movements.Refresh(true, false);
  54. Movements.Location = new StockLocation();
  55. Locations.OnSelectItem += Locations_OnSelectItem;
  56. Movements.OnChanged += Movements_OnChanged;
  57. Holdings.OnChanged += Holdings_OnChanged;
  58. }
  59. public void Shutdown()
  60. {
  61. }
  62. #region Treatment PO
  63. private void DoCreatePurchaseOrder(PanelAction obj)
  64. {
  65. if (!Locations.SelectedRows.Any())
  66. return;
  67. var id = Locations.SelectedRows.First().Get<StockLocation, Guid>(c => c.ID);
  68. var window = new StockTreatmentWindow(id);
  69. if (window.ShowDialog() != true)
  70. return;
  71. //Progress.ShowModal("Creating Purchase Order", (progress) =>
  72. //{
  73. // PurchaseOrder order = new PurchaseOrder();
  74. // order.SupplierLink.ID = window.SupplierID;
  75. // order.SupplierLink.Name = window.SupplierName; //supplier != null ? supplier.Name : "Unknown Supplier";
  76. // order.Notes = String.Format("Materials Processing Request raised by {0} from Factory Floor", Employees[myID]);
  77. // order.RaisedBy.ID = myID;
  78. // order.IssuedBy.ID = myID;
  79. // order.IssuedDate = DateTime.Today;
  80. // order.DueDate = DateTime.Today.AddDays(7);
  81. // new Client<PurchaseOrder>().Save(order, "Created Treatment Purchase Order");
  82. //});
  83. //Supplier supplier = new Client<Supplier>().Query(
  84. // new Filter<Supplier>(x => x.ID).IsEqualTo(window.SupplierID),
  85. // new Columns<Supplier>(x=>x.ID
  86. //).Rows.FirstOrDefault()?.ToObject<Supplier>());
  87. //Progress.SetMessage("Creating Consignment");
  88. //Consignment consignment = new Consignment();
  89. //consignment.Number = String.Format("MFG PO {0}", order.PONumber);
  90. //consignment.Supplier.ID = order.SupplierLink.ID;
  91. //consignment.EstimatedDispatchDate = order.IssuedDate;
  92. //consignment.EstimatedWarehouseArrival = order.DueDate;
  93. //consignment.Description = String.Format("{0} #{1} - {2}", CurrentSection.Name, CurrentStation, Employees.ContainsKey(myID) ? Employees[myID] : "(unknown)");
  94. //new Client<Consignment>().Save(consignment, "Created by Factory Floor Purchase");
  95. //Progress.SetMessage("Processing Order");
  96. //List<PurchaseOrderItem> orderitems = new List<PurchaseOrderItem>();
  97. //List<ManufacturingPacket> packetupdates = new List<ManufacturingPacket>();
  98. //MultiQuery query = new MultiQuery();
  99. //query.Add<SupplierProduct>(
  100. // new Filter<SupplierProduct>(x => x.ProductLink.ID).IsEqualTo(window.ProductID).And(x => x.SupplierLink.ID).IsEqualTo(window.SupplierID),
  101. // new Columns<SupplierProduct>(x => x.Job.ID).Add(x => x.CostPrice).Add(x => x.ProductLink.NettCost)
  102. //);
  103. //query.Add<Product>(
  104. // new Filter<Product>(x => x.ID).IsEqualTo(window.ProductID),
  105. // new Columns<Product>(x => x.NettCost)
  106. //);
  107. //query.Query();
  108. //var supprods = query.Get<SupplierProduct>().Rows.Select(x => x.ToObject<SupplierProduct>()).ToArray();
  109. //var supprice = supprods.FirstOrDefault(x => Guid.Equals(x.Job.ID, Guid.Empty));
  110. //var prodprice = query.Get<Product>().Rows.FirstOrDefault()?.ToObject<Product>();
  111. //double stdcost = supprice != null
  112. // ? supprice.CostPrice
  113. // : prodprice != null
  114. // ? prodprice.NettCost
  115. // : 0.0F;
  116. //foreach (var row in window.Selected)
  117. //{
  118. // var treatment = row.ToObject<ManufacturingTreatment>();
  119. // var packet = Packets.Rows.First(p => p.Get<ManufacturingPacket, Guid>(c => c.ID).Equals(treatment.Packet.ID)).ToObject<ManufacturingPacket>();
  120. // packetupdates.Add(packet);
  121. // PurchaseOrderItem item = new PurchaseOrderItem();
  122. // item.PurchaseOrderLink.ID = order.ID;
  123. // item.Job.ID = treatment.Packet.SetoutLink.JobLink.ID;
  124. // item.Packet.ID = packet.ID;
  125. // item.Consignment.ID = consignment.ID;
  126. // item.ProductLink.ID = window.ProductID;
  127. // item.Qty = packet.BarcodeQty;
  128. // item.UnitSize = treatment.Parameter == 0.0F ? 1.0F : treatment.Parameter;
  129. // var jobprice = supprods.FirstOrDefault(x => x.Job.ID.Equals(item.Job.ID));
  130. // item.Cost = jobprice != null ? jobprice.CostPrice : stdcost;
  131. // List<String> description = new List<String>();
  132. // description.Add(String.Format("{0} x {1} - {2}", packet.BarcodeQty, packet.Serial, packet.Title));
  133. // List<String> dimensions = new List<string>();
  134. // if (packet.Height > 0.0F)
  135. // dimensions.Add(String.Format("H:{0:F2}mm", packet.Height));
  136. // if (packet.Width > 0.0F)
  137. // dimensions.Add(String.Format("W:{0:F2}mm", packet.Width));
  138. // if (packet.Length > 0.0F)
  139. // dimensions.Add(String.Format("L:{0:F2}mm", packet.Length));
  140. // dimensions.Add(String.Format("Param:{0:F4}", treatment.Parameter));
  141. // if (dimensions.Any())
  142. // description.Add(String.Format("Dimensions: {0}", String.Join(" ", dimensions)));
  143. // item.Description = String.Join("\n", description);
  144. // orderitems.Add(item);
  145. //}
  146. //new Client<PurchaseOrderItem>().Save(orderitems, "Created by Factory Floor Purchase");
  147. //Progress.SetMessage("Updating Packets");
  148. //foreach (var orderitem in orderitems)
  149. //{
  150. // var packet = packetupdates.FirstOrDefault(x => x.ID.Equals(orderitem.Packet.ID));
  151. // if (packet != null)
  152. // packet.OrderItem.ID = orderitem.ID;
  153. //}
  154. //new Client<ManufacturingPacket>().Save(packetupdates.Where(x => x.IsChanged()), "");
  155. //Progress.SetMessage("Creating Delivery");
  156. //Delivery delivery = new Delivery();
  157. //delivery.Date = DateTime.Today;
  158. //delivery.Due = DateTime.Today;
  159. //delivery.Employee.ID = myID;
  160. //StringBuilder sb = new StringBuilder();
  161. //sb.Append("Serial # ");
  162. //sb.AppendLine();
  163. //if (Employees.ContainsKey(myID))
  164. // sb.AppendLine();
  165. //delivery.Notes = String.Format("Delivery of Items for processing to {0}\nOrder #{1} (raised on {2:dd MMM yy} by {3})\nItems: {4}",
  166. // order.SupplierLink.Name,
  167. // order.PONumber,
  168. // delivery.Date,
  169. // myName,
  170. // String.Join(", ", orderitems.Select(x => String.Format("{0}{1}", x.Description.Split('\n').FirstOrDefault(), x.Qty > 1 ? " (x" + x.Qty.ToString("F0") + ")" : "")))
  171. //);
  172. //new Client<Delivery>().Save(delivery, "Created by Factory Floor Purchase");
  173. //Progress.Close();
  174. //PrintDeliveryBarcode(delivery, order, consignment);
  175. //PrintOrderItemBarcodes(delivery, order, consignment);
  176. //SendPurchaseNotification(order.PONumber);
  177. //MessageBox.Show("All Done");
  178. //new DeliveryBuilder(delivery.ID, order.ID).ShowDialog();
  179. //Refresh();
  180. }
  181. //private void SendPurchaseNotification(String PONumber)
  182. //{
  183. // List<Notification> updates = new List<Notification>();
  184. // CoreTable roles = new Client<EmployeeRole>().Query(new Filter<EmployeeRole>(x => x.RoleLink.Code).IsEqualTo("PURCHASES"));
  185. // foreach (var role in roles.Rows)
  186. // {
  187. // Notification notification = new Notification()
  188. // {
  189. // Title = String.Format("Treatment PO #{0} has been raised", PONumber),
  190. // Description = "The above Purchase Order has been created, and is ready to be checked and issued to the relevant supplier."
  191. // };
  192. // notification.Sender.ID = myID;
  193. // notification.Employee.ID = role.Get<EmployeeRole, Guid>(x => x.EmployeeLink.ID);
  194. // updates.Add(notification);
  195. // }
  196. // new Client<Notification>().Save(updates, "Sent Notification");
  197. //}
  198. //private void PrintDeliveryBarcode(Delivery delivery, PurchaseOrder order, Consignment consignment)
  199. //{
  200. // ManufacturingTreatmentDataModel model = new ManufacturingTreatmentDataModel(order, delivery, consignment);
  201. // String templatename = "Print Treatment Delivery Bar Code";
  202. // ReportTemplate template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.Name).IsEqualTo(templatename).And(x => x.Section).IsEqualTo(model.Name)).FirstOrDefault();
  203. // if (template == null)
  204. // {
  205. // template = new ReportTemplate()
  206. // {
  207. // Section = model.Name,
  208. // Name = templatename
  209. // };
  210. // new Client<ReportTemplate>().Save(template, "Auto Created Report Template");
  211. // }
  212. // ReportUtils.PreviewReport(template, model, !Security.IsAllowed<CanDesignReports>(), Security.IsAllowed<CanDesignReports>());
  213. //}
  214. //private void PrintOrderItemBarcodes(Delivery delivery, PurchaseOrder order, Consignment consignment)
  215. //{
  216. // ManufacturingTreatmentDataModel model = new ManufacturingTreatmentDataModel(order, delivery, consignment);
  217. // String templatename = "Print Treatment Item Bar Codes";
  218. // ReportTemplate template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.Name).IsEqualTo(templatename).And(x => x.Section).IsEqualTo(model.Name)).FirstOrDefault();
  219. // if (template == null)
  220. // {
  221. // template = new ReportTemplate()
  222. // {
  223. // Section = model.Name,
  224. // Name = templatename
  225. // };
  226. // new Client<ReportTemplate>().Save(template, "Auto Created Report Template");
  227. // }
  228. // ReportUtils.PreviewReport(template, model, !Security.IsAllowed<CanDesignReports>(), Security.IsAllowed<CanDesignReports>());
  229. //}
  230. //private CoreTable POItemTable(IEnumerable<PurchaseOrderItem> items)
  231. //{
  232. // var result = new CoreTable();
  233. // result.LoadColumns(typeof(PurchaseOrderItem));
  234. // result.LoadRows(items);
  235. // return result;
  236. //}
  237. #endregion
  238. public Dictionary<Type, CoreTable> DataEnvironment()
  239. {
  240. var result = new Dictionary<Type, CoreTable>();
  241. if (Locations.SelectedRows.Any())
  242. {
  243. var id = Locations.SelectedRows.FirstOrDefault().Get<StockLocation, Guid>(c => c.ID);
  244. result[typeof(StockLocation)] = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.ID).IsEqualTo(id));
  245. result[typeof(StockHolding)] = new Client<StockHolding>().Query(new Filter<StockHolding>(x => x.Location.ID).IsEqualTo(id));
  246. result[typeof(StockMovement)] = new Client<StockMovement>().Query(new Filter<StockMovement>(x => x.Location.ID).IsEqualTo(id));
  247. var productids = new List<Guid>();
  248. foreach (var row in result[typeof(StockHolding)].Rows)
  249. {
  250. var productid = row.Get<StockHolding, Guid>(x => x.Product.ID);
  251. if (!productids.Contains(productid))
  252. productids.Add(productid);
  253. }
  254. result[typeof(Product)] = new Client<Product>().Query(new Filter<Product>(x => x.ID).InList(productids.ToArray()));
  255. var docids = new List<Guid>();
  256. foreach (var row in result[typeof(Product)].Rows)
  257. {
  258. var docid = row.EntityLinkID<Product, ImageDocumentLink>(x => x.Image) ?? Guid.Empty;
  259. if (docid != Guid.Empty && !docids.Contains(docid))
  260. docids.Add(docid);
  261. }
  262. result[typeof(Document)] = new Client<Document>().Query(new Filter<Document>(x => x.ID).InList(docids.ToArray()));
  263. }
  264. return result;
  265. }
  266. private void Holdings_OnChanged(IDynamicGrid sender)
  267. {
  268. Movements.Refresh(false, true);
  269. }
  270. private void Movements_OnChanged(IDynamicGrid sender)
  271. {
  272. Holdings.Refresh(false, true);
  273. }
  274. private void Locations_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  275. {
  276. var row = e.Rows?.FirstOrDefault();
  277. var id = row != null ? row.Get<StockLocation, Guid>(c => c.ID) : CoreUtils.FullGuid;
  278. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  279. timer.Tick += (o, args) =>
  280. {
  281. timer.Stop();
  282. var location = row?.ToObject<StockLocation>();
  283. Holdings.Location = location;
  284. Holdings.Refresh(false, true);
  285. Movements.Location = location;
  286. Movements.Refresh(false, true);
  287. };
  288. timer.Start();
  289. }
  290. }
  291. }