DeliveryGrid.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using Syncfusion.SfMaps.XForms;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using Xamarin.Forms;
  11. using XF.Material.Forms.UI.Dialogs;
  12. using PRSSecurity = InABox.Core.Security;
  13. namespace comal.timesheets
  14. {
  15. public class DeliveryGrid : MobileDataGrid
  16. {
  17. Filter<Delivery> filter = new Filter<Delivery>(x => x.Created).IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-120));
  18. public DeliveryGrid(DataGridSaveType saveType = DataGridSaveType.Single)
  19. {
  20. Load(saveType);
  21. OnItemSelected += DeliveryGrid_OnItemSelected;
  22. OnImageSelected += DeliveryGrid_OnImageSelected;
  23. }
  24. private async void Load(DataGridSaveType saveType)
  25. {
  26. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  27. {
  28. CheckFilter();
  29. Setup(LoadList(), typeof(Delivery), saveType);
  30. }
  31. }
  32. private List<DataGridViewModelItem> LoadList()
  33. {
  34. CoreTable table = DoQuery();
  35. if (!table.Rows.Any())
  36. return null;
  37. List<DataGridViewModelItem> items = new List<DataGridViewModelItem>();
  38. foreach (var row in table.Rows)
  39. items.Add(CreateItem(row));
  40. return items;
  41. }
  42. private DataGridViewModelItem CreateItem(CoreRow row)
  43. {
  44. var tuples = GenerateDataTuples(row);
  45. var address = GenerateAddress(row);
  46. return new DataGridViewModelItem(
  47. id: row.Get<Delivery, Guid>(x => x.ID),
  48. data: tuples,
  49. image: CheckAddress(address) ? new Image
  50. {
  51. Source = Device.RuntimePlatform.Equals(Device.iOS) ? "locationmarker" : "locationpointer.png"
  52. }
  53. : new Image(),
  54. imagePopupDetail: address
  55. );
  56. }
  57. private bool CheckAddress(Dictionary<string, string> address)
  58. {
  59. if (address.ContainsKey("Address") || address.ContainsKey("Latitude"))
  60. return true;
  61. return false;
  62. }
  63. private List<Tuple<string, string>> GenerateDataTuples(CoreRow row)
  64. {
  65. string extraTitle = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Contact.Name)) ? " (" + row.Get<Delivery, string>(x => x.Contact.Name) + ")" :
  66. "";
  67. string extraJob = !string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Job.Name)) ? " (" + row.Get<Delivery, string>(x => x.Job.Name) + ")" :
  68. "";
  69. return new List<Tuple<string, string>>
  70. {
  71. new Tuple<string, string>("No.", row.Get<Delivery, int>(x => x.Number).ToString() + extraTitle),
  72. new Tuple<string, string>("Job", row.Get<Delivery, string>(x => x.Job.JobNumber) + extraJob),
  73. new Tuple<string, string>("Del", row.Get<Delivery, DateTime>(x => x.Delivered) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Delivered).ToString("dd MMM yy")),
  74. new Tuple<string, string>("Bkd", row.Get<Delivery, DateTime>(x => x.Assignment.Date) == DateTime.MinValue? " " : row.Get<Delivery, DateTime>(x => x.Assignment.Date).ToString("dd MMM yy"))
  75. };
  76. }
  77. private CoreTable DoQuery()
  78. {
  79. return new Client<Delivery>().Query(
  80. filter,
  81. new Columns<Delivery>(
  82. x => x.ID,
  83. x => x.Job.JobNumber,
  84. x => x.Job.ID,
  85. x => x.Job.Name,
  86. x => x.Number,
  87. x => x.Date,
  88. x => x.Contact.Name,
  89. x => x.Location.Latitude,
  90. x => x.Location.Longitude,
  91. x => x.Assignment.Date,
  92. x => x.DeliveredBy.Name,
  93. x => x.Delivered,
  94. x => x.Due,
  95. x => x.Location.Address,
  96. x => x.Created
  97. ),
  98. new SortOrder<Delivery>(x => x.Created, SortDirection.Descending)
  99. );
  100. }
  101. private Filter<Delivery> CheckFilter()
  102. {
  103. if (GlobalVariables.IsJobOnlyEmployee)
  104. {
  105. List<Guid> jobids = new List<Guid>();
  106. foreach (var job in GlobalVariables.EmployeeJobs)
  107. jobids.Add(job.ID);
  108. filter = new Filter<Delivery>(x => x.Job.ID).InList(jobids);
  109. }
  110. return filter;
  111. }
  112. private Dictionary<string, string> GenerateAddress(CoreRow row)
  113. {
  114. var dict = new Dictionary<string, string>();
  115. if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.Location.Address)))
  116. {
  117. dict.Add("Address", row.Get<Delivery, string>(x => x.Location.Address));
  118. }
  119. double lat = row.Get<Delivery, double>(x => x.Location.Latitude);
  120. double longitude = row.Get<Delivery, double>(x => x.Location.Longitude);
  121. if (lat != 0 && longitude != 0)
  122. {
  123. dict.Add("Latitude", lat.ToString());
  124. dict.Add("Longitude", longitude.ToString());
  125. }
  126. if (row.Get<Delivery, DateTime>(x => x.Delivered) != DateTime.MinValue)
  127. {
  128. dict.Add("Delivered", row.Get<Delivery, DateTime>(x => x.Delivered).ToString("hh:mm tt dd MMM yy"));
  129. }
  130. if (!string.IsNullOrWhiteSpace(row.Get<Delivery, string>(x => x.DeliveredBy.Name)))
  131. {
  132. dict.Add("Delivered by", row.Get<Delivery, string>(x => x.DeliveredBy.Name));
  133. }
  134. return dict;
  135. }
  136. private object DeliveryGrid_OnItemSelected(DataGridViewModelItem item)
  137. {
  138. DeliveryDetails page = new DeliveryDetails(item.ID);
  139. Navigation.PushAsync(page);
  140. return null;
  141. }
  142. private object DeliveryGrid_OnImageSelected(DataGridViewModelItem item)
  143. {
  144. StackLayout stack = new StackLayout();
  145. if (item.ImagePopupDetail.ContainsKey("Address"))
  146. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = item.ImagePopupDetail["Address"], FontAttributes = FontAttributes.Bold });
  147. if (item.ImagePopupDetail.ContainsKey("Delivered"))
  148. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered: " + item.ImagePopupDetail["Delivered"], FontAttributes = FontAttributes.Bold });
  149. if (item.ImagePopupDetail.ContainsKey("Delivered by"))
  150. stack.Children.Add(new Label { HorizontalOptions = LayoutOptions.Center, Text = "Delivered by: " + item.ImagePopupDetail["Delivered by"], FontAttributes = FontAttributes.Bold });
  151. if (!item.ImagePopupDetail.ContainsKey("Latitude"))
  152. return stack;
  153. SfMaps map = new SfMaps();
  154. ImageryLayer layer = new ImageryLayer();
  155. if (Device.RuntimePlatform.Equals(Device.iOS))
  156. {
  157. //ImageSource imageSource = "mapmarker.png";
  158. //sfMapLayer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image;
  159. //sfMapLayer.MarkerSettings.ImageSource = "location-pin.pdf";
  160. //sfMapLayer.MarkerSettings.IconSize = 35;
  161. layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Circle;
  162. layer.MarkerSettings.IconColor = Color.DarkBlue;
  163. layer.MarkerSettings.IconSize = 15;
  164. }
  165. else
  166. {
  167. layer.MarkerSettings.MarkerIcon = MapMarkerIcon.Image;
  168. layer.MarkerSettings.ImageSource = "mapmarker.png";
  169. layer.MarkerSettings.IconSize = 35;
  170. }
  171. layer.MarkerSettings.FontAttributes = FontAttributes.Bold;
  172. layer.MarkerSettings.LabelSize = 20;
  173. layer.MarkerSettings.LabelColor = Color.DarkBlue;
  174. layer.GeoCoordinates = new Point(double.Parse(item.ImagePopupDetail["Latitude"]), double.Parse(item.ImagePopupDetail["Longitude"]));
  175. map.ZoomLevel = 15;
  176. map.MinZoom = 10;
  177. map.MaxZoom = 18;
  178. MapMarker marker = new MapMarker();
  179. marker.Latitude = item.ImagePopupDetail["Latitude"];
  180. marker.Longitude = item.ImagePopupDetail["Longitude"];
  181. layer.Markers.Add(marker);
  182. map.Layers.Add(layer);
  183. map.HorizontalOptions = LayoutOptions.CenterAndExpand;
  184. map.VerticalOptions = LayoutOptions.CenterAndExpand;
  185. stack.Children.Add(map);
  186. return new ScrollView { Content = stack };
  187. }
  188. public void RefreshGrid()
  189. {
  190. var list = LoadList();
  191. Items.Clear();
  192. Items.AddRange(list);
  193. Refresh(Items);
  194. }
  195. }
  196. }