| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using Plugin.Media;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class RecTransCompletion : ContentPage
- {
- public delegate void RecTransCompleted();
- public event RecTransCompleted OnRecTransCompleted;
- List<StockHoldingShell> receivingShells = new List<StockHoldingShell>();
- List<StockHoldingShell> originalHoldings = new List<StockHoldingShell>();
- StockLocation issuingLocation = new StockLocation();
- StockLocation receivingLocation = new StockLocation();
- List<StockMovement> stockMovements = new List<StockMovement>();
- Job receivingJob = new Job();
- Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
- List<string> favourites = new List<string>
- { "Issued to Cutting","Issued to Factory" , "Transferred For Painting", "Returned From Painting",
- "Received on PO", "Issued to Site" };
- public RecTransCompletion(List<StockHoldingShell> _receivingShells, List<StockHoldingShell> _originalHoldings, StockLocation _issuingLocation, StockLocation _receivingLocation, Job _receivingJob)
- {
- InitializeComponent();
- receivingShells = _receivingShells;
- issuingLocation = _issuingLocation;
- receivingLocation = _receivingLocation;
- originalHoldings = _originalHoldings;
- receivingJob = _receivingJob;
- AddFavButtons();
- if (receivingLocation.ID != Guid.Empty)
- DownloadIssuingLocation();
- }
- private void AddFavButtons()
- {
- foreach (string s in favourites)
- {
- Button button = new Button
- {
- Text = s,
- TextColor = Color.White,
- BackgroundColor = Color.FromHex("#15C7C1"),
- CornerRadius = 10,
- Margin = 2,
- FontAttributes = FontAttributes.Bold,
- VerticalOptions = LayoutOptions.Center,
- HorizontalOptions = LayoutOptions.Center,
- Padding = new Thickness(6, 3, 6, 3)
- };
- button.Clicked += FavButton_Clicked;
- optionsFlexLayout.Children.Add(button);
- }
- }
- private void FavButton_Clicked(object sender, EventArgs e)
- {
- Button button = sender as Button;
- if (string.IsNullOrWhiteSpace(notesEdt.Text))
- {
- notesEdt.Text = button.Text;
- }
- else
- {
- notesEdt.Text = notesEdt.Text + " " + button.Text;
- }
- }
- private async void DownloadIssuingLocation()
- {
- await Task.Run(() =>
- {
- CoreTable table = new Client<StockLocation>().Query
- (
- new Filter<StockLocation>(x => x.ID).IsEqualTo(issuingLocation.ID),
- new Columns<StockLocation>(x => x.ID, x => x.Code, x => x.Description)
- );
- issuingLocation = table.Rows.First().ToObject<StockLocation>();
- });
- }
- private async void SaveBatch_Clicked(object sender, EventArgs e)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- // Create a Stock Movement Batch - save and wait for return
- StockMovementBatch batch = new StockMovementBatch()
- {
- Notes = notesEdt.Text
- };
- if (receivingLocation.ID != Guid.Empty)
- {
- batch.Type = StockMovementBatchType.Transfer;
- }
- else if (receivingJob.ID != Guid.Empty)
- {
- batch.Type = StockMovementBatchType.Issue;
- }
- new Client<StockMovementBatch>().Save(batch, "Created on mobile");
- // Save photos - async, no wait needed
- SavePhotos(batch.ID);
- if (receivingLocation.ID != Guid.Empty)
- {
- CreateStockMovements(batch, true, false); //issuing
- CreateStockMovements(batch, false, true); //receiving
- }
- else if (receivingJob.ID != Guid.Empty)
- {
- CreateStockMovements(batch, true, false); //issuing only
- }
- Task.Run(() =>
- {
- new Client<StockMovement>().Save(stockMovements, "Updated from mobile device");
- });
- Device.BeginInvokeOnMainThread(async () =>
- {
- saveBatchBtn.IsVisible = false;
- await DisplayAlert("Success", "Batch Saved", "OK");
- OnRecTransCompleted?.Invoke();
- Navigation.PopAsync();
- });
- }
- }
- private void CreateStockMovements(StockMovementBatch batch, bool issuing = false, bool receiving = false)
- {
- foreach (StockHoldingShell shell in receivingShells)
- {
- CreateStockMovement(shell, batch, issuing, receiving);
- }
- }
- private void CreateStockMovement(StockHoldingShell shell, StockMovementBatch batch, bool issuing = false, bool receiving = false, bool innerloop = false)
- {
- StockMovement movement = new StockMovement();
- movement.Batch.ID = batch.ID;
- movement.Date = DateTime.Now;
- if (batch.Type == StockMovementBatchType.Transfer)
- movement.IsTransfer = true;
- movement.Notes = notesEdt.Text;
- movement.Dimensions.Unit.ID = shell.DimensionsUnitID;
- movement.Dimensions.Quantity = shell.DimensionsQuantity;
- movement.Dimensions.Length = shell.DimensionsLength;
- movement.Dimensions.Width = shell.DimensionsWidth;
- movement.Dimensions.Height = shell.DimensionsHeight;
- movement.Dimensions.Weight = shell.DimensionsWeight;
- movement.Dimensions.Unit.HasHeight = shell.DimensionsHasHeight;
- movement.Dimensions.Unit.HasLength = shell.DimensionsHasLength;
- movement.Dimensions.Unit.HasWidth = shell.DimensionsHasWidth;
- movement.Dimensions.Unit.HasWeight = shell.DimensionsHasWeight;
- movement.Dimensions.Unit.HasQuantity = shell.DimensionsHasQuantity;
- movement.Dimensions.Unit.Formula = shell.DimensionsUnitFormula;
- movement.Dimensions.Unit.Format = shell.DimensionsUnitFormat;
- movement.Dimensions.Unit.Code = shell.DimensionsUnitCode;
- movement.Dimensions.Unit.Description = shell.DimensionsUnitDescription;
- movement.Dimensions.UnitSize = shell.DimensionsUnitSize;
- movement.Dimensions.Value = shell.DimensionsValue;
- movement.Product.ID = shell.ProductID;
- movement.Product.Code = shell.Code;
- movement.Product.Name = shell.Name;
- movement.Employee.ID = GlobalVariables.EmpID;
- movement.Employee.Name = GlobalVariables.EmpName;
- if (issuing)
- {
- StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
- movement.Style.ID = originalShell.StyleID;
- movement.Style.Code = originalShell.StyleCode;
- movement.Style.Description = originalShell.Finish;
- movement.Job.ID = originalShell.JobID;
- movement.Job.JobNumber = originalShell.JobNumber;
- movement.Job.Name = originalShell.JobName;
- if (innerloop)
- {
- movement.System = true;
- movement.Job.ID = receivingJob.ID;
- movement.Job.JobNumber = receivingJob.JobNumber;
- movement.Job.Name = receivingJob.Name;
- }
- movement.Location.ID = issuingLocation.ID;
- movement.Location.Code = issuingLocation.Code;
- movement.Location.Description = issuingLocation.Description;
- movement.Issued = shell.Units;
- //innerloop for creating extra system stock movements if needed when issuing
- if (receivingJob.ID != Guid.Empty && !innerloop)
- {
- if (originalShell.JobID != receivingJob.ID)
- {
- CreateStockMovement(shell, batch, false, true, true);
- CreateStockMovement(shell, batch, true, false, true);
- }
- }
- }
- else if (receiving)
- {
- StockHoldingShell originalShell = originalHoldings.First(x => x.ID == shell.ID);
- movement.Style.ID = shell.StyleID;
- movement.Style.Code = shell.StyleCode;
- movement.Style.Description = shell.Finish;
- movement.Job.ID = shell.JobID;
- movement.Job.JobNumber = shell.JobNumber;
- movement.Job.Name = shell.JobName;
- movement.Location.ID = receivingLocation.ID;
- movement.Location.Code = receivingLocation.Code;
- movement.Location.Description = receivingLocation.Description;
- movement.Received = shell.Units;
- if (receivingJob.ID != Guid.Empty && innerloop)
- {
- movement.Style.ID = originalShell.StyleID;
- movement.Style.Code = originalShell.StyleCode;
- movement.Style.Description = originalShell.Finish;
- movement.Job.ID = receivingJob.ID;
- movement.Job.JobNumber = receivingJob.JobNumber;
- movement.Job.Name = receivingJob.Name;
- movement.Location.ID = issuingLocation.ID;
- movement.Location.Code = issuingLocation.Code;
- movement.Location.Description = issuingLocation.Description;
- movement.System = true;
- }
- }
- stockMovements.Add(movement);
- }
- #region Photos
- private async void SavePhotos(Guid batchID)
- {
- await Task.Run(() =>
- {
- new Client<Document>().Save(imagesDocuments.Values, "");
- // Link the photos to the batch
- List<StockMovementBatchDocument> stockMovementBatchDocuments = new List<StockMovementBatchDocument>();
- foreach (var doc in imagesDocuments.Values)
- {
- var smd = new StockMovementBatchDocument();
- smd.EntityLink.ID = batchID;
- smd.DocumentLink.ID = doc.ID;
- smd.DocumentLink.FileName = doc.FileName;
- if (smd.EntityLink.ID != Guid.Empty)
- stockMovementBatchDocuments.Add(smd);
- }
- new Client<StockMovementBatchDocument>().Save(stockMovementBatchDocuments, "");
- });
- }
- async void TakePhoto_Clicked(object sender, EventArgs e)
- {
- TakeAPhoto();
- }
- async void ChooseImage_Clicked(object sender, EventArgs e)
- {
- ChooseAPhoto();
- }
- private async void TakeAPhoto()
- {
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
- {
- await DisplayAlert("No Camera", ":( No camera available.", "OK");
- return;
- }
- String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
- var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
- {
- Name = filename,
- CompressionQuality = 10,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
- });
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- file.GetStream().CopyTo(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = filename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- private async void ChooseAPhoto()
- {
- await CrossMedia.Current.Initialize();
- if (!CrossMedia.Current.IsPickPhotoSupported)
- {
- await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
- return;
- }
- var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
- {
- CompressionQuality = 10,
- PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
- });
- if (file == null)
- return;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- file.GetStream().CopyTo(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = Path.GetFileName(file.Path),
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- img = new Image();
- img.HeightRequest = 150;
- img.WidthRequest = 150;
- img.Aspect = Aspect.AspectFit;
- img.Source = src;
- img.GestureRecognizers.Add(new TapGestureRecognizer
- {
- Command = new Command(OnTap),
- CommandParameter = src,
- NumberOfTapsRequired = 1
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- private void OnTap(object obj)
- {
- ImageViewer viewer = new ImageViewer(obj as ImageSource);
- Navigation.PushAsync(viewer);
- viewer.ChooseDelete();
- viewer.OnDeleteSelected += () =>
- {
- Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
- imagesDocuments.Remove(img);
- Device.BeginInvokeOnMainThread(() =>
- {
- images.Children.Clear();
- if (imagesDocuments.Count > 0)
- {
- foreach (Image image in imagesDocuments.Keys)
- {
- images.Children.Add(image);
- }
- }
- UpdateColours();
- });
- };
- }
- #endregion
- #region Updating Screen
- private void NotesEdt_Changed(object sender, EventArgs e)
- {
- UpdateColours();
- }
- private void UpdateColours()
- {
- if (photosFrame.BorderColor == Color.Red)
- {
- if (imagesDocuments.Values.Count > 0)
- {
- photosFrame.BorderColor = Color.Gray;
- }
- }
- if (!string.IsNullOrWhiteSpace(notesEdt.Text))
- {
- if (notesFrame.BorderColor == Color.Red)
- {
- notesFrame.BorderColor = Color.Gray;
- }
- }
- else
- {
- notesFrame.BorderColor = Color.Red;
- }
- ShowSave();
- }
- private void ShowSave()
- {
- if (notesFrame.BorderColor == Color.Gray && photosFrame.BorderColor == Color.Gray)
- {
- saveBatchBtn.IsVisible = true;
- }
- else
- {
- saveBatchBtn.IsVisible = false;
- }
- }
- #endregion
- }
- }
|