ConsignmentCompletionPage.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Xamarin.Essentials;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. public delegate void ConsignmentSavedEvent();
  16. [XamlCompilation(XamlCompilationOptions.Compile)]
  17. public partial class ConsignmentCompletionPage
  18. {
  19. public event ConsignmentSavedEvent OnConsignmentSaved;
  20. List<POItemShell> listReceived = new List<POItemShell>();
  21. int shortSupplyIssues = 0;
  22. int extraSupplyIssues = 0;
  23. int count = 1;
  24. Guid consignmentID = Guid.Empty;
  25. string consignmentNumber = "";
  26. Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
  27. public ConsignmentCompletionPage(List<POItemShell> _listReceived, Guid _consignmentID, string _consignmentNumber)
  28. {
  29. InitializeComponent();
  30. consignmentID = _consignmentID;
  31. consignmentNumber = _consignmentNumber;
  32. listReceived = _listReceived;
  33. LoadScreen();
  34. }
  35. private void LoadScreen()
  36. {
  37. try
  38. {
  39. titleLbl.Text = "Consignment " + consignmentNumber;
  40. totalReceivedNumberLbl.Text = listReceived.Count.ToString();
  41. string text = new Client<Consignment>().Query(
  42. new Filter<Consignment>(x => x.ID).IsEqualTo(consignmentID),
  43. new Columns<Consignment>(ColumnTypeFlags.None).Add(x => x.Description)
  44. ).Rows.FirstOrDefault().Values[0].ToString();
  45. List<POItemShell> issuesList = new List<POItemShell>();
  46. foreach (POItemShell item in listReceived)
  47. {
  48. if (item.Description.Contains("due to short supply"))
  49. {
  50. shortSupplyIssues++;
  51. issuesList.Add(item);
  52. }
  53. else if (item.Description.Contains("Purchase Order Item Line split due to extra"))
  54. {
  55. extraSupplyIssues++;
  56. issuesList.Add(item);
  57. }
  58. }
  59. if (issuesList.Count > 0)
  60. {
  61. foreach (POItemShell item in issuesList)
  62. {
  63. if (item.Description.Contains("due to short supply"))
  64. {
  65. text = text + System.Environment.NewLine + count + ". " + item.Description;
  66. count++;
  67. }
  68. else if (item.Description.Contains("Purchase Order Item Line split due to extra"))
  69. {
  70. text = text + System.Environment.NewLine + count + ". " + item.Description;
  71. count++;
  72. }
  73. }
  74. }
  75. descriptionLbl.Text = text;
  76. }
  77. catch { }
  78. }
  79. async void TakePhoto_Clicked(object sender, EventArgs e)
  80. {
  81. TakeAPhoto();
  82. }
  83. async void ChooseImage_Clicked(object sender, EventArgs e)
  84. {
  85. ChooseAPhoto();
  86. }
  87. private async void ConfirmBtn_Clicked(object sender, EventArgs e)
  88. {
  89. try
  90. {
  91. if (photosFrame.BorderColor == Color.Red)
  92. {
  93. return;
  94. }
  95. else
  96. {
  97. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  98. {
  99. Consignment consignment = new Consignment()
  100. {
  101. ID = consignmentID,
  102. Number = consignmentNumber
  103. };
  104. consignment.ActualWarehouseArrival = DateTime.Now;
  105. consignment.Description = descriptionLbl.Text;
  106. consignment.Employee.ID = App.Data.Me.ID;
  107. SaveConsignment(consignment);
  108. await DisplayAlert("Success", "Receival saved for " + consignmentNumber, "OK");
  109. OnConsignmentSaved?.Invoke();
  110. }
  111. Navigation.PopAsync();
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. OnConsignmentSaved?.Invoke();
  117. }
  118. }
  119. private void SaveConsignment(Consignment consignment)
  120. {
  121. try
  122. {
  123. new Client<Consignment>().Save(consignment, "Updated from mobile device");
  124. }
  125. catch (Exception ex)
  126. {
  127. InABox.Mobile.MobileLogging.Log(ex);
  128. SaveConsignment(consignment);
  129. }
  130. }
  131. //Note that photos are saved as they are taken, to avoid saving a long list of photos which some devices can't handle
  132. //PurchaseOrderDocuments, StockMovementBatch and StockMovementBatchDocuments are saved in ConsignmentStore
  133. #region Photos
  134. private async void TakeAPhoto()
  135. {
  136. try
  137. {
  138. var file = await MediaPicker.CapturePhotoAsync();
  139. if (file == null)
  140. return;
  141. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  142. {
  143. Image img = null;
  144. var memoryStream = new MemoryStream();
  145. using (var stream = await file.OpenReadAsync())
  146. await stream.CopyToAsync(memoryStream);
  147. var data = memoryStream.ToArray();
  148. Document doc = new Document()
  149. {
  150. FileName = file.FileName,
  151. Data = data,
  152. CRC = CoreUtils.CalculateCRC(data),
  153. TimeStamp = DateTime.Now
  154. };
  155. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  156. img = new Image();
  157. img.HeightRequest = 150;
  158. img.WidthRequest = 150;
  159. img.Aspect = Aspect.AspectFit;
  160. img.Source = src;
  161. img.GestureRecognizers.Add(new TapGestureRecognizer
  162. {
  163. Command = new Command(OnTap),
  164. CommandParameter = src,
  165. NumberOfTapsRequired = 1
  166. });
  167. doc = SavePhoto(doc);
  168. SaveConsignmentDocument(doc.ID);
  169. imagesDocuments.Add(img, doc);
  170. //file.Dispose();
  171. if (img != null)
  172. {
  173. Device.BeginInvokeOnMainThread(() =>
  174. {
  175. ImageScroller.IsVisible = true;
  176. images.Children.Add(img);
  177. UpdateColours();
  178. });
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. DisplayAlert("Error saving - please try again", ex.Message, "OK");
  185. }
  186. }
  187. private Document SavePhoto(Document doc)
  188. {
  189. try
  190. {
  191. new Client<Document>().Save(doc, "Saved on Mobile Receivals Module");
  192. return doc;
  193. }
  194. catch (Exception ex)
  195. {
  196. InABox.Mobile.MobileLogging.Log(ex);
  197. return SavePhoto(doc);
  198. }
  199. }
  200. private async void ChooseAPhoto()
  201. {
  202. try
  203. {
  204. var file = await MediaPicker.CapturePhotoAsync();
  205. if (file == null)
  206. return;
  207. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  208. {
  209. Image img = null;
  210. var memoryStream = new MemoryStream();
  211. using (var stream = await file.OpenReadAsync())
  212. await stream.CopyToAsync(memoryStream);
  213. var data = memoryStream.ToArray();
  214. Document doc = new Document()
  215. {
  216. FileName = Path.GetFileName(file.FileName),
  217. Data = data,
  218. CRC = CoreUtils.CalculateCRC(data),
  219. TimeStamp = DateTime.Now
  220. };
  221. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  222. img = new Image();
  223. img.HeightRequest = 150;
  224. img.WidthRequest = 150;
  225. img.Aspect = Aspect.AspectFit;
  226. img.Source = src;
  227. img.GestureRecognizers.Add(new TapGestureRecognizer
  228. {
  229. Command = new Command(OnTap),
  230. CommandParameter = src,
  231. NumberOfTapsRequired = 1
  232. });
  233. doc = SavePhoto(doc);
  234. SaveConsignmentDocument(doc.ID);
  235. imagesDocuments.Add(img, doc);
  236. //file.Dispose();
  237. if (img != null)
  238. {
  239. Device.BeginInvokeOnMainThread(() =>
  240. {
  241. ImageScroller.IsVisible = true;
  242. images.Children.Add(img);
  243. UpdateColours();
  244. });
  245. }
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. DisplayAlert("Error saving - please try again", ex.Message, "OK");
  251. }
  252. }
  253. private void SaveConsignmentDocument(Guid docid)
  254. {
  255. Task.Run(() =>
  256. {
  257. try
  258. {
  259. ConsignmentDocument consignmentDocument = new ConsignmentDocument();
  260. consignmentDocument.DocumentLink.ID = docid;
  261. consignmentDocument.EntityLink.ID = consignmentID;
  262. new Client<ConsignmentDocument>().Save(consignmentDocument, "Saved on Mobile Receivals Module");
  263. }
  264. catch (Exception ex)
  265. {
  266. InABox.Mobile.MobileLogging.Log(ex);
  267. SaveConsignmentDocument(docid);
  268. }
  269. });
  270. }
  271. private void OnTap(object obj)
  272. {
  273. try
  274. {
  275. ImageViewerPage viewer = new ImageViewerPage(obj as ImageSource, () => DeleteImage(obj));
  276. Navigation.PushAsync(viewer);
  277. }
  278. catch { }
  279. }
  280. private void DeleteImage(object obj)
  281. {
  282. Image img = imagesDocuments.Keys.FirstOrDefault(x => x.Source.Equals(obj as ImageSource));
  283. DeleteDocuments(imagesDocuments[img].ID);
  284. imagesDocuments.Remove(img);
  285. Device.BeginInvokeOnMainThread(() =>
  286. {
  287. images.Children.Clear();
  288. if (imagesDocuments.Count > 0)
  289. {
  290. foreach (Image image in imagesDocuments.Keys)
  291. {
  292. images.Children.Add(image);
  293. }
  294. }
  295. UpdateColours();
  296. });
  297. }
  298. private void DeleteDocuments(Guid docid)
  299. {
  300. Task.Run(() =>
  301. {
  302. try
  303. {
  304. Document doc = new Document { ID = docid };
  305. new Client<Document>().Delete(doc, "User removed unwanted photo on mobile receivals module");
  306. CoreTable table = new Client<ConsignmentDocument>().Query(new Filter<ConsignmentDocument>(x => x.DocumentLink.ID).IsEqualTo(docid),
  307. new Columns<ConsignmentDocument>(ColumnTypeFlags.None).Add(x => x.ID));
  308. if (table.Rows.Any())
  309. {
  310. ConsignmentDocument consignmentDocument = table.Rows.FirstOrDefault().ToObject<ConsignmentDocument>();
  311. new Client<ConsignmentDocument>().Delete(consignmentDocument, "User removed unwanted photo on mobile receivals module");
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. InABox.Mobile.MobileLogging.Log(ex);
  317. DeleteDocuments(docid);
  318. }
  319. });
  320. }
  321. #endregion
  322. private void UpdateColours()
  323. {
  324. if (imagesDocuments.Count > 0)
  325. photosFrame.BorderColor = Color.Gray;
  326. else
  327. photosFrame.BorderColor = Color.Red;
  328. }
  329. }
  330. }