123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- public class InvoiceRequisitionItemGrid : DynamicDataGrid<RequisitionItem>
- {
- private readonly BitmapImage chargeable_excluded = PRSDesktop.Resources.tick.Fade(0.8F).AsGrayScale().AsBitmapImage();
- private readonly BitmapImage chargeable_included = PRSDesktop.Resources.tick.AsBitmapImage();
-
- private readonly BitmapImage unchargeable_excluded = PRSDesktop.Resources.warning.Fade(0.8F).AsGrayScale().AsBitmapImage();
- private readonly BitmapImage unchargeable_included = PRSDesktop.Resources.warning.AsBitmapImage();
-
- private readonly Button IncludeButton;
- private readonly Button ShowAllButton;
- private bool _showall = false;
- public InvoiceRequisitionItemGrid()
- {
- JobID = CoreUtils.FullGuid;
- InvoiceID = CoreUtils.FullGuid;
-
- ColumnsTag = "InvoiceParts";
-
- Options
- .BeginUpdate()
- .Clear()
- .Add(DynamicGridOption.EditRows)
- .Add(DynamicGridOption.RecordCount)
- .Add(DynamicGridOption.SelectColumns)
- .Add(DynamicGridOption.MultiSelect)
- .Add(DynamicGridOption.FilterRows)
- .EndUpdate();
-
- HiddenColumns.Add(x => x.Invoice.ID);
- HiddenColumns.Add(x => x.Invoice.Deleted);
- HiddenColumns.Add(x => x.Charge.Chargeable);
- ActionColumns.Add(new DynamicImageColumn(IncludeImage, IncludeOne));
- AddButton("Exclude", chargeable_excluded, IncludeSelected, DynamicGridButtonPosition.Right);
- IncludeButton = AddButton("Include", chargeable_included, IncludeSelected, DynamicGridButtonPosition.Right);
- ShowAllButton = AddButton("Show All", null, ToggleShowAll);
- }
- public Guid JobID { get; set; }
- public Guid InvoiceID { get; set; }
- private bool IncludeSelected(Button sender, CoreRow[] rows)
- {
- if (InvoiceID != Guid.Empty)
- {
- using (new WaitCursor())
- {
- var bAdd = sender == IncludeButton;
- var items = rows.Select(r => r.ToObject<RequisitionItem>()).ToArray();
- foreach (var item in items)
- item.Invoice.ID = bAdd ? InvoiceID : Guid.Empty;
- new Client<RequisitionItem>().Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
- foreach (var row in rows)
- {
- row.Set<RequisitionItem, Guid>(x => x.Invoice.ID, bAdd ? InvoiceID : Guid.Empty);
- InvalidateRow(row);
- }
- }
- }
- else
- MessageBox.Show("Please Select or Create an Invoice First!");
- return false;
- }
- private bool IncludeOne(CoreRow arg)
- {
- if (arg == null)
- return false;
-
- if (InvoiceID != Guid.Empty)
- {
- var id = arg.Get<RequisitionItem, Guid>(x => x.ID);
- var item = arg.ToObject<RequisitionItem>();
- if (item != null)
- {
- item.Invoice.ID = !item.Invoice.IsValid() ? InvoiceID : Guid.Empty;
- new Client<RequisitionItem>().Save(item, "Added to Invoice", (o,e) => { });
- arg.Set<RequisitionItem, Guid>(x=>x.Invoice.ID, item.Invoice.ID);
- InvalidateRow(arg);
- }
- }
- else
- MessageBox.Show("Please Select or Create an Invoice First!");
- return false;
- }
- private BitmapImage IncludeImage(CoreRow arg)
- {
- if (arg == null)
- return chargeable_included;
- bool chargeable = arg.Get<RequisitionItem, bool>(x => x.Charge.Chargeable);
- bool included = Entity.IsEntityLinkValid<RequisitionItem, InvoiceLink>(x => x.Invoice, arg);
- return chargeable
- ? included
- ? chargeable_included
- : chargeable_excluded
- : included
- ? unchargeable_included
- : unchargeable_excluded;
- }
-
- private bool ToggleShowAll(Button arg1, CoreRow[] rows)
- {
- _showall = !_showall;
- UpdateButton(ShowAllButton, null, _showall ? "Selected Only" : "Show All");
- return true;
- }
- protected override void Reload(Filters<RequisitionItem> criteria, Columns<RequisitionItem> columns, ref SortOrder<RequisitionItem> sort,
- Action<CoreTable, Exception> action)
- {
- if ((new Guid[] { CoreUtils.FullGuid, Guid.Empty }).Intersect(new Guid[] { JobID, InvoiceID }).Any())
- criteria.Add(new Filter<RequisitionItem>().None());
- else
- {
-
- if (JobID != Guid.Empty)
- criteria.Add(new Filter<RequisitionItem>(x => x.RequisitionLink.JobLink.ID).IsEqualTo(JobID));
-
- if (_showall)
- criteria.Add(new Filter<RequisitionItem>(x => x.Invoice.ID).IsEqualTo(InvoiceID).Or(x => x.Invoice).NotLinkValid());
- else
- criteria.Add(new Filter<RequisitionItem>(x => x.Invoice.ID).IsEqualTo(InvoiceID));
-
- }
- base.Reload(criteria, columns, ref sort, action);
- }
- }
- }
|