Delivery.cs 6.7 KB

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