Invoice.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Job { get; set; }
  26. [Obsolete("Replaced by Job")]
  27. public JobLink JobLink
  28. {
  29. get => Job;
  30. set { }
  31. }
  32. [EditorSequence(5)]
  33. [RequiredColumn]
  34. public CustomerLink Customer { get; set; }
  35. [Obsolete("Replaced by Customer")]
  36. public CustomerLink CustomerLink
  37. {
  38. get => Customer;
  39. set { }
  40. }
  41. private class ClaimedFormula : ComplexFormulaGenerator<Invoice, double>
  42. {
  43. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  44. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.ExTax))
  45. .WithLink(x => x.Invoice.ID, x => x.ID);
  46. }
  47. [EditorSequence(6)]
  48. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  49. [ComplexFormula(typeof(ClaimedFormula))]
  50. public double Claimed { get; set; }
  51. [EditorSequence(7)]
  52. [CurrencyEditor(Summary = Summary.Sum)]
  53. public double Retained { get; set; }
  54. private class ExTaxFormula : ComplexFormulaGenerator<Invoice, double>
  55. {
  56. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  57. Formula(FormulaOperator.Subtract, Property(x => x.Claimed), Property(x => x.Retained));
  58. }
  59. [EditorSequence(8)]
  60. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  61. [ComplexFormula(typeof(ExTaxFormula))]
  62. public double ExTax { get; set; }
  63. private class TaxFormula : ComplexFormulaGenerator<Invoice, double>
  64. {
  65. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  66. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
  67. .WithLink(x => x.Invoice.ID, x => x.ID);
  68. }
  69. [EditorSequence(9)]
  70. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  71. [ComplexFormula(typeof(TaxFormula))]
  72. public double Tax { get; set; }
  73. private class IncTaxFormula : ComplexFormulaGenerator<Invoice, double>
  74. {
  75. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  76. Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
  77. .WithLink(x => x.Invoice.ID, x => x.ID);
  78. }
  79. [EditorSequence(10)]
  80. [CurrencyEditor(Visible = Visible.Default, Editable = Editable.Hidden, Summary = Summary.Sum)]
  81. [ComplexFormula(typeof(IncTaxFormula))]
  82. public double IncTax { get; set; }
  83. private class AmountPaidFormula : ComplexFormulaGenerator<Invoice, double>
  84. {
  85. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  86. Aggregate<InvoiceReceipt>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
  87. .WithLink(x => x.Invoice.ID, x => x.ID);
  88. }
  89. [EditorSequence(11)]
  90. [CurrencyEditor(Editable = Editable.Hidden)]
  91. [ComplexFormula(typeof(AmountPaidFormula))]
  92. public double AmountPaid { get; set; }
  93. private class BalanceFormula : ComplexFormulaGenerator<Invoice, double>
  94. {
  95. public override IComplexFormulaNode<Invoice, double> GetFormula() =>
  96. Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
  97. }
  98. /// <summary>
  99. /// Balance = <see cref="IncTax"/> - <see cref="AmountPaid"/>.
  100. /// </summary>
  101. [EditorSequence(12)]
  102. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  103. [ComplexFormula(typeof(BalanceFormula))]
  104. public double Balance { get; set; }
  105. [EditorSequence(13)]
  106. [LookupDefinition(typeof(AccountsReceivablePaymentTermsLookup<Invoice>))]
  107. [RequiredColumn]
  108. public PaymentTermsLink Terms { get; set; }
  109. [EditorSequence(14)]
  110. [DateEditor(Visible = Visible.Default, TodayVisible = true)]
  111. public DateTime DueDate { get; set; } = DateTime.Today;
  112. [EditorSequence(15)]
  113. public SalesGLCodeLink SellGL { get; set; }
  114. [NullEditor]
  115. [LoggableProperty]
  116. [RequiredColumn]
  117. public PostedStatus PostedStatus { get; set; } = PostedStatus.NeverPosted;
  118. [NullEditor]
  119. [LoggableProperty]
  120. public DateTime Posted { get; set; }
  121. [NullEditor]
  122. public string PostedNote { get; set; }
  123. [NullEditor]
  124. public string PostedReference { get; set; }
  125. [NullEditor]
  126. public InvoiceType Type { get; set; }
  127. public Expression<Func<Invoice, int>> AutoIncrementField() => x => x.Number;
  128. public Filter<Invoice>? AutoIncrementFilter() => null;
  129. public int AutoIncrementDefault() => 1;
  130. public override string ToString()
  131. {
  132. return string.Format("{0}: {1}", Number, Description);
  133. }
  134. private static string DATE = CoreUtils.GetFullPropertyName<Invoice, DateTime>(x => x.Date, ".");
  135. private static string CUSTOMERLINKTERMSCALCULATION = CoreUtils.GetFullPropertyName<Invoice, String>(x => x.Customer.Terms.Calculation, ".");
  136. private static string TERMSCALCULATION = CoreUtils.GetFullPropertyName<Invoice, String>(x => x.Terms.Calculation, ".");
  137. private static string AMOUNTPAID = CoreUtils.GetFullPropertyName<Invoice, double>(x => x.AmountPaid, ".");
  138. private static string INCTAX = CoreUtils.GetFullPropertyName<Invoice, double>(x => x.IncTax, ".");
  139. protected override void DoPropertyChanged(string name, object? before, object? after)
  140. {
  141. base.DoPropertyChanged(name, before, after);
  142. if (name.Equals(AMOUNTPAID))
  143. Balance = IncTax - (double)(after ?? 0.0);
  144. else if (name.Equals(INCTAX))
  145. Balance = (double)(after ?? 0.0) - AmountPaid;
  146. else if (name.Equals(DATE))
  147. DueDate = CalculateTerms((DateTime)(after ?? ""),Terms.Calculation);
  148. else if (name.Equals(TERMSCALCULATION))
  149. DueDate = CalculateTerms(Date, (string)(after ?? ""));
  150. }
  151. private DateTime CalculateTerms(DateTime date, string calculation)
  152. {
  153. if (string.IsNullOrWhiteSpace(calculation))
  154. return date;
  155. var model = new PaymentTermsCalculationModel { Date = date };
  156. var expr = new CoreExpression<PaymentTermsCalculationModel, DateTime>(calculation);
  157. if(expr.Evaluate(model).Get(out var eval, out var e))
  158. {
  159. return eval;
  160. }
  161. else
  162. {
  163. Logger.Send(LogType.Information, "", $"Error in Formula: [{calculation}] ({e.Message})");
  164. return date;
  165. }
  166. }
  167. static Invoice()
  168. {
  169. LinkedProperties.Register<Invoice,PaymentTermsLink,Guid>(x=>x.Customer.Terms,x=>x.ID, x=>x.Terms.ID);
  170. LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.Customer.Terms,x=>x.Code, x=>x.Terms.Code);
  171. LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.Customer.Terms,x=>x.Description, x=>x.Terms.Description);
  172. LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.Customer.Terms,x=>x.Calculation, x=>x.Terms.Calculation);
  173. LinkedProperties.Register<Invoice,SalesGLCodeLink,Guid>(x=>x.Customer.GLCode,x=>x.ID, x=>x.SellGL.ID);
  174. }
  175. }
  176. }