123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Essentials;
- using Xamarin.Forms;
- using ZXing;
- using ZXing;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- using comal.timesheets.CustomControls;
- using Comal.Classes;
- using InABox.Core;
- using InABox.Clients;
- using System.Threading;
- using Plugin.Media;
- using XF.Material.Forms.UI.Dialogs;
- using System.IO;
- namespace comal.timesheets.StoreRequis
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class StoreRequiConfirmationPage : ContentPage
- {
- #region Fields
- public delegate void SaveSelectedEvent();
- public event SaveSelectedEvent OnSaveSelected;
- JobShell job = new JobShell();
- bool newRequi;
- Requisition requisition;
- bool isNewRequest = false;
- Dictionary<Image, Document> imagesDocuments = new Dictionary<Image, Document>();
- List<StoreRequiItemShell> requiItems = new List<StoreRequiItemShell>();
- List<StoreRequiItemShell> oldrequiitems = new List<StoreRequiItemShell>();
- #endregion
- #region Constructor and appearing
- public StoreRequiConfirmationPage(Requisition _requisition, List<StoreRequiItemShell> _requiitems, List<StoreRequiItemShell> _oldrequiitems) //requi could be existing or a blank/new requi (ID empty)
- {
- InitializeComponent();
- requiItems = _requiitems;
- if (requiItems[0].IsNotes == true)
- {
- requiItems.RemoveAt(0);
- }
- oldrequiitems = _oldrequiitems;
- requiStatusLbl.IsVisible = true;
- requiStatusFrame.IsVisible = true;
- boxesLbl.IsVisible = true;
- boxesQtyFrame.IsVisible = true;
- photosLbl.IsVisible = true;
- photosFrame.IsVisible = true;
- if (_requisition.ID != Guid.Empty)
- forDeliveryRb.IsChecked = true;
- else
- takenNowRb.IsChecked = true;
- dueLbl.IsVisible = false;
- dueFrame.IsVisible = false;
- ChooseTrack(_requisition);
- }
- public StoreRequiConfirmationPage() //when no requi is passed, app is creating a new requi request
- {
- InitializeComponent();
- isNewRequest = true;
- NewRequiTrack();
- UpdateColours();
- notesTitleLbl.Text = "Request";
- dueDatePckr.IsEnabled = true;
- reduceQtyBtn.IsEnabled = false;
- increaseQtyBtn.IsEnabled = false;
- TakePhoto.IsEnabled = false;
- ChooseImage.IsEnabled = false;
- }
- private void ChooseTrack(Requisition _requisition)
- {
- if (_requisition.ID == Guid.Empty)
- {
- NewRequiTrack();
- }
- else
- {
- ExistingRequiTrack(_requisition);
- }
- }
- private void NewRequiTrack()
- {
- newRequi = true;
- requisition = new Requisition();
- requisition.Title = "New Requisition";
- requisition.Boxes = 1;
- boxQtyLbl.Text = requisition.Boxes.ToString();
- titleLbl.Text = "New Requisition";
- requisition.Title = "New Requisition";
- requisition.Due = DateTime.Now;
- requisition.RequestedBy.ID = GlobalVariables.EmpID;
- requisition.RequestedBy.Name = GlobalVariables.EmpName;
- //create new requi
- }
- private void ExistingRequiTrack(Requisition _requisition)
- {
- newRequi = false;
- requisition = _requisition;
- titleLbl.Text = "Requi " + _requisition.Number.ToString();
- LoadPhotos();
- UpdateScreen();
- }
- #endregion
- #region Screen Update
- private void UpdateScreen()
- {
- jobLbl.Text = requisition.JobLink.JobNumber + " " + requisition.JobLink.Name;
- boxQtyLbl.Text = requisition.Boxes.ToString();
- if (string.IsNullOrEmpty(notesEdt.Text))
- {
- List<string> notes = new List<string>();
- foreach (string s in requisition.Notes)
- {
- string substring = s.Replace("=", string.Empty);
- substring = substring.Trim();
- if (!string.IsNullOrWhiteSpace(substring))
- notes.Add(substring);
- }
- notesEdt.Text = String.Join("\n", notes);
- }
- UpdateColours();
- }
- private void UpdateColours()
- {
- if (requisition.JobLink.ID == Guid.Empty)
- jobFrame.BorderColor = Color.Red;
- else
- jobFrame.BorderColor = Color.Gray;
- if (images.Children.Count == 0)
- photosFrame.BorderColor = Color.Red;
- else
- photosFrame.BorderColor = Color.Gray;
- if (isNewRequest)
- photosFrame.BorderColor = Color.Gray;
- }
- private void LoadPhotos()
- {
- if (requisition.Documents != 0)
- {
- var table = QueryPhotos();
- while (table == null)
- table = QueryPhotos();
- if (table.Rows.Count != 0)
- {
- foreach (var row in table.Rows)
- {
- List<object> list = row.Values;
- if (list[0] == null) { list[0] = Guid.Empty; }
- Guid requisitionDocLinkID = Guid.Parse(list[0].ToString());
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(requisitionDocLinkID),
- null,
- null,
- (t, e) =>
- {
- CoreRow docrow = t.Rows.FirstOrDefault();
- if (docrow != null)
- {
- byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
- ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
- Image 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
- });
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- );
- }
- }
- }
- }
- private CoreTable QueryPhotos()
- {
- try
- {
- return new Client<RequisitionDocument>().Query(
- new Filter<RequisitionDocument>(x => x.EntityLink.ID).IsEqualTo(requisition.ID),
- new Columns<RequisitionDocument>(x => x.DocumentLink.ID),
- null
- );
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Query, "QueryPhotos()", ex.Message + ex.StackTrace, this.GetType().Name);
- return null;
- }
- }
- #endregion
- #region Selection
- private void SelectJobBtn_Clicked(object sender, EventArgs e)
- {
- JobSelectionPage jobSelectionPage = new JobSelectionPage();
- jobSelectionPage.OnItemSelected += (() =>
- {
- requisition.JobLink.ID = jobSelectionPage.Job.ID;
- requisition.JobLink.Name = jobSelectionPage.Job.Name;
- requisition.JobLink.JobNumber = jobSelectionPage.Job.JobNumber;
- UpdateScreen();
- });
- Navigation.PushAsync(jobSelectionPage);
- }
- private void ReduceQtyBtn_Clicked(object sender, EventArgs e)
- {
- if (requisition.Boxes <= 1)
- return;
- else
- {
- requisition.Boxes--;
- UpdateScreen();
- }
- }
- private void IncreaseQtyBtn_Clicked(object sender, EventArgs e)
- {
- requisition.Boxes++;
- UpdateScreen();
- }
- private void DueDatePckr_DateSelected(object sender, EventArgs e)
- {
- requisition.Due = dueDatePckr.Date;
- }
- #endregion
- #region Photos
- private void TakePhoto_Clicked(object sender, EventArgs e)
- {
- TakeAPhoto();
- }
- private void ChooseImage_Clicked(object sender, EventArgs e)
- {
- ChooseAPhoto();
- }
- private async void TakeAPhoto()
- {
- 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
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- private async void ChooseAPhoto()
- {
- 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
- });
- imagesDocuments.Add(img, doc);
- file.Dispose();
- if (img != null)
- {
- Device.BeginInvokeOnMainThread(() =>
- {
- ImageScroller.IsVisible = true;
- images.Children.Add(img);
- UpdateColours();
- });
- }
- }
- }
- private void OnTap(object obj)
- {
- ImageViewer viewer = new ImageViewer(obj as ImageSource);
- Navigation.PushAsync(viewer);
- viewer.ChooseDelete();
- viewer.OnDeleteSelected += () =>
- {
- Image img = imagesDocuments.Keys.First(x => x.Source.Equals(obj as ImageSource));
- imagesDocuments.Remove(img);
- Device.BeginInvokeOnMainThread(() =>
- {
- images.Children.Clear();
- if (imagesDocuments.Count > 0)
- {
- foreach (Image image in imagesDocuments.Keys)
- {
- images.Children.Add(image);
- }
- }
- UpdateColours();
- });
- };
- }
- #endregion
- #region Save
- private async void SaveBtn_Clicked(object sender, EventArgs e)
- {
- if (CheckSaveOk())
- {
- if (isNewRequest)
- {
- requisition.Request = notesEdt.Text;
- DoSaveRequi(requisition);
- DisplayAlert("Success", "Requisition " + requisition.Number + " Created", "Ok");
- }
- else
- {
- if (takenNowRb.IsChecked)
- {
- requisition.TakenBy.ID = GlobalVariables.EmpID;
- requisition.TakenBy.Name = GlobalVariables.EmpName;
- }
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- SaveRequi();
- }
- if (requisition.ID != Guid.Empty)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving"))
- {
- SavePhotos();
- SaveItems();
- }
- await DisplayAlert("Success", "Requisition " + requisition.Number + " Saved", "Ok");
- OnSaveSelected?.Invoke();
- Navigation.PopAsync();
- }
- }
- }
- }
- private Requisition DoSaveRequi(Requisition requisition)
- {
- try
- {
- new Client<Requisition>().Save(requisition, "Created on Mobile Device");
- return requisition;
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "DoSaveRequi()", ex.Message + ex.StackTrace, this.GetType().Name);
- return DoSaveRequi(requisition);
- }
- }
- private void SaveRequi()
- {
- requisition.Filled = DateTime.Now;
- requisition.Notes = new string[] { notesEdt.Text };
- DoSaveRequi(requisition);
- }
- private void SaveItems()
- {
- Task.Run(() =>
- {
- List<RequisitionItem> toDelete = new List<RequisitionItem>();
- if (oldrequiitems.Count > 0)
- {
- foreach (StoreRequiItemShell oldshell in oldrequiitems)
- {
- StoreRequiItemShell shell = requiItems.Find(x => x.ID.Equals(oldshell.ID));
- if (shell == null)
- {
- toDelete.Add(new RequisitionItem { ID = oldshell.ID });
- }
- }
- if (toDelete.Count > 0)
- {
- DoDeleteRequiItems(toDelete);
- }
- }
- });
- List<RequisitionItem> toSave = new List<RequisitionItem>();
- foreach (StoreRequiItemShell requiItemShell in requiItems)
- {
- RequisitionItem item = new RequisitionItem();
- item.RequisitionLink.ID = requisition.ID;
- item.ID = requiItemShell.ID;
- item.Product.ID = requiItemShell.ProductID;
- item.Product.Code = requiItemShell.ProductCode;
- item.Product.Name = requiItemShell.ProductName;
- item.Location.ID = requiItemShell.LocationID;
- item.Quantity = requiItemShell.Quantity;
- item.Description = requiItemShell.ProductName;
- item.Code = requiItemShell.ProductCode;
- item.Job.ID = requiItemShell.JobID;
- item.Style.ID = requiItemShell.StyleID;
- item.Picked = item.Picked == DateTime.MinValue ? DateTime.Now : item.Picked;
- item.Dimensions.Unit.ID = requiItemShell.Dimensions.Unit.ID;
- item.Dimensions.Unit.HasQuantity = requiItemShell.Dimensions.Unit.HasQuantity;
- item.Dimensions.Unit.HasLength = requiItemShell.Dimensions.Unit.HasLength;
- item.Dimensions.Unit.HasHeight = requiItemShell.Dimensions.Unit.HasHeight;
- item.Dimensions.Unit.HasWeight = requiItemShell.Dimensions.Unit.HasWeight;
- item.Dimensions.Unit.HasWidth = requiItemShell.Dimensions.Unit.HasWidth;
- item.Dimensions.Quantity = requiItemShell.Dimensions.Quantity;
- item.Dimensions.Length = requiItemShell.Dimensions.Length;
- item.Dimensions.Height = requiItemShell.Dimensions.Height;
- item.Dimensions.Weight = requiItemShell.Dimensions.Weight;
- item.Dimensions.Width = requiItemShell.Dimensions.Width;
- item.Dimensions.Unit.Format = requiItemShell.Dimensions.Unit.Format;
- item.Dimensions.Unit.Formula = requiItemShell.Dimensions.Unit.Formula;
- item.Dimensions.UnitSize = requiItemShell.Dimensions.UnitSize;
- toSave.Add(item);
- }
- DoSaveRequiItems(toSave);
- }
- private void DoDeleteRequiItems(List<RequisitionItem> toDelete)
- {
- try
- {
- new Client<RequisitionItem>().Delete(toDelete, "Updated from mobile device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Delete, "DoDeleteRequiItems()", ex.Message + ex.StackTrace, this.GetType().Name);
- DoDeleteRequiItems(toDelete);
- }
- }
- private void DoSaveRequiItems(List<RequisitionItem> toSave)
- {
- try
- {
- new Client<RequisitionItem>().Save(toSave, "Saved requi on mobile device");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "DoSaveRequiItems", ex.Message + ex.StackTrace, this.GetType().Name);
- DoSaveRequiItems(toSave);
- }
- }
- private void SavePhotos()
- {
- Task.Run(() =>
- {
- if (imagesDocuments.Count != 0)
- {
- DoSaveDocuments();
- List<RequisitionDocument> newRequiDocuments = new List<RequisitionDocument>();
- foreach (Document doc in imagesDocuments.Values)
- {
- var requiDocument = new RequisitionDocument();
- requiDocument.EntityLink.ID = requisition.ID;
- requiDocument.DocumentLink.ID = doc.ID;
- requiDocument.DocumentLink.FileName = doc.FileName;
- newRequiDocuments.Add(requiDocument);
- }
- DoSaveRequiDocuments(newRequiDocuments);
- }
- });
- }
- private void DoSaveDocuments()
- {
- try
- {
- new Client<Document>().Save(imagesDocuments.Values, "Photo Taken on Mobile device Requis module");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "DoSaveDocuments()", ex.Message + ex.StackTrace, this.GetType().Name);
- DoSaveDocuments();
- }
- }
- private void DoSaveRequiDocuments(List<RequisitionDocument> newRequiDocuments)
- {
- try
- {
- new Client<RequisitionDocument>().Save(newRequiDocuments, "Photo Taken on Mobile device Requis module");
- }
- catch (Exception ex)
- {
- var log = new MobileLogging(LogType.Save, "DoSaveRequiDocuments", ex.Message + ex.StackTrace, this.GetType().Name);
- DoSaveRequiDocuments(newRequiDocuments);
- }
- }
- private bool CheckSaveOk()
- {
- bool bOK = false;
- if (requisition.JobLink.ID != Guid.Empty)
- {
- bOK = true;
- }
- else
- {
- DisplayAlert("Unable to save", "Add a Job", "OK");
- return bOK;
- }
- if (images.Children.Count > 0)
- {
- bOK = true;
- }
- else if (isNewRequest)
- {
- bOK = true;
- }
- else
- {
- bOK = false;
- DisplayAlert("Unable to save", "Add a Photo", "OK");
- return bOK;
- }
- return bOK;
- }
- #endregion
- }
- }
|