ConsignmentCompletionPage.xaml.cs 14 KB

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