Invoice.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. // public class InvoiceChargeableHours : CoreAggregate<Invoice, Assignment, TimeSpan>
  8. // {
  9. // public override Expression<Func<Assignment, TimeSpan>> Aggregate => x => x.ChargeableHours;
  10. //
  11. // public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  12. //
  13. // public override Dictionary<Expression<Func<Assignment, object>>, Expression<Func<Invoice, object>>> Links =>
  14. // new Dictionary<Expression<Func<Assignment, object>>, Expression<Func<Invoice, object>>>()
  15. // {
  16. // { Assignment => Assignment.Invoice.ID, Invoice => Invoice.ID }
  17. // };
  18. //
  19. // public override Filter<Assignment>? Filter => new Filter<Assignment>(x => x.Chargeable).IsEqualTo(true);
  20. // }
  21. public class InvoiceExTax : CoreAggregate<Invoice, InvoiceLine, double>
  22. {
  23. public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.ExTax;
  24. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  25. public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>> Links =>
  26. new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>>()
  27. {
  28. { InvoiceLine => InvoiceLine.InvoiceLink.ID, Invoice => Invoice.ID }
  29. };
  30. }
  31. public class InvoiceTax : CoreAggregate<Invoice, InvoiceLine, double>
  32. {
  33. public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.Tax;
  34. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  35. public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>> Links =>
  36. new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>>()
  37. {
  38. { InvoiceLine => InvoiceLine.InvoiceLink.ID, Invoice => Invoice.ID }
  39. };
  40. }
  41. public class InvoiceIncTax : CoreAggregate<Invoice, InvoiceLine, double>
  42. {
  43. public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.IncTax;
  44. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  45. public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>> Links =>
  46. new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>>()
  47. {
  48. { InvoiceLine => InvoiceLine.InvoiceLink.ID, Invoice => Invoice.ID }
  49. };
  50. }
  51. public class InvoiceAmountPaid : CoreAggregate<Invoice, InvoiceReceipt, double>
  52. {
  53. public override Expression<Func<InvoiceReceipt, double>> Aggregate => x => x.Amount;
  54. public override AggregateCalculation Calculation => AggregateCalculation.Sum;
  55. public override Dictionary<Expression<Func<InvoiceReceipt, object>>, Expression<Func<Invoice, object>>> Links =>
  56. new Dictionary<Expression<Func<InvoiceReceipt, object>>, Expression<Func<Invoice, object>>>()
  57. {
  58. { InvoiceReceipt => InvoiceReceipt.InvoiceLink.ID, Invoice => Invoice.ID }
  59. };
  60. }
  61. public class InvoiceBalance : IFormula<Invoice, double>
  62. {
  63. public Expression<Func<Invoice, double>> Value => x => x.IncTax;
  64. public Expression<Func<Invoice, double>>[] Modifiers => new Expression<Func<Invoice, double>>[] { x => x.AmountPaid };
  65. public FormulaOperator Operator => FormulaOperator.Subtract;
  66. public FormulaType Type => FormulaType.Virtual;
  67. }
  68. public enum InvoiceType
  69. {
  70. Standard,
  71. ProgressClaim
  72. }
  73. [UserTracking("Accounts Receivable")]
  74. public class Invoice : Entity, IPersistent, IRemotable, INumericAutoIncrement<Invoice>, ILicense<AccountsReceivableLicense>, IExportable, IPostable
  75. {
  76. [EditorSequence(1)]
  77. [IntegerEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  78. public int Number { get; set; }
  79. [EditorSequence(2)]
  80. [DateEditor(Visible = Visible.Default, TodayVisible = true)]
  81. public DateTime Date { get; set; } = DateTime.Today;
  82. [EditorSequence(3)]
  83. [MemoEditor(Visible = Visible.Default)]
  84. public string Description { get; set; }
  85. [EditorSequence(4)]
  86. public JobLink JobLink { get; set; }
  87. [EditorSequence(5)]
  88. public CustomerLink CustomerLink { get; set; }
  89. [EditorSequence(6)]
  90. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  91. [Aggregate(typeof(InvoiceExTax))]
  92. public double ExTax { get; set; }
  93. [EditorSequence(7)]
  94. [CurrencyEditor(Summary = Summary.Sum)]
  95. public double Retained { get; set; }
  96. [EditorSequence(8)]
  97. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  98. [Aggregate(typeof(InvoiceTax))]
  99. public double Tax { get; set; }
  100. [EditorSequence(9)]
  101. [CurrencyEditor(Visible = Visible.Default, Editable = Editable.Hidden, Summary = Summary.Sum)]
  102. [Aggregate(typeof(InvoiceIncTax))]
  103. public double IncTax { get; set; }
  104. [EditorSequence(10)]
  105. [CurrencyEditor(Editable = Editable.Hidden)]
  106. [Aggregate(typeof(InvoiceExTax))]
  107. public double Total { get; set; }
  108. [EditorSequence(11)]
  109. [CurrencyEditor(Editable = Editable.Hidden)]
  110. [Aggregate(typeof(InvoiceAmountPaid))]
  111. public double AmountPaid { get; set; }
  112. /// <summary>
  113. /// Balance = <see cref="IncTax"/> - <see cref="AmountPaid"/>.
  114. /// </summary>
  115. [EditorSequence(12)]
  116. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  117. [Formula(typeof(InvoiceBalance))]
  118. public double Balance { get; set; }
  119. [NullEditor]
  120. [LoggableProperty]
  121. [RequiredColumn]
  122. public PostedStatus PostedStatus { get; set; } = PostedStatus.NeverPosted;
  123. [NullEditor]
  124. [LoggableProperty]
  125. public DateTime Posted { get; set; }
  126. [NullEditor]
  127. public string PostedNote { get; set; }
  128. [NullEditor]
  129. public string PostedReference { get; set; }
  130. [NullEditor]
  131. public InvoiceType Type { get; set; }
  132. public Expression<Func<Invoice, int>> AutoIncrementField()
  133. {
  134. return x => x.Number;
  135. }
  136. public Filter<Invoice>? AutoIncrementFilter()
  137. {
  138. return null;
  139. }
  140. public override string ToString()
  141. {
  142. return string.Format("{0}: {1}", Number, Description);
  143. }
  144. protected override void DoPropertyChanged(string name, object? before, object? after)
  145. {
  146. base.DoPropertyChanged(name, before, after);
  147. if (name.Equals("IncTax"))
  148. Balance = (double)after - AmountPaid;
  149. else if (name.Equals("AmountPaid"))
  150. Balance = IncTax - (double)after;
  151. }
  152. }
  153. }