Invoice.cs 7.1 KB

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