ConsignmentDetailsPopup.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using System;
  4. using System.Collections.Generic;
  5. using Xamarin.Forms;
  6. using Xamarin.Forms.Xaml;
  7. using XF.Material.Forms.UI.Dialogs;
  8. namespace PRS.Mobile
  9. {
  10. [XamlCompilation(XamlCompilationOptions.Compile)]
  11. public partial class ConsignmentDetailsPopup
  12. {
  13. public delegate void ConsignmentPopupAcceptedEvent();
  14. public event ConsignmentPopupAcceptedEvent OnConsignmentPopupAccepted;
  15. public POItemShell _POItemShell { get; set; }
  16. StockLocation defaultLocation = new StockLocation();
  17. List<StockLocationShell> stockLocationShells = new List<StockLocationShell>();
  18. public ConsignmentDetailsPopup(POItemShell poitemshell, List<StockLocationShell> _stockLocationShells = null)
  19. {
  20. InitializeComponent();
  21. if (_stockLocationShells != null)
  22. stockLocationShells = _stockLocationShells;
  23. //this prevents changes to the original poitemshell in case of cancelling or back button pressed
  24. _POItemShell = poitemshell.DuplicateNewShell(poitemshell);
  25. if (_POItemShell.StockLocationID != Guid.Empty) //item has a default location already assigned
  26. {
  27. AddDefaultLocationButton();
  28. }
  29. LoadButtons();
  30. qtyEnt.Text = _POItemShell.ExpectedQty.ToString();
  31. titleLbl.Text = _POItemShell.Description;
  32. }
  33. private void LoadButtons()
  34. {
  35. foreach (StockLocationShell shell in stockLocationShells)
  36. {
  37. Button button = new Button
  38. {
  39. Text = shell.Description,
  40. BackgroundColor = Color.FromHex("#15C7C1"),
  41. TextColor = Color.White,
  42. CornerRadius = 5,
  43. Padding = new Thickness(3),
  44. FontAttributes = FontAttributes.Bold,
  45. };
  46. button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
  47. button.Clicked += (s, e) =>
  48. {
  49. foreach (Button button1 in locationStackLayout.Children)
  50. {
  51. button1.BackgroundColor = Color.FromHex("#15C7C1");
  52. }
  53. (s as Button).BackgroundColor = Color.FromHex("#9f4576");
  54. StockLocationShell shell2 = stockLocationShells.Find(x => x.Description == (s as Button).Text);
  55. _POItemShell.StockLocationID = shell2.ID;
  56. _POItemShell.StockLocationCode = shell2.Code;
  57. _POItemShell.StockLocationDescription = shell2.Description;
  58. };
  59. locationStackLayout.Children.Add(button);
  60. }
  61. if (_POItemShell.StockLocationID == Guid.Empty) //item does not have a default location assigned - defaults to incoming stores
  62. {
  63. try
  64. {
  65. StockLocationShell shell = stockLocationShells.Find(x => x.ID.Equals(Guid.Parse("3ebb1fe3-21f4-4731-b80a-a28f814982ea")));
  66. _POItemShell.StockLocationID = shell.ID;
  67. _POItemShell.StockLocationCode = shell.Code;
  68. _POItemShell.StockLocationDescription = shell.Description;
  69. (locationStackLayout.Children[0] as Button).BackgroundColor = Color.FromHex("#9f4576");
  70. }
  71. catch { }
  72. }
  73. }
  74. private void AddDefaultLocationButton()
  75. {
  76. if (stockLocationShells.Count > 0 && stockLocationShells.Contains(stockLocationShells.Find(x => x.ID == _POItemShell.StockLocationID)))
  77. return;
  78. defaultLocation.ID = _POItemShell.StockLocationID;
  79. defaultLocation.Code = _POItemShell.StockLocationCode;
  80. defaultLocation.Description = _POItemShell.StockLocationDescription;
  81. Button button = new Button
  82. {
  83. Text = _POItemShell.StockLocationDescription,
  84. BackgroundColor = Color.FromHex("#9f4576"),
  85. TextColor = Color.White,
  86. CornerRadius = 5,
  87. Padding = new Thickness(3),
  88. FontAttributes = FontAttributes.Bold,
  89. };
  90. button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
  91. button.Clicked += (sender, e) =>
  92. {
  93. foreach (Button button1 in locationStackLayout.Children)
  94. {
  95. button1.BackgroundColor = Color.FromHex("#15C7C1");
  96. }
  97. (sender as Button).BackgroundColor = Color.FromHex("#9f4576");
  98. _POItemShell.StockLocationID = defaultLocation.ID;
  99. _POItemShell.StockLocationCode = defaultLocation.Code;
  100. _POItemShell.StockLocationDescription = defaultLocation.Description;
  101. };
  102. locationStackLayout.Children.Add(button);
  103. }
  104. private async void AddLocation_Clicked(object sender, EventArgs e)
  105. {
  106. string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, "New Location", "Existing Location");
  107. switch (chosenOption)
  108. {
  109. case "Cancel":
  110. return;
  111. case "New Location":
  112. ChooseNewLocation();
  113. break;
  114. case "Existing Location":
  115. ChooseReceivingLocation();
  116. break;
  117. default:
  118. return;
  119. }
  120. }
  121. private void ChooseNewLocation()
  122. {
  123. StockLocationShell location = new StockLocationShell();
  124. // CoreTable table = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.ID).IsEqualTo(Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd")),
  125. // new Columns<StockLocation>(x => x.ID));
  126. // if (table.Rows.Any())
  127. // {
  128. // location.Warehouse.ID = Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd"); //EXTRUSIONS
  129. // location.Warehouse.Description = "Extrusions";
  130. // location.Warehouse.Code = "EXTRUSIONS";
  131. // }
  132. // location.Active = true;
  133. StockLocationDetailsPage stockLocationDetailsPage = new StockLocationDetailsPage(location);
  134. stockLocationDetailsPage.Saved += (o, e) =>
  135. {
  136. // if (String.IsNullOrWhiteSpace(loc.Code))
  137. // {
  138. // MaterialDialog.Instance.AlertAsync(message: "Code may not be blank!");
  139. // return false;
  140. // }
  141. //
  142. // if (String.IsNullOrWhiteSpace(loc.Description))
  143. // {
  144. // MaterialDialog.Instance.AlertAsync(message: "Description may not be blank!");
  145. // return false;
  146. // }
  147. //
  148. // if (loc.Area.ID == Guid.Empty)
  149. // {
  150. // MaterialDialog.Instance.AlertAsync(message: "Area may not be blank!");
  151. // return false;
  152. // }
  153. //
  154. // if (loc.Warehouse.ID == Guid.Empty)
  155. // {
  156. // MaterialDialog.Instance.AlertAsync(message: "Warehouse may not be blank!");
  157. // return false;
  158. // }
  159. //
  160. // CoreTable others = new Client<StockLocation>().Query(
  161. // new Filter<StockLocation>(x => x.Code).IsEqualTo(loc.Code).And(x => x.ID).IsNotEqualTo(loc.ID),
  162. // new Columns<StockLocation>(x => x.ID)
  163. // );
  164. // if (others.Rows.Any())
  165. // {
  166. // MaterialDialog.Instance.AlertAsync(message: "Location Code already exists!");
  167. // return false;
  168. // }
  169. // try
  170. // {
  171. // new Client<StockLocation>().Save(loc, "Created Location");
  172. // _POItemShell.StockLocationID = loc.ID;
  173. // _POItemShell.StockLocationCode = loc.Code;
  174. // _POItemShell.StockLocationDescription = loc.Description;
  175. //
  176. // StockLocationShell_Old shell = new StockLocationShell_Old
  177. // {
  178. // ID = loc.ID,
  179. // Code = loc.Code,
  180. // Description = loc.Description
  181. // };
  182. // stockLocationShells.Add(shell);
  183. // Button button = new Button
  184. // {
  185. // Text = shell.Description,
  186. // BackgroundColor = Color.FromHex("#9f4576"),
  187. // TextColor = Color.White,
  188. // CornerRadius = 5,
  189. // Padding = new Thickness(3),
  190. // FontAttributes = FontAttributes.Bold,
  191. // };
  192. // button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
  193. // button.Clicked += (s, e) =>
  194. // {
  195. // foreach (Button button1 in locationStackLayout.Children)
  196. // {
  197. // button1.BackgroundColor = Color.FromHex("#15C7C1");
  198. // }
  199. // (s as Button).BackgroundColor = Color.FromHex("#9f4576");
  200. // StockLocationShell_Old shell2 = stockLocationShells.Find(x => x.Description == (s as Button).Text);
  201. // _POItemShell.StockLocationID = shell2.ID;
  202. // _POItemShell.StockLocationCode = shell2.Code;
  203. // _POItemShell.StockLocationDescription = shell2.Description;
  204. // };
  205. // Device.BeginInvokeOnMainThread(() =>
  206. // {
  207. // foreach (Button button1 in locationStackLayout.Children)
  208. // {
  209. // button1.BackgroundColor = Color.FromHex("#15C7C1");
  210. // }
  211. // button.BackgroundColor = Color.FromHex("#9f4576");
  212. // locationStackLayout.Children.Add(button);
  213. // });
  214. // }
  215. // catch (Exception err)
  216. // {
  217. // MaterialDialog.Instance.AlertAsync(message: "Unable to save Location\n" + err.Message);
  218. // return false;
  219. // }
  220. // return true;
  221. };
  222. Navigation.PushAsync(stockLocationDetailsPage);
  223. }
  224. private void ChooseReceivingLocation()
  225. {
  226. StockLocationSelectionPage page = new StockLocationSelectionPage();
  227. page.OnLocationSelected += (s, e) =>
  228. {
  229. // _POItemShell.StockLocationID = s.ID;
  230. // _POItemShell.StockLocationCode = s.Code;
  231. // _POItemShell.StockLocationDescription = s.Description;
  232. // StockLocationShell_Old shell = new StockLocationShell_Old
  233. // {
  234. // ID = s.ID,
  235. // Code = s.Code,
  236. // Description = s.Description
  237. // };
  238. //
  239. // if (!stockLocationShells.Contains(shell))
  240. // stockLocationShells.Add(shell);
  241. //
  242. // Button button = new Button
  243. // {
  244. // Text = shell.Description,
  245. // BackgroundColor = Color.FromHex("#9f4576"),
  246. // TextColor = Color.White,
  247. // CornerRadius = 5,
  248. // Padding = new Thickness(3),
  249. // FontAttributes = FontAttributes.Bold,
  250. // };
  251. // button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
  252. // button.Clicked += (object sender, EventArgs e) =>
  253. // {
  254. // foreach (Button button1 in locationStackLayout.Children)
  255. // {
  256. // button1.BackgroundColor = Color.FromHex("#15C7C1");
  257. // }
  258. // (sender as Button).BackgroundColor = Color.FromHex("#9f4576");
  259. // StockLocationShell_Old shell2 = stockLocationShells.Find(x => x.Description == (sender as Button).Text);
  260. // _POItemShell.StockLocationID = shell2.ID;
  261. // _POItemShell.StockLocationCode = shell2.Code;
  262. // _POItemShell.StockLocationDescription = shell2.Description;
  263. // };
  264. // Device.BeginInvokeOnMainThread(() =>
  265. // {
  266. // foreach (Button button1 in locationStackLayout.Children)
  267. // {
  268. // button1.BackgroundColor = Color.FromHex("#15C7C1");
  269. // }
  270. // button.BackgroundColor = Color.FromHex("#9f4576");
  271. // locationStackLayout.Children.Add(button);
  272. // });
  273. };
  274. Navigation.PushAsync(page);
  275. }
  276. private async void SetDefaultLocationCb_CheckChanged(object sender, CheckedChangedEventArgs e)
  277. {
  278. if (setDefaultCb.IsChecked)
  279. {
  280. if (_POItemShell.StockLocationID != Guid.Empty)
  281. {
  282. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Default Location"))
  283. {
  284. Product product = new Product();
  285. product.ID = _POItemShell.ProductID;
  286. product.DefaultLocation.ID = _POItemShell.StockLocationID;
  287. new Client<Product>().Save(product, "Updated default location from Mobile Receivals Module");
  288. }
  289. }
  290. else
  291. {
  292. DisplayAlert("Alert", "No Location selected", "OK");
  293. setDefaultCb.IsChecked = false;
  294. }
  295. }
  296. else
  297. {
  298. }
  299. }
  300. private void Qty_Changed(object sender, EventArgs e)
  301. {
  302. if (!string.IsNullOrWhiteSpace(qtyEnt.Text))
  303. {
  304. if (double.TryParse(qtyEnt.Text, out double value))
  305. {
  306. _POItemShell.ReceivedQty = value;
  307. }
  308. else
  309. {
  310. qtyEnt.Text = _POItemShell.ExpectedQty.ToString();
  311. _POItemShell.ReceivedQty = _POItemShell.ExpectedQty;
  312. }
  313. }
  314. }
  315. private void AcceptBtn_Clicked(object sender, EventArgs e)
  316. {
  317. if (string.IsNullOrWhiteSpace(qtyEnt.Text) || double.Parse(qtyEnt.Text) == 0)
  318. {
  319. DisplayAlert("Alert", "Quantity may not be blank or 0", "OK");
  320. return;
  321. }
  322. if (_POItemShell.StockLocationID == Guid.Empty)
  323. {
  324. DisplayAlert("Alert", "Location may not be blank", "OK");
  325. return;
  326. }
  327. OnConsignmentPopupAccepted?.Invoke();
  328. Navigation.PopAsync();
  329. }
  330. }
  331. }