123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using Comal.Classes.SecurityDescriptors;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Integration.Logikal;
- using InABox.Wpf;
- using InABox.WPF;
- using PRSDesktop.Integrations.V6;
- using PRSDesktop.Integrations.Logikal;
- namespace PRSDesktop;
- public class JobGridSettings : IUserConfigurationSettings
- {
- [Obsolete]
- private CoreFilterDefinition? _currentFilter;
- [Obsolete]
- public CoreFilterDefinition? CurrentFilter
- {
- get => _currentFilter;
- set
- {
- if (value is not null)
- {
- Filters = new DynamicGridSelectedFilterSettings(new List<ICoreFilterDefinition> { value }, false, null);
- }
- }
- }
- public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
- }
- public class ProjectsGrid : DynamicDataGrid<Job>
- {
- private Guid _statusid = Guid.Empty;
- private JobGridSettings _settings;
- public ProjectsGrid()
- {
- _settings = new UserConfiguration<JobGridSettings>().Load();
- FilterComponent.SetSettings(_settings.Filters, false);
- FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
- HiddenColumns.Add(x => x.ID);
- HiddenColumns.Add(x => x.JobNumber);
- HiddenColumns.Add(x => x.Name);
- HiddenColumns.Add(x => x.Customer.ID);
- HiddenColumns.Add(x => x.Customer.Code);
- HiddenColumns.Add(x => x.Customer.Name);
- HiddenColumns.Add(x => x.Customer.Terms.ID);
- HiddenColumns.Add(x => x.Customer.Terms.Code);
- HiddenColumns.Add(x => x.Customer.Terms.Description);
- HiddenColumns.Add(x => x.Customer.Terms.Calculation);
- HiddenColumns.Add(x => x.SiteAddress.Street);
- HiddenColumns.Add(x => x.SiteAddress.City);
- HiddenColumns.Add(x => x.SiteAddress.State);
- HiddenColumns.Add(x => x.SiteAddress.PostCode);
- HiddenColumns.Add(x => x.Account.ID);
- HiddenColumns.Add(x => x.Account.Code);
- HiddenColumns.Add(x => x.Account.Name);
- HiddenColumns.Add(x => x.Account.Terms.ID);
- HiddenColumns.Add(x => x.Account.Terms.Code);
- HiddenColumns.Add(x => x.Account.Terms.Description);
- HiddenColumns.Add(x => x.Account.Terms.Calculation);
- HiddenColumns.Add(x => x.Notes);
- HiddenColumns.Add(x => x.JobStatus.ID);
- HiddenColumns.Add(x => x.JobStatus.Active);
- HiddenColumns.Add(x => x.DefaultScope.ID);
- HiddenColumns.Add(x => x.SourceRef);
- ActionColumns.Add(new DynamicMapColumn<Job>(this, x => x.SiteAddress));
- //ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.FilterRows = true;
- }
- // private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
- // {
- // if (Security.IsAllowed<CanCancelAllJobRequisitions>())
- // {
- // column.AddItem("Cancel all active requisitions", null, CancelJobRequisitions_Click);
- // }
- // if (Security.IsAllowed<CanReleaseJobReserves>())
- // {
- // column.AddItem("Release Job Reserves", null, ReleaseJobReserves_Click);
- // }
- // }
- //
- // private void CancelJobRequisitions_Click(CoreRow? obj)
- // {
- // var job = obj?.ToObject<Job>();
- // if (job is null)
- // {
- // MessageWindow.ShowMessage("Please select a job.", "No job selected");
- // return;
- // }
- //
- // if(MessageWindow.ShowYesNoCancel("Are you sure you wish to do this? This will cancel all requisitions for this job.", "Confirm") != MessageWindowResult.Yes)
- // {
- // MessageWindow.ShowMessage("No action taken.", "Process aborted");
- // return;
- // }
- //
- // var requisitionItems = Client.Query(
- // new Filter<JobRequisitionItem>(x => x.Requisition.Job.ID).IsEqualTo(job.ID)
- // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Cancelled)
- // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Issued)
- // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Archived),
- // new Columns<JobRequisitionItem>(x => x.ID)
- // .Add(x => x.Cancelled))
- // .ToList<JobRequisitionItem>();
- // foreach(var jri in requisitionItems)
- // {
- // jri.Cancelled = DateTime.Now;
- // }
- // Client.Save(requisitionItems, "Cancelled all job requisitions for job");
- // MessageWindow.ShowMessage("All job requisitions cancelled.", "Success");
- // }
- //
- // private void ReleaseJobReserves_Click(CoreRow? obj)
- // {
- // var job = obj?.ToObject<Job>();
- // if (job is null)
- // {
- // MessageWindow.ShowMessage("Please select a job.", "No job selected");
- // return;
- // }
- //
- // if(MessageWindow.ShowYesNoCancel("Are you sure you wish to do this? This will release all reserves for this job.", "Confirm") != MessageWindowResult.Yes)
- // {
- // MessageWindow.ShowMessage("No action taken.", "Process aborted");
- // return;
- // }
- //
- // var movements = Client.Query<StockMovement>(
- // new Filter<StockMovement>(x => x.Job.ID).IsEqualTo(job.ID),
- // new Columns<StockMovement>(x => x.Units)
- // .Add(x => x.Location.ID)
- // .Add(x => x.Product.ID)
- // .Add(x => x.Style.ID)
- // .AddDimensionsColumns(x => x.Dimensions)
- // .Add(x => x.Cost)
- // .Add(x => x.OrderItem.ID)
- // .Add(x => x.JobRequisitionItem.ID))
- // .ToObjects<StockMovement>()
- // .GroupBy(x => new
- // {
- // Location = x.Location.ID,
- // Product = x.Product.ID,
- // Style = x.Style.ID,
- // x.Dimensions,
- // x.Cost,
- // OrderItem = x.OrderItem.ID,
- // JobRequisitionItem = x.JobRequisitionItem.ID
- // })
- // .Select(x => new { x.Key, Units = x.Sum(x => x.Units) });
- //
- // var toSave = new List<StockMovement>();
- // foreach(var group in movements)
- // {
- // var from = new StockMovement();
- // from.Location.ID = group.Key.Location;
- // from.Style.ID = group.Key.Style;
- // from.Product.ID = group.Key.Product;
- // from.Dimensions.CopyFrom(group.Key.Dimensions);
- //
- // var to = from.CreateMovement();
- //
- // from.Job.ID = job.ID;
- //
- // from.Cost = group.Key.Cost;
- // to.Cost = group.Key.Cost;
- // from.OrderItem.ID = group.Key.OrderItem;
- // to.OrderItem.ID = group.Key.OrderItem;
- // from.JobRequisitionItem.ID = group.Key.JobRequisitionItem;
- // to.JobRequisitionItem.ID = group.Key.JobRequisitionItem;
- //
- // from.Type = StockMovementType.TransferOut;
- // to.Type = StockMovementType.TransferIn;
- // to.Transaction = from.Transaction;
- //
- // from.Units = -group.Units;
- // to.Units = group.Units;
- //
- // toSave.Add(from);
- // toSave.Add(to);
- // }
- // Client.Save(toSave, "Released all job stock.");
- // MessageWindow.ShowMessage("All job stock reserves released.", "Success");
- // }
- public Guid StatusID
- {
- get => _statusid;
- set
- {
- _statusid = value;
- }
- }
- protected override void Reload(
- Filters<Job> criteria, Columns<Job> columns, ref SortOrder<Job>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- if (_statusid != Guid.Empty)
- criteria.Add(new Filter<Job>(x => x.JobStatus.ID).IsEqualTo(_statusid));
- //criteria.Add(new Filter<Job>(x => x.JobType).IsEqualTo(JobType.Project));
- sort = new SortOrder<Job>(x => x.JobNumber, SortDirection.Descending);
- base.Reload(criteria, columns, ref sort, token, action);
- }
- public override DynamicEditorPages LoadEditorPages(Job item)
- {
- var pages = new DynamicEditorPages(new IDynamicEditorPage[]
- {
- new DynamicManyToManyGrid<JobDocument,Job>(),
- //new DynamicManyToManyGrid<JobStyle,Job>(),
- new DynamicOneToManyGrid<Job,JobDocumentSetTag>(),
- new JobFormDefinitionGrid(),
- //new DynamicOneToManyGrid<Job, JobProductMapping>(),
- });
- return pages;
- }
- private LogikalSettings? _logikalSettings;
- private V6Settings? _v6Settings;
- protected override void DoAdd(bool openEditorOnDirectEdit = false)
- {
- ContextMenu? menu = null;
- if (Security.IsAllowed<ImportV6Projects>())
- {
- _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
- if (_v6Settings.ImportProjects == V6ProjectType.Project)
- {
- menu ??= new ContextMenu();
- var item = new MenuItem()
- {
- Header = "Import from V6",
- Icon = new Image() { Source = PRSDesktop.Resources.v6.AsBitmapImage() }
- };
- item.Click += ImportFromV6;
- menu.Items.Add(item);
- }
- }
- if (Security.IsAllowed<ImportLogikalProjects>())
- {
- _logikalSettings ??= new GlobalConfiguration<LogikalSettings>().Load();
- if (_logikalSettings.ImportProjects == LogikalProjectType.Project)
- {
- menu ??= new ContextMenu();
- var item = new MenuItem()
- {
- Header = "Import from Logikal",
- Icon = new Image() { Source = PRSDesktop.Resources.logikal.AsBitmapImage() }
- };
- item.Click += ImportFromLogikal;
- menu.Items.Add(item);
- }
- }
- if (menu != null)
- {
- menu.Items.Insert(0,new Separator());
- var item = new MenuItem()
- {
- Header = "New Project"
- };
- item.Click += (o, e) => base.DoAdd(openEditorOnDirectEdit);
- menu.Items.Insert(0,item);
- menu.IsOpen = true;
- }
- else
- base.DoAdd(openEditorOnDirectEdit);
- }
-
- private void ImportFromLogikal(object sender, RoutedEventArgs e)
- {
- LogikalClient.Instance.Initialize()
- .Error(error =>
- {
- MessageWindow.ShowMessage($"Unable to locate Logikal App!\n\n{error.Message}", "Error");
- })
- .Success<LogikalInitializeResponse>(initialize =>
- {
- LogikalClient.Instance.Connect(initialize.Path)
- .Error(error => MessageWindow.ShowMessage($"Unable to connect to Logikal!\n\n{initialize.Path}\n\n{error.Message}", "Error"))
- .Success<LogikalConnectResponse>(connect =>
- {
- LogikalClient.Instance.Login()
- .Error(error =>
- MessageWindow.ShowMessage($"Unable to log in to Logikal!\n\n{error.Message}", "Error"))
- .Success<LogikalLoginResponse>(login =>
- {
- var import = new LogikalProjectImport(FilterLogikalProjects, CreateLogikalProject);
- if (import.ShowDialog() == true)
- Refresh(false, true);
- });
- });
- });
- }
-
- private List<string>? _logikalSources;
-
- private bool FilterLogikalProjects(LogikalProject project)
- {
- _logikalSettings ??= new GlobalConfiguration<LogikalSettings>().Load();
- if (_logikalSettings.UseLogikalProjectNumber)
- {
- _logikalSources ??= Client.Query(
- null,
- Columns.None<Job>().Add(x => x.JobNumber)
- ).Rows
- .Select(r => r.Get<Job, string>(c => c.JobNumber))
- .ToList();
- return !string.IsNullOrWhiteSpace(project.JobNumber) && !_logikalSources.Contains($"{_logikalSettings.ProjectPrefix}{project.JobNumber}");
- }
- else
- {
- _logikalSources ??= Client.Query(
- new Filter<Job>(x => x.SourceRef).BeginsWith("Logikal:"),
- Columns.None<Job>().Add(x => x.SourceRef)
- ).Rows
- .Select(r => r.Get<Job, string>(c => c.SourceRef))
- .ToList();
- return !_logikalSources.Contains(project.GetReference());
- }
- }
-
- private void CreateLogikalProject(LogikalProject project)
- {
- _logikalSettings ??= new GlobalConfiguration<LogikalSettings>().Load();
-
- var _job = new Job();
-
- if (_logikalSettings.UseLogikalProjectNumber)
- _job.JobNumber = $"{_logikalSettings.ProjectPrefix}{project.JobNumber}";
- else
- _job.SourceRef = project.GetReference();
-
- _job.Name = project?.Title ?? "New Logikal Project";
- //_job.Customer.CopyFrom(_customer);
- //_job.Account.CopyFrom(_customer.Account);
- // _job.SiteAddress.Street = project.Street;
- // _job.SiteAddress.City = project.City;
- // _job.SiteAddress.State = project.State;
- // _job.SiteAddress.PostCode = project.PostCode;
-
- Client.Save(_job,"Imported From Logikal");
-
- if (_logikalSources != null)
- _logikalSources.Add(project.GetReference());
-
- // var _scope = Client.Query(
- // new Filter<JobScope>(x=>x.ID).IsEqualTo(_job.DefaultScope.ID),
- // Columns.Required<JobScope>()
- // ).Rows.FirstOrDefault()?.ToObject<JobScope>();
- // if (_scope != null)
- // {
- // _scope.ExTax = project.SellPrice;
- // Client.Save(_scope, "Imported From V6");
- // }
-
- }
- private void ImportFromV6(object sender, RoutedEventArgs e)
- {
- var _client = new V6Client();
- if (!_client.Connect())
- {
- MessageBox.Show("Unable to connect to V6!");
- return;
- }
-
- var import = new V6ProjectSelection(_client,FilterV6Projects,CreateV6Project);
- if (import.ShowDialog() == true)
- Refresh(false,true);
- }
- private List<string>? _v6Sources;
- private bool FilterV6Projects(V6Project project)
- {
-
- _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
- if (_v6Settings.UseV6QuoteNumber)
- {
- _v6Sources ??= Client.Query(
- null,
- Columns.None<Job>().Add(x => x.JobNumber)
- ).Rows
- .Select(r => r.Get<Job, string>(c => c.JobNumber))
- .ToList();
- return !_v6Sources.Contains($"{_v6Settings.ProjectPrefix}{project.Number}");
- }
- else
- {
- _v6Sources ??= Client.Query(
- new Filter<Job>(x => x.SourceRef).BeginsWith("V6:"),
- Columns.None<Job>().Add(x => x.SourceRef)
- ).Rows
- .Select(r => r.Get<Job, string>(c => c.SourceRef))
- .ToList();
- return !_v6Sources.Contains(project.GetReference());
- }
-
- }
- private void CreateV6Project(V6Project project)
- {
- _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
-
- var _job = new Job();
- var _customer = new Customer();
-
- if (!string.IsNullOrWhiteSpace(project.ClientID))
- {
- _customer = Client.Query(
- new Filter<Customer>(x => x.Code).IsEqualTo(project.ClientID),
- Columns.Required<Customer>()
- ).ToObjects<Customer>()
- .FirstOrDefault();
- if (_customer == null)
- {
- _customer = new Customer();
- _customer.Code = project.ClientID;
- _customer.Name = project.ClientName;
- _customer.Delivery.Street = project.Street;
- _customer.Delivery.City = project.City;
- _customer.Delivery.State = project.State;
- _customer.Delivery.PostCode = project.PostCode;
- Client.Save(_customer, "Imported From V6");
- }
- }
- if (_v6Settings.UseV6QuoteNumber)
- _job.JobNumber = $"{_v6Settings.ProjectPrefix}{project.Number}";
- else
- _job.SourceRef = project.GetReference();
-
- _job.Name = project.Title;
- _job.Customer.CopyFrom(_customer);
- _job.Account.CopyFrom(_customer.Account);
- _job.SiteAddress.Street = project.Street;
- _job.SiteAddress.City = project.City;
- _job.SiteAddress.State = project.State;
- _job.SiteAddress.PostCode = project.PostCode;
-
- Client.Save(_job,"Imported From V6");
-
- if (_v6Sources != null)
- _v6Sources.Add($"V6:{project.Number}");
-
- var _scope = Client.Query(
- new Filter<JobScope>(x=>x.ID).IsEqualTo(_job.DefaultScope.ID),
- Columns.Required<JobScope>()
- ).Rows.FirstOrDefault()?.ToObject<JobScope>();
- if (_scope != null)
- {
- _scope.ExTax = project.SellPrice;
- Client.Save(_scope, "Imported From V6");
- }
- }
-
- public override Job CreateItem()
- {
- var result = base.CreateItem();
- result.JobType = JobType.Project;
- return result;
- }
- protected override void CustomiseEditor(IDynamicEditorForm form, Job[] items, DynamicGridColumn column, BaseEditor editor)
- {
- base.CustomiseEditor(form, items, column, editor);
- if(new Column<Job>(x => x.Customer.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
- {
- popup.CanAdd = Security.CanEdit<Customer>();
- }
- }
- protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Job[] items, string name, object value)
- {
- var result = base.EditorValueChanged(editor, items, name, value);
- if (name.Equals("Customer.ID"))
- {
- // false here because a customer has a defaultcontact
- // and we need to load the lookups before we set the default value
- var contact = editor.FindEditor("Contact.ID") as ILookupEditorControl;
- if (contact != null)
- DefineLookups(contact,items,false);
- }
- return result;
- }
- private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
- {
- _settings.Filters = settings;
- new UserConfiguration<JobGridSettings>().Save(_settings);
- }
- }
|