| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- using comal.timesheets.CustomControls;
- 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.Linq.Expressions;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.CommunityToolkit.Extensions;
- using Xamarin.CommunityToolkit.UI.Views;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- namespace comal.timesheets
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class ConsignmentDetailsPopup
- {
- public delegate void ConsignmentPopupAcceptedEvent();
- public event ConsignmentPopupAcceptedEvent OnConsignmentPopupAccepted;
- public POItemShell _POItemShell { get; set; }
- StockLocation defaultLocation = new StockLocation();
- List<StockLocationShell> stockLocationShells = new List<StockLocationShell>();
- public ConsignmentDetailsPopup(POItemShell poitemshell, List<StockLocationShell> _stockLocationShells = null)
- {
- InitializeComponent();
- if (_stockLocationShells != null)
- stockLocationShells = _stockLocationShells;
- //this prevents changes to the original poitemshell in case of cancelling or back button pressed
- _POItemShell = poitemshell.DuplicateNewShell(poitemshell);
- if (_POItemShell.StockLocationID != Guid.Empty) //item has a default location already assigned
- {
- AddDefaultLocationButton();
- }
- LoadButtons();
- qtyEnt.Text = _POItemShell.ExpectedQty.ToString();
- titleLbl.Text = _POItemShell.Description;
- }
- private void LoadButtons()
- {
- foreach (StockLocationShell shell in stockLocationShells)
- {
- Button button = new Button
- {
- Text = shell.Description,
- BackgroundColor = Color.FromHex("#15C7C1"),
- TextColor = Color.White,
- CornerRadius = 5,
- Padding = new Thickness(3),
- FontAttributes = FontAttributes.Bold,
- };
- button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
- button.Clicked += (s, e) =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- (s as Button).BackgroundColor = Color.FromHex("#9f4576");
- StockLocationShell shell2 = stockLocationShells.Find(x => x.Description == (s as Button).Text);
- _POItemShell.StockLocationID = shell2.ID;
- _POItemShell.StockLocationCode = shell2.Code;
- _POItemShell.StockLocationDescription = shell2.Description;
- };
- locationStackLayout.Children.Add(button);
- }
- if (_POItemShell.StockLocationID == Guid.Empty) //item does not have a default location assigned - defaults to incoming stores
- {
- try
- {
- StockLocationShell shell = stockLocationShells.Find(x => x.ID.Equals(Guid.Parse("3ebb1fe3-21f4-4731-b80a-a28f814982ea")));
- _POItemShell.StockLocationID = shell.ID;
- _POItemShell.StockLocationCode = shell.Code;
- _POItemShell.StockLocationDescription = shell.Description;
- (locationStackLayout.Children[0] as Button).BackgroundColor = Color.FromHex("#9f4576");
- }
- catch { }
- }
- }
- private void AddDefaultLocationButton()
- {
- if (stockLocationShells.Count > 0 && stockLocationShells.Contains(stockLocationShells.Find(x => x.ID == _POItemShell.StockLocationID)))
- return;
- defaultLocation.ID = _POItemShell.StockLocationID;
- defaultLocation.Code = _POItemShell.StockLocationCode;
- defaultLocation.Description = _POItemShell.StockLocationDescription;
- Button button = new Button
- {
- Text = _POItemShell.StockLocationDescription,
- BackgroundColor = Color.FromHex("#9f4576"),
- TextColor = Color.White,
- CornerRadius = 5,
- Padding = new Thickness(3),
- FontAttributes = FontAttributes.Bold,
- };
- button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
- button.Clicked += (sender, e) =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- (sender as Button).BackgroundColor = Color.FromHex("#9f4576");
- _POItemShell.StockLocationID = defaultLocation.ID;
- _POItemShell.StockLocationCode = defaultLocation.Code;
- _POItemShell.StockLocationDescription = defaultLocation.Description;
- };
- locationStackLayout.Children.Add(button);
- }
- private async void AddLocation_Clicked(object sender, EventArgs e)
- {
- string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, "New Location", "Existing Location");
- switch (chosenOption)
- {
- case "Cancel":
- return;
- case "New Location":
- ChooseNewLocation();
- break;
- case "Existing Location":
- ChooseReceivingLocation();
- break;
- default:
- return;
- }
- }
- private void ChooseNewLocation()
- {
- StockLocation location = new StockLocation();
- CoreTable table = new Client<StockLocation>().Query(new Filter<StockLocation>(x => x.ID).IsEqualTo(Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd")),
- new Columns<StockLocation>(x => x.ID));
- if (table.Rows.Any())
- {
- location.Warehouse.ID = Guid.Parse("b6249c4a-a834-4927-a42c-87a07895d6bd"); //EXTRUSIONS
- location.Warehouse.Description = "Extrusions";
- location.Warehouse.Code = "EXTRUSIONS";
- }
- location.Active = true;
- LocationDetailsPage locationDetailsPage = new LocationDetailsPage(location);
- locationDetailsPage.OnSave += (o, loc) =>
- {
- if (String.IsNullOrWhiteSpace(loc.Code))
- {
- MaterialDialog.Instance.AlertAsync(message: "Code may not be blank!");
- return false;
- }
- if (String.IsNullOrWhiteSpace(loc.Description))
- {
- MaterialDialog.Instance.AlertAsync(message: "Description may not be blank!");
- return false;
- }
- if (loc.Area.ID == Guid.Empty)
- {
- MaterialDialog.Instance.AlertAsync(message: "Area may not be blank!");
- return false;
- }
- if (loc.Warehouse.ID == Guid.Empty)
- {
- MaterialDialog.Instance.AlertAsync(message: "Warehouse may not be blank!");
- return false;
- }
- CoreTable others = new Client<StockLocation>().Query(
- new Filter<StockLocation>(x => x.Code).IsEqualTo(loc.Code).And(x => x.ID).IsNotEqualTo(loc.ID),
- new Columns<StockLocation>(x => x.ID)
- );
- if (others.Rows.Any())
- {
- MaterialDialog.Instance.AlertAsync(message: "Location Code already exists!");
- return false;
- }
- try
- {
- new Client<StockLocation>().Save(loc, "Created Location");
- _POItemShell.StockLocationID = loc.ID;
- _POItemShell.StockLocationCode = loc.Code;
- _POItemShell.StockLocationDescription = loc.Description;
- StockLocationShell shell = new StockLocationShell
- {
- ID = loc.ID,
- Code = loc.Code,
- Description = loc.Description
- };
- stockLocationShells.Add(shell);
- Button button = new Button
- {
- Text = shell.Description,
- BackgroundColor = Color.FromHex("#9f4576"),
- TextColor = Color.White,
- CornerRadius = 5,
- Padding = new Thickness(3),
- FontAttributes = FontAttributes.Bold,
- };
- button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
- button.Clicked += (s, e) =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- (s as Button).BackgroundColor = Color.FromHex("#9f4576");
- StockLocationShell shell2 = stockLocationShells.Find(x => x.Description == (s as Button).Text);
- _POItemShell.StockLocationID = shell2.ID;
- _POItemShell.StockLocationCode = shell2.Code;
- _POItemShell.StockLocationDescription = shell2.Description;
- };
- Device.BeginInvokeOnMainThread(() =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- button.BackgroundColor = Color.FromHex("#9f4576");
- locationStackLayout.Children.Add(button);
- });
- }
- catch (Exception err)
- {
- MaterialDialog.Instance.AlertAsync(message: "Unable to save Location\n" + err.Message);
- return false;
- }
- return true;
- };
- Navigation.PushAsync(locationDetailsPage);
- }
- private void ChooseReceivingLocation()
- {
- StockLocationSelectionPage page = new StockLocationSelectionPage();
- page.OnLocationSelected += (s) =>
- {
- _POItemShell.StockLocationID = s.ID;
- _POItemShell.StockLocationCode = s.Code;
- _POItemShell.StockLocationDescription = s.Description;
- StockLocationShell shell = new StockLocationShell
- {
- ID = s.ID,
- Code = s.Code,
- Description = s.Description
- };
- if (!stockLocationShells.Contains(shell))
- stockLocationShells.Add(shell);
- Button button = new Button
- {
- Text = shell.Description,
- BackgroundColor = Color.FromHex("#9f4576"),
- TextColor = Color.White,
- CornerRadius = 5,
- Padding = new Thickness(3),
- FontAttributes = FontAttributes.Bold,
- };
- button.FontSize = Device.GetNamedSize(NamedSize.Medium, button);
- button.Clicked += (object sender, EventArgs e) =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- (sender as Button).BackgroundColor = Color.FromHex("#9f4576");
- StockLocationShell shell2 = stockLocationShells.Find(x => x.Description == (sender as Button).Text);
- _POItemShell.StockLocationID = shell2.ID;
- _POItemShell.StockLocationCode = shell2.Code;
- _POItemShell.StockLocationDescription = shell2.Description;
- };
- Device.BeginInvokeOnMainThread(() =>
- {
- foreach (Button button1 in locationStackLayout.Children)
- {
- button1.BackgroundColor = Color.FromHex("#15C7C1");
- }
- button.BackgroundColor = Color.FromHex("#9f4576");
- locationStackLayout.Children.Add(button);
- });
- };
- Navigation.PushAsync(page);
- }
- private async void SetDefaultLocationCb_CheckChanged(object sender, CheckedChangedEventArgs e)
- {
- if (setDefaultCb.IsChecked)
- {
- if (_POItemShell.StockLocationID != Guid.Empty)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Default Location"))
- {
- Product product = new Product();
- product.ID = _POItemShell.ProductID;
- product.DefaultLocation.ID = _POItemShell.StockLocationID;
- new Client<Product>().Save(product, "Updated default location from Mobile Receivals Module");
- }
- }
- else
- {
- DisplayAlert("Alert", "No Location selected", "OK");
- setDefaultCb.IsChecked = false;
- }
- }
- else
- {
- }
- }
- private void Qty_Changed(object sender, EventArgs e)
- {
- if (!string.IsNullOrWhiteSpace(qtyEnt.Text))
- {
- if (double.TryParse(qtyEnt.Text, out double value))
- {
- _POItemShell.ReceivedQty = value;
- }
- else
- {
- qtyEnt.Text = _POItemShell.ExpectedQty.ToString();
- _POItemShell.ReceivedQty = _POItemShell.ExpectedQty;
- }
- }
- }
- private void AcceptBtn_Clicked(object sender, EventArgs e)
- {
- if (string.IsNullOrWhiteSpace(qtyEnt.Text) || double.Parse(qtyEnt.Text) == 0)
- {
- DisplayAlert("Alert", "Quantity may not be blank or 0", "OK");
- return;
- }
- if (_POItemShell.StockLocationID == Guid.Empty)
- {
- DisplayAlert("Alert", "Location may not be blank", "OK");
- return;
- }
- OnConsignmentPopupAccepted?.Invoke();
- Navigation.PopAsync();
- }
- }
- }
|