| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using comal.timesheets.Tasks;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using Plugin.Media;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets.Products
- {
- public delegate void ExitHoldingsSelectedEvent();
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class HoldingViewer
- {
- public ExitHoldingsSelectedEvent OnExitSelected;
- Guid productID = Guid.Empty;
- Guid imageID = Guid.Empty;
- ObservableList<StockHoldingShell> stockHoldingShells = new ObservableList<StockHoldingShell>();
- ObservableList<Document> newPhotoDocuments = new ObservableList<Document>();
- String productName = "";
- Image img = null;
- public HoldingViewer(Guid _productID, string _productName, Guid _imageID)
- {
- InitializeComponent();
- NavigationPage.SetHasBackButton(this, false);
- productID = _productID;
- productName = _productName;
- titleLbl.Text = productName;
- imageID = _imageID;
- }
- protected override void OnAppearing()
- {
- LoadPhoto();
- LoadStockHoldings();
- base.OnAppearing();
- }
- private void Exit_Clicked(object sender, EventArgs e)
- {
- Navigation.PopAsync();
- }
- //saves photo on disappearing if new photo present
- protected override async void OnDisappearing()
- {
- base.OnDisappearing();
- if (newPhotoDocuments.Count > 0)
- {
- await Task.Run(() =>
- {
- Document doc = newPhotoDocuments.FirstOrDefault<Document>();
- new Client<Document>().Save(doc, "Updated from mobile device");
- new Client<Product>().Query(
- new Filter<Product>(x => x.ID).IsEqualTo(productID),
- new Columns<Product>(x => x.ID, x => x.Image.ID),
- null,
- (t, e) =>
- {
- CoreRow row = t.Rows.FirstOrDefault();
- if (row != null)
- {
- Product product = row.ToObject<Product>();
- product.Image.ID = doc.ID;
- imageID = doc.ID;
- new Client<Product>().Save(product, "Updated from mobile device");
- }
- });
- });
- }
- }
- private async void LoadStockHoldings()
- {
- titleLbl.Text = "Loading Holdings...";
- stockHoldingShells.Clear();
- await Task.Run(() =>
- {
- CoreTable table = new Client<StockHolding>().Query(
- new Filter<StockHolding>(x => x.Product.ID).IsEqualTo(productID),
- new Columns<StockHolding>(
- x => x.Location.Description, //0
- x => x.Location.Area.Code, //1
- x => x.Job.JobNumber, //2
- x => x.Style.Description, //3
- x => x.Units, //4
- x => x.Location.ID, //5
- x => x.Dimensions.UnitSize //6 STRING
- ),
- null
- );
- string emptyString = "";
- foreach (CoreRow row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = emptyString; } //0
- if (list[1] == null) { list[1] = emptyString; } //1
- if (list[2] == null) { list[2] = emptyString; } //2
- if (list[3] == null) { list[3] = emptyString; } //3
- if (list[4] == null) { list[4] = 0; } //4
- if (list[5] == null) { list[5] = Guid.Empty; } //5
- if (list[6] == null) { list[6] = emptyString; } //3
- double units = double.Parse(list[4].ToString());
- if (units > 0)
- {
- if (((Math.Round(units) * 100) / 100) != 0)
- {
- StockHoldingShell stockHoldingShell = new StockHoldingShell();
- stockHoldingShell.LocationName = list[0].ToString();
- stockHoldingShell.AreaName = "Area: " + list[1].ToString();
- stockHoldingShell.JobNumber = "Job: " + list[2].ToString();
- stockHoldingShell.StyleName = "Style: " + list[3].ToString();
- stockHoldingShell.Units = double.Parse(list[4].ToString());
- stockHoldingShell.LocationID = Guid.Parse(list[5].ToString());
- stockHoldingShell.DimensionsUnitSize = "Unit Size: " + list[6].ToString();
- stockHoldingShells.Add(stockHoldingShell);
- }
- }
- }
- Device.BeginInvokeOnMainThread(() =>
- {
- titleLbl.Text = "Holdings (" + stockHoldingShells.Count + ")";
- holdingListView.ItemsSource = null;
- holdingListView.ItemsSource = stockHoldingShells;
- photoFrame.IsVisible = true;
- });
- });
- }
- private async void Pack_Tapped(object sender, EventArgs e)
- {
- string chosenOption = await DisplayActionSheet("Choose stock movements to create", "Cancel", null,
- "Issue from this location", "Transfer from this location", "Stocktake this location");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "Issue from this location":
- OpenLocation(StockMovementBatchType.Issue);
- break;
- case "Transfer from this location":
- OpenLocation(StockMovementBatchType.Transfer);
- break;
- case "Stocktake this location":
- OpenLocation(StockMovementBatchType.Stocktake);
- break;
- default:
- return;
- }
- //if (ClientFactory.IsAllowed<CanModifyStockMovements>())
- //{
- // string chosenOption = await DisplayActionSheet("Choose stock movements to create", "Cancel", null, "Issue from this location", "Transfer from this location");
- // switch (chosenOption)
- // {
- // case "Cancel":
- // return;
- // case "Issue from this location":
- // OpenLocation(StockMovementBatchType.Issue);
- // break;
- // case "Transfer from this location":
- // OpenLocation(StockMovementBatchType.Transfer);
- // break;
- // default:
- // return;
- // }
- //}
- //else
- //{
- // DisplayAlert("Alert", "Your current security settings do not allow you to view this module"
- // + System.Environment.NewLine + "(Setting - Can Modify Stock Movements)", "OK");
- //}
- }
- private void OpenLocation(StockMovementBatchType type)
- {
- StockHoldingShell stockHoldingShell = holdingListView.SelectedItem as StockHoldingShell;
- if (type == StockMovementBatchType.Stocktake)
- {
- StocktakePage stocktakePage = new StocktakePage(stockHoldingShell.LocationID);
- Navigation.PushAsync(stocktakePage);
- }
- else
- {
- RecTrans recTrans = new RecTrans(type, stockHoldingShell.LocationID.ToString());
- Navigation.PushAsync(recTrans);
- }
- }
- #region Photos
- private async void LoadPhoto()
- {
- if (imageID != Guid.Empty)
- {
- photoFrame.IsVisible = true;
- photoLbl.Text = "Loading Image";
- await Task.Run(() =>
- {
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(imageID),
- null,
- null,
- (t, e) =>
- {
- CoreRow docrow = t.Rows.FirstOrDefault();
- if (docrow != null)
- {
- byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
- DataToImage(data);
- }
- }
- );
- });
- }
- }
- private async void TakePhoto_Clicked(object sender, EventArgs e)
- {
- if (img != null)
- {
- string chosenOption = await DisplayActionSheet("Would you like to replace the current photo?", "Cancel", null, "Yes", "No");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "Yes":
- break;
- case "No":
- return;
- default:
- return;
- }
- }
- 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;
- AddPhoto(file);
- }
- private async void ChooseImage_Clicked(object sender, EventArgs e)
- {
- if (img != null)
- {
- string chosenOption = await DisplayActionSheet("Would you like to replace the current photo?", "Cancel", null, "Yes", "No");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "Yes":
- break;
- case "No":
- return;
- default:
- return;
- }
- }
- 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;
- AddPhoto(file);
- }
- private async void AddPhoto(Plugin.Media.Abstractions.MediaFile file)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- 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
- };
- newPhotoDocuments.Clear();
- newPhotoDocuments.Add(doc);
- DataToImage(data);
- file.Dispose();
- }
- }
- private void DataToImage(byte[] data)
- {
- 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
- });
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- photoLbl.Text = "Product Image";
- images.Children.Clear();
- images.Children.Add(img);
- images.IsVisible = true;
- });
- }
- }
- private void OnTap(object obj)
- {
- ImageViewer viewer = new ImageViewer(obj as ImageSource);
- Navigation.PushAsync(viewer);
- }
- #endregion Photos
- }
- }
|