RecTransCompletion.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.Employee.ID = App.Data.Me.ID;
  202. movement.Location.ID = location.ID;
  203. return movement;
  204. }
  205. private StockMovement IssueStockMovement(StockHoldingShell_Old shell, StockMovement movement)
  206. {
  207. var originalShell = GetOriginalShell(shell);
  208. movement.Style.ID = originalShell.StyleID;
  209. movement.Job.ID = originalShell.JobID;
  210. movement.Issued = shell.Units;
  211. return movement;
  212. }
  213. private StockMovement ReceiveStockMovement(StockHoldingShell_Old shell, StockMovement movement)
  214. {
  215. movement.Style.ID = shell.StyleID;
  216. movement.Job.ID = shell.JobID;
  217. movement.Received = shell.Units;
  218. return movement;
  219. }
  220. #region Photos
  221. private async void SavePhotos(Guid batchID)
  222. {
  223. await Task.Run(() =>
  224. {
  225. new Client<Document>().Save(imagesDocuments.Values, "");
  226. // Link the photos to the batch
  227. List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
  228. foreach (var doc in imagesDocuments.Values)
  229. {
  230. var smd = new StockMovementBatchDocument();
  231. smd.EntityLink.ID = batchID;
  232. smd.DocumentLink.ID = doc.ID;
  233. smd.DocumentLink.FileName = doc.FileName;
  234. if (smd.EntityLink.ID != Guid.Empty)
  235. stockMovementBatchDocuments.Add(smd);
  236. }
  237. new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "");
  238. });
  239. }
  240. async void TakePhoto_Clicked(object sender, EventArgs e)
  241. {
  242. TakeAPhoto();
  243. }
  244. async void ChooseImage_Clicked(object sender, EventArgs e)
  245. {
  246. ChooseAPhoto();
  247. }
  248. private async void TakeAPhoto()
  249. {
  250. var file = await MediaPicker.CapturePhotoAsync();
  251. if (file == null)
  252. return;
  253. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  254. {
  255. Image img = null;
  256. var memoryStream = new MemoryStream();
  257. using (var stream = await file.OpenReadAsync())
  258. await stream.CopyToAsync(memoryStream);
  259. var data = memoryStream.ToArray();
  260. Document doc = new Document()
  261. {
  262. FileName = Path.GetFileName(file.FileName),
  263. Data = data,
  264. CRC = CoreUtils.CalculateCRC(data),
  265. TimeStamp = DateTime.Now
  266. };
  267. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  268. img = new Image();
  269. img.HeightRequest = 150;
  270. img.WidthRequest = 150;
  271. img.Aspect = Aspect.AspectFit;
  272. img.Source = src;
  273. img.GestureRecognizers.Add(new TapGestureRecognizer
  274. {
  275. Command = new Command(OnTap),
  276. CommandParameter = src,
  277. NumberOfTapsRequired = 1
  278. });
  279. imagesDocuments.Add(img, doc);
  280. if (img != null)
  281. {
  282. Device.BeginInvokeOnMainThread(() =>
  283. {
  284. ImageScroller.IsVisible = true;
  285. images.Children.Add(img);
  286. UpdateColours();
  287. });
  288. }
  289. }
  290. }
  291. private async void ChooseAPhoto()
  292. {
  293. var file = await MediaPicker.PickPhotoAsync();
  294. if (file == null)
  295. return;
  296. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  297. {
  298. Image img = null;
  299. var memoryStream = new MemoryStream();
  300. using (var stream = await file.OpenReadAsync())
  301. await stream.CopyToAsync(memoryStream);
  302. var data = memoryStream.ToArray();
  303. Document doc = new Document()
  304. {
  305. FileName = Path.GetFileName(file.FileName),
  306. Data = data,
  307. CRC = CoreUtils.CalculateCRC(data),
  308. TimeStamp = DateTime.Now
  309. };
  310. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  311. img = new Image();
  312. img.HeightRequest = 150;
  313. img.WidthRequest = 150;
  314. img.Aspect = Aspect.AspectFit;
  315. img.Source = src;
  316. img.GestureRecognizers.Add(new TapGestureRecognizer
  317. {
  318. Command = new Command(OnTap),
  319. CommandParameter = src,
  320. NumberOfTapsRequired = 1
  321. });
  322. imagesDocuments.Add(img, doc);
  323. if (img != null)
  324. {
  325. Device.BeginInvokeOnMainThread(() =>
  326. {
  327. ImageScroller.IsVisible = true;
  328. images.Children.Add(img);
  329. UpdateColours();
  330. });
  331. }
  332. }
  333. }
  334. private void OnTap(object obj)
  335. {
  336. ImageViewerPage viewer = new ImageViewerPage(obj as ImageSource, () => DeleteImage(obj));
  337. Navigation.PushAsync(viewer);
  338. }
  339. private void DeleteImage(object obj)
  340. {
  341. Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
  342. imagesDocuments.Remove(img);
  343. Device.BeginInvokeOnMainThread(() =>
  344. {
  345. images.Children.Clear();
  346. if (imagesDocuments.Count > 0)
  347. {
  348. foreach (Image image in imagesDocuments.Keys)
  349. {
  350. images.Children.Add(image);
  351. }
  352. }
  353. UpdateColours();
  354. });
  355. }
  356. #endregion
  357. #region Updating Screen
  358. private void NotesEdt_Changed(object sender, EventArgs e)
  359. {
  360. UpdateColours();
  361. }
  362. private void UpdateColours()
  363. {
  364. if (photosFrame.BorderColor == Color.Red)
  365. {
  366. if (imagesDocuments.Values.Count > 0)
  367. {
  368. photosFrame.BorderColor = Color.Gray;
  369. }
  370. }
  371. if (!string.IsNullOrWhiteSpace(notesEdt.Text))
  372. {
  373. if (notesFrame.BorderColor == Color.Red)
  374. {
  375. notesFrame.BorderColor = Color.Gray;
  376. }
  377. }
  378. else
  379. {
  380. notesFrame.BorderColor = Color.Red;
  381. }
  382. ShowSave();
  383. }
  384. private void ShowSave()
  385. {
  386. if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
  387. {
  388. saveBatchBtn.IsVisible = true;
  389. }
  390. else
  391. {
  392. saveBatchBtn.IsVisible = false;
  393. }
  394. }
  395. #endregion
  396. }
  397. }