RecTransCompletion.xaml.cs 17 KB

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