using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using InABox.Clients; using InABox.Core; namespace Comal.Classes { public class QuoteExTax : CoreAggregate { public override Expression> Aggregate => x => x.ProposalExTax; public override AggregateCalculation Calculation => AggregateCalculation.Sum; public override Dictionary>, Expression>> Links => new Dictionary>, Expression>> () { { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID } }; public override Filter Filter => new Filter(x => x.Default).IsEqualTo(true); } public class QuoteTax : CoreAggregate { public override Expression> Aggregate => x => x.ProposalTax; public override AggregateCalculation Calculation => AggregateCalculation.Sum; public override Dictionary>, Expression>> Links => new Dictionary>, Expression>>() { { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID } }; public override Filter Filter => new Filter(x => x.Default).IsEqualTo(true); } public class QuoteIncTax : CoreAggregate { public override Expression> Aggregate => x => x.ProposalIncTax; public override AggregateCalculation Calculation => AggregateCalculation.Sum; public override Dictionary>, Expression>> Links => new Dictionary>, Expression>>() { { QuoteProposal => QuoteProposal.Quote.ID, Quote => Quote.ID } }; public override Filter Filter => new Filter(x => x.Default).IsEqualTo(true); } [UserTracking("Quote Management")] public class Quote : Entity, IRemotable, IPersistent, IStringAutoIncrement, ILicense, IDataEntryInstance { [UniqueCodeEditor(Visible = Visible.Default, Editable = Editable.Enabled)] [EditorSequence(0)] public string Number { get; set; } [TextBoxEditor] [EditorSequence(1)] public string Title { get; set; } [EditorSequence(2)] public CustomerLink Customer { get; set; } private class ContactLookup : LookupDefinitionGenerator { public override Filter DefineFilter(Quote[] items) { if (items == null || !items.Any()) return new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid); var customer = items.First().Customer; if (!customer.IsValid()) return new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid); var contacts = new Client().Query( new Filter(x => x.Customer.ID).IsEqualTo(customer.ID), new Columns(x => x.Contact.ID) ); var ids = contacts.Rows.Select(x => x.Get(c => c.Contact.ID)); if (!ids.Any()) return new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid); return new Filter(x => x.ID).InList(ids.ToArray()); } public override Columns DefineFilterColumns() => new Columns(x => x.Customer.ID); } [EditorSequence(3)] [LookupDefinition(typeof(ContactLookup))] public ContactLink Contact { get; set; } [EditorSequence(4)] public Address SiteAddress { get; set; } [EditorSequence(5)] public AccountLink Account { get; set; } [DoubleEditor(Editable = Editable.Hidden)] [EditorSequence(6)] [Aggregate(typeof(QuoteExTax))] public double ExTax { get; set; } [DoubleEditor(Editable = Editable.Hidden)] [EditorSequence(7)] [Aggregate(typeof(QuoteTax))] public double Tax { get; set; } [DoubleEditor(Editable = Editable.Hidden)] [EditorSequence(8)] [Aggregate(typeof(QuoteIncTax))] public double IncTax { get; set; } [EditorSequence(9)] public QuoteStatusLink Status { get; set; } #region Management [TimestampEditor] [EditorSequence("Management", 1)] public DateTime DataEntered { get; set; } [DateEditor] [EditorSequence("Management", 2)] public DateTime ReceivedDate { get; set; } [DateEditor] [EditorSequence("Management", 3)] public DateTime DueDate { get; set; } [DateEditor] [EditorSequence("Management", 4)] public DateTime QuoteDate { get; set; } [DateEditor] [EditorSequence("Management", 5)] public DateTime ArchivedDate { get; set; } [EditorSequence("Management", 6)] public EmployeeLink Salesperson { get; set; } #endregion #region Notes [NotesEditor] [EditorSequence("Notes", 1)] public string[] Notes { get; set; } #endregion [DoNotPersist] [DoNotSerialize] public static String NumberPrefix { get; set; } public Expression> AutoIncrementField() => x => x.Number; public Filter AutoIncrementFilter() => null; public string AutoIncrementPrefix() => NumberPrefix; public string AutoIncrementFormat() => "{0:D6}"; protected override void Init() { base.Init(); Customer.Account.PropertyChanged += Account_PropertyChanged; Customer.Delivery.PropertyChanged += Delivery_PropertyChanged; } private void Delivery_PropertyChanged(object sender, PropertyChangedEventArgs e) { var value = CoreUtils.GetPropertyValue(Customer.Delivery, e.PropertyName); CoreUtils.SetPropertyValue(SiteAddress, e.PropertyName, value); } private void Account_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName.Equals("ID")) Account.ID = Customer.Account.ID; else if (e.PropertyName.Equals("Code")) Account.Code = Customer.Account.Code; else if (e.PropertyName.Equals("Name")) Account.Name = Customer.Account.Name; } } }