| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 | 
							- using System;
 
- using System.Collections.Generic;
 
- using InABox.Core;
 
- namespace Comal.Classes
 
- {
 
-     [UserTracking("Accounts Payable")]
 
-     public class Bill : Entity, IPersistent, IRemotable, ILicense<AccountsPayableLicense>, IDataEntryInstance, IPostable, IExportable, 
 
-         IProblems<ManagedProblem>
 
-     {
 
-      
 
-         [RequiredColumn]
 
-         [EditorSequence(1)]
 
-         [EntityRelationship(DeleteAction.Cascade)]
 
-         public SupplierLink SupplierLink { get; set; }
 
-         
 
-         [EditorSequence(2)]
 
-         [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]
 
-         public string Number { get; set; }
 
-         [EditorSequence(3)]
 
-         [MemoEditor]
 
-         public string Description { get; set; }
 
-         [EditorSequence(4)]
 
-         [DateEditor]
 
-         public DateTime BillDate { get; set; }
 
-         
 
-         [EditorSequence(5)]
 
-         [LookupDefinition(typeof(AccountsPayablePaymentTermsLookup<Bill>))]
 
-         public PaymentTermsLink Terms { get; set; }
 
-         
 
-         [EditorSequence(6)]
 
-         [DateEditor]
 
-         public DateTime PaymentDate { get; set; }
 
-         [EditorSequence(7)]
 
-         [DateEditor]
 
-         public DateTime AccountingDate { get; set; }
 
-         
 
-         [EditorSequence("Additional",1)]
 
-         [TimestampEditor]
 
-         public DateTime DataEntered { get; set; }
 
-         
 
-         [EditorSequence("Additional",2)]
 
-         [TimestampEditor]
 
-         [Security(typeof(CanCheckBills), Editable = Editable.Disabled)]
 
-         public DateTime Checked { get; set; }
 
-         [EditorSequence("Additional", 4)]
 
-         [Editable(Editable.Disabled)]
 
-         [RequiredColumn]
 
-         public BillApprovalSetLink ApprovalSet { get; set; }
 
-         private class IsApprovedFormula : ComplexFormulaGenerator<Bill, bool>
 
-         {
 
-             public override IComplexFormulaNode<Bill, bool> GetFormula() =>
 
-                 If<int>(x => x.Count<BillApproval, Guid>(
 
-                         x => x.Property(x => x.ID),
 
-                         filter: new Filter<BillApproval>(x => x.Approved).IsEqualTo(DateTime.MinValue))
 
-                     .WithLink(x => x.Bill.ID, x => x.ID),
 
-                     Condition.Equals,
 
-                     x => x.Constant(0))
 
-                 .Then(Constant(true))
 
-                 .Else(Constant(false));
 
-         }
 
-         [Editable(Editable.Hidden)]
 
-         [ComplexFormula(typeof(IsApprovedFormula))]
 
-         public bool IsApproved { get; set; }
 
-         
 
-         private class ExTaxFormula : ComplexFormulaGenerator<Bill, double>
 
-         {
 
-             public override IComplexFormulaNode<Bill, double> GetFormula() =>
 
-                 Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.ExTax))
 
-                     .WithLink(x => x.BillLink.ID, x => x.ID);
 
-         }
 
-         [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
 
-         [ComplexFormula(typeof(ExTaxFormula))]
 
-         public double ExTax { get; set; }
 
-         private class TaxFormula : ComplexFormulaGenerator<Bill, double>
 
-         {
 
-             public override IComplexFormulaNode<Bill, double> GetFormula() =>
 
-                 Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
 
-                     .WithLink(x => x.BillLink.ID, x => x.ID);
 
-         }
 
-         [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
 
-         [ComplexFormula(typeof(TaxFormula))]
 
-         public double Tax { get; set; }
 
-         private class IncTaxFormula : ComplexFormulaGenerator<Bill, double>
 
-         {
 
-             public override IComplexFormulaNode<Bill, double> GetFormula() =>
 
-                 Aggregate<BillLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
 
-                     .WithLink(x => x.BillLink.ID, x => x.ID);
 
-         }
 
-         [DoubleEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
 
-         [ComplexFormula(typeof(IncTaxFormula))]
 
-         public double IncTax { get; set; }
 
-         private class AmountPaidFormula : ComplexFormulaGenerator<Bill, double>
 
-         {
 
-             public override IComplexFormulaNode<Bill, double> GetFormula() =>
 
-                 Aggregate<BillPayment>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
 
-                     .WithLink(x => x.BillLink.ID, x => x.ID);
 
-         }
 
-         [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
 
-         [ComplexFormula(typeof(AmountPaidFormula))]
 
-         public double AmountPaid { get; set; }
 
-         private class BalanceFormula : ComplexFormulaGenerator<Bill, double>
 
-         {
 
-             public override IComplexFormulaNode<Bill, double> GetFormula() =>
 
-                 Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
 
-         }
 
-         [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
 
-         [ComplexFormula(typeof(BalanceFormula))]
 
-         public double Balance { get; set; }
 
-         private class DocumentsFormula : ComplexFormulaGenerator<Bill, int>
 
-         {
 
-             public override IComplexFormulaNode<Bill, int> GetFormula() =>
 
-                 Count<BillDocument, Guid>(x => x.Property(x => x.ID))
 
-                     .WithLink(x => x.EntityLink.ID, x => x.ID);
 
-         }
 
-         [ComplexFormula(typeof(DocumentsFormula))]
 
-         [IntegerEditor(Editable = Editable.Hidden)]
 
-         public int Documents { get; set; }
 
-         private class OutstandingApprovalsFormula : ComplexFormulaGenerator<Bill, string>
 
-         {
 
-             public override IComplexFormulaNode<Bill, string> GetFormula() =>
 
-                 Aggregate(AggregateCalculation.Concat,
 
-                     x => x.Property(x => x.Employee.Code),
 
-                     filter: new Filter<BillApproval>(x => x.Approved).IsEqualTo(DateTime.MinValue))
 
-                 .WithLink(x => x.Bill.ID, x => x.ID);
 
-         }
 
-         [ComplexFormula(typeof(OutstandingApprovalsFormula))]
 
-         [Editable(Editable.Hidden)]
 
-         public string OutstandingApprovals { get; set; }
 
-         [NullEditor]
 
-         [LoggableProperty]
 
-         public DateTime Posted { get; set; }
 
-         [NullEditor]
 
-         [LoggableProperty]
 
-         [RequiredColumn]
 
-         public PostedStatus PostedStatus { get; set; }
 
-         [NullEditor]
 
-         public string PostedNote { get; set; }
 
-         [NullEditor]
 
-         public string PostedReference { get; set; }
 
-         private static string BILLDATE = CoreUtils.GetFullPropertyName<Bill, DateTime>(x => x.BillDate, ".");
 
-         private static string TERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.Terms.Calculation, ".");
 
-         private static string SUPPLIERLINKTERMSCALCULATION = CoreUtils.GetFullPropertyName<Bill, String>(x => x.SupplierLink.Terms.Calculation, ".");
 
-         private static string AMOUNTPAID = CoreUtils.GetFullPropertyName<Bill, double>(x => x.AmountPaid, ".");
 
-         private static string INCTAX = CoreUtils.GetFullPropertyName<Bill, double>(x => x.IncTax, ".");
 
-         
 
-         protected override void DoPropertyChanged(string name, object? before, object? after)
 
-         {
 
-             base.DoPropertyChanged(name, before, after);
 
-             if (name.Equals(AMOUNTPAID))
 
-                 Balance = IncTax - (double)(after ?? 0.0);
 
-             else if (name.Equals(INCTAX))
 
-                 Balance = (double)(after ?? 0.0) - AmountPaid;
 
-             else if (name.Equals(BILLDATE))
 
-                 PaymentDate =  CalculateTerms((DateTime)(after ?? ""),Terms.Calculation);
 
-             else if (name.Equals(TERMSCALCULATION))
 
-                 PaymentDate =  CalculateTerms(BillDate, (string)(after ?? ""));
 
-             else if (name.Equals(SUPPLIERLINKTERMSCALCULATION))
 
-                 PaymentDate =  CalculateTerms(BillDate, (string)(after ?? ""));
 
-         }
 
-         private DateTime CalculateTerms(DateTime date, string calculation)
 
-         {
 
-             if (string.IsNullOrWhiteSpace(calculation))
 
-                 return date;
 
-             
 
-             var variables = new Dictionary<string, object?>()
 
-             {
 
-                 { "Date", date }
 
-             };
 
-             
 
-             try
 
-             {
 
-                 var expr = new CoreExpression(calculation);
 
-                 var eval = expr.Evaluate(variables);
 
-                 return (DateTime)(CoreUtils.ChangeType(eval, typeof(DateTime)) ?? date);
 
-             }
 
-             catch (Exception e)
 
-             {
 
-                 Logger.Send(LogType.Information, "", $"Error in Formula: [{calculation}] ({e.Message})");
 
-                 return date;
 
-             }
 
-         }
 
-         static Bill()
 
-         {
 
-             LinkedProperties.Register<Bill,PaymentTermsLink,Guid>(x=>x.SupplierLink.Terms,x=>x.ID, x=>x.Terms.ID);
 
-             LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Code, x=>x.Terms.Code);
 
-             LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Description, x=>x.Terms.Description);
 
-             LinkedProperties.Register<Bill,PaymentTermsLink,String>(x=>x.SupplierLink.Terms,x=>x.Calculation, x=>x.Terms.Calculation);
 
-         }
 
-         //[NullEditor]
 
-         //public string Issues { get; set; }
 
-         
 
-         [EditorSequence("Issues", 1)]
 
-         public ManagedProblem Problem { get; set; }
 
-         [Obsolete("Replaced with IsApproved aggregate")]
 
-         public DateTime Approved { get; set; }
 
-     }
 
- }
 
 
  |