Invoice.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using InABox.Core;
  5. namespace Comal.Classes
  6. {
  7. public enum InvoiceType
  8. {
  9. Standard,
  10. ProgressClaim
  11. }
  12. [UserTracking("Accounts Receivable")]
  13. public class Invoice : Entity, IPersistent, IRemotable, INumericAutoIncrement<Invoice>, ILicense<AccountsReceivableLicense>, IExportable, IPostable
  14. {
  15. [EditorSequence(1)]
  16. [IntegerEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  17. public int Number { get; set; }
  18. [EditorSequence(2)]
  19. [DateEditor(Visible = Visible.Default, TodayVisible = true)]
  20. public DateTime Date { get; set; } = DateTime.Today;
  21. [EditorSequence(3)]
  22. [MemoEditor(Visible = Visible.Default)]
  23. public string Description { get; set; }
  24. [EditorSequence(4)]
  25. public JobLink JobLink { get; set; }
  26. [EditorSequence(5)]
  27. public CustomerLink CustomerLink { get; set; }
  28. private class ClaimedFormula : ComplexFormulaGenerator<Invoice, double>
  29. {
  30. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  31. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.ExTax))
  32. .WithLink(x => x.InvoiceLink.ID, x => x.ID);
  33. }
  34. [EditorSequence(6)]
  35. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  36. [ComplexFormula(typeof(ClaimedFormula))]
  37. public double Claimed { get; set; }
  38. [EditorSequence(7)]
  39. [CurrencyEditor(Summary = Summary.Sum)]
  40. public double Retained { get; set; }
  41. private class ExTaxFormula : ComplexFormulaGenerator<Invoice, double>
  42. {
  43. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  44. Formula(FormulaOperator.Subtract, Property(x => x.Claimed), Property(x => x.Retained));
  45. }
  46. [EditorSequence(8)]
  47. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  48. [ComplexFormula(typeof(ExTaxFormula))]
  49. public double ExTax { get; set; }
  50. private class TaxFormula : ComplexFormulaGenerator<Invoice, double>
  51. {
  52. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  53. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
  54. .WithLink(x => x.InvoiceLink.ID, x => x.ID);
  55. }
  56. [EditorSequence(9)]
  57. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  58. [ComplexFormula(typeof(TaxFormula))]
  59. public double Tax { get; set; }
  60. private class IncTaxFormula : ComplexFormulaGenerator<Invoice, double>
  61. {
  62. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  63. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
  64. .WithLink(x => x.InvoiceLink.ID, x => x.ID);
  65. }
  66. [EditorSequence(10)]
  67. [CurrencyEditor(Visible = Visible.Default, Editable = Editable.Hidden, Summary = Summary.Sum)]
  68. [ComplexFormula(typeof(IncTaxFormula))]
  69. public double IncTax { get; set; }
  70. private class AmountPaidFormula : ComplexFormulaGenerator<Invoice, double>
  71. {
  72. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  73. Aggregate<InvoiceReceipt>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
  74. .WithLink(x => x.InvoiceLink.ID, x => x.ID);
  75. }
  76. [EditorSequence(11)]
  77. [CurrencyEditor(Editable = Editable.Hidden)]
  78. [ComplexFormula(typeof(AmountPaidFormula))]
  79. public double AmountPaid { get; set; }
  80. private class BalanceFormula : ComplexFormulaGenerator<Invoice, double>
  81. {
  82. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  83. Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
  84. }
  85. /// <summary>
  86. /// Balance = <see cref="IncTax"/> - <see cref="AmountPaid"/>.
  87. /// </summary>
  88. [EditorSequence(12)]
  89. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  90. [ComplexFormula(typeof(BalanceFormula))]
  91. public double Balance { get; set; }
  92. [NullEditor]
  93. [LoggableProperty]
  94. [RequiredColumn]
  95. public PostedStatus PostedStatus { get; set; } = PostedStatus.NeverPosted;
  96. [NullEditor]
  97. [LoggableProperty]
  98. public DateTime Posted { get; set; }
  99. [NullEditor]
  100. public string PostedNote { get; set; }
  101. [NullEditor]
  102. public string PostedReference { get; set; }
  103. [NullEditor]
  104. public InvoiceType Type { get; set; }
  105. public Expression<Func<Invoice, int>> AutoIncrementField()
  106. {
  107. return x => x.Number;
  108. }
  109. public Filter<Invoice>? AutoIncrementFilter()
  110. {
  111. return null;
  112. }
  113. public override string ToString()
  114. {
  115. return string.Format("{0}: {1}", Number, Description);
  116. }
  117. protected override void DoPropertyChanged(string name, object? before, object? after)
  118. {
  119. base.DoPropertyChanged(name, before, after);
  120. if (name.Equals("IncTax"))
  121. Balance = (double)after - AmountPaid;
  122. else if (name.Equals("AmountPaid"))
  123. Balance = IncTax - (double)after;
  124. }
  125. }
  126. }