DeliveryDetails.xaml.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Threading;
  5. using System.Linq;
  6. using Xamarin.Forms;
  7. using XF.Material.Forms.UI;
  8. using XF.Material.Forms.UI.Dialogs;
  9. using InABox.Core;
  10. using InABox.Clients;
  11. using Comal.Classes;
  12. using System.IO;
  13. using Plugin.Media;
  14. using InABox.Mobile;
  15. using System.Linq.Expressions;
  16. using Xamarin.Essentials;
  17. using Contact = Comal.Classes.Contact;
  18. using comal.timesheets.CustomControls;
  19. using comal.timesheets.Deliveries;
  20. namespace comal.timesheets
  21. {
  22. public delegate void DeliverySaved();
  23. public partial class DeliveryDetails
  24. {
  25. public Guid DeliveryID { get; set; }
  26. private Delivery _delivery = null;
  27. public event DeliverySaved OnDeliverySaved;
  28. private string address = "";
  29. public IEnumerable<Placemark> addresses;
  30. ToolbarItem CompleteDeliveryButton;
  31. ToolbarItem CreateDeliveryButton;
  32. private SelectionPage _selectionpage = null;
  33. ObservableList<Docket> dockets = new ObservableList<Docket>();
  34. private List<ImageSource> images = new List<ImageSource>();
  35. ObservableList<Document> newPhotoDocuments = new ObservableList<Document>();
  36. List<Guid> existingDocumentIDs = new List<Guid>();
  37. List<Tuple<ActionButton, Guid, String, List<DeliveryItem>>> requis = new List<Tuple<ActionButton, Guid, String, List<DeliveryItem>>>();
  38. List<Tuple<ActionButton, Guid, String, List<DeliveryItem>>> shipments = new List<Tuple<ActionButton, Guid, String, List<DeliveryItem>>>();
  39. public DeliveryDetails()
  40. {
  41. InitializeComponent();
  42. CompleteDeliveryButton = new ToolbarItem()
  43. {
  44. Text = "Record 'Delivered' Flag",
  45. Command = new DropDownMenuCommand(CompleteDelivery),
  46. Order = ToolbarItemOrder.Primary
  47. };
  48. ToolbarItems.Add(CompleteDeliveryButton);
  49. CreateDeliveryButton = new ToolbarItem()
  50. {
  51. Text = "Create Delivery/Pickup",
  52. Command = new Command(CreateDelivery)
  53. };
  54. ToolbarItems.Add(CreateDeliveryButton);
  55. }
  56. async void LoadData()
  57. {
  58. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading Delivery"))
  59. {
  60. if (DeliveryID != Guid.Empty)
  61. {
  62. var deltask = Task.Run(() =>
  63. {
  64. return _delivery = new Client<Delivery>().Query(
  65. new Filter<Delivery>(x => x.ID).IsEqualTo(DeliveryID)
  66. ).Rows
  67. .FirstOrDefault()?
  68. .ToObject<Delivery>();
  69. });
  70. var dkttask = Task.Run(() =>
  71. {
  72. var table = new Client<DeliveryItem>().Query(
  73. new Filter<DeliveryItem>(x => x.Delivery.ID).IsEqualTo(DeliveryID),
  74. new Columns<DeliveryItem>(
  75. X => X.ManufacturingPacketLink.SetoutLink.Number,
  76. X => X.ShipmentCode,
  77. X => X.Description,
  78. X => X.RequisitionLink.Number
  79. ),
  80. new SortOrder<DeliveryItem>(x => x.ShipmentCode)
  81. );
  82. foreach (var row in table.Rows)
  83. {
  84. Docket docket = new Docket();
  85. docket.Setout =
  86. $"Setout # {row.Get<DeliveryItem, String>(x => x.ManufacturingPacketLink.SetoutLink.Number)}";
  87. docket.Rack = $"Rack # {row.Get<DeliveryItem, String>(x => x.ShipmentCode)}";
  88. docket.Description = row.Get<DeliveryItem, String>(x => x.Description);
  89. docket.Requi = $"Requi # {row.Get<DeliveryItem, int>(x => x.RequisitionLink.Number)}";
  90. dockets.Add(docket);
  91. }
  92. });
  93. Task.WaitAll(new Task[] { deltask, dkttask });
  94. new Client<Document>().Query(
  95. new Filter<Document>(x => x.ID).InQuery(
  96. new Filter<DeliveryDocument>(x => x.EntityLink.ID).IsEqualTo(DeliveryID),
  97. x => x.DocumentLink.ID),
  98. null,
  99. null,
  100. (o,e) => Device.BeginInvokeOnMainThread(() => UpdateImages(o))
  101. );
  102. }
  103. if (_delivery == null)
  104. {
  105. _delivery = new Delivery();
  106. _delivery.Employee.ID = App.Data.Me.ID;
  107. _delivery.Employee.Name = App.Data.Me.Name;
  108. }
  109. }
  110. }
  111. private void ImageTapped(object sender, EventArgs e)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. private void UpdateImages(CoreTable table)
  116. {
  117. if (table == null)
  118. return;
  119. foreach (var docrow in table.Rows)
  120. {
  121. byte[] data = docrow.Get<Document, byte[]>(x => x.Data);
  122. ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  123. images.Add(src);
  124. }
  125. Device.BeginInvokeOnMainThread(RefreshPage);
  126. }
  127. private void RefreshPage()
  128. {
  129. Title = _delivery.ID != Guid.Empty
  130. ? _delivery.Number.ToString()
  131. : "New Delivery";
  132. JobName.Text = (!string.IsNullOrWhiteSpace(_delivery.Job.Name))
  133. ? _delivery.Job.Name + " (" + _delivery.Job.JobNumber + ")"
  134. : "";
  135. TypeLbl.Text = _delivery.Type.Description;
  136. ContactNameLbl.Text = _delivery.Contact.Name;
  137. streetEdt.Text = _delivery.Address.Street;
  138. cityEdt.Text = _delivery.Address.City;
  139. statePicker.SelectedIndex = SelectState();
  140. postCodeEdt.Text = _delivery.Address.PostCode;
  141. EmployeeName.Text = _delivery.Employee.Name;
  142. deliveryNotesEdt.Text = _delivery.Notes;
  143. dueDate.Date = !_delivery.Due.IsEmpty()
  144. ? _delivery.Due
  145. : DateTime.Today;
  146. convertToTaskBtn.Text = _delivery.KanbanNumber != 0
  147. ? "Saved To Task " + _delivery.KanbanNumber
  148. : "Create Task";
  149. convertToTaskBtn.IsEnabled = (_delivery.ID != Guid.Empty) && (_delivery.KanbanNumber == 0);
  150. convertToTaskBtn.IsEnabled = _delivery.ID != Guid.Empty;
  151. ItemsToBeDelivered.IsVisible = _delivery.ID != Guid.Empty;
  152. CompleteDeliveryButton.IsEnabled = !_delivery.Delivered.IsEmpty() && (_delivery.ID != Guid.Empty);
  153. ItemsFrame.IsVisible = _delivery.ID != Guid.Empty;
  154. loadDeliveryItems.IsVisible = _delivery.ID != Guid.Empty;
  155. streetEdt.Text = _delivery.Contact.Address.Street;
  156. cityEdt.Text = _delivery.Contact.Address.City;
  157. postCodeEdt.Text = _delivery.Contact.Address.PostCode;
  158. ContactNameLbl.Text = _delivery.Contact.Name;
  159. EmployeeName.Text = _delivery.Employee.Name;
  160. if (_delivery.Location.Longitude != 0 && _delivery.Location.Latitude != 0)
  161. {
  162. DeliveredFlagFrame.IsVisible = true;
  163. DeliveredLbl.IsVisible = true;
  164. DeliveredDetails.Text = "Loading Delivered Flag Details";
  165. if (string.IsNullOrWhiteSpace(_delivery.Location.Address))
  166. {
  167. GetLocationFromDetails();
  168. }
  169. else
  170. ShowDeliveredDetails(false);
  171. }
  172. loadDeliveryItems.Text = dockets.Any()
  173. ? $"View {dockets.Count} items"
  174. : "No Items Found";
  175. loadDeliveryItems.IsEnabled = dockets.Any();
  176. Images.IsVisible = images.Count > 0;
  177. photosLbl.Text = $"{images.Count} Photo(s)";
  178. }
  179. protected override async void OnAppearing()
  180. {
  181. if (_delivery == null)
  182. LoadData();
  183. _selectionpage = null;
  184. Device.BeginInvokeOnMainThread(RefreshPage);
  185. base.OnAppearing();
  186. }
  187. protected async override void OnDisappearing()
  188. {
  189. await Task.Run(() =>
  190. {
  191. if (_delivery.ID != Guid.Empty && (_selectionpage == null))
  192. {
  193. try
  194. {
  195. new Client<Delivery>().Save(_delivery, "Updated from mobile device");
  196. }
  197. catch (Exception ex)
  198. {
  199. InABox.Mobile.MobileLogging.Log(ex);
  200. }
  201. }
  202. });
  203. base.OnDisappearing();
  204. }
  205. #region Create Delivery or add delivered flag
  206. async void CreateDelivery()
  207. {
  208. try
  209. {
  210. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Saving Delivery"))
  211. {
  212. _delivery.Due = dueDate.Date; //unable to make duedate changed event without crashing the screen on startup....
  213. new Client<Delivery>().Save(_delivery, "Updated From Mobile Device");
  214. if (newPhotoDocuments.Count != 0)
  215. {
  216. new Client<Document>().Save(newPhotoDocuments, "Photo Taken on Device");
  217. ObservableList<DeliveryDocument> newDeliveryDocuments = new ObservableList<DeliveryDocument>();
  218. foreach (Document doc in newPhotoDocuments)
  219. {
  220. var deliveryDocument = new DeliveryDocument();
  221. deliveryDocument.EntityLink.ID = _delivery.ID;
  222. deliveryDocument.DocumentLink.ID = doc.ID;
  223. deliveryDocument.DocumentLink.FileName = doc.FileName;
  224. newDeliveryDocuments.Add(deliveryDocument);
  225. }
  226. new Client<DeliveryDocument>().Save(newDeliveryDocuments, "Photo Taken on Device");
  227. newPhotoDocuments.Clear();
  228. }
  229. }
  230. string successMessage = "Delivery Created" + System.Environment.NewLine + "Delivery Number: " + _delivery.Number;
  231. }
  232. catch (Exception ex)
  233. {
  234. DisplayAlert("Error creating delivery", ex.Message, "OK");
  235. }
  236. OnDeliverySaved?.Invoke();
  237. await Navigation.PopAsync();
  238. }
  239. private async void CompleteDelivery()
  240. {
  241. try
  242. {
  243. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Getting Location..Please wait"))
  244. {
  245. LocationServices locationServices = new LocationServices();
  246. locationServices.OnLocationFound += LocationFound;
  247. locationServices.OnLocationError += LocationError;
  248. locationServices.GetLocation();
  249. Thread.Sleep(1000); //"Getting Location" notification disappears too quickly on some devices - ensures a minimum of 1 second while location tasks execute on other threads
  250. }
  251. }
  252. catch { }
  253. }
  254. private async void LocationFound(LocationServices sender)
  255. {
  256. try
  257. {
  258. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Getting Location..Please wait"))
  259. {
  260. _delivery.Location.Latitude = sender.Latitude;
  261. _delivery.Location.Longitude = sender.Longitude;
  262. _delivery.Location.Timestamp = DateTime.Now;
  263. _delivery.Location.Address = sender.Address;
  264. _delivery.DeliveredBy.ID = App.Data.Me.ID;
  265. _delivery.DeliveredBy.Name = App.Data.Me.Name;
  266. address = sender.Address;
  267. _delivery.Delivered = DateTime.Now;
  268. await Task.Run(() =>
  269. {
  270. new Client<Delivery>().Save(_delivery, "Updated From Mobile Device");
  271. });
  272. }
  273. await DisplayAlert("Success", "Delivered: " + _delivery.Delivered.ToString("hh:mm:ss tt dd-MMM-yy")
  274. + System.Environment.NewLine + "Location Now: " + address
  275. , "OK");
  276. ShowDeliveredDetails();
  277. }
  278. catch { }
  279. }
  280. private void LocationError(LocationServices sender, Exception error)
  281. {
  282. DisplayAlert("GPS Error", error.Message, "OK");
  283. }
  284. #endregion
  285. #region Load Screen values from Database
  286. public IEnumerable<Placemark> Addresses
  287. {
  288. get { return addresses; }
  289. set { addresses = value; ShowDeliveredDetails(); }
  290. }
  291. private void ShowDeliveredDetails(bool wait = true)
  292. {
  293. Task.Run(() =>
  294. {
  295. if (wait)
  296. {
  297. Thread.Sleep(5000);
  298. }
  299. Device.BeginInvokeOnMainThread(() =>
  300. {
  301. CompleteDeliveryButton.IsEnabled = false;
  302. DeliveredFlagFrame.IsVisible = true;
  303. DeliveredLbl.IsVisible = true;
  304. if (string.IsNullOrWhiteSpace(address))
  305. {
  306. if (!string.IsNullOrWhiteSpace(_delivery.Location.Address))
  307. address = _delivery.Location.Address;
  308. else
  309. address = "(Address service failed). Driver was at Latitude: " + _delivery.Location.Latitude + " Longitude: " + _delivery.Location.Longitude;
  310. }
  311. DeliveredDetails.Text = address + " at " + _delivery.Delivered.ToString("hh:mm:ss tt dd MMM yy") + System.Environment.NewLine
  312. + "by: " + _delivery.DeliveredBy.Name;
  313. openMapBtn.IsEnabled = true;
  314. });
  315. });
  316. }
  317. private async void GetLocationFromDetails()
  318. {
  319. await Task.Run(async () =>
  320. {
  321. try
  322. {
  323. Addresses = await Geocoding.GetPlacemarksAsync(_delivery.Location.Latitude, _delivery.Location.Longitude);
  324. Placemark placemark = null;
  325. foreach (var cur in Addresses)
  326. {
  327. placemark = cur;
  328. if (placemark != null)
  329. {
  330. address = String.Format("{0} {1} {2}", placemark.SubThoroughfare, placemark.Thoroughfare, placemark.Locality);
  331. _delivery.Location.Address = address;
  332. return;
  333. }
  334. }
  335. }
  336. catch
  337. {
  338. address = "Longitude: " + _delivery.Location.Longitude + "Latitude: " + _delivery.Location.Latitude + " Address service failed to load address";
  339. }
  340. });
  341. }
  342. private async void OpenMapBtn_Clicked(object sender, EventArgs e)
  343. {
  344. var location = new Xamarin.Essentials.Location(_delivery.Location.Latitude, _delivery.Location.Longitude);
  345. var options = new MapLaunchOptions { };
  346. await Map.OpenAsync(location, options);
  347. }
  348. #endregion
  349. #region Values Changed Events
  350. private async void SelectType_Clicked(object sender, EventArgs e)
  351. {
  352. try
  353. {
  354. Dictionary<string, Guid> deliveryTypePairs = new Dictionary<string, Guid>();
  355. List<string> _deliveryTypes = new List<string>();
  356. var table = new Client<DeliveryType>().Query(new Filter<DeliveryType>(x => x.Description).IsNotEqualTo(string.Empty));
  357. foreach (CoreRow row in table.Rows)
  358. {
  359. DeliveryType _deliveryType = row.ToObject<DeliveryType>();
  360. _deliveryTypes.Add(_deliveryType.Description);
  361. deliveryTypePairs.Add(_deliveryType.Description, _deliveryType.ID);
  362. }
  363. if (_deliveryTypes.Count != 0)
  364. {
  365. string[] array = _deliveryTypes.ToArray();
  366. string chosenOption = await DisplayActionSheet("Choose Delivery Type", "Cancel", null, array);
  367. if (!chosenOption.Equals("Cancel"))
  368. {
  369. _delivery.Type.Description = chosenOption;
  370. TypeLbl.Text = _delivery.Type.Description;
  371. _delivery.Type.ID = deliveryTypePairs[chosenOption];
  372. }
  373. }
  374. }
  375. catch { }
  376. }
  377. private void SelectJob_Clicked(object sender, EventArgs e)
  378. {
  379. _selectionpage = new JobSelectionPage(
  380. (job) =>
  381. {
  382. _delivery.Job.ID = job.ID;
  383. _delivery.Job.Name = job.Name;
  384. _delivery.Job.JobNumber = job.JobNumber;
  385. JobName.Text = _delivery.Job.Name + " (" + _delivery.Job.JobNumber + ")";
  386. }
  387. );
  388. Navigation.PushAsync(_selectionpage);
  389. }
  390. #region Address
  391. private void StreetEdt_Changed(object sender, EventArgs e)
  392. {
  393. _delivery.Address.Street = streetEdt.Text;
  394. }
  395. private void CityEdt_Changed(object sender, EventArgs e)
  396. {
  397. _delivery.Address.City = cityEdt.Text;
  398. }
  399. private void StatePicker_Changed(object sender, EventArgs e)
  400. {
  401. _delivery.Address.State = statePicker.SelectedItem.ToString();
  402. }
  403. private void PostCode_Changed(object sender, EventArgs e)
  404. {
  405. _delivery.Address.PostCode = postCodeEdt.Text;
  406. }
  407. private int SelectState()
  408. {
  409. int state = 0;
  410. switch (_delivery.Address.State)
  411. {
  412. case "Western Australia":
  413. state = 0;
  414. break;
  415. case "Victoria":
  416. state = 1;
  417. break;
  418. case "South Australia":
  419. state = 2;
  420. break;
  421. case "New South Wales":
  422. state = 3;
  423. break;
  424. case "Queensland":
  425. state = 4;
  426. break;
  427. case "Tasmania":
  428. state = 5;
  429. break;
  430. default:
  431. state = 0;
  432. break;
  433. }
  434. return state;
  435. }
  436. #endregion Address
  437. private async void SelectContact_Clicked(object sender, EventArgs e)
  438. {
  439. _selectionpage = new ContactSelectionPage(
  440. (contact) =>
  441. {
  442. if (contact != null)
  443. {
  444. _delivery.Contact.Address.Street = contact.Street;
  445. _delivery.Contact.Address.City = contact.City;
  446. _delivery.Contact.Address.PostCode = contact.PostCode;
  447. _delivery.Contact.Address.State = contact.State;
  448. }
  449. }
  450. );
  451. Navigation.PushAsync(_selectionpage);
  452. }
  453. private void SelectCustomerPage()
  454. {
  455. //if (_searching)
  456. // return;
  457. //else
  458. //{
  459. // _searching = true;
  460. // GenericSelectionPage page = new GenericSelectionPage
  461. // (
  462. // "Select Customer",
  463. // new SelectionViewModel<CustomerContact>
  464. // (
  465. // new Filter<CustomerContact>(x => x.Contact.ID).IsNotEqualTo(Guid.Empty).And(x => x.Contact.Address.Street).IsNotEqualTo(string.Empty),
  466. // new Expression<Func<CustomerContact, object>>[] { x => x.Contact.Name, x => x.Contact.Address.Street, x => x.Contact.Address.City },
  467. // new Expression<Func<CustomerContact, object>>[] { x => x.Contact.Address.PostCode, x => x.Contact.ID, x => x.Contact },
  468. // new SortOrder<CustomerContact>(x => x.Contact.Name)
  469. // ));
  470. // page.OnItemSelected += (row) =>
  471. // {
  472. // var customerContact = row.ToObject<CustomerContact>();
  473. // _delivery.Contact.Address.Street = customerContact.Contact.Address.Street;
  474. // _delivery.Contact.Address.City = customerContact.Contact.Address.City;
  475. // _delivery.Contact.Address.PostCode = customerContact.Contact.Address.PostCode;
  476. // _delivery.Contact.Address.State = customerContact.Contact.Address.State;
  477. // Device.BeginInvokeOnMainThread(() =>
  478. // {
  479. // streetEdt.Text = _delivery.Contact.Address.Street;
  480. // cityEdt.Text = _delivery.Contact.Address.City;
  481. // postCodeEdt.Text = _delivery.Contact.Address.PostCode;
  482. // ContactNameLbl.Text = _delivery.Contact.Name;
  483. // });
  484. // };
  485. // Navigation.PushAsync(page);
  486. //}
  487. }
  488. private void SelectSupplierPage()
  489. {
  490. //if (_searching)
  491. // return;
  492. //else
  493. //{
  494. // _searching = true;
  495. // GenericSelectionPage page = new GenericSelectionPage
  496. // (
  497. // "Select Supplier",
  498. // new SelectionViewModel<SupplierContact>
  499. // (
  500. // new Filter<SupplierContact>(x => x.Contact.ID).IsNotEqualTo(Guid.Empty).And(x =>x.Contact.Address.Street).IsNotEqualTo(string.Empty),
  501. // new Expression<Func<SupplierContact, object>>[] { x => x.Contact.Name, x => x.Contact.Address.Street, x => x.Contact.Address.City },
  502. // new Expression<Func<SupplierContact, object>>[] { x => x.Contact.Address.PostCode, x => x.Contact.ID, x => x.Contact },
  503. // new SortOrder<SupplierContact>(x => x.Contact.Name)
  504. // ));
  505. // page.OnItemSelected += (row) =>
  506. // {
  507. // SupplierContact supplierContact = row.ToObject<SupplierContact>();
  508. // _delivery.Contact.ID = supplierContact.Contact.ID;
  509. // _delivery.Contact.Address.Street = supplierContact.Contact.Address.Street;
  510. // _delivery.Contact.Address.City = supplierContact.Contact.Address.City;
  511. // _delivery.Contact.Address.PostCode = supplierContact.Contact.Address.PostCode;
  512. // _delivery.Contact.Address.State = supplierContact.Contact.Address.State;
  513. // Device.BeginInvokeOnMainThread(() =>
  514. // {
  515. // streetEdt.Text = _delivery.Contact.Address.Street;
  516. // cityEdt.Text = _delivery.Contact.Address.City;
  517. // postCodeEdt.Text = _delivery.Contact.Address.PostCode;
  518. // ContactNameLbl.Text = _delivery.Contact.Name;
  519. // });
  520. // };
  521. // Navigation.PushAsync(page);
  522. //}
  523. }
  524. private void SelectOtherPage()
  525. {
  526. //if (_searching)
  527. // return;
  528. //else
  529. //{
  530. // _searching = true;
  531. // GenericSelectionPage page = new GenericSelectionPage
  532. // (
  533. // "Select",
  534. // new SelectionViewModel<Comal.Classes.Contact>
  535. // (
  536. // new Filter<Comal.Classes.Contact>(x => x.Name).IsNotEqualTo(String.Empty).And(x => x.Address.Street).IsNotEqualTo(string.Empty),
  537. // new Expression<Func<Comal.Classes.Contact, object>>[] { x => x.Name, x => x.Address.Street, x => x.Address.City },
  538. // new Expression<Func<Comal.Classes.Contact, object>>[] { },
  539. // new SortOrder<Comal.Classes.Contact>(x => x.Name)
  540. // ));
  541. // page.OnItemSelected += (row) =>
  542. // {
  543. // Comal.Classes.Contact contact = row.ToObject<Comal.Classes.Contact>();
  544. // _delivery.Contact.ID = contact.ID;
  545. // _delivery.Contact.Address.Street = contact.Address.Street;
  546. // _delivery.Contact.Address.City = contact.Address.City;
  547. // _delivery.Contact.Address.PostCode = contact.Address.PostCode;
  548. // _delivery.Contact.Address.State = contact.Address.State;
  549. // Device.BeginInvokeOnMainThread(() =>
  550. // {
  551. // streetEdt.Text = _delivery.Contact.Address.Street;
  552. // cityEdt.Text = _delivery.Contact.Address.City;
  553. // postCodeEdt.Text = _delivery.Contact.Address.PostCode;
  554. // ContactNameLbl.Text = _delivery.Contact.Name;
  555. // });
  556. // };
  557. // Navigation.PushAsync(page);
  558. //}
  559. }
  560. private void SelectEmployee_Clicked(object sender, EventArgs e)
  561. {
  562. _selectionpage = new EmployeeSelectionPage(
  563. (employee) =>
  564. {
  565. if (employee != null)
  566. {
  567. _delivery.Employee.ID = employee.ID;
  568. _delivery.Employee.Name = employee.Name;
  569. }
  570. }
  571. );
  572. Navigation.PushAsync(_selectionpage);
  573. }
  574. private void DeliveryNotes_Completed(object sender, EventArgs e)
  575. {
  576. _delivery.Notes = deliveryNotesEdt.Text;
  577. }
  578. #endregion
  579. #region Photos
  580. async void ChooseImage_Clicked(System.Object sender, System.EventArgs e)
  581. {
  582. // try
  583. // {
  584. // //openingAnotherPage = true;
  585. // await CrossMedia.Current.Initialize();
  586. //
  587. // if (!CrossMedia.Current.IsPickPhotoSupported)
  588. // {
  589. // await DisplayAlert("No Library", ":( No Photo Library available.", "OK");
  590. // //openingAnotherPage = false;
  591. // return;
  592. // }
  593. //
  594. // var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions()
  595. // {
  596. // CompressionQuality = 10,
  597. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  598. // });
  599. //
  600. // if (file == null)
  601. // {
  602. // //openingAnotherPage = false;
  603. // return;
  604. // }
  605. //
  606. // Image img = null;
  607. // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  608. // {
  609. // var memoryStream = new MemoryStream();
  610. // file.GetStream().CopyTo(memoryStream);
  611. // var data = memoryStream.ToArray();
  612. //
  613. // Document doc = new Document()
  614. // {
  615. // FileName = Path.GetFileName(file.Path),
  616. // Data = data,
  617. // CRC = CoreUtils.CalculateCRC(data),
  618. // TimeStamp = DateTime.Now
  619. // };
  620. //
  621. // SavePhoto(doc);
  622. //
  623. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  624. // img = new Image();
  625. // img.HeightRequest = 150;
  626. // img.WidthRequest = 150;
  627. // img.Aspect = Aspect.AspectFit;
  628. // img.Source = src;
  629. // img.GestureRecognizers.Add(new TapGestureRecognizer
  630. // {
  631. // Command = new Command(OnTap),
  632. // CommandParameter = src,
  633. // NumberOfTapsRequired = 1
  634. // });
  635. // file.Dispose();
  636. // }
  637. // if (img != null)
  638. // {
  639. //
  640. // {
  641. // //Images.IsVisible = true;
  642. // //images.Children.Add(img);
  643. // //openingAnotherPage = false;
  644. // });
  645. // //await pageScroller.ScrollToAsync(ImageScroller, ScrollToPosition.Center, false);
  646. // }
  647. // }
  648. // catch { }
  649. }
  650. async void SavePhoto(Document doc)
  651. {
  652. try
  653. {
  654. await Task.Run(() =>
  655. {
  656. if (_delivery.ID != Guid.Empty)
  657. {
  658. new Client<Document>().Save(doc, "Photo Taken on Device");
  659. var deliveryDocument = new DeliveryDocument();
  660. deliveryDocument.EntityLink.ID = _delivery.ID;
  661. deliveryDocument.DocumentLink.ID = doc.ID;
  662. deliveryDocument.DocumentLink.FileName = doc.FileName;
  663. new Client<DeliveryDocument>().Save(deliveryDocument, "Photo Taken on Device");
  664. existingDocumentIDs.Add(doc.ID);
  665. }
  666. else
  667. {
  668. newPhotoDocuments.Add(doc);
  669. }
  670. });
  671. }
  672. catch { }
  673. }
  674. async void TakePhoto_Clicked(System.Object sender, System.EventArgs e)
  675. {
  676. // try
  677. // {
  678. // openingAnotherPage = true;
  679. // await CrossMedia.Current.Initialize();
  680. //
  681. // if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
  682. // {
  683. // await DisplayAlert("No Camera", ":( No camera available.", "OK");
  684. // openingAnotherPage = false;
  685. // return;
  686. // }
  687. //
  688. // String filename = String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}.png", DateTime.Now);
  689. //
  690. // var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
  691. // {
  692. // Name = filename,
  693. // CompressionQuality = 10,
  694. // PhotoSize = Plugin.Media.Abstractions.PhotoSize.Full
  695. // });
  696. //
  697. // if (file == null)
  698. // {
  699. // openingAnotherPage = false;
  700. // return;
  701. // }
  702. //
  703. // Image img = null;
  704. // using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Adding Photo"))
  705. // {
  706. // var memoryStream = new MemoryStream();
  707. // file.GetStream().CopyTo(memoryStream);
  708. // var data = memoryStream.ToArray();
  709. //
  710. // Document doc = new Document()
  711. // {
  712. // FileName = filename,
  713. // Data = data,
  714. // CRC = CoreUtils.CalculateCRC(data),
  715. // TimeStamp = DateTime.Now
  716. // };
  717. //
  718. // SavePhoto(doc);
  719. //
  720. // ImageSource src = ImageSource.FromStream(() => new MemoryStream(data));
  721. // img = new Image();
  722. // img.HeightRequest = 150;
  723. // img.WidthRequest = 150;
  724. // img.Aspect = Aspect.AspectFit;
  725. // img.Source = src;
  726. // img.GestureRecognizers.Add(new TapGestureRecognizer
  727. // {
  728. // Command = new Command(OnTap),
  729. // CommandParameter = src,
  730. // NumberOfTapsRequired = 1
  731. // });
  732. // file.Dispose();
  733. // }
  734. // if (img != null)
  735. // {
  736. // Device.BeginInvokeOnMainThread(() =>
  737. // {
  738. // ImageScroller.IsVisible = true;
  739. // images.Children.Add(img);
  740. // openingAnotherPage = false;
  741. // });
  742. // await pageScroller.ScrollToAsync(ImageScroller, ScrollToPosition.Center, false);
  743. // }
  744. // }
  745. // catch { }
  746. }
  747. private void OnTap(object obj)
  748. {
  749. ImageViewer viewer = new ImageViewer(obj as ImageSource);
  750. Navigation.PushAsync(viewer);
  751. }
  752. #endregion
  753. private void Docket_Clicked(object sender, EventArgs e)
  754. {
  755. try
  756. {
  757. //openingAnotherPage = true;
  758. var docket = new DeliveryDocket(_delivery.Number, dockets);
  759. Navigation.PushAsync(docket);
  760. }
  761. catch { }
  762. }
  763. #region last row buttons
  764. void ConvertToTaskBtn_Clicked(Object sender, EventArgs e)
  765. {
  766. _selectionpage = new EmployeeSelectionPage(
  767. (employee) =>
  768. {
  769. if (employee != null)
  770. {
  771. Kanban kanban = new Kanban();
  772. kanban.EmployeeLink.ID = employee.ID;
  773. ConfirmPerson(employee.Name, kanban);
  774. }
  775. }
  776. );
  777. Navigation.PushAsync(_selectionpage);
  778. }
  779. async void ConfirmPerson(string name, Kanban kanban)
  780. {
  781. string chosenOption = await DisplayActionSheet("Confirm send to " + name, "Cancel", null, "Yes", "No");
  782. switch (chosenOption)
  783. {
  784. case "Cancel":
  785. break;
  786. case "No":
  787. break;
  788. case "Yes":
  789. CreateKanban(kanban);
  790. break;
  791. default:
  792. break;
  793. }
  794. }
  795. async void CreateKanban(Kanban kanban)
  796. {
  797. try
  798. {
  799. convertToTaskBtn.Text = "Saving..";
  800. convertToTaskBtn.IsEnabled = false;
  801. await Task.Run(() =>
  802. {
  803. kanban.ManagerLink.ID = App.Data.Me.ID;
  804. kanban.Title = "Delivery " + _delivery.Number + System.Environment.NewLine +
  805. "Delivery Type: " + _delivery.Type.Description + System.Environment.NewLine +
  806. App.Data.Me.Name + " converted to Task for further action";
  807. kanban.DueDate = DateTime.Today;
  808. kanban.Category = "Open";
  809. if (!_delivery.Job.ID.Equals(Guid.Empty)) kanban.JobLink.ID = _delivery.Job.ID;
  810. kanban.Description = _delivery.Notes;
  811. new Client<Kanban>().Save(kanban, "Kanban Created from Deliveries Module");
  812. _delivery.KanbanNumber = kanban.Number;
  813. new Client<Delivery>().Save(_delivery, "Updated From Mobile Device");
  814. List<KanbanDocument> kanbanDocuments = new List<KanbanDocument>();
  815. if (existingDocumentIDs.Count != 0)
  816. {
  817. foreach (Guid id in existingDocumentIDs)
  818. {
  819. KanbanDocument kanbanDocument = new KanbanDocument();
  820. kanbanDocument.DocumentLink.ID = id;
  821. kanbanDocument.EntityLink.ID = kanban.ID;
  822. kanbanDocuments.Add(kanbanDocument);
  823. }
  824. }
  825. if (kanbanDocuments.Count != 0)
  826. {
  827. new Client<KanbanDocument>().Save(kanbanDocuments, "Updated from mobile device");
  828. }
  829. List<KanbanSubscriber> subscribers = new List<KanbanSubscriber>();
  830. KanbanSubscriber sub1 = new KanbanSubscriber();
  831. sub1.Kanban.ID = kanban.ID;
  832. sub1.Employee.ID = kanban.EmployeeLink.ID;
  833. sub1.Assignee = true;
  834. subscribers.Add(sub1);
  835. //KanbanSubscriber sub2 = new KanbanSubscriber();
  836. //sub2.Kanban.ID = kanban.ID;
  837. //sub2.Employee.ID = kanban.ManagerLink.ID;
  838. //sub2.Manager = true;
  839. //if (sub2.Employee.ID != sub1.Employee.ID)
  840. // subscribers.Add(sub2);
  841. new Client<KanbanSubscriber>().Save(subscribers, "Updated from mobile device");
  842. Device.BeginInvokeOnMainThread(() =>
  843. {
  844. convertToTaskBtn.Text = "Saved To Task " + kanban.Number;
  845. });
  846. });
  847. //save kanban - return kanban ID
  848. //load list of photos - save as kanbandocs
  849. //save subscribers
  850. }
  851. catch { }
  852. }
  853. #endregion
  854. }
  855. }