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 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 progress = new Progress((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(); 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()) ); } } }