| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using InABox.Wpf;
- using System.Threading;
- using System.Windows.Controls;
- using Comal.Classes.SecurityDescriptors;
- using InABox.Configuration;
- using PRSDesktop.Integrations.V6;
- namespace PRSDesktop
- {
- internal class JobScopeGrid : DynamicDataGrid<JobScope>, IMasterDetailControl<Job,JobScope>
- {
- private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
-
- public Job? Master { get; set; }
- public Filter<JobScope> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
- ? new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Master.ID)
- : new Filter<JobScope>().None();
-
- public JobScopeGrid()
- {
- HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
- HiddenColumns.Add(x=>x.Job.ID);
- HiddenColumns.Add(x=>x.SourceRef);
- ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.End });
- }
-
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.FilterRows = true;
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- base.DeleteItems(rows);
- var row = rows.FirstOrDefault(x => x.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == x.Get<JobScope, Guid>(x => x.ID));
- if (row is not null)
- {
- var job = Client.Query(
- new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
- Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
- ).Rows.FirstOrDefault()?.ToObject<Job>();
- if (job != null)
- {
- job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
- Client.Save(job, "Updated Default Scope");
- }
- }
- }
- private BitmapImage? IsDefaultImage(CoreRow? row)
- {
- return row == null
- ? tick
- : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
- ? tick
- : null;
- }
- private bool SelectDefaultAction(CoreRow? row)
- {
- if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
- return false;
- using (new WaitCursor())
- {
- var job = new Client<Job>().Query(
- new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
- Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
- ).Rows.FirstOrDefault()?.ToObject<Job>();
- if (job != null)
- {
- job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
- new Client<Job>().Save(job, "Updated Default Scope");
- return true;
- }
- }
- return false;
- }
- protected override bool CanCreateItems()
- {
- return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
- }
-
- public override JobScope CreateItem()
- {
- var result = base.CreateItem();
- result.Job.ID = Master?.ID ?? Guid.Empty;
- result.Job.Synchronise(Master ?? new Job());
- result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
- return result;
- }
- 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 (menu != null)
- {
- menu.Items.Insert(0,new Separator());
- var item = new MenuItem()
- {
- Header = "New Scope"
- };
- item.Click += (o, e) => base.DoAdd(openEditorOnDirectEdit);
- menu.Items.Insert(0,item);
- menu.IsOpen = true;
- }
- else
- base.DoAdd(openEditorOnDirectEdit);
-
- }
- private void ImportFromV6(object sender, RoutedEventArgs e)
- {
- var _client = new V6Client();
- if (!_client.Connect())
- {
- MessageBox.Show("Unable to connect to V6!");
- return;
- }
- var master = _client.GetProject(Master?.JobNumber,Master?.SourceRef);
- if (master == null)
- {
- MessageBox.Show("This is not a V6 project!");
- return;
- }
-
- var import = new V6VariationSelection(
- _client,
- master,
- (v) => FilterV6Variations(master,v),
- CreateV6Variation
- );
-
- if (import.ShowDialog() == true)
- Refresh(false,true);
- }
-
- private bool FilterV6Variations(V6Project master, V6Variation variation)
- {
- // Exclude previously imported variations
- return !Data.Rows.Any(r => string.Equals(variation.ID, r.Get<JobScope, string>(x => x.SourceRef)));
- }
-
- private bool CreateV6Variation(V6Variation variation)
- {
- _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
-
- JobScope scope = new JobScope();
- scope.Job.ID = Master?.ID ?? Guid.Empty;
- if (_v6Settings.UseV6QuoteNumber)
- scope.Number = variation.ID;
- scope.Description = variation.Description;
- scope.Type = JobScopeType.Variation;
- scope.SourceRef = variation.GetReference();
- Client.Save(scope, "Imported From V6");
-
- return true;
- }
-
- protected override void Reload(
- Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, token, action);
- }
-
- private DynamicGridTreeUIComponent<JobScope, Guid>? _uiComponent;
- private DynamicGridTreeUIComponent<JobScope, Guid> UIComponent
- {
- get
- {
- if(_uiComponent is null)
- {
- _uiComponent = new DynamicGridTreeUIComponent<JobScope, Guid>(
- x => x.ID,
- x => x.Parent.ID,
- Guid.Empty)
- {
- Parent = this,
- MaxRowHeight = 30,
- };
- //_uiComponent.OnContextMenuOpening += JobDocumentSetFolderTree_OnContextMenuOpening;
- }
- return _uiComponent;
- }
- }
- protected override IDynamicGridUIComponent<JobScope> CreateUIComponent()
- {
- return UIComponent;
- }
-
- }
- }
|