123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
-
-
- public class EquipmentPanelSettings : BaseObject, IGlobalConfigurationSettings
- {
- public bool CustomerFilterEnabled { get; set; }
- public EquipmentPanelSettings()
- {
- CustomerFilterEnabled = false;
- }
- }
-
- /// <summary>
- /// Interaction logic for EquipmentPanel.xaml
- /// </summary>
- public partial class EquipmentPanel : UserControl, IPanel<Equipment>
- {
- private EquipmentPanelSettings _settings = null;
-
- private String _searchfilter = "";
-
- public ObservableCollection<Tuple<string, Guid, BitmapImage>> GroupList { get; private set; }
- public bool IsReady { get; set; }
- private String _customercode = "";
- private List<Tuple<Guid, String, String>> _customers;
- public EquipmentPanel()
- {
- GroupList = new ObservableCollection<Tuple<string, Guid, BitmapImage>>();
-
- InitializeComponent();
- }
-
- public Type DataType()
- {
- return typeof(Equipment);
- }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]> { { typeof(Equipment).EntityName(), _Equipment.SelectedRows } };
- }
- public void Setup()
- {
-
- _Equipment.CustomerID = Guid.Empty;
- _Equipment.GroupID = CoreUtils.FullGuid;
-
- _settings = new GlobalConfiguration<EquipmentPanelSettings>().Load();
- ReconfigurePanel();
-
- _Equipment.Refresh(true, false);
-
- GroupList.Add(new Tuple<string, Guid, BitmapImage>("All Items", CoreUtils.FullGuid, PRSDesktop.Resources.edit.AsBitmapImage()));
- var groups = new Client<EquipmentGroup>().Query(
- null,
- new Columns<EquipmentGroup>(x=>x.ID)
- .Add(x=>x.Description)
- .Add(x=>x.Thumbnail.ID),
- new SortOrder<EquipmentGroup>(x => x.Code)
- );
-
- var ids = groups.ExtractValues<EquipmentGroup, Guid>(c => c.Thumbnail.ID).Distinct().Where(x => x != Guid.Empty).ToArray();
- var Images = ids.Any()
- ? new Client<Document>().Load(new Filter<Document>(x=>x.ID).InList(ids))
- : new Document[] { };
-
- foreach (var row in groups.Rows)
- {
- var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)));
- BitmapImage img = (image != null && image.Data != null && image.Data.Length > 0)
- ? ImageUtils.LoadImage(image.Data)
- : PRSDesktop.Resources.specifications.AsBitmapImage();
- GroupList.Add(
- new Tuple<string, Guid, BitmapImage>(
- row.Get<EquipmentGroup,String>(c=>c.Description),
- row.Get<EquipmentGroup,Guid>(c=>c.ID),
- img
- )
- );
- }
-
- Groups.Visibility = groups.Rows.Any() ? Visibility.Visible : Visibility.Collapsed;
- Groups.ItemsSource = GroupList;
- Groups.SelectedIndex = 0;
- }
- public void Shutdown()
- {
- }
- public void CreateToolbarButtons(IPanelHost host)
- {
- host.CreateSetupAction(new PanelAction() { Caption = "Equipment Settings", Image = PRSDesktop.Resources.specifications, OnExecute = EquipmentSettingsClick });
- }
- private void EquipmentSettingsClick(PanelAction obj)
- {
- var pages = new DynamicEditorPages();
- var buttons = new DynamicEditorButtons();
- buttons.Add(
- "",
- PRSDesktop.Resources.help.AsBitmapImage(),
- _settings,
- (f, i) =>
- {
- Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + typeof(Equipment).Name.SplitCamelCase().Replace(" ", "_"))
- { UseShellExecute = true });
- }
- );
- var propertyEditor = new DynamicEditorForm(typeof(EquipmentPanelSettings), pages, buttons);
- propertyEditor.Items = new BaseObject[] { _settings };
- if (propertyEditor.ShowDialog() == true)
- {
- new GlobalConfiguration<EquipmentPanelSettings>().Save(_settings);
- ReconfigurePanel();
- }
- }
- private void ReconfigurePanel()
- {
- CustomerLabel.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
- Customer.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
- CustomerSearch.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
- CustomerName.Visibility = _settings.CustomerFilterEnabled ? Visibility.Visible : Visibility.Collapsed;
-
- if (!_settings.CustomerFilterEnabled)
- {
- _Equipment.CustomerID = CoreUtils.FullGuid;
- _customercode = "";
- Customer.Text = "";
- CustomerName.Text = "";
- }
- else if (_Equipment.CustomerID == CoreUtils.FullGuid)
- _Equipment.CustomerID = Guid.Empty;
-
- if (IsReady)
- _Equipment.Refresh(false,true);
- }
- public void Refresh()
- {
- _Equipment.Refresh(false, true);
- }
- public string SectionName => "Equipment";
- public DataModel DataModel(Selection selection)
- {
- var ids = _Equipment?.ExtractValues(x => x.ID, selection)?.ToArray() ?? new Guid[] { };
- return new BaseDataModel<Equipment>(new Filter<Equipment>(x => x.ID).InList(ids));
- }
- public void Heartbeat(TimeSpan time)
- {
- }
-
-
- private bool _Equipment_OnFilterRecord(CoreRow row)
- {
- if (!String.IsNullOrWhiteSpace(_searchfilter))
- {
- for (int i=0; i<row.Table.Columns.Count; i++)
- {
- if ((row.Table.Columns[i].DataType == typeof(String)))
- {
- string value = row.Values[i] as string;
- if (!String.IsNullOrEmpty(value) && value.ToUpper().Contains(_searchfilter.ToUpper()))
- return true;
- }
- }
- return false;
- }
-
- return true;
- }
- private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var sCur = _Equipment.GroupID;
- if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
- {
- _Equipment.GroupID = CoreUtils.FullGuid;
- }
- else
- {
- var selected = (Tuple<string, Guid, BitmapImage>)e.AddedItems[0];
- _Equipment.GroupID = selected.Item2;
- }
- if (sCur != _Equipment.GroupID)
- _Equipment.Refresh(false, true);
- }
-
- private void Search_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- if (String.IsNullOrEmpty(Search.Text) && !String.Equals(Search.Text, _searchfilter))
- {
- _searchfilter = "";
- _Equipment.Refresh(false, false);
- }
- }
- private void Search_OnKeyUp(object sender, KeyEventArgs e)
- {
- if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key) && !String.Equals(Search.Text, _searchfilter))
- {
- _searchfilter = Search.Text;
- _Equipment.Refresh(false, false);
- }
- }
- private void CheckCustomerCache()
- {
- if (_customers == null)
- {
- _customers = new Client<Customer>().Query(
- null,
- new Columns<Customer>(x => x.ID).Add(x => x.Code).Add(x => x.Name),
- new SortOrder<Customer>(x => x.Name)
- ).ToTuples<Customer,Guid,String,String>(x =>x.ID,x=>x.Code,x=>x.Name).ToList();
- }
- }
-
- private void DoSearchCustomers()
- {
- var dlg = new MultiSelectDialog<Customer>(null, new Columns<Customer>(x => x.ID).Add(x => x.Code).Add(x => x.Name), false);
- if (dlg.ShowDialog("Code",Customer.Text))
- {
- var customer = dlg.Items().First();
- _customercode = customer.Code;
- Customer.Text = customer.Code;
- CustomerName.Text = customer.Name;
- _Equipment.CustomerID = customer.ID;
- }
- else
- {
- _customercode = "";
- Customer.Text = "";
- CustomerName.Text = "";
- _Equipment.CustomerID = Guid.Empty;
- }
- _Equipment.Refresh(false, true);
- }
-
- private void CustomerSearch_OnClick(object sender, RoutedEventArgs e)
- {
- DoSearchCustomers();
- }
-
- private void Customer_OnKeyUp(object sender, KeyEventArgs e)
- {
- if (new Key[] { Key.Enter, Key.Return }.Contains(e.Key))
- {
- if (!String.Equals(Customer.Text, _customercode))
- {
- CheckCustomerCache();
- var lookup = _customers.FirstOrDefault(x => String.Equals(x.Item2, Customer.Text));
- if (lookup != null)
- {
- _customercode = lookup.Item2;
- CustomerName.Text = lookup.Item3;
- _Equipment.CustomerID = lookup.Item1;
- _Equipment.Refresh(false, true);
- }
- else
- DoSearchCustomers();
- }
- }
- }
- private void Customer_OnTextChanged(object sender, TextChangedEventArgs e)
- {
- if (String.IsNullOrEmpty(Customer.Text) && !String.Equals(Customer.Text, _customercode))
- {
- _Equipment.CustomerID = Guid.Empty;
- _customercode = "";
- CustomerName.Text = "";
- _Equipment.Refresh(false, true);
- }
- }
- }
- }
|