RecTransCompletion.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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;
  11. using System.Threading.Tasks;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. using XF.Material.Forms.UI.Dialogs;
  15. namespace comal.timesheets
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class RecTransCompletion : ContentPage
  19. {
  20. public delegate void RecTransCompleted();
  21. public event RecTransCompleted OnRecTransCompleted;
  22. List<StockHoldingShell> receivingShells = new List<StockHoldingShell>();
  23. List<StockHoldingShell> originalHoldings = new List<StockHoldingShell>();
  24. StockLocation issuingLocation = new StockLocation();
  25. StockLocation receivingLocation = new StockLocation();
  26. List<StockMovement> stockMovements = new List<StockMovement>();
  27. Job receivingJob = new Job();
  28. Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
  29. List<string> favourites = new List<string>
  30. { "Issued to Cutting","Issued to Factory" , "Transferred For Painting", "Returned From Painting",
  31. "Received on PO", "Issued to Site" };
  32. public RecTransCompletion(List<StockHoldingShell> _receivingShells, List<StockHoldingShell> _originalHoldings, StockLocation _issuingLocation, StockLocation _receivingLocation, Job _receivingJob)
  33. {
  34. InitializeComponent();
  35. receivingShells = _receivingShells;
  36. issuingLocation = _issuingLocation;
  37. receivingLocation = _receivingLocation;
  38. originalHoldings = _originalHoldings;
  39. receivingJob = _receivingJob;
  40. AddFavButtons();
  41. if (receivingLocation.ID != Guid.Empty)
  42. DownloadIssuingLocation();
  43. }
  44. private void AddFavButtons()
  45. {
  46. foreach (string s in favourites)
  47. {
  48. Button button = new Button
  49. {
  50. Text = s,
  51. TextColor = Color.White,
  52. BackgroundColor = Color.FromHex("#15C7C1"),
  53. CornerRadius = 10,
  54. Margin = 2,
  55. FontAttributes = FontAttributes.Bold,
  56. VerticalOptions = LayoutOptions.Center,
  57. HorizontalOptions = LayoutOptions.Center,
  58. Padding = new Thickness(6, 3, 6, 3)
  59. };
  60. button.Clicked += FavButton_Clicked;
  61. optionsFlexLayout.Children.Add(button);
  62. }
  63. }
  64. private void FavButton_Clicked(object sender, EventArgs e)
  65. {
  66. Button button = sender as Button;
  67. if (string.IsNullOrWhiteSpace(notesEdt.Text))
  68. {
  69. notesEdt.Text = button.Text;
  70. }
  71. else
  72. {
  73. notesEdt.Text = notesEdt.Text + " " + button.Text;
  74. }
  75. }
  76. private async void DownloadIssuingLocation()
  77. {
  78. await Task.Run(() =>
  79. {
  80. CoreTable table = new Client<StockLocation>().Query
  81. (
  82. new Filter<StockLocation>(x => x.ID).IsEqualTo(issuingLocation.ID),
  83. new Columns<StockLocation>(x => x.ID, x => x.Code, x => x.Description)
  84. );
  85. issuingLocation = table.Rows.First().ToObject<StockLocation>();
  86. });
  87. }
  88. private async void SaveBatch_Clicked(object sender, EventArgs e)
  89. {
  90. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
  91. {
  92. // Create a Stock Movement Batch - save and wait for return
  93. StockMovementBatch batch = new StockMovementBatch()
  94. {
  95. Notes = notesEdt.Text
  96. };
  97. if (receivingLocation.ID != Guid.Empty)
  98. {
  99. batch.Type = StockMovementBatchType.Transfer;
  100. }
  101. else if (receivingJob.ID != Guid.Empty)
  102. {
  103. batch.Type = StockMovementBatchType.Issue;
  104. }
  105. new Client<StockMovementBatch>().Save(batch, "Created on mobile");
  106. // Save photos - async, no wait needed
  107. SavePhotos(batch.ID);
  108. if (receivingLocation.ID != Guid.Empty)
  109. {
  110. CreateStockMovements(batch, true, false); //issuing
  111. CreateStockMovements(batch, false, true); //receiving
  112. }
  113. else if (receivingJob.ID != Guid.Empty)
  114. {
  115. CreateStockMovements(batch, true, false); //issuing only
  116. }
  117. Task.Run(() =>
  118. {
  119. new Client<StockMovement>().Save(stockMovements, "Updated from mobile device");
  120. });
  121. Device.BeginInvokeOnMainThread(async () =>
  122. {
  123. saveBatchBtn.IsVisible = false;
  124. await DisplayAlert("Success", "Batch Saved", "OK");
  125. OnRecTransCompleted?.Invoke();
  126. Navigation.PopAsync();
  127. });
  128. }
  129. }
  130. private void CreateStockMovements(StockMovementBatch batch, bool issuing = false, bool receiving = false)
  131. {
  132. foreach (StockHoldingShell shell in receivingShells)
  133. {
  134. CreateStockMovement(shell, batch, issuing, receiving);
  135. }
  136. }
  137. private void CreateStockMovement(StockHoldingShell shell, StockMovementBatch batch, bool issuing = false, bool receiving = false, bool innerloop = false)
  138. {
  139. StockMovement movement = new StockMovement();
  140. movement.Batch.ID = batch.ID;
  141. movement.Date = DateTime.Now;
  142. if (batch.Type == StockMovementBatchType.Transfer)
  143. movement.IsTransfer = true;
  144. movement.Notes = notesEdt.Text;
  145. movement.Dimensions.Unit.ID = shell.DimensionsUnitID;
  146. movement.Dimensions.Quantity = shell.DimensionsQuantity;
  147. movement.Dimensions.Length = shell.DimensionsLength;
  148. movement.Dimensions.Width = shell.DimensionsWidth;
  149. movement.Dimensions.Height = shell.DimensionsHeight;
  150. movement.Dimensions.Weight = shell.DimensionsWeight;
  151. movement.Dimensions.Unit.HasHeight = shell.DimensionsHasHeight;
  152. movement.Dimensions.Unit.HasLength = shell.DimensionsHasLength;
  153. movement.Dimensions.Unit.HasWidth = shell.DimensionsHasWidth;
  154. movement.Dimensions.Unit.HasWeight = shell.DimensionsHasWeight;
  155. movement.Dimensions.Unit.HasQuantity = shell.DimensionsHasQuantity;
  156. movement.Dimensions.Unit.Formula = shell.DimensionsUnitFormula;
  157. movement.Dimensions.Unit.Format = shell.DimensionsUnitFormat;
  158. movement.Dimensions.Unit.Code = shell.DimensionsUnitCode;
  159. movement.Dimensions.Unit.Description = shell.DimensionsUnitDescription;
  160. movement.Dimensions.UnitSize = shell.DimensionsUnitSize;
  161. movement.Dimensions.Value = shell.DimensionsValue;
  162. movement.Product.ID = shell.ProductID;
  163. movement.Product.Code = shell.Code;
  164. movement.Product.Name = shell.Name;
  165. movement.Employee.ID = GlobalVariables.EmpID;
  166. movement.Employee.Name = GlobalVariables.EmpName;
  167. if (issuing)
  168. {
  169. StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
  170. movement.Style.ID = originalShell.StyleID;
  171. movement.Style.Code = originalShell.StyleCode;
  172. movement.Style.Description = originalShell.Finish;
  173. movement.Job.ID = originalShell.JobID;
  174. movement.Job.JobNumber = originalShell.JobNumber;
  175. movement.Job.Name = originalShell.JobName;
  176. if (innerloop)
  177. {
  178. movement.System = true;
  179. movement.Job.ID = receivingJob.ID;
  180. movement.Job.JobNumber = receivingJob.JobNumber;
  181. movement.Job.Name = receivingJob.Name;
  182. }
  183. movement.Location.ID = issuingLocation.ID;
  184. movement.Location.Code = issuingLocation.Code;
  185. movement.Location.Description = issuingLocation.Description;
  186. movement.Issued = shell.Units;
  187. //innerloop for creating extra system stock movements if needed when issuing
  188. if (receivingJob.ID != Guid.Empty && !innerloop)
  189. {
  190. if (originalShell.JobID != receivingJob.ID)
  191. {
  192. CreateStockMovement(shell, batch, false, true, true);
  193. CreateStockMovement(shell, batch, true, false, true);
  194. }
  195. }
  196. }
  197. else if (receiving)
  198. {
  199. StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
  200. movement.Style.ID = shell.StyleID;
  201. movement.Style.Code = shell.StyleCode;
  202. movement.Style.Description = shell.Finish;
  203. movement.Job.ID = shell.JobID;
  204. movement.Job.JobNumber = shell.JobNumber;
  205. movement.Job.Name = shell.JobName;
  206. movement.Location.ID = receivingLocation.ID;
  207. movement.Location.Code = receivingLocation.Code;
  208. movement.Location.Description = receivingLocation.Description;
  209. movement.Received = shell.Units;
  210. if (receivingJob.ID != Guid.Empty && innerloop)
  211. {
  212. movement.Style.ID = originalShell.StyleID;
  213. movement.Style.Code = originalShell.StyleCode;
  214. movement.Style.Description = originalShell.Finish;
  215. movement.Job.ID = receivingJob.ID;
  216. movement.Job.JobNumber = receivingJob.JobNumber;
  217. movement.Job.Name = receivingJob.Name;
  218. movement.Location.ID = issuingLocation.ID;
  219. movement.Location.Code = issuingLocation.Code;
  220. movement.Location.Description = issuingLocation.Description;
  221. movement.System = true;
  222. }
  223. }
  224. stockMovements.Add(movement);
  225. }
  226. #region Photos
  227. private async void SavePhotos(Guid batchID)
  228. {
  229. await Task.Run(() =>
  230. {
  231. new Client<Document>().Save(imagesDocuments.Values, "");
  232. // Link the photos to the batch
  233. List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
  234. foreach (var doc in imagesDocuments.Values)
  235. {
  236. var smd = new StockMovementBatchDocument();
  237. smd.EntityLink.ID = batchID;
  238. smd.DocumentLink.ID = doc.ID;
  239. smd.DocumentLink.FileName = doc.FileName;
  240. if (smd.EntityLink.ID != Guid.Empty)
  241. stockMovementBatchDocuments.Add(smd);
  242. }
  243. new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "");
  244. });
  245. }
  246. async void TakePhoto_Clicked(object sender, EventArgs e)
  247. {
  248. TakeAPhoto();
  249. }
  250. async void ChooseImage_Clicked(object sender, EventArgs e)
  251. {
  252. ChooseAPhoto();
  253. }
  254. private async void TakeAPhoto()
  255. {
  256. await CrossMedia.Current.Initialize();
  257. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  258. {
  259. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  260. return;
  261. }
  262. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  263. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  264. {
  265. Name = filename,
  266. CompressionQuality = 10,
  267. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  268. });
  269. if (file == null)
  270. return;
  271. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  272. {
  273. Image img = null;
  274. var memoryStream = new MemoryStream();
  275. file.GetStream().CopyTo(memoryStream);
  276. var data = memoryStream.ToArray();
  277. Document doc = new Document()
  278. {
  279. FileName = filename,
  280. Data = data,
  281. CRC = CoreUtils.CalculateCRC(data),
  282. TimeStamp = DateTime.Now
  283. };
  284. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  285. img = new Image();
  286. img.HeightRequest = 150;
  287. img.WidthRequest = 150;
  288. img.Aspect = Aspect.AspectFit;
  289. img.Source = src;
  290. img.GestureRecognizers.Add(new TapGestureRecognizer
  291. {
  292. Command = new Command(OnTap),
  293. CommandParameter = src,
  294. NumberOfTapsRequired = 1
  295. });
  296. imagesDocuments.Add(img, doc);
  297. file.Dispose();
  298. if (img != null)
  299. {
  300. Device.BeginInvokeOnMainThread(() =>
  301. {
  302. ImageScroller.IsVisible = true;
  303. images.Children.Add(img);
  304. UpdateColours();
  305. });
  306. }
  307. }
  308. }
  309. private async void ChooseAPhoto()
  310. {
  311. await CrossMedia.Current.Initialize();
  312. if (!CrossMedia.Current.IsPickPhotoSupported)
  313. {
  314. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  315. return;
  316. }
  317. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  318. {
  319. CompressionQuality = 10,
  320. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  321. });
  322. if (file == null)
  323. return;
  324. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  325. {
  326. Image img = null;
  327. var memoryStream = new MemoryStream();
  328. file.GetStream().CopyTo(memoryStream);
  329. var data = memoryStream.ToArray();
  330. Document doc = new Document()
  331. {
  332. FileName = Path.GetFileName(file.Path),
  333. Data = data,
  334. CRC = CoreUtils.CalculateCRC(data),
  335. TimeStamp = DateTime.Now
  336. };
  337. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  338. img = new Image();
  339. img.HeightRequest = 150;
  340. img.WidthRequest = 150;
  341. img.Aspect = Aspect.AspectFit;
  342. img.Source = src;
  343. img.GestureRecognizers.Add(new TapGestureRecognizer
  344. {
  345. Command = new Command(OnTap),
  346. CommandParameter = src,
  347. NumberOfTapsRequired = 1
  348. });
  349. imagesDocuments.Add(img, doc);
  350. file.Dispose();
  351. if (img != null)
  352. {
  353. Device.BeginInvokeOnMainThread(() =>
  354. {
  355. ImageScroller.IsVisible = true;
  356. images.Children.Add(img);
  357. UpdateColours();
  358. });
  359. }
  360. }
  361. }
  362. private void OnTap(object obj)
  363. {
  364. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  365. Navigation.PushAsync(viewer);
  366. viewer.ChooseDelete();
  367. viewer.OnDeleteSelected += () =>
  368. {
  369. Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
  370. imagesDocuments.Remove(img);
  371. Device.BeginInvokeOnMainThread(() =>
  372. {
  373. images.Children.Clear();
  374. if (imagesDocuments.Count > 0)
  375. {
  376. foreach (Image image in imagesDocuments.Keys)
  377. {
  378. images.Children.Add(image);
  379. }
  380. }
  381. UpdateColours();
  382. });
  383. };
  384. }
  385. #endregion
  386. #region Updating Screen
  387. private void NotesEdt_Changed(object sender, EventArgs e)
  388. {
  389. UpdateColours();
  390. }
  391. private void UpdateColours()
  392. {
  393. if (photosFrame.BorderColor == Color.Red)
  394. {
  395. if (imagesDocuments.Values.Count > 0)
  396. {
  397. photosFrame.BorderColor = Color.Gray;
  398. }
  399. }
  400. if (!string.IsNullOrWhiteSpace(notesEdt.Text))
  401. {
  402. if (notesFrame.BorderColor == Color.Red)
  403. {
  404. notesFrame.BorderColor = Color.Gray;
  405. }
  406. }
  407. else
  408. {
  409. notesFrame.BorderColor = Color.Red;
  410. }
  411. ShowSave();
  412. }
  413. private void ShowSave()
  414. {
  415. if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
  416. {
  417. saveBatchBtn.IsVisible = true;
  418. }
  419. else
  420. {
  421. saveBatchBtn.IsVisible = false;
  422. }
  423. }
  424. #endregion
  425. }
  426. }