123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Linq;
- using System.Threading;
- 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 InvoiceStockMovementGrid : DynamicDataGrid<StockMovement>
- {
- 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 InvoiceStockMovementGrid()
- {
-
- ColumnsTag = "InvoiceParts";
- HiddenColumns.Add(x => x.Invoice.ID);
- 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);
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.Clear();
- options.EditRows = true;
- options.RecordCount = true;
- options.SelectColumns = true;
- options.MultiSelect = true;
- options.FilterRows = true;
- }
-
- public Invoice Invoice { get; set; }
- private bool IncludeSelected(Button sender, CoreRow[] rows)
- {
- if (Invoice.ID != Guid.Empty)
- {
- using (new WaitCursor())
- {
- var bAdd = sender == IncludeButton;
- var items = rows.ToArray<StockMovement>();
- foreach (var item in items)
- {
- if (bAdd)
- {
- item.Invoice.ID = Invoice.ID;
- item.Invoice.Synchronise(Invoice);
- }
- else
- {
- item.Invoice.ID = Guid.Empty;
- item.Invoice.Synchronise(new Invoice());
- }
- }
- Client.Save(items, bAdd ? "Added to Invoice" : "Removed From Invoice", (o, e) => { });
- foreach (var row in rows)
- {
- row.Set<StockMovement, Guid>(x => x.Invoice.ID, bAdd ? Invoice.ID : 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 (Invoice.ID != Guid.Empty)
- {
- var id = arg.Get<StockMovement, Guid>(x => x.ID);
- var item = arg.ToObject<StockMovement>();
- if (item != null)
- {
- if (!item.Invoice.IsValid())
- {
- item.Invoice.ID = Invoice.ID;
- item.Invoice.Synchronise(Invoice);
- }
- else
- {
- item.Invoice.ID = Guid.Empty;
- item.Invoice.Synchronise(new Invoice());
- }
- Client.Save(item, "Added to Invoice", (o,e) => { });
- arg.Set<StockMovement, 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<StockMovement, bool>(x => x.Charge.Chargeable);
- bool included = Entity.IsEntityLinkValid<StockMovement, 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<StockMovement> criteria, Columns<StockMovement> columns, ref SortOrder<StockMovement>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- if (Invoice.ID == Guid.Empty)
- criteria.Add(new Filter<StockMovement>().None());
- else
- {
- if (_showall)
- criteria.Add(new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Invoice.ID).Or(x => x.Invoice).NotLinkValid());
- else
- criteria.Add(new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(Invoice.ID));
-
- }
- base.Reload(criteria, columns, ref sort, token, action);
- }
- }
|