Bill.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using InABox.Core;
  4. namespace Comal.Classes
  5. {
  6. [UserTracking("Accounts Payable")]
  7. public class Bill : Entity, IPersistent, IRemotable, ILicense<AccountsPayableLicense>, IDataEntryInstance, IPostable, IExportable,
  8. IProblems<ManagedProblem>
  9. {
  10. [RequiredColumn]
  11. [EditorSequence(1)]
  12. [EntityRelationship(DeleteAction.Cascade)]
  13. public SupplierLink SupplierLink { get; set; }
  14. [EditorSequence(2)]
  15. [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
  16. public string Number { get; set; }
  17. [EditorSequence(3)]
  18. [MemoEditor]
  19. public string Description { get; set; }
  20. [EditorSequence(4)]
  21. [DateEditor]
  22. public DateTime BillDate { get; set; }
  23. [EditorSequence(5)]
  24. [LookupDefinition(typeof(AccountsPayablePaymentTermsLookup<Bill>))]
  25. public PaymentTermsLink Terms { get; set; }
  26. [EditorSequence(6)]
  27. [DateEditor]
  28. public DateTime PaymentDate { get; set; }
  29. [EditorSequence(7)]
  30. [DateEditor]
  31. public DateTime AccountingDate { get; set; }
  32. [EditorSequence("Additional",1)]
  33. [TimestampEditor]
  34. public DateTime DataEntered { get; set; }
  35. [EditorSequence("Additional",2)]
  36. [TimestampEditor]
  37. [Security(typeof(CanCheckBills), Editable = Editable.Disabled)]
  38. public DateTime Checked { get; set; }
  39. [EditorSequence("Additional", 4)]
  40. [Editable(Editable.Disabled)]
  41. [RequiredColumn]
  42. public BillApprovalSetLink ApprovalSet { get; set; }
  43. private class IsApprovedFormula : ComplexFormulaGenerator<Bill, bool>
  44. {
  45. public override IComplexFormulaNode<Bill, bool> GetFormula() =>
  46. If<int>(x => x.Count<BillApproval, Guid>(
  47. x => x.Property(x => x.ID),
  48. filter: new Filter<BillApproval>(x => x.Approved).IsEqualTo(DateTime.MinValue))
  49. .WithLink(x => x.Bill.ID, x => x.ID),
  50. Condition.Equals,
  51. x => x.Constant(0))
  52. .Then(Constant(true))
  53. .Else(Constant(false));
  54. }
  55. [Editable(Editable.Hidden)]
  56. [ComplexFormula(typeof(IsApprovedFormula))]
  57. public bool IsApproved { get; set; }
  58. private class ExTaxFormula : ComplexFormulaGenerator<Bill, double>
  59. {
  60. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  61. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.ExTax))
  62. .WithLink(x => x.BillLink.ID, x => x.ID);
  63. }
  64. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  65. [ComplexFormula(typeof(ExTaxFormula))]
  66. public double ExTax { get; set; }
  67. private class TaxFormula : ComplexFormulaGenerator<Bill, double>
  68. {
  69. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  70. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
  71. .WithLink(x => x.BillLink.ID, x => x.ID);
  72. }
  73. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  74. [ComplexFormula(typeof(TaxFormula))]
  75. public double Tax { get; set; }
  76. private class IncTaxFormula : ComplexFormulaGenerator<Bill, double>
  77. {
  78. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  79. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
  80. .WithLink(x => x.BillLink.ID, x => x.ID);
  81. }
  82. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  83. [ComplexFormula(typeof(IncTaxFormula))]
  84. public double IncTax { get; set; }
  85. private class AmountPaidFormula : ComplexFormulaGenerator<Bill, double>
  86. {
  87. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  88. Aggregate<BillPayment>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
  89. .WithLink(x => x.BillLink.ID, x => x.ID);
  90. }
  91. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  92. [ComplexFormula(typeof(AmountPaidFormula))]
  93. public double AmountPaid { get; set; }
  94. private class BalanceFormula : ComplexFormulaGenerator<Bill, double>
  95. {
  96. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  97. Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
  98. }
  99. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  100. [ComplexFormula(typeof(BalanceFormula))]
  101. public double Balance { get; set; }
  102. private class DocumentsFormula : ComplexFormulaGenerator<Bill, int>
  103. {
  104. public override IComplexFormulaNode<Bill, int> GetFormula() =>
  105. Count<BillDocument, Guid>(x => x.Property(x => x.ID))
  106. .WithLink(x => x.EntityLink.ID, x => x.ID);
  107. }
  108. [ComplexFormula(typeof(DocumentsFormula))]
  109. [IntegerEditor(Editable = Editable.Hidden)]
  110. public int Documents { get; set; }
  111. private class OutstandingApprovalsFormula : ComplexFormulaGenerator<Bill, string>
  112. {
  113. public override IComplexFormulaNode<Bill, string> GetFormula() =>
  114. Aggregate(AggregateCalculation.Concat,
  115. x => x.Property(x => x.Employee.Code),
  116. filter: new Filter<BillApproval>(x => x.Approved).IsEqualTo(DateTime.MinValue))
  117. .WithLink(x => x.Bill.ID, x => x.ID);
  118. }
  119. [ComplexFormula(typeof(OutstandingApprovalsFormula))]
  120. [Editable(Editable.Hidden)]
  121. public string OutstandingApprovals { get; set; }
  122. [NullEditor]
  123. [LoggableProperty]
  124. public DateTime Posted { get; set; }
  125. [NullEditor]
  126. [LoggableProperty]
  127. [RequiredColumn]
  128. public PostedStatus PostedStatus { get; set; }
  129. [NullEditor]
  130. public string PostedNote { get; set; }
  131. [NullEditor]
  132. public string PostedReference { get; set; }
  133. private static string BILLDATE = CoreUtils.GetFullPropertyName<Bill, DateTime>(x => x.BillDate, ".");
  134. private static string TERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.Terms.Calculation, ".");
  135. private static string SUPPLIERLINKTERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.SupplierLink.Terms.Calculation, ".");
  136. private static string AMOUNTPAID = CoreUtils.GetFullPropertyName<Bill, double>(x => x.AmountPaid, ".");
  137. private static string INCTAX = CoreUtils.GetFullPropertyName<Bill, double>(x => x.IncTax, ".");
  138. protected override void DoPropertyChanged(string name, object? before, object? after)
  139. {
  140. base.DoPropertyChanged(name, before, after);
  141. if (name.Equals(AMOUNTPAID))
  142. Balance = IncTax - (double)(after ?? 0.0);
  143. else if (name.Equals(INCTAX))
  144. Balance = (double)(after ?? 0.0) - AmountPaid;
  145. else if (name.Equals(BILLDATE))
  146. PaymentDate = CalculateTerms((DateTime)(after ?? ""),Terms.Calculation);
  147. else if (name.Equals(TERMSCALCULATION))
  148. PaymentDate = CalculateTerms(BillDate, (string)(after ?? ""));
  149. else if (name.Equals(SUPPLIERLINKTERMSCALCULATION))
  150. PaymentDate = CalculateTerms(BillDate, (string)(after ?? ""));
  151. }
  152. private DateTime CalculateTerms(DateTime date, string calculation)
  153. {
  154. if (string.IsNullOrWhiteSpace(calculation))
  155. return date;
  156. var variables = new Dictionary<string, object?>()
  157. {
  158. { "Date", date }
  159. };
  160. try
  161. {
  162. var expr = new CoreExpression(calculation);
  163. var eval = expr.Evaluate(variables);
  164. return (DateTime)(CoreUtils.ChangeType(eval, typeof(DateTime)) ?? date);
  165. }
  166. catch (Exception e)
  167. {
  168. Logger.Send(LogType.Information, "", $"Error in Formula: [{calculation}] ({e.Message})");
  169. return date;
  170. }
  171. }
  172. static Bill()
  173. {
  174. LinkedProperties.Register<Bill,PaymentTermsLink,Guid>(x=>x.SupplierLink.Terms,x=>x.ID, x=>x.Terms.ID);
  175. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Code, x=>x.Terms.Code);
  176. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Description, x=>x.Terms.Description);
  177. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Calculation, x=>x.Terms.Calculation);
  178. }
  179. //[NullEditor]
  180. //public string Issues { get; set; }
  181. [EditorSequence("Issues", 1)]
  182. public ManagedProblem Problem { get; set; }
  183. [Obsolete("Replaced with IsApproved aggregate")]
  184. public DateTime Approved { get; set; }
  185. }
  186. }