Bill.cs 10 KB

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