| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 | using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Security;using InABox.Clients;using InABox.Core;namespace Comal.Classes{        public class JobScopeInvoiceExTax : CoreAggregate<JobScope, InvoiceLine, double>    {        public override Expression<Func<InvoiceLine, double>> Aggregate => x => x.ExTax;        public override AggregateCalculation Calculation => AggregateCalculation.Sum;        public override Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<JobScope, object>>> Links =>            new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<JobScope, object>>>()            {                { InvoiceLine => InvoiceLine.Scope.ID, JobPrice => JobPrice.ID }            };    }    public class JobScopeInvoiceTax : CoreAggregate<JobScope, 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<JobScope, object>>> Links =>            new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<JobScope, object>>>()            {                { InvoiceLine => InvoiceLine.Scope.ID, JobPrice => JobPrice.ID }            };    }    public class JobScopeInvoiceIncTax : CoreAggregate<JobScope, 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<JobScope, object>>> Links =>            new Dictionary<Expression<Func<InvoiceLine, object>>, Expression<Func<JobScope, object>>>()            {                { InvoiceLine => InvoiceLine.Scope.ID, JobPrice => JobPrice.ID }            };    }    public class JobScopeUninvoicedExTax : IFormula<JobScope, double>    {        public Expression<Func<JobScope, double>> Value => x => x.ExTax;        public Expression<Func<JobScope, double>>[] Modifiers => new Expression<Func<JobScope, double>>[] { x => x.InvoiceExTax };        public FormulaOperator Operator => FormulaOperator.Subtract;        public FormulaType Type => FormulaType.Virtual;    }    public interface IJobScopedItem    {        JobLink JobLink { get; set; }        JobScopeLink JobScope { get; set; }    }    [UserTracking(typeof(Job))]    public class JobScope : Entity, IRemotable, IPersistent, ITaxable, IStringAutoIncrement<JobScope>, ILicense<ProjectManagementLicense>    {        [NullEditor]        public InternalJobLink Job { get; set; }        [EnumLookupEditor(typeof(JobScopeType), Visible = Visible.Default)]        [EditorSequence(1)]        public JobScopeType Type { get; set; } = JobScopeType.Variation;        [CodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)]        [EditorSequence(2)]        public string Number { get; set; }        [MemoEditor(Visible = Visible.Default)]        [EditorSequence(3)]        public string Description { get; set; }                [EditorSequence(4)]        [TextBoxEditor]        public string SourceRef{ get; set; }                 private class JobScopeLookup : LookupDefinitionGenerator<JobScope, JobScope>        {            public override Filter<JobScope> DefineFilter(JobScope[] items)            {                if (items?.Any() != true)                    return Filter.None<JobScope>();                var data = Client.Query(                    new Filter<JobScope>(x=>x.Job.ID).IsEqualTo(items.First().Job.ID),                    Columns.None<JobScope>().Add(x => x.ID).Add(x => x.Parent.ID));                var nodes = new CoreTreeNodes();                nodes.Load<JobScope>(data, x => x.ID, x => x.Parent.ID);                var exclusions = items.SelectMany(x => nodes.GetChildren(x.ID)).ToArray();                var ids = exclusions.Select(x => x.ID).ToArray();                return new Filter<JobScope>(x=>x.Job.ID).IsEqualTo(items.First().Job.ID).And(x => x.ID).NotInList(ids);            }            public override Columns<JobScope> DefineFilterColumns()                => Columns.None<JobScope>().Add(x => x.ID).Add(x => x.Parent.ID).Add(x => x.Number);        }                [LookupDefinition(typeof(JobScopeLookup))]        [EditorSequence(4)]        public JobScopeLink Parent { get; set; }                [CurrencyEditor(Summary = Summary.Sum, Visible = Visible.Default)]        [EditorSequence(5)]        public double ExTax { get; set; }        [EditorSequence(6)]        public TaxCodeLink TaxCode { get; set; }                [CurrencyEditor(Summary = Summary.Sum, Visible = Visible.Optional)]        [EditorSequence(7)]        public double IncTax { get; set; }        [EditorSequence(8)]        public JobScopeStatusLink Status { get; set; }                [CurrencyEditor(Visible = Visible.Default, Editable = Editable.Hidden, Summary = Summary.Sum)]        [Aggregate(typeof(JobScopeInvoiceExTax))]        public double InvoiceExTax { get; set; }        [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]        [Aggregate(typeof(JobScopeInvoiceTax))]        public double InvoiceTax { get; set; }        [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]        [Aggregate(typeof(JobScopeInvoiceIncTax))]        public double InvoiceIncTax { get; set; }        [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]        [Formula(typeof(JobScopeUninvoicedExTax))]        public double UninvoiceIncTax { get; set; }        private class MaterialsExTaxFormula : ComplexFormulaGenerator<JobScope, double>        {            public override IComplexFormulaNode<JobScope, double> GetFormula() =>                Formula(FormulaOperator.Subtract,                    Aggregate<StockMovement>(AggregateCalculation.Sum, x => x.Property(x => x.Value))                        .WithFilter(                            new Filter<StockMovement>(x => x.Type).IsEqualTo(StockMovementType.Issue))                        .WithLink(x => x.JobScope.ID, x => x.ID));        }        [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]        [ComplexFormula(typeof(MaterialsExTaxFormula))]        public double MaterialsExTax { get; set; }        private class UninvoicedMaterial : ComplexFormulaGenerator<JobScope, double>        {            public override IComplexFormulaNode<JobScope, double> GetFormula() =>                Formula(FormulaOperator.Subtract,                    Aggregate<StockMovement>(AggregateCalculation.Sum, x => x.Property(x => x.Value))                        .WithFilter(                            new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Guid.Empty)                                .And(x => x.Type).IsEqualTo(StockMovementType.Issue))                        .WithLink(x => x.JobScope.ID, x => x.ID));        }        [CurrencyEditor(Visible = Visible.Optional, Editable = Editable.Hidden, Summary = Summary.Sum)]        [ComplexFormula(typeof(UninvoicedMaterial))]        public double UninvoicedMaterialsExTax { get; set; }        public Expression<Func<JobScope, string>> AutoIncrementField() => x => x.Number;        public Filter<JobScope> AutoIncrementFilter() =>  new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Job.ID);        public String AutoIncrementPrefix() => "";        public string AutoIncrementFormat() => "{0:D3}";                [NullEditor]        public double TaxRate { get; set; }        [NullEditor(Summary = Summary.Sum, Visible = Visible.Optional)]        public double Tax { get; set; }                static JobScope()        {            LinkedProperties.Register<JobScope, TaxCodeLink, double>(x=>x.TaxCode, x => x.Rate, x => x.TaxRate);        }        public static void LinkScopeProperties<TScoped>() where TScoped : class, IJobScopedItem        {            LinkedProperties.Register<TScoped, JobScopeLink, Guid>(ass => ass.JobLink.DefaultScope, scope => scope.ID, ass => ass.JobScope.ID);            LinkedProperties.Register<TScoped, JobScopeLink, String>(ass => ass.JobLink.DefaultScope, scope => scope.Number, ass => ass.JobScope.Number);            LinkedProperties.Register<TScoped, JobScopeLink, String>(ass => ass.JobLink.DefaultScope, scope => scope.Description, ass => ass.JobScope.Description);        }    }}
 |