Bill.cs 10 KB

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