ConsignmentDetailsPopup.xaml.cs 15 KB

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