using System; using System.Collections.Generic; using System.Linq; using System.Windows.Controls; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; namespace PRSDesktop { /// /// Interaction logic for QuoteDetails.xaml /// public partial class QuoteDetails : UserControl, IPanel, IQuotePage, IDynamicEditorHost { private CoreRow[] _rows; public QuoteDetails() { InitializeComponent(); } public event DataModelUpdateEvent OnUpdateDataModel; public bool IsReady { get; set; } public void CreateToolbarButtons(IPanelHost host) { } public string SectionName => "Quotes"; public DataModel DataModel(Selection selection) { return new QuoteDataModel(new Filter(x => x.ID).IsEqualTo(ParentID)); } public void Heartbeat(TimeSpan time) { } public void Refresh() { new Client().Query( new Filter(x => x.ID).IsEqualTo(ParentID), new Columns( col => col.Number, col => col.Customer.ID, col => col.Customer.Code, col => col.Customer.Name, col => col.SiteAddress.Street, col => col.SiteAddress.City, col => col.SiteAddress.State, col => col.SiteAddress.PostCode, col => col.Account.ID, col => col.Account.Code, col => col.Account.Name, col => col.Title, col => col.Notes, col => col.Status.ID, col => col.ExTax ), null, (o, e) => { UpdateScreen(o, e); } ); Proposals.ParentID = ParentID; } public Dictionary Selected() { return new Dictionary() { { typeof(Quote).EntityName(), _rows } }; } public void Setup() { UpdateScreen(null, null); var statuscodes = new Dictionary { { Guid.Empty, "" } }; var statuses = new Client().Query(); foreach (var row in statuses.Rows) statuscodes[row.Get(x => x.ID)] = row.Get(x => x.Description); Status.ItemsSource = statuscodes; Proposals.Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns); Proposals.Refresh(true, false); Title.EditorDefinition = new TextBoxEditor(); Title.ColumnName = "Title"; Title.Configure(); Title.OnEditorValueChanged += Title_OnEditorValueChanged; Title.Loaded = true; Customer.EditorDefinition = new CodePopupEditor(typeof(Customer)); Customer.ColumnName = "Customer.ID"; Customer.CodeColumn = "Code"; Customer.OtherColumns["Code"] = "Customer.Code"; Customer.OtherColumns["Name"] = "Customer.Name"; Customer.OtherColumns["Account.ID"] = "Account.ID"; Customer.OtherColumns["Account.Code"] = "Account.Code"; Customer.OtherColumns["Account.Name"] = "Account.Name"; Customer.Host = this; Customer.Configure(); Customer.OnEditorValueChanged += Customer_OnEditorValueChanged; Customer.Loaded = true; Account.EditorDefinition = new CodePopupEditor(typeof(Customer)); Account.ColumnName = "Account.ID"; Account.CodeColumn = "Code"; Account.OtherColumns["Code"] = "Account.Code"; Account.OtherColumns["Name"] = "Account.Name"; Account.Host = this; Account.Configure(); Account.OnEditorValueChanged += Account_OnEditorValueChanged; Account.Loaded = true; } public void Shutdown() { } public Guid ParentID { get; set; } public Dictionary DataEnvironment() { return new Dictionary(); } private void UpdateScreen(CoreTable o, Exception e) { _rows = o?.Rows.ToArray() ?? new CoreRow[] { }; Dispatcher.Invoke(() => { var bIsReady = IsReady; IsReady = false; Title.Value = o?.Rows.FirstOrDefault()?.Get(col => col.Title) ?? ""; Customer.Value = o?.Rows.FirstOrDefault()?.Get(col => col.Customer.ID) ?? Guid.Empty; Address.Text = o?.Rows.FirstOrDefault()?.Get(col => col.SiteAddress.Street) ?? ""; City.Text = o?.Rows.FirstOrDefault()?.Get(col => col.SiteAddress.City) ?? ""; State.Text = o?.Rows.FirstOrDefault()?.Get(col => col.SiteAddress.State) ?? ""; PostCode.Text = o?.Rows.FirstOrDefault()?.Get(col => col.SiteAddress.PostCode) ?? ""; Account.Value = o?.Rows.FirstOrDefault()?.Get(col => col.Account.ID) ?? Guid.Empty; var notes = o?.Rows.FirstOrDefault()?.Get(col => col.Notes) ?? null; Notes.Text = notes != null ? string.Join("\n\n", notes).Replace("\r\n\r\n", "\r\n").Replace("\n\n", "\n") : ""; Status.SelectedValue = o?.Rows.FirstOrDefault()?.Get(col => col.Status.ID) ?? Guid.Empty; QuoteValue.Text = o != null && o.Rows.Any() ? string.Format("${0:F2}", o.Rows.First().Get(col => col.ExTax)) : ""; IsReady = bIsReady; }); } private void Title_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary values) { if (IsReady) { var quote = new Quote { ID = ParentID }; quote.Number = _rows?.FirstOrDefault()?.Get(col => col.Number) ?? ""; quote.Title = Title.Value; new Client().Save(quote, "Updated Title", (j, err) => { }); } } private void Customer_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary values) { Account.Value = (Guid)values["Account.ID"]; if (IsReady) { var quote = new Quote { ID = ParentID }; quote.Number = _rows.FirstOrDefault()?.Get(col => col.Number) ?? ""; quote.Customer.ID = (Guid)values["Customer.ID"]; quote.Account.ID = (Guid)values["Account.ID"]; new Client().Save(quote, "Updated Customer ID", (j, err) => { }); } } private void Account_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary values) { if (IsReady) { var quote = new Quote { ID = ParentID }; quote.Number = _rows?.FirstOrDefault()?.Get(col => col.Number) ?? ""; quote.Account.ID = (Guid)values["Account.ID"]; new Client().Save(quote, "Updated Account ID", (j, err) => { }); } } private void Status_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (IsReady) { var quote = new Quote { ID = ParentID }; quote.Number = _rows?.FirstOrDefault()?.Get(col => col.Number) ?? ""; quote.Status.ID = Status.SelectedValue != null ? (Guid)Status.SelectedValue : Guid.Empty; new Client().Save(quote, "Updated Quote Status", (j, err) => { }); } } #region IDynamicEditorHost public IEnumerable Columns { get; } = InitialiseColumns(); private static DynamicGridColumns InitialiseColumns() { var columns = new DynamicGridColumns(); columns.ExtractColumns(typeof(Quote)); return columns; } public void LoadColumns(string column, Dictionary columns) { columns.Clear(); var comps = column.Split('.').ToList(); comps.RemoveAt(comps.Count - 1); var prefix = string.Format("{0}.", string.Join(".", comps)); var cols = Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix)); foreach (var col in cols) columns[col.ColumnName.Replace(prefix, "")] = col.ColumnName; } public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(new Quote[] { new Quote() }, type); public void LoadLookups(ILookupEditorControl editor) { //if (editor == Activity) Activity_OnDefineLookups(editor); //else if (editor == Kanban) Task_OnDefineLookups(editor); //else if (editor == ITP) ITP_OnDefineLookups(editor); } public Document? FindDocument(string filename) { return new Client().Load(new Filter(x => x.FileName).IsEqualTo(filename)).FirstOrDefault(); } public Document? GetDocument(Guid id) { Document? doc = null; if (id != Guid.Empty) doc = new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); return doc; } public void SaveDocument(Document document) { new Client().Save(document, ""); } object?[] IDynamicEditorHost.GetItems() => new object?[] { new Quote() }; public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor(); #endregion } }