Delivery.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. namespace Comal.Classes
  9. {
  10. [Caption("Digital Forms")]
  11. [UserTracking("Digital Forms")]
  12. public class DeliveryForm : EntityForm<Delivery, DeliveryLink, DeliveryForm>
  13. {
  14. public override string AutoIncrementPrefix() => "DF";
  15. }
  16. public class DeliveryDocumentCount : CoreAggregate<Delivery, DeliveryDocument, Guid>
  17. {
  18. public override Expression<Func<DeliveryDocument, Guid>> Aggregate => x => x.ID;
  19. public override AggregateCalculation Calculation => AggregateCalculation.Count;
  20. public override Dictionary<Expression<Func<DeliveryDocument, object>>, Expression<Func<Delivery, object>>> Links =>
  21. new Dictionary<Expression<Func<DeliveryDocument, object>>, Expression<Func<Delivery, object>>>()
  22. {
  23. { DeliveryDocument => DeliveryDocument.EntityLink.ID, Delivery => Delivery.ID }
  24. };
  25. }
  26. [UserTracking("Logistics")]
  27. [Caption("Deliveries")]
  28. public class Delivery : Entity, IPersistent, IRemotable, INumericAutoIncrement<Delivery>, IOneToMany<Assignment>, ILicense<LogisticsLicense>,
  29. IExportable, IImportable
  30. {
  31. [EditorSequence(1)]
  32. [IntegerEditor]
  33. public int Number { get; set; }
  34. [EditorSequence(2)]
  35. public DeliveryTypeLink Type { get; set; }
  36. [EditorSequence(3)]
  37. public JobLink Job { get; set; }
  38. [EditorSequence(4)]
  39. public ContactLink Contact { get; set; }
  40. [EditorSequence(5)]
  41. public Address Address { get; set; }
  42. [EditorSequence(6)]
  43. [MemoEditor]
  44. public string Notes { get; set; }
  45. [EntityRelationship(DeleteAction.SetNull)]
  46. [EditorSequence(7)]
  47. public AssignmentLink Assignment { get; set; }
  48. [EditorSequence(8)]
  49. public EmployeeLink Employee { get; set; }
  50. [DateTimeEditor]
  51. [EditorSequence(9)]
  52. public DateTime Due { get; set; }
  53. [Aggregate(typeof(DeliveryDocumentCount))]
  54. [IntegerEditor(Editable = Editable.Hidden)]
  55. public int Documents { get; set; }
  56. [TimestampEditor]
  57. [EditorSequence(10)]
  58. public DateTime Delivered { get; set; }
  59. [EditorSequence(11)]
  60. public EmployeeLink DeliveredBy { get; set; }
  61. [NullEditor]
  62. public Location Location { get; set; }
  63. [TimestampEditor(Editable = Editable.Disabled)]
  64. [EditorSequence(12)]
  65. public DateTime Completed { get; set; }
  66. [NullEditor]
  67. public int KanbanNumber { get; set; }
  68. public Expression<Func<Delivery, int>> AutoIncrementField()
  69. {
  70. return x => x.Number;
  71. }
  72. public Filter<Delivery> AutoIncrementFilter()
  73. {
  74. return null;
  75. }
  76. //this only refers to a kanban generated by the deliveries mobile app - not a kanban which may have generated the delivery
  77. protected override void Init()
  78. {
  79. base.Init();
  80. Employee = new EmployeeLink();
  81. Assignment = new AssignmentLink();
  82. Job = new JobLink();
  83. Job.PropertyChanged += Job_PropertyChanged;
  84. Contact = new ContactLink();
  85. Contact.PropertyChanged += Contact_PropertyChanged;
  86. Address = new Address();
  87. Location = new Location();
  88. Type = new DeliveryTypeLink();
  89. DeliveredBy = new EmployeeLink();
  90. KanbanNumber = 0;
  91. }
  92. private void Contact_PropertyChanged(object sender, PropertyChangedEventArgs e)
  93. {
  94. if (string.Equals(e.PropertyName, "ID"))
  95. {
  96. var contactLink = (sender as ContactLink)!;
  97. CoreTable results = null;
  98. if (contactLink.IsValid())
  99. results = new Client<Contact>().Query(
  100. new Filter<Contact>(x => x.ID).IsEqualTo(contactLink.ID),
  101. new Columns<Contact>(
  102. x => x.Address.Street,
  103. x => x.Address.City,
  104. x => x.Address.State,
  105. x => x.Address.PostCode
  106. )
  107. );
  108. var row = results?.Rows.FirstOrDefault();
  109. Address.Street = row != null ? row.Get<Contact, string>(x => x.Address.Street) : "";
  110. Address.City = row != null ? row.Get<Contact, string>(x => x.Address.City) : "";
  111. Address.State = row != null ? row.Get<Contact, string>(x => x.Address.State) : "";
  112. Address.PostCode = row != null ? row.Get<Contact, string>(x => x.Address.PostCode) : "";
  113. }
  114. }
  115. private void Job_PropertyChanged(object sender, PropertyChangedEventArgs e)
  116. {
  117. //if (string.Equals(e.PropertyName, "ID") && !Contact.IsValid())
  118. //{
  119. // var jobLink = sender as JobLink;
  120. // CoreTable results = null;
  121. // if (jobLink.IsValid())
  122. // results = new Client<Job>().Query(
  123. // new Filter<Job>(x => x.ID).IsEqualTo(jobLink.ID),
  124. // new Columns<Job>(
  125. // x => x.SiteAddress.Street,
  126. // x => x.SiteAddress.City,
  127. // x => x.SiteAddress.State,
  128. // x => x.SiteAddress.PostCode
  129. // )
  130. // );
  131. // var row = results?.Rows.FirstOrDefault();
  132. // Address.Street = row != null ? row.Get<Job, string>(x => x.SiteAddress.Street) : "";
  133. // Address.City = row != null ? row.Get<Job, string>(x => x.SiteAddress.City) : "";
  134. // Address.State = row != null ? row.Get<Job, string>(x => x.SiteAddress.State) : "";
  135. // Address.PostCode = row != null ? row.Get<Job, string>(x => x.SiteAddress.PostCode) : "";
  136. //}
  137. }
  138. #region Move to Assignments?
  139. [NullEditor]
  140. public DateTime Date { get; set; }
  141. [NullEditor]
  142. public TimeSpan Start { get; set; }
  143. [NullEditor]
  144. public TimeSpan Finish { get; set; }
  145. private bool bChanging;
  146. protected override void DoPropertyChanged(string name, object before, object after)
  147. {
  148. base.DoPropertyChanged(name, before, after);
  149. if (bChanging)
  150. return;
  151. bChanging = true;
  152. if (name.Equals("Start"))
  153. Finish = ((TimeSpan)after).Add(Duration);
  154. else if (name.Equals("Finish"))
  155. Duration = ((TimeSpan)after).Subtract(Start);
  156. else if (name.Equals("Duration")) Finish = Start.Add((TimeSpan)after);
  157. bChanging = false;
  158. }
  159. [NullEditor]
  160. public TimeSpan Duration { get; set; }
  161. #endregion
  162. }
  163. }