|  | @@ -103,6 +103,14 @@ namespace Comal.Classes
 | 
	
		
			
				|  |  |          [ComplexFormula(typeof(BalanceFormula))]
 | 
	
		
			
				|  |  |          public double Balance { get; set; }
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  | +        [EditorSequence(13)]
 | 
	
		
			
				|  |  | +        [LookupDefinition(typeof(AccountsReceivablePaymentTermsLookup<Invoice>))]
 | 
	
		
			
				|  |  | +        public PaymentTermsLink Terms { get; set; }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        [EditorSequence(14)]
 | 
	
		
			
				|  |  | +        [DateEditor(Visible = Visible.Default, TodayVisible = true)]
 | 
	
		
			
				|  |  | +        public DateTime DueDate { get; set; }
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          [NullEditor]
 | 
	
		
			
				|  |  |          [LoggableProperty]
 | 
	
		
			
				|  |  |          [RequiredColumn]
 | 
	
	
		
			
				|  | @@ -135,14 +143,57 @@ namespace Comal.Classes
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              return string.Format("{0}: {1}", Number, Description);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  | +        private static string DATE = CoreUtils.GetFullPropertyName<Invoice, DateTime>(x => x.Date, ".");
 | 
	
		
			
				|  |  | +        private static string CUSTOMERLINKTERMSCALCULATION = CoreUtils.GetFullPropertyName<Invoice, String>(x => x.CustomerLink.Terms.Calculation, ".");
 | 
	
		
			
				|  |  | +        private static string TERMSCALCULATION = CoreUtils.GetFullPropertyName<Invoice, String>(x => x.Terms.Calculation, ".");
 | 
	
		
			
				|  |  | +        private static string AMOUNTPAID = CoreUtils.GetFullPropertyName<Invoice, double>(x => x.AmountPaid, ".");
 | 
	
		
			
				|  |  | +        private static string INCTAX = CoreUtils.GetFullPropertyName<Invoice, double>(x => x.IncTax, ".");
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          protected override void DoPropertyChanged(string name, object? before, object? after)
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              base.DoPropertyChanged(name, before, after);
 | 
	
		
			
				|  |  | -            if (name.Equals("IncTax"))
 | 
	
		
			
				|  |  | -                Balance = (double)after - AmountPaid;
 | 
	
		
			
				|  |  | -            else if (name.Equals("AmountPaid"))
 | 
	
		
			
				|  |  | -                Balance = IncTax - (double)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(DATE))
 | 
	
		
			
				|  |  | +                DueDate =  CalculateTerms((DateTime)(after ?? ""),Terms.Calculation);
 | 
	
		
			
				|  |  | +            else if (name.Equals(TERMSCALCULATION))
 | 
	
		
			
				|  |  | +                DueDate =  CalculateTerms(Date, (string)(after ?? ""));
 | 
	
		
			
				|  |  | +            else if (name.Equals(CUSTOMERLINKTERMSCALCULATION))
 | 
	
		
			
				|  |  | +                DueDate =  CalculateTerms(Date, (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 Invoice()
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            LinkedProperties.Register<Invoice,PaymentTermsLink,Guid>(x=>x.CustomerLink.Terms,x=>x.ID, x=>x.Terms.ID);
 | 
	
		
			
				|  |  | +            LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.CustomerLink.Terms,x=>x.Code, x=>x.Terms.Code);
 | 
	
		
			
				|  |  | +            LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.CustomerLink.Terms,x=>x.Description, x=>x.Terms.Description);
 | 
	
		
			
				|  |  | +            LinkedProperties.Register<Invoice,PaymentTermsLink,String>(x=>x.CustomerLink.Terms,x=>x.Calculation, x=>x.Terms.Calculation);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  |      }
 |