| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using System.Linq;
- using System.Threading;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- namespace PRSDesktop;
- public class JobScopeCostCentreGrid : DynamicDataGrid<JobScopeCostCentre>, IJobScopeGrid
- {
- public JobScope? Scope { get; set; }
-
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x=>x.CostCentre.ID);
- HiddenColumns.Add(x => x.Cost);
- HiddenColumns.Add(x => x.Sell);
- AddButton("Reset", null, ResetGLCodes);
- if (Security.CanView<CostCentre>())
- AddButton("Manage", null, ManageCostCentres, DynamicGridButtonPosition.Right);
- }
-
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.SelectColumns = true;
- }
-
- public override DynamicGridColumns GenerateColumns()
- {
- var _columns = new DynamicGridColumns();
- _columns.Add<JobScopeCostCentre>(x => x.CostCentre.Code, 120, "Code", "", Alignment.MiddleLeft);
- _columns.Add<JobScopeCostCentre>(x => x.CostCentre.Description, 0, "Description", "", Alignment.MiddleLeft);
- _columns.Add<JobScopeCostCentre>(x => x.Cost, 70, "Cost", "F2", Alignment.MiddleCenter);
- _columns.Add<JobScopeCostCentre>(x => x.Markup, 70, "M/Up", "F2", Alignment.MiddleCenter);
- _columns.Add<JobScopeCostCentre>(x => x.Sell, 70, "Sell", "F2", Alignment.MiddleCenter);
- return _columns;
- }
- protected override void Reload(
- Filters<JobScopeCostCentre> criteria, Columns<JobScopeCostCentre> columns, ref SortOrder<JobScopeCostCentre>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- if ((Scope == null) || (Scope.ID == Guid.Empty))
- criteria.Add(Filter.None<JobScopeCostCentre>());
- else
- criteria.Add(Filter<JobScopeCostCentre>.Where(x => x.Scope.ID).IsEqualTo(Scope.ID));
- base.Reload(criteria, columns, ref sort, token, action);
- }
- protected override bool CanCreateItems()
- {
- return base.CanCreateItems() && Scope != null;
- }
- public override JobScopeCostCentre CreateItem()
- {
- var _result = base.CreateItem();
- _result.Scope.Synchronise(Scope ?? new JobScope());
- return _result;
- }
-
- private bool ManageCostCentres(Button button, CoreRow[] rows)
- {
- var list = new MasterList(typeof(CostCentre));
- list.ShowDialog();
- return true;
- }
- private bool ResetGLCodes(Button button, CoreRow[] rows)
- {
- var _current = ExtractValues(x => x.CostCentre.ID, Selection.All).ToArray();
-
- var _masters = Client.Query(
- Filter<CostCentre>.Where(x => x.IsJobCostCentre).IsEqualTo(true),
- Columns.None<CostCentre>().Add(x => x.ID)
- ).Rows
- .Select(row =>
- {
- var result = new JobScopeCostCentre();
- result.Job.ID = Scope.Job.ID;
- result.Scope.ID = Scope.ID;
- result.CostCentre.ID = row.Get<CostCentre, Guid>(x => x.ID);
- return result;
- }
- ).ToArray();
-
- var _new = _masters.Where(a => _current.All(x => x != a.CostCentre.ID)).ToList();
- if (_new.Any())
- Client.Save(_new, "Updated from Master CostCentre List");
-
- var _unused = Data.ToObjects<JobScopeCostCentre>()
- .Where(x => x.Cost.IsEffectivelyEqual(0.0F) && x.Sell.IsEffectivelyEqual(0.0F))
- .Where(x => _masters.All(m => m.CostCentre.ID != x.CostCentre.ID))
- .ToArray();
- if (_unused.Any())
- Client.Delete(_unused, "Deleted from Master CostCentre List");
-
- return _new.Any() || _unused.Any();
- }
- }
|