RecTransCompletion.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. movement.IsTransfer = true;
  143. movement.Notes = notesEdt.Text;
  144. movement.Dimensions.Unit.ID = shell.DimensionsUnitID;
  145. movement.Dimensions.Quantity = shell.DimensionsQuantity;
  146. movement.Dimensions.Length = shell.DimensionsLength;
  147. movement.Dimensions.Width = shell.DimensionsWidth;
  148. movement.Dimensions.Height = shell.DimensionsHeight;
  149. movement.Dimensions.Weight = shell.DimensionsWeight;
  150. movement.Dimensions.Value = shell.DimensionsValue;
  151. movement.Dimensions.UnitSize = shell.DimensionsUnitSize;
  152. movement.Dimensions.Unit.HasHeight = shell.DimensionsHasHeight;
  153. movement.Dimensions.Unit.HasLength = shell.DimensionsHasLength;
  154. movement.Dimensions.Unit.HasWidth = shell.DimensionsHasWidth;
  155. movement.Dimensions.Unit.HasWeight = shell.DimensionsHasWeight;
  156. movement.Dimensions.Unit.HasQuantity = shell.DimensionsHasQuantity;
  157. movement.Dimensions.Unit.Formula = shell.DimensionsUnitFormula;
  158. movement.Dimensions.Unit.Format = shell.DimensionsUnitFormat;
  159. movement.Dimensions.Unit.Code = shell.DimensionsUnitCode;
  160. movement.Dimensions.Unit.Description = shell.DimensionsUnitDescription;
  161. movement.Product.ID = shell.ProductID;
  162. movement.Product.Code = shell.Code;
  163. movement.Product.Name = shell.Name;
  164. movement.Employee.ID = GlobalVariables.EmpID;
  165. movement.Employee.Name = GlobalVariables.EmpName;
  166. if (issuing)
  167. {
  168. StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
  169. movement.Style.ID = originalShell.StyleID;
  170. movement.Style.Code = originalShell.StyleCode;
  171. movement.Style.Description = originalShell.Finish;
  172. movement.Job.ID = originalShell.JobID;
  173. movement.Job.JobNumber = originalShell.JobNumber;
  174. movement.Job.Name = originalShell.JobName;
  175. if (innerloop)
  176. {
  177. movement.System = true;
  178. movement.Job.ID = receivingJob.ID;
  179. movement.Job.JobNumber = receivingJob.JobNumber;
  180. movement.Job.Name = receivingJob.Name;
  181. }
  182. movement.Location.ID = issuingLocation.ID;
  183. movement.Location.Code = issuingLocation.Code;
  184. movement.Location.Description = issuingLocation.Description;
  185. movement.Issued = shell.Units;
  186. //innerloop for creating extra system stock movements if needed when issuing
  187. if (receivingJob.ID != Guid.Empty && !innerloop)
  188. {
  189. if (originalShell.JobID != receivingJob.ID)
  190. {
  191. CreateStockMovement(shell, batch, false, true, true);
  192. CreateStockMovement(shell, batch, true, false, true);
  193. }
  194. }
  195. }
  196. else if (receiving)
  197. {
  198. StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
  199. movement.Style.ID = shell.StyleID;
  200. movement.Style.Code = shell.StyleCode;
  201. movement.Style.Description = shell.Finish;
  202. movement.Job.ID = shell.JobID;
  203. movement.Job.JobNumber = shell.JobNumber;
  204. movement.Job.Name = shell.JobName;
  205. movement.Location.ID = receivingLocation.ID;
  206. movement.Location.Code = receivingLocation.Code;
  207. movement.Location.Description = receivingLocation.Description;
  208. movement.Received = shell.Units;
  209. if (receivingJob.ID != Guid.Empty && innerloop)
  210. {
  211. movement.Style.ID = originalShell.StyleID;
  212. movement.Style.Code = originalShell.StyleCode;
  213. movement.Style.Description = originalShell.Finish;
  214. movement.Job.ID = receivingJob.ID;
  215. movement.Job.JobNumber = receivingJob.JobNumber;
  216. movement.Job.Name = receivingJob.Name;
  217. movement.Location.ID = issuingLocation.ID;
  218. movement.Location.Code = issuingLocation.Code;
  219. movement.Location.Description = issuingLocation.Description;
  220. movement.System = true;
  221. }
  222. }
  223. stockMovements.Add(movement);
  224. }
  225. #region Photos
  226. private async void SavePhotos(Guid batchID)
  227. {
  228. await Task.Run(() =>
  229. {
  230. new Client<Document>().Save(imagesDocuments.Values, "");
  231. // Link the photos to the batch
  232. List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
  233. foreach (var doc in imagesDocuments.Values)
  234. {
  235. var smd = new StockMovementBatchDocument();
  236. smd.EntityLink.ID = batchID;
  237. smd.DocumentLink.ID = doc.ID;
  238. smd.DocumentLink.FileName = doc.FileName;
  239. if (smd.EntityLink.ID != Guid.Empty)
  240. stockMovementBatchDocuments.Add(smd);
  241. }
  242. new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "");
  243. });
  244. }
  245. async void TakePhoto_Clicked(object sender, EventArgs e)
  246. {
  247. TakeAPhoto();
  248. }
  249. async void ChooseImage_Clicked(object sender, EventArgs e)
  250. {
  251. ChooseAPhoto();
  252. }
  253. private async void TakeAPhoto()
  254. {
  255. await CrossMedia.Current.Initialize();
  256. if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  257. {
  258. await DisplayAlert("No Camera", ":( No camera available.", "OK");
  259. return;
  260. }
  261. String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  262. var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  263. {
  264. Name = filename,
  265. CompressionQuality = 10,
  266. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  267. });
  268. if (file == null)
  269. return;
  270. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  271. {
  272. Image img = null;
  273. var memoryStream = new MemoryStream();
  274. file.GetStream().CopyTo(memoryStream);
  275. var data = memoryStream.ToArray();
  276. Document doc = new Document()
  277. {
  278. FileName = filename,
  279. Data = data,
  280. CRC = CoreUtils.CalculateCRC(data),
  281. TimeStamp = DateTime.Now
  282. };
  283. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  284. img = new Image();
  285. img.HeightRequest = 150;
  286. img.WidthRequest = 150;
  287. img.Aspect = Aspect.AspectFit;
  288. img.Source = src;
  289. img.GestureRecognizers.Add(new TapGestureRecognizer
  290. {
  291. Command = new Command(OnTap),
  292. CommandParameter = src,
  293. NumberOfTapsRequired = 1
  294. });
  295. imagesDocuments.Add(img, doc);
  296. file.Dispose();
  297. if (img != null)
  298. {
  299. Device.BeginInvokeOnMainThread(() =>
  300. {
  301. ImageScroller.IsVisible = true;
  302. images.Children.Add(img);
  303. UpdateColours();
  304. });
  305. }
  306. }
  307. }
  308. private async void ChooseAPhoto()
  309. {
  310. await CrossMedia.Current.Initialize();
  311. if (!CrossMedia.Current.IsPickPhotoSupported)
  312. {
  313. await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  314. return;
  315. }
  316. var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  317. {
  318. CompressionQuality = 10,
  319. PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  320. });
  321. if (file == null)
  322. return;
  323. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  324. {
  325. Image img = null;
  326. var memoryStream = new MemoryStream();
  327. file.GetStream().CopyTo(memoryStream);
  328. var data = memoryStream.ToArray();
  329. Document doc = new Document()
  330. {
  331. FileName = Path.GetFileName(file.Path),
  332. Data = data,
  333. CRC = CoreUtils.CalculateCRC(data),
  334. TimeStamp = DateTime.Now
  335. };
  336. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  337. img = new Image();
  338. img.HeightRequest = 150;
  339. img.WidthRequest = 150;
  340. img.Aspect = Aspect.AspectFit;
  341. img.Source = src;
  342. img.GestureRecognizers.Add(new TapGestureRecognizer
  343. {
  344. Command = new Command(OnTap),
  345. CommandParameter = src,
  346. NumberOfTapsRequired = 1
  347. });
  348. imagesDocuments.Add(img, doc);
  349. file.Dispose();
  350. if (img != null)
  351. {
  352. Device.BeginInvokeOnMainThread(() =>
  353. {
  354. ImageScroller.IsVisible = true;
  355. images.Children.Add(img);
  356. UpdateColours();
  357. });
  358. }
  359. }
  360. }
  361. private void OnTap(object obj)
  362. {
  363. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  364. Navigation.PushAsync(viewer);
  365. viewer.ChooseDelete();
  366. viewer.OnDeleteSelected += () =>
  367. {
  368. Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
  369. imagesDocuments.Remove(img);
  370. Device.BeginInvokeOnMainThread(() =>
  371. {
  372. images.Children.Clear();
  373. if (imagesDocuments.Count > 0)
  374. {
  375. foreach (Image image in imagesDocuments.Keys)
  376. {
  377. images.Children.Add(image);
  378. }
  379. }
  380. UpdateColours();
  381. });
  382. };
  383. }
  384. #endregion
  385. #region Updating Screen
  386. private void NotesEdt_Changed(object sender, EventArgs e)
  387. {
  388. UpdateColours();
  389. }
  390. private void UpdateColours()
  391. {
  392. if (photosFrame.BorderColor == Color.Red)
  393. {
  394. if (imagesDocuments.Values.Count > 0)
  395. {
  396. photosFrame.BorderColor = Color.Gray;
  397. }
  398. }
  399. if (!string.IsNullOrWhiteSpace(notesEdt.Text))
  400. {
  401. if (notesFrame.BorderColor == Color.Red)
  402. {
  403. notesFrame.BorderColor = Color.Gray;
  404. }
  405. }
  406. else
  407. {
  408. notesFrame.BorderColor = Color.Red;
  409. }
  410. ShowSave();
  411. }
  412. private void ShowSave()
  413. {
  414. if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
  415. {
  416. saveBatchBtn.IsVisible = true;
  417. }
  418. else
  419. {
  420. saveBatchBtn.IsVisible = false;
  421. }
  422. }
  423. #endregion
  424. }
  425. }