Quote.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 QuoteExTax : CoreAggregate<Quote, QuoteProposal, double>
  11. {
  12. public override Expression<Func<QuoteProposal, double>> Aggregate => x => x.ExTax;
  13. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  14. public override Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>> Links =>
  15. new Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>> ()
  16. {
  17. { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID }
  18. };
  19. public override Filter<QuoteProposal> Filter => new Filter<QuoteProposal>(x => x.Default).IsEqualTo(true);
  20. }
  21. public class QuoteTax : CoreAggregate<Quote, QuoteProposal, double>
  22. {
  23. public override Expression<Func<QuoteProposal, double>> Aggregate => x => x.Tax;
  24. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  25. public override Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>> Links =>
  26. new Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>>()
  27. {
  28. { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID }
  29. };
  30. public override Filter<QuoteProposal> Filter => new Filter<QuoteProposal>(x => x.Default).IsEqualTo(true);
  31. }
  32. public class QuoteIncTax : CoreAggregate<Quote, QuoteProposal, double>
  33. {
  34. public override Expression<Func<QuoteProposal, double>> Aggregate => x => x.IncTax;
  35. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  36. public override Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>> Links =>
  37. new Dictionary<Expression<Func<QuoteProposal, object>>, Expression<Func<Quote, object>>>()
  38. {
  39. { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID }
  40. };
  41. public override Filter<QuoteProposal> Filter => new Filter<QuoteProposal>(x => x.Default).IsEqualTo(true);
  42. }
  43. [UserTracking("Quote Management")]
  44. public class Quote : Entity, IRemotable, IPersistent, IStringAutoIncrement<Quote>, ILicense<QuotesManagementLicense>, IDataEntryInstance
  45. {
  46. [UniqueCodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  47. [EditorSequence(0)]
  48. public string Number { get; set; }
  49. [TextBoxEditor]
  50. [EditorSequence(1)]
  51. public string Title { get; set; }
  52. [EditorSequence(2)]
  53. public CustomerLink Customer { get; set; }
  54. private class ContactLookup : LookupDefinitionGenerator<Contact, Quote>
  55. {
  56. public override Filter<Contact> DefineFilter(Quote[] items)
  57. {
  58. if (items == null || !items.Any())
  59. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  60. var customer = items.First().Customer;
  61. if (!customer.IsValid())
  62. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  63. var contacts = new Client<CustomerContact>().Query(
  64. new Filter<CustomerContact>(x => x.Customer.ID).IsEqualTo(customer.ID),
  65. Columns.None<CustomerContact>().Add(x => x.Contact.ID)
  66. );
  67. var ids = contacts.Rows.Select(x => x.Get<CustomerContact, Guid>(c => c.Contact.ID));
  68. if (!ids.Any())
  69. return new Filter<Contact>(x => x.ID).IsEqualTo(CoreUtils.FullGuid);
  70. return new Filter<Contact>(x => x.ID).InList(ids.ToArray());
  71. }
  72. public override Columns<Quote> DefineFilterColumns()
  73. => Columns.None<Quote>().Add(x => x.Customer.ID);
  74. }
  75. [EditorSequence(3)]
  76. [LookupDefinition(typeof(ContactLookup))]
  77. public ContactLink Contact { get; set; }
  78. [EditorSequence(4)]
  79. public Address SiteAddress { get; set; }
  80. [EditorSequence(5)]
  81. public AccountLink Account { get; set; }
  82. [DoubleEditor(Editable = Editable.Hidden)]
  83. [EditorSequence(6)]
  84. [Aggregate(typeof(QuoteExTax))]
  85. public double ExTax { get; set; }
  86. [DoubleEditor(Editable = Editable.Hidden)]
  87. [EditorSequence(7)]
  88. [Aggregate(typeof(QuoteTax))]
  89. public double Tax { get; set; }
  90. [DoubleEditor(Editable = Editable.Hidden)]
  91. [EditorSequence(8)]
  92. [Aggregate(typeof(QuoteIncTax))]
  93. public double IncTax { get; set; }
  94. [EditorSequence(9)]
  95. public QuoteStatusLink Status { get; set; }
  96. [EditorSequence(10)]
  97. public JobLink Job { get; set; }
  98. #region Management
  99. [TimestampEditor]
  100. [EditorSequence("Management", 1)]
  101. public DateTime DataEntered { get; set; }
  102. [DateEditor]
  103. [EditorSequence("Management", 2)]
  104. public DateTime ReceivedDate { get; set; }
  105. [DateEditor]
  106. [EditorSequence("Management", 3)]
  107. public DateTime DueDate { get; set; }
  108. [DateEditor]
  109. [EditorSequence("Management", 4)]
  110. public DateTime QuoteDate { get; set; }
  111. [DateEditor]
  112. [EditorSequence("Management", 5)]
  113. public DateTime ArchivedDate { get; set; }
  114. [EditorSequence("Management", 6)]
  115. public EmployeeLink Salesperson { get; set; }
  116. #endregion
  117. #region Notes
  118. [NotesEditor]
  119. [EditorSequence("Notes", 1)]
  120. public string[] Notes { get; set; }
  121. #endregion
  122. [DoNotPersist]
  123. [DoNotSerialize]
  124. public static String NumberPrefix { get; set; }
  125. public Expression<Func<Quote, string>> AutoIncrementField() => x => x.Number;
  126. public Filter<Quote> AutoIncrementFilter() => null;
  127. public string AutoIncrementPrefix() => NumberPrefix;
  128. public string AutoIncrementFormat() => "{0:D6}";
  129. protected override void Init()
  130. {
  131. base.Init();
  132. Customer.Account.PropertyChanged += Account_PropertyChanged;
  133. Customer.Delivery.PropertyChanged += Delivery_PropertyChanged;
  134. }
  135. private void Delivery_PropertyChanged(object sender, PropertyChangedEventArgs e)
  136. {
  137. var value = CoreUtils.GetPropertyValue(Customer.Delivery, e.PropertyName);
  138. CoreUtils.SetPropertyValue(SiteAddress, e.PropertyName, value);
  139. }
  140. private void Account_PropertyChanged(object sender, PropertyChangedEventArgs e)
  141. {
  142. if (e.PropertyName.Equals("ID"))
  143. Account.ID = Customer.Account.ID;
  144. else if (e.PropertyName.Equals("Code"))
  145. Account.Code = Customer.Account.Code;
  146. else if (e.PropertyName.Equals("Name"))
  147. Account.Name = Customer.Account.Name;
  148. }
  149. }
  150. }