| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- 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.Text;
- using System.Threading.Tasks;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using XF.Material.Forms.UI.Dialogs;
- using LogType = InABox.Core.LogType;
- namespace comal.timesheets
- {
- public delegate void ConsignmentSavedEvent();
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class ConsignmentCompletionPage
- {
- public event ConsignmentSavedEvent OnConsignmentSaved;
- List<POItemShell> listReceived = new List<POItemShell>();
- int shortSupplyIssues = 0;
- int extraSupplyIssues = 0;
- int count = 1;
- Guid consignmentID = Guid.Empty;
- string consignmentNumber = "";
- Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
- public ConsignmentCompletionPage(List<POItemShell> _listReceived, Guid _consignmentID, string _consignmentNumber)
- {
- InitializeComponent();
- consignmentID = _consignmentID;
- consignmentNumber = _consignmentNumber;
- listReceived = _listReceived;
- LoadScreen();
- }
- private void LoadScreen()
- {
- try
- {
- titleLbl.Text = "Consignment " + consignmentNumber;
- totalReceivedNumberLbl.Text = listReceived.Count.ToString();
- string text = new Client<Consignment>().Query(
- new Filter<Consignment>(x => x.ID).IsEqualTo(consignmentID),
- new Columns<Consignment>(x => x.Description)
- ).Rows.FirstOrDefault().Values[0].ToString();
- List<POItemShell> issuesList = new List<POItemShell>();
- foreach (POItemShell item in listReceived)
- {
- if (item.Description.Contains("due to short supply"))
- {
- shortSupplyIssues++;
- issuesList.Add(item);
- }
- else if (item.Description.Contains("Purchase Order Item Line split due to extra"))
- {
- extraSupplyIssues++;
- issuesList.Add(item);
- }
- }
- if (issuesList.Count > 0)
- {
- foreach (POItemShell item in issuesList)
- {
- if (item.Description.Contains("due to short supply"))
- {
- text = text + System.Environment.NewLine + count + ". " + item.Description;
- count++;
- }
- else if (item.Description.Contains("Purchase Order Item Line split due to extra"))
- {
- text = text + System.Environment.NewLine + count + ". " + item.Description;
- count++;
- }
- }
- }
- descriptionLbl.Text = text;
- }
- catch { }
- }
- async void TakePhoto_Clicked(object sender, EventArgs e)
- {
- TakeAPhoto();
- }
- async void ChooseImage_Clicked(object sender, EventArgs e)
- {
- ChooseAPhoto();
- }
- private async void ConfirmBtn_Clicked(object sender, EventArgs e)
- {
- try
- {
- if (photosFrame.BorderColor == Color.Red)
- {
- return;
- }
- else
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- Consignment consignment = new Consignment()
- {
- ID = consignmentID,
- Number = consignmentNumber
- };
- consignment.ActualWarehouseArrival = DateTime.Now;
- consignment.Description = descriptionLbl.Text;
- consignment.Employee.ID = App.Data.Me.ID;
- SaveConsignment(consignment);
- await DisplayAlert("Success", "Receival saved for " + consignmentNumber, "OK");
- OnConsignmentSaved?.Invoke();
- }
- Navigation.PopAsync();
- }
- }
- catch (Exception ex)
- {
- OnConsignmentSaved?.Invoke();
- }
- }
- private void SaveConsignment(Consignment consignment)
- {
- try
- {
- new Client<Consignment>().Save(consignment, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- SaveConsignment(consignment);
- }
- }
- //Note that photos are saved as they are taken, to avoid saving a long list of photos which some devices can't handle
- //PurchaseOrderDocuments, StockMovementBatch and StockMovementBatchDocuments are saved in ConsignmentStore
- #region Photos
- private async void TakeAPhoto()
- {
- try
- {
- 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;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- var memoryStream = new MemoryStream();
- file.GetStream().CopyTo(memoryStream);
- var data = memoryStream.ToArray();
- Document doc = new Document()
- {
- FileName = filename,
- Data = data,
- CRC = CoreUtils.CalculateCRC(data),
- TimeStamp = DateTime.Now
- };
- 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
- });
- doc = SavePhoto(doc);
- SaveConsignmentDocument(doc.ID);
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- catch (Exception ex)
- {
- DisplayAlert("Error saving - please try again", ex.Message, "OK");
- }
- }
- private Document SavePhoto(Document doc)
- {
- try
- {
- new Client<Document>().Save(doc, "Saved on Mobile Receivals Module");
- return doc;
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- return SavePhoto(doc);
- }
- }
- private async void ChooseAPhoto()
- {
- try
- {
- 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;
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
- {
- Image img = null;
- 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
- };
- 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
- });
- doc = SavePhoto(doc);
- SaveConsignmentDocument(doc.ID);
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- catch (Exception ex)
- {
- DisplayAlert("Error saving - please try again", ex.Message, "OK");
- }
- }
- private void SaveConsignmentDocument(Guid docid)
- {
- Task.Run(() =>
- {
- try
- {
- ConsignmentDocument consignmentDocument = new ConsignmentDocument();
- consignmentDocument.DocumentLink.ID = docid;
- consignmentDocument.EntityLink.ID = consignmentID;
- new Client<ConsignmentDocument>().Save(consignmentDocument, "Saved on Mobile Receivals Module");
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- SaveConsignmentDocument(docid);
- }
- });
- }
- private void OnTap(object obj)
- {
- try
- {
- ImageViewer viewer = new ImageViewer(obj as ImageSource);
- Navigation.PushAsync(viewer);
- viewer.ChooseDelete();
- viewer.OnDeleteSelected += () =>
- {
- Image img = imagesDocuments.Keys.FirstOrDefault(x => x.Source.Equals(obj as ImageSource));
- DeleteDocuments(imagesDocuments[img].ID);
- imagesDocuments.Remove(img);
- Device.BeginInvokeOnMainThread(() =>
- {
- images.Children.Clear();
- if (imagesDocuments.Count > 0)
- {
- foreach (Image image in imagesDocuments.Keys)
- {
- images.Children.Add(image);
- }
- }
- UpdateColours();
- });
- };
- }
- catch { }
- }
- private void DeleteDocuments(Guid docid)
- {
- Task.Run(() =>
- {
- try
- {
- Document doc = new Document { ID = docid };
- new Client<Document>().Delete(doc, "User removed unwanted photo on mobile receivals module");
- CoreTable table = new Client<ConsignmentDocument>().Query(new Filter<ConsignmentDocument>(x => x.DocumentLink.ID).IsEqualTo(docid),
- new Columns<ConsignmentDocument>(x => x.ID));
- if (table.Rows.Any())
- {
- ConsignmentDocument consignmentDocument = table.Rows.FirstOrDefault().ToObject<ConsignmentDocument>();
- new Client<ConsignmentDocument>().Delete(consignmentDocument, "User removed unwanted photo on mobile receivals module");
- }
- }
- catch (Exception ex)
- {
- InABox.Mobile.MobileLogging.Log(ex);
- DeleteDocuments(docid);
- }
- });
- }
- #endregion
- private void UpdateColours()
- {
- if (imagesDocuments.Count > 0)
- photosFrame.BorderColor = Color.Gray;
- else
- photosFrame.BorderColor = Color.Red;
- }
- }
- }
|