123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using InABox.Clients;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using System.Threading.Tasks;
- using InABox.Core;
- using InABox.Mobile;
- using Syncfusion.SfImageEditor.XForms;
- using Xamarin.CommunityToolkit.UI.Views;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class TransferModule
- {
- #region Fields + Constructor + Loading
-
- public TransferModule(Guid? locationid = null)
- {
- InitializeComponent();
- // Need to deal with Launching from specific location
- }
- protected override async Task<bool> OnBackButtonClicked()
- {
- if (_viewModel.Transactions.Count > 0 || _viewModel.Images.Count > 0)
- {
- if (await DisplayAlert("Cancel", "Leave without saving?", "OK", "CANCEL") != true)
- return false;
- }
- return true;
- }
- #endregion
-
-
- #region Search
-
- private void SearchEnt_Changed(object sender, EventArgs e)
- {
- // if (string.IsNullOrWhiteSpace(searchEnt.Text))
- // {
- // issuingListView.ItemsSource = issuingHoldings;
- // }
- // else
- // {
- // RunSearch();
- // }
- }
-
- #endregion
-
- private async void SaveBatch_Clicked(object sender, EventArgs e)
- {
- if (_viewModel.Transactions.Count == 0)
- {
- DisplayAlert("Error","No Transfers to Save","OK");
- return;
- }
-
- if (!_viewModel.Images.Any())
- {
- DisplayAlert("Error","No Images provided","OK");
- return;
- }
- if (String.IsNullOrWhiteSpace(_viewModel.Notes))
- {
- DisplayAlert("Error","Transfer Notes are blank","OK");
- return;
- }
- if (await DisplayAlert("Confirm", "Save Transfer Batch?", "OK", "CANCEL") == true)
- {
- using(var dialog = await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Data"))
- {
- Progress<String> progress = new Progress<string>((s) => dialog.MessageText = s);
- await _viewModel.Save(progress);
- }
- Navigation.PopAsync();
- }
-
- }
- private void From_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- StockLocationSelectionPage page = new StockLocationSelectionPage(
- new StockSelectionPageOptions()
- {
- ExcludeLocations = new Guid[] { _viewModel?.Source?.ID ?? Guid.Empty, _viewModel?.Target?.ID ?? Guid.Empty }
- }
- );
- page.OnLocationSelected += (s, e) =>
- {
- var location = e.Locations.FirstOrDefault();
- _viewModel.Source = location != null
- ? location
- : new StockLocationShell();
- };
- Navigation.PushAsync(page);
- }
- private void To_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- StockLocationSelectionPage page = new StockLocationSelectionPage(
- new StockSelectionPageOptions()
- {
- ExcludeLocations = new Guid[] { _viewModel?.Source?.ID ?? Guid.Empty, _viewModel?.Target?.ID ?? Guid.Empty }
- }
- );
- page.OnLocationSelected += (s, e) =>
- {
- var location = e.Locations.FirstOrDefault();
- _viewModel.Target = location != null
- ? location
- : new StockLocationShell();
- };
- Navigation.PushAsync(page);
- }
- private void Holding_Clicked(object sender, EventArgs e)
- {
- if ((sender as MobileCard)?.BindingContext is StockHoldingShell shell && shell.LocationID == _viewModel.Source.ID)
- {
- if (_viewModel.Target.ID == Guid.Empty)
- {
- DisplayAlert("ERROR", "Please select a Location to Transfer to!", "OK");
- return;
- }
- var transaction = new StockTransaction(StockTransactionType.Transfer, shell, shell);
- if (_viewModel.Target.ID != Guid.Empty)
- {
- transaction.Target.LocationID = _viewModel.Target.ID;
- transaction.Target.LocationCode = _viewModel.Target.Code;
- transaction.Target.LocationDescription = _viewModel.Target.Description;
- transaction.Allocations = shell.Allocations;
-
- var others = _viewModel.Transactions.Where(x =>
- x.Source.LocationID == shell.LocationID
- && x.Source.StyleID == shell.StyleID
- && x.ProductID == shell.ProductID
- && x.DimensionsUnitID == shell.DimensionsUnitID
- && x.DimensionsHeight == shell.DimensionsHeight
- && x.DimensionsWidth == shell.DimensionsWidth
- && x.DimensionsLength == shell.DimensionsLength
- && x.DimensionsQuantity == shell.DimensionsQuantity
- && x.DimensionsWeight == shell.DimensionsWeight).ToArray();
-
- var allocations = new List<StockTransactionAllocation>();
- foreach (var alloc in shell.Allocations)
- {
- var newalloc = new StockTransactionAllocation();
- newalloc.ID = alloc.ID;
- newalloc.Description = alloc.Description;
- newalloc.Quantity = alloc.Quantity;
- foreach (var other in others)
- newalloc.Quantity -= other.Allocations.Where(x => x.ID == alloc.ID).Sum(x => x.Quantity);
- newalloc.Maximum = newalloc.Quantity;
- allocations.Add(newalloc);
- }
- transaction.Allocations = allocations.ToArray();
- }
-
- var popup = new TransferEdit(transaction, _viewModel.Transactions);
- popup.TransactionSaved += (o,e) =>
- {
- shell.Parent.Transactions?.Add(transaction);
- _sourceHoldings.ItemsSource = null;
- _sourceHoldings.ItemsSource = _viewModel.SourceHoldings.Items;
- _targetHoldings.ItemsSource = null;
- _targetHoldings.ItemsSource = _viewModel.TargetHoldings.Items;
- };
- Navigation.PushAsync(popup);
- }
- }
-
- private async void TakePhoto_Clicked(object sender, EventArgs args)
- {
- try
- {
- MobileDocument file = await MobileDocument.From(PhotoUtils.CreateCameraOptions());
- if (file?.Data?.Any() == true)
- _viewModel.Images.Add(
- new StockTransactionImage(
- file,
- MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256)
- )
- );
- }
- catch (Exception e)
- {
- await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
- }
- }
- private async void PickPhoto_Clicked(object sender, EventArgs args)
- {
- try
- {
- MobileDocument file = await MobileDocument.From(PhotoUtils.CreatePhotoLibraryOptions());
- if (file?.Data?.Any() == true)
- _viewModel.Images.Add(
- new StockTransactionImage(
- file,
- MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256)
- )
- );
- }
- catch (Exception e)
- {
- await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
- }
- }
- private void Image_Clicked(object sender, EventArgs e)
- {
- if ((sender as MobileCard)?.BindingContext is StockTransactionImage image)
- {
- ShowPopup(() =>
- {
- var editor = new SfImageEditor()
- {
- Source = ImageSource.FromStream(() => new MemoryStream(image.Document.Data)),
- BackgroundColor = Color.Transparent,
- };
- editor.ToolbarSettings.HeaderToolbarHeight = 0;
-
- editor.ImageEdited += (o, args) =>
- {
- // Trigger the Dispatcher to allow the edit to complete before saving
- // Otherwise the autosaved image is one edit behind the editor itself
- Dispatcher.BeginInvokeOnMainThread(() => editor.Save());
- };
-
- editor.ImageSaving += (o, args) =>
- {
- using (var ms = new MemoryStream())
- {
- args.Stream.CopyTo(ms);
- image.Document.Data = ms.GetBuffer();
- image.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(image.Document.Data, 256, 256);
- }
- args.Cancel = true;
- };
-
- return editor;
- });
- }
- }
- private async void DeleteTransaction_Clicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- if ((sender as MobileMenuButton)?.BindingContext is StockTransaction transaction)
- {
- if (await DisplayAlert("Confirm", "Remove Tranasaction?", "OK", "CANCEL") == true)
- _viewModel.Transactions.Remove(transaction);
- }
- }
- private void TransactionImage_Clicked(object sender, EventArgs args)
- {
- if ((sender as Image)?.BindingContext is StockTransaction transaction && transaction.Image?.Any() == true)
- {
- ShowPopup(() =>
- {
- var editor = new SfImageEditor()
- {
- Source = ImageSource.FromStream(() => new MemoryStream(transaction.Image)),
- BackgroundColor = Color.Transparent,
- };
- editor.ToolbarSettings.IsVisible = false;
- return editor;
- });
- }
- }
- private void Transaction_Clicked(object sender, EventArgs e)
- {
- if ((sender as MobileCard)?.BindingContext is StockTransaction transaction)
- {
- var popup = new TransferEdit(transaction, _viewModel.Transactions);
- //popup.TransactionSaved += (o, e) => _viewModel.Transactions?.Add(transaction);
- Navigation.PushAsync(popup);
- }
- }
- private void TargetSearch_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- _viewModel.TargetHoldings.Search(x =>
- String.IsNullOrWhiteSpace(e.NewTextValue)
- || x.ProductDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- || x.StyleDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- || x.JobDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- );
- }
- private void SourceSearch_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- _viewModel.SourceHoldings.Search(x =>
- String.IsNullOrWhiteSpace(e.NewTextValue)
- || x.ProductDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- || x.StyleDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- || x.JobDisplay.ToUpper().Contains(e.NewTextValue.ToUpper())
- );
- }
- }
- }
|