using System; using System.Linq; using InABox.Core; namespace Comal.Classes { [Caption("Labour")] public class JobBillOfMaterialsActivity : Entity, IRemotable, IPersistent, IJobActivity, IOneToMany, IOneToMany, ISequenceable, ILicense, IProblems { [NullEditor] public long Sequence { get; set; } [EntityRelationship(DeleteAction.Cascade)] [Editable(Editable.Hidden)] public JobLink JobLink { get; set; } [EntityRelationship(DeleteAction.Cascade)] [Editable(Editable.Hidden)] public JobBillOfMaterialsLink BillOfMaterials { get; set; } [EditorSequence(1)] [EntityRelationship(DeleteAction.SetNull)] [RequiredColumn] public AssignmentActivityLink ActivityLink { get; set; } [EditorSequence(2)] [RequiredColumn] [DurationEditor(Summary = Summary.Sum)] public TimeSpan Duration { get; set; } [EditorSequence(3)] [CurrencyEditor] public double HourlyCost { get; set; } [EditorSequence(4)] [CurrencyEditor(Summary = Summary.Sum)] public double TotalCost { get; set; } [EditorSequence(5)] [RequiredColumn] public JobScopeLink Scope { get; set; } private class JobITPLookup : LookupDefinitionGenerator { public override Filter DefineFilter(JobBillOfMaterialsItem[] items) { if (items.Length == 1) return new Filter(x => x.Job.ID).IsEqualTo(items.First().Job.ID); return LookupFactory.DefineFilter(); } public override Columns DefineFilterColumns() => Columns.None().Add(x => x.Job.ID); } [EditorSequence(6)] [EntityRelationship(DeleteAction.SetNull)] [LookupDefinition(typeof(JobITPLookup))] public JobITPLink ITP { get; set; } [EditorSequence("Issues", 1)] public ManagedProblem Problem { get; set; } private bool bChanging; protected override void DoPropertyChanged(string name, object? before, object? after) { if (bChanging) return; try { bChanging = true; if (name.Equals(nameof(Duration)) && after is TimeSpan duration) TotalCost = HourlyCost * duration.TotalHours; else if (name.Equals(nameof(HourlyCost)) && after is double cost) TotalCost = cost * Duration.TotalHours; else if (name.Equals(nameof(TotalCost)) && after is double total) { if (Duration == TimeSpan.Zero) Duration = TimeSpan.FromHours(1.0); HourlyCost = total / Duration.TotalHours; } } finally { bChanging = false; } base.DoPropertyChanged(name, before, after); } } }