Invoice.cs 8.1 KB

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