Quote.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.ProposalExTax;
  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.ProposalTax;
  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.ProposalIncTax;
  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. new Columns<CustomerContact>(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. => new Columns<Quote>(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. #region Management
  97. [TimestampEditor]
  98. [EditorSequence("Management", 1)]
  99. public DateTime DataEntered { get; set; }
  100. [DateEditor]
  101. [EditorSequence("Management", 2)]
  102. public DateTime ReceivedDate { get; set; }
  103. [DateEditor]
  104. [EditorSequence("Management", 3)]
  105. public DateTime DueDate { get; set; }
  106. [DateEditor]
  107. [EditorSequence("Management", 4)]
  108. public DateTime QuoteDate { get; set; }
  109. [DateEditor]
  110. [EditorSequence("Management", 5)]
  111. public DateTime ArchivedDate { get; set; }
  112. [EditorSequence("Management", 6)]
  113. public EmployeeLink Salesperson { get; set; }
  114. #endregion
  115. #region Notes
  116. [NotesEditor]
  117. [EditorSequence("Notes", 1)]
  118. public string[] Notes { get; set; }
  119. #endregion
  120. [DoNotPersist]
  121. [DoNotSerialize]
  122. public static String NumberPrefix { get; set; }
  123. public Expression<Func<Quote, string>> AutoIncrementField() => x => x.Number;
  124. public Filter<Quote> AutoIncrementFilter() => null;
  125. public string AutoIncrementPrefix() => NumberPrefix;
  126. public string AutoIncrementFormat() => "{0:D6}";
  127. protected override void Init()
  128. {
  129. base.Init();
  130. Customer.Account.PropertyChanged += Account_PropertyChanged;
  131. Customer.Delivery.PropertyChanged += Delivery_PropertyChanged;
  132. }
  133. private void Delivery_PropertyChanged(object sender, PropertyChangedEventArgs e)
  134. {
  135. var value = CoreUtils.GetPropertyValue(Customer.Delivery, e.PropertyName);
  136. CoreUtils.SetPropertyValue(SiteAddress, e.PropertyName, value);
  137. }
  138. private void Account_PropertyChanged(object sender, PropertyChangedEventArgs e)
  139. {
  140. if (e.PropertyName.Equals("ID"))
  141. Account.ID = Customer.Account.ID;
  142. else if (e.PropertyName.Equals("Code"))
  143. Account.Code = Customer.Account.Code;
  144. else if (e.PropertyName.Equals("Name"))
  145. Account.Name = Customer.Account.Name;
  146. }
  147. }
  148. }