123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- using System.Transactions;
- using InABox.Mobile;
- using Syncfusion.SfPdfViewer.XForms;
- using Syncfusion.XForms.PopupLayout;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
-
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StockTakeEdit
- {
- public event StockTransactionSavedEvent TransactionSaved;
-
- public StockTakeEdit(StockTransaction transaction)
- {
- InitializeComponent();
- ViewModel.Transaction = transaction;
- Quantity.TextChanged += InputView_OnTextChanged;
- }
-
- private async void _tick_OnClicked(object sender, MobileMenuButtonClickedEventArgs args)
- {
- if (ViewModel.Transaction.ProductID == Guid.Empty)
- {
- await DisplayAlert("ERROR", "Please select a Product first.", "OK");
- return;
- }
-
- if (ViewModel.IsNew && ViewModel.Transaction.Quantity.IsEffectivelyEqual(0.0))
- {
- await DisplayAlert("ERROR", "New Holdings must have a quantity!", "OK");
- return;
- }
- if (ViewModel.Transaction.ImageID == Guid.Empty)
- {
- if (await DisplayAlert("No Image","There is no image attached to this product.\nSave anyway?","Yes","No") == false)
- return;
- }
- TransactionSaved?.Invoke(this, new StockTransactionSavedArgs(ViewModel.Transaction));
- Navigation.PopAsync();
- }
- private void IncreaseQty_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var alloc = ViewModel.Transaction.Allocations?.FirstOrDefault();
- if (alloc != null)
- alloc.Quantity = alloc.Quantity + 1;
- ViewModel.Transaction.RefreshQuantity();
- }
- private void DecreaseQty_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var alloc = ViewModel.Transaction.Allocations?.FirstOrDefault();
- if (alloc != null)
- alloc.Quantity = Math.Max(0, alloc.Quantity - 1);
- ViewModel.Transaction.RefreshQuantity();
- }
-
- private void SelectStyle_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() =>
- SelectionView.Execute<ProductStyleShell>(
- columns =>
- {
- columns.Add(new MobileGridTextColumn<ProductStyleShell>()
- { Column = x => x.Code, Width = GridLength.Auto });
- columns.Add(new MobileGridTextColumn<ProductStyleShell>()
- { Column = x => x.Description, Width = GridLength.Star });
- },
- refresh => App.Data.ProductStyles.Refresh(false),
- styles =>
- {
- ViewModel.Transaction.Target.StyleID = styles.FirstOrDefault()?.ID ?? Guid.Empty;
- ViewModel.Transaction.Target.StyleCode = styles.FirstOrDefault()?.Code ?? string.Empty;
- ViewModel.Transaction.Target.StyleDescription = styles.FirstOrDefault()?.Description ?? string.Empty;
- DismissPopup();
- })
- );
- }
- private void SelectJob_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() =>
- SelectionView.Execute<JobShell>(
- columns =>
- {
- columns.Add(new MobileGridTextColumn<JobShell>()
- { Column = x => x.JobNumber, Width = GridLength.Auto });
- columns.Add(new MobileGridTextColumn<JobShell>()
- { Column = x => x.Name, Width = GridLength.Star });
- },
- refresh => App.Data.Jobs.Refresh(false),
- jobs =>
- {
- ViewModel.Transaction.Target.JobID = jobs.FirstOrDefault()?.ID ?? Guid.Empty;
- ViewModel.Transaction.Target.JobNumber = jobs.FirstOrDefault()?.JobNumber ?? string.Empty;
- ViewModel.Transaction.Target.JobName = jobs.FirstOrDefault()?.Name ?? string.Empty;
- DismissPopup();
- })
- );
- }
- private void SelectProduct_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- ShowPopup(() =>
- {
- bool force = false;
- return SelectionView.Execute<ProductShell>(
- columns =>
- {
- columns.Add(new MobileGridTextColumn<ProductShell>()
- { Column = x => x.Code, Width = 200 });
- columns.Add(new MobileGridTextColumn<ProductShell>()
- { Column = x => x.Name, Width = GridLength.Star });
- },
- refresh =>
- {
- var result = App.Data.Products.Refresh(force);
- force = true;
- return result;
- },
- products =>
- {
- ViewModel.Transaction.ProductID = products.FirstOrDefault()?.ID ?? Guid.Empty;
- ViewModel.Transaction.ProductCode = products.FirstOrDefault()?.Code ?? string.Empty;
- ViewModel.Transaction.ProductName = products.FirstOrDefault()?.Name ?? string.Empty;
- if (ViewModel.Transaction.Source.Shell != null)
- {
- ViewModel.Transaction.Source.Shell.DimensionsHasLength =
- products.FirstOrDefault()?.DimensionsUnit?.HasLength ?? false;
- ViewModel.Transaction.Source.Shell.DimensionsHasWidth =
- products.FirstOrDefault()?.DimensionsUnit?.HasWidth ?? false;
- ViewModel.Transaction.Source.Shell.DimensionsHasHeight =
- products.FirstOrDefault()?.DimensionsUnit?.HasHeight ?? false;
- ViewModel.Transaction.Source.Shell.DimensionsHasQuantity =
- products.FirstOrDefault()?.DimensionsUnit?.HasQuantity ?? false;
- ViewModel.Transaction.Source.Shell.DimensionsHasWeight =
- products.FirstOrDefault()?.DimensionsUnit?.HasWeight ?? false;
- }
- ViewModel.Transaction.DimensionsUnitID =
- products.FirstOrDefault()?.DimensionsUnitID ?? Guid.Empty;
- ViewModel.Transaction.DimensionsHeight = products.FirstOrDefault()?.DimensionsHeight ?? 0.0;
- ViewModel.Transaction.DimensionsWidth = products.FirstOrDefault()?.DimensionsWidth ?? 0.0;
- ViewModel.Transaction.DimensionsLength = products.FirstOrDefault()?.DimensionsLength ?? 0.0;
- ViewModel.Transaction.DimensionsQuantity =
- products.FirstOrDefault()?.DimensionsQuantity ?? 0.0;
- ViewModel.Transaction.DimensionsValue = products.FirstOrDefault()?.DimensionsValue ?? 0.0;
- ViewModel.Transaction.DimensionsUnitSize =
- products.FirstOrDefault()?.DimensionsUnitSize ?? string.Empty;
- ViewModel.Transaction.Cost = products.FirstOrDefault()?.AverageCost ?? 0.0;
- ViewModel.Transaction.ImageID = products.FirstOrDefault()?.ImageID ?? Guid.Empty;
- if (ViewModel.Transaction.ImageID != Guid.Empty &&
- products.FirstOrDefault()?.Parent != null)
- {
- if (products.FirstOrDefault()!.Parent.Images.TryGetValue(ViewModel.Transaction.ImageID,
- out var data))
- ViewModel.Transaction.Image = data;
- else
- {
- DocumentModel docmodel = new DocumentModel(App.Data,
- () => new Filter<Document>(x => x.ID).IsEqualTo(ViewModel.Transaction.ImageID));
- docmodel.Refresh(true);
- ViewModel.Transaction.Image =
- docmodel.Items.FirstOrDefault()?.Data ?? new byte[] { };
- }
- }
- DismissPopup();
- });
- }
- );
-
- }
-
- private void InputView_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- if (!Quantity.IsVisible)
- return;
- if (sender is Entry entry)
- {
- if (double.TryParse(e.NewTextValue, out double qty))
- {
- var alloc = ViewModel.Transaction.Allocations?.FirstOrDefault();
- if (alloc != null)
- alloc.Quantity = qty;
- }
- else
- entry.Text = e.OldTextValue;
- ViewModel.Transaction.RefreshQuantity();
- }
- }
- private async void Delete_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- if (await DisplayAlert("Confirm", "Are you sure you wish to delete holding?", "Yes", "No") == true)
- {
- var holding = ViewModel.Transaction?.Source?.Shell;
- if (holding?.Parent != null)
- {
- holding.Parent.Items.Remove(holding);
- holding.Parent.Transactions.Remove(ViewModel.Transaction);
- }
- Navigation.PopAsync();
- }
- }
- private async Task AddImage<T, TOptions>(TOptions options)
- where T : MobileImageSource<T, TOptions>
- where TOptions : MobileImageOptions<T>, new()
- {
- App.Data.Products.Refresh(false);
- var product = App.Data.Products.FirstOrDefault(x => x.ID == ViewModel.Transaction.ProductID);
- if (product != null)
- {
- MobileDocument file = null;
- try
- {
- file = await MobileDocument.From<T>(options);
- }
- catch (Exception e)
- {
- await DisplayAlert("ERROR", e.Message, "OK");
- }
- if (file != null)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
- {
- Document doc = new Document()
- {
- FileName = file.FileName,
- Data = file.Data,
- CRC = CoreUtils.CalculateCRC(file.Data),
- TimeStamp = DateTime.Now
- };
- new Client<Document>().Save(doc, "Created on Mobile Device");
- product.ImageID = doc.ID;
- product.Save("Photo Updated from Mobile Device");
- product.Parent.Images[doc.ID] = file.Data;
- ViewModel.Transaction.ImageID = doc.ID;
- ViewModel.Transaction.Image = file.Data;
- }
- }
- }
- else
- DisplayAlert("Error","Please select a Product first!","OK");
- }
-
- private async void _imagesphoto_Clicked(object sender, EventArgs e)
- {
- if (ViewModel.Transaction.ImageID != Guid.Empty)
- {
- if (await DisplayAlert("Confirm", "Are you sure you wish to replace the existing image?", "Yes",
- "No") == false)
- return;
- }
- await AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions>(
- PhotoUtils.CreateCameraOptions());
- }
- private async void _imageslibrary_Clicked(object sender, EventArgs e)
- {
- if (ViewModel.Transaction.ImageID != Guid.Empty)
- {
- if (await DisplayAlert("Confirm", "Are you sure you wish to replace the existing image?", "Yes",
- "No") == false)
- return;
- }
- await AddImage<MobileDocumentPhotoLibrarySource, MobileDocumentPhotoLibraryOptions>(
- PhotoUtils.CreatePhotoLibraryOptions());
- }
-
- private void SelectUnitSize_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var editor = new DimensionsEditor() { Transaction = ViewModel.Transaction };
- editor.CloseRequested += (o, eventArgs) => PopupManager.DismissPopup();
- ShowPopup(
- ()=> editor,
- new PopupManagerConfiguration()
- {
- SizeMode = AutoSizeMode.Height,
- Modal = true,
- }
- );
- }
- private void SelectAllocations_Clicked(object sender, MobileButtonClickEventArgs args)
- {
- var editor = new AllocationsEditor() { Transaction = ViewModel.Transaction };
- editor.CloseRequested += (o, eventArgs) => PopupManager.DismissPopup();
- ShowPopup(
- () => editor,
- new PopupManagerConfiguration()
- {
- SizeMode = AutoSizeMode.Height,
- Modal = true
- }
- );
- }
- }
- }
|