Bill.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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",3)]
  40. [TimestampEditor]
  41. [Security(typeof(CanApproveBills), Editable = Editable.Disabled)]
  42. public DateTime Approved { get; set; }
  43. [EditorSequence("Additional", 4)]
  44. [Editable(Editable.Disabled)]
  45. public BillTypeLink BillType { get; set; }
  46. private class ExTaxFormula : ComplexFormulaGenerator<Bill, double>
  47. {
  48. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  49. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.ExTax))
  50. .WithLink(x => x.BillLink.ID, x => x.ID);
  51. }
  52. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  53. [ComplexFormula(typeof(ExTaxFormula))]
  54. public double ExTax { get; set; }
  55. private class TaxFormula : ComplexFormulaGenerator<Bill, double>
  56. {
  57. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  58. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
  59. .WithLink(x => x.BillLink.ID, x => x.ID);
  60. }
  61. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  62. [ComplexFormula(typeof(TaxFormula))]
  63. public double Tax { get; set; }
  64. private class IncTaxFormula : ComplexFormulaGenerator<Bill, double>
  65. {
  66. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  67. Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
  68. .WithLink(x => x.BillLink.ID, x => x.ID);
  69. }
  70. [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  71. [ComplexFormula(typeof(IncTaxFormula))]
  72. public double IncTax { get; set; }
  73. private class AmountPaidFormula : ComplexFormulaGenerator<Bill, double>
  74. {
  75. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  76. Aggregate<BillPayment>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
  77. .WithLink(x => x.BillLink.ID, x => x.ID);
  78. }
  79. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  80. [ComplexFormula(typeof(AmountPaidFormula))]
  81. public double AmountPaid { get; set; }
  82. private class BalanceFormula : ComplexFormulaGenerator<Bill, double>
  83. {
  84. public override IComplexFormulaNode<Bill, double> GetFormula() =>
  85. Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
  86. }
  87. [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
  88. [ComplexFormula(typeof(BalanceFormula))]
  89. public double Balance { get; set; }
  90. private class DocumentsFormula : ComplexFormulaGenerator<Bill, int>
  91. {
  92. public override IComplexFormulaNode<Bill, int> GetFormula() =>
  93. Count<BillDocument, Guid>(x => x.Property(x => x.ID))
  94. .WithLink(x => x.EntityLink.ID, x => x.ID);
  95. }
  96. [ComplexFormula(typeof(DocumentsFormula))]
  97. [IntegerEditor(Editable = Editable.Hidden)]
  98. public int Documents { get; set; }
  99. private class OutstandingApprovalsFormula : ComplexFormulaGenerator<Bill, string>
  100. {
  101. public override IComplexFormulaNode<Bill, string> GetFormula() =>
  102. Aggregate(AggregateCalculation.Concat,
  103. x => x.Property(x => x.Employee.Code),
  104. filter: new Filter<BillApproval>(x => x.Approved).IsEqualTo(DateTime.MinValue))
  105. .WithLink(x => x.Bill.ID, x => x.ID);
  106. }
  107. [ComplexFormula(typeof(OutstandingApprovalsFormula))]
  108. [Editable(Editable.Hidden)]
  109. public string OutstandingApprovals { get; set; }
  110. [NullEditor]
  111. [LoggableProperty]
  112. public DateTime Posted { get; set; }
  113. [NullEditor]
  114. [LoggableProperty]
  115. [RequiredColumn]
  116. public PostedStatus PostedStatus { get; set; }
  117. [NullEditor]
  118. public string PostedNote { get; set; }
  119. [NullEditor]
  120. public string PostedReference { get; set; }
  121. private static string BILLDATE = CoreUtils.GetFullPropertyName<Bill, DateTime>(x => x.BillDate, ".");
  122. private static string TERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.Terms.Calculation, ".");
  123. private static string SUPPLIERLINKTERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.SupplierLink.Terms.Calculation, ".");
  124. private static string AMOUNTPAID = CoreUtils.GetFullPropertyName<Bill, double>(x => x.AmountPaid, ".");
  125. private static string INCTAX = CoreUtils.GetFullPropertyName<Bill, double>(x => x.IncTax, ".");
  126. protected override void DoPropertyChanged(string name, object? before, object? after)
  127. {
  128. base.DoPropertyChanged(name, before, after);
  129. if (name.Equals(AMOUNTPAID))
  130. Balance = IncTax - (double)(after ?? 0.0);
  131. else if (name.Equals(INCTAX))
  132. Balance = (double)(after ?? 0.0) - AmountPaid;
  133. else if (name.Equals(BILLDATE))
  134. PaymentDate = CalculateTerms((DateTime)(after ?? ""),Terms.Calculation);
  135. else if (name.Equals(TERMSCALCULATION))
  136. PaymentDate = CalculateTerms(BillDate, (string)(after ?? ""));
  137. else if (name.Equals(SUPPLIERLINKTERMSCALCULATION))
  138. PaymentDate = CalculateTerms(BillDate, (string)(after ?? ""));
  139. }
  140. private DateTime CalculateTerms(DateTime date, string calculation)
  141. {
  142. if (string.IsNullOrWhiteSpace(calculation))
  143. return date;
  144. var variables = new Dictionary<string, object?>()
  145. {
  146. { "Date", date }
  147. };
  148. try
  149. {
  150. var expr = new CoreExpression(calculation);
  151. var eval = expr.Evaluate(variables);
  152. return (DateTime)(CoreUtils.ChangeType(eval, typeof(DateTime)) ?? date);
  153. }
  154. catch (Exception e)
  155. {
  156. Logger.Send(LogType.Information, "", $"Error in Formula: [{calculation}] ({e.Message})");
  157. return date;
  158. }
  159. }
  160. static Bill()
  161. {
  162. LinkedProperties.Register<Bill,PaymentTermsLink,Guid>(x=>x.SupplierLink.Terms,x=>x.ID, x=>x.Terms.ID);
  163. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Code, x=>x.Terms.Code);
  164. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Description, x=>x.Terms.Description);
  165. LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Calculation, x=>x.Terms.Calculation);
  166. }
  167. //[NullEditor]
  168. //public string Issues { get; set; }
  169. [EditorSequence("Issues", 1)]
  170. public ManagedProblem Problem { get; set; }
  171. }
  172. }