Pārlūkot izejas kodu

Fixing up invoice formulae and aggregates

Kenric Nugteren 1 gadu atpakaļ
vecāks
revīzija
382cbae731
1 mainītis faili ar 27 papildinājumiem un 66 dzēšanām
  1. 27 66
      prs.classes/Entities/Invoice/Invoice.cs

+ 27 - 66
prs.classes/Entities/Invoice/Invoice.cs

@@ -5,68 +5,6 @@ using InABox.Core;
 
 namespace Comal.Classes
 {
-    // public class InvoiceChargeableHours : CoreAggregate<Invoice, Assignment, TimeSpan>
-    // {
-    //     public override Expression<Func<Assignment, TimeSpan>> Aggregate => x => x.ChargeableHours;
-    //
-    //     public override AggregateCalculation Calculation => AggregateCalculation.Sum;
-    //
-    //     public override Dictionary<Expression<Func<Assignment, object>>, Expression<Func<Invoice, object>>> Links =>
-    //         new Dictionary<Expression<Func<Assignment, object>>, Expression<Func<Invoice, object>>>()
-    //         {
-    //             { Assignment => Assignment.Invoice.ID, Invoice => Invoice.ID }
-    //         };
-    //
-    //     public override Filter<Assignment>? Filter => new Filter<Assignment>(x => x.Chargeable).IsEqualTo(true);
-    // }
-
-    public class InvoiceTax : CoreAggregate<Invoice, InvoiceLine, double>
-    {
-        public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.Tax;
-
-        public override AggregateCalculation Calculation => AggregateCalculation.Sum;
-
-        public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>> Links =>
-            new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>>()
-            {
-                { InvoiceLine => InvoiceLine.InvoiceLink.ID, Invoice => Invoice.ID }
-            };
-    }
-
-    public class InvoiceIncTax : CoreAggregate<Invoice, InvoiceLine, double>
-    {
-        public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.IncTax;
-
-        public override AggregateCalculation Calculation => AggregateCalculation.Sum;
-
-        public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>> Links =>
-            new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<Invoice, object>>>()
-            {
-                { InvoiceLine => InvoiceLine.InvoiceLink.ID, Invoice => Invoice.ID }
-            };
-    }
-
-    public class InvoiceAmountPaid : CoreAggregate<Invoice, InvoiceReceipt, double>
-    {
-        public override Expression<Func<InvoiceReceipt, double>> Aggregate => x => x.Amount;
-
-        public override AggregateCalculation Calculation => AggregateCalculation.Sum;
-
-        public override Dictionary<Expression<Func<InvoiceReceipt, object>>, Expression<Func<Invoice, object>>> Links =>
-            new Dictionary<Expression<Func<InvoiceReceipt, object>>, Expression<Func<Invoice, object>>>()
-            {
-                { InvoiceReceipt => InvoiceReceipt.InvoiceLink.ID, Invoice => Invoice.ID }
-            };
-    }
-
-    public class InvoiceBalance : IFormula<Invoice, double>
-    {
-        public Expression<Func<Invoice, double>> Value => x => x.IncTax;
-        public Expression<Func<Invoice, double>>[] Modifiers => new Expression<Func<Invoice, double>>[] { x => x.AmountPaid };
-        public FormulaOperator Operator => FormulaOperator.Subtract;
-        public FormulaType Type => FormulaType.Virtual;
-    }
-
     public enum InvoiceType
     {
         Standard,
@@ -119,27 +57,50 @@ namespace Comal.Classes
         [ComplexFormula(typeof(ExTaxFormula))]
         public double ExTax { get; set; }
 
+        private class TaxFormula : ComplexFormulaGenerator<Invoice, double>
+        {
+            public override IComplexFormulaNode<Invoice, double> GetFormula() =>
+                Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.Tax))
+                    .WithLink(x => x.InvoiceLink.ID, x => x.ID);
+        }
         [EditorSequence(9)]
         [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
-        [Aggregate(typeof(InvoiceTax))]
+        [ComplexFormula(typeof(TaxFormula))]
         public double Tax { get; set; }
 
+        private class IncTaxFormula : ComplexFormulaGenerator<Invoice, double>
+        {
+            public override IComplexFormulaNode<Invoice, double> GetFormula() =>
+                Aggregate<InvoiceLine>(AggregateCalculation.Sum, x => x.Property(x => x.IncTax))
+                    .WithLink(x => x.InvoiceLink.ID, x => x.ID);
+        }
         [EditorSequence(10)]
         [CurrencyEditor(Visible = Visible.Default, Editable = Editable.Hidden, Summary = Summary.Sum)]
-        [Aggregate(typeof(InvoiceIncTax))]
+        [ComplexFormula(typeof(IncTaxFormula))]
         public double IncTax { get; set; }
 
+        private class AmountPaidFormula : ComplexFormulaGenerator<Invoice, double>
+        {
+            public override IComplexFormulaNode<Invoice, double> GetFormula() =>
+                Aggregate<InvoiceReceipt>(AggregateCalculation.Sum, x => x.Property(x => x.Amount))
+                    .WithLink(x => x.InvoiceLink.ID, x => x.ID);
+        }
         [EditorSequence(11)]
         [CurrencyEditor(Editable = Editable.Hidden)]
-        [Aggregate(typeof(InvoiceAmountPaid))]
+        [ComplexFormula(typeof(AmountPaidFormula))]
         public double AmountPaid { get; set; }
 
+        private class BalanceFormula : ComplexFormulaGenerator<Invoice, double>
+        {
+            public override IComplexFormulaNode<Invoice, double> GetFormula() =>
+                Formula(FormulaOperator.Subtract, Property(x => x.IncTax), Property(x => x.AmountPaid));
+        }
         /// <summary>
         /// Balance = <see cref="IncTax"/> - <see cref="AmountPaid"/>.
         /// </summary>
         [EditorSequence(12)]
         [CurrencyEditor(Editable = Editable.Hidden, Summary = Summary.Sum)]
-        [Formula(typeof(InvoiceBalance))]
+        [ComplexFormula(typeof(BalanceFormula))]
         public double Balance { get; set; }
         
         [NullEditor]