using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { public class StockMovementGrid : DynamicDataGrid, IDataModelSource { public static readonly DependencyProperty AllowNullLocationProperty = DependencyProperty.Register("AllowNullLocation", typeof(bool), typeof(StockMovementGrid), new UIPropertyMetadata(null)); public static readonly DependencyProperty AllowNullBatchProperty = DependencyProperty.Register("AllowNullBatch", typeof(bool), typeof(StockMovementGrid), new UIPropertyMetadata(null)); private readonly Button AllButton; private bool bShowAll = true; private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage(); private int syscolumn = -1; public StockMovementGrid() { Options .BeginUpdate() .Add(DynamicGridOption.RecordCount) .Add(DynamicGridOption.SelectColumns) .Add(DynamicGridOption.FilterRows) .EndUpdate(); ColumnsTag = "StockMovementGrid"; HiddenColumns.Add(x => x.System); HiddenColumns.Add(x => x.Transaction); ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick) { Position = DynamicActionColumnPosition.Start }); HiddenColumns.Add(x => x.Documents); HiddenColumns.Add(x => x.Batch.ID); HiddenColumns.Add(x => x.Batch.Type); AllButton = AddButton("Hide System", null, ToggleAllTransations); } public DateTime StartDate { get; set; } = DateTime.MinValue; public DateTime EndDate { get; set; } = DateTime.MaxValue; public IStockLocation Location { get; set; } public bool AllowNullLocation { get => (bool)GetValue(AllowNullLocationProperty); set => SetValue(AllowNullLocationProperty, value); } public IStockMovementBatch Batch { get; set; } public bool AllowNullBatch { get => (bool)GetValue(AllowNullBatchProperty); set => SetValue(AllowNullBatchProperty, value); } public event DataModelUpdateEvent OnUpdateDataModel; public string SectionName => "Stock Movements"; public DataModel DataModel(Selection selection) { var ids = ExtractValues(x => x.ID, selection).ToArray(); return new BaseDataModel(new Filter(x => x.ID).InList(ids)); } private bool DocumentsClick(CoreRow arg) { if (arg == null || arg.Get(x => x.Documents) == 0) return false; var docs = new List(); using (new WaitCursor()) { var batchid = arg.Get(x => x.Batch.ID); var table = new Client().Query( new Filter(x => x.EntityLink.ID).IsEqualTo(batchid) ); foreach (var row in table.Rows) docs.Add(row.ToObject()); } if (docs.Any()) { var editor = new DocumentEditor(docs.ToArray()); //editor.PrintAllowed = Security.IsAllowed(); editor.SaveAllowed = Security.IsAllowed(); editor.ShowDialog(); } else { MessageBox.Show("No Documents Available!"); } return false; } private BitmapImage DocumentsImage(CoreRow arg) { if (arg == null) return docs; return arg.Get(x => x.Documents) > 0 ? docs : null; } private bool ToggleAllTransations(Button arg1, CoreRow[] arg2) { bShowAll = !bShowAll; AllButton.Content = bShowAll ? "Hide System" : "Show All"; return true; } protected override void Reload(Filters criteria, Columns columns, ref SortOrder? sort, Action action) { if (!AllowNullLocation && (Location == null || Location.ID == Guid.Empty)) { criteria.Add(new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid)); criteria.Add(new Filter(x => x.ID).IsEqualTo(Guid.Empty)); } else { if (Location != null) criteria.Add(new Filter(x => x.Location.ID).IsEqualTo(Location.ID)); } if (!AllowNullBatch && (Batch == null || Batch.ID == Guid.Empty)) { criteria.Add(new Filter(x => x.ID).IsEqualTo(CoreUtils.FullGuid)); criteria.Add(new Filter(x => x.ID).IsEqualTo(Guid.Empty)); } else { if (Batch != null) criteria.Add(new Filter(x => x.Batch.ID).IsEqualTo(Batch.ID)); } if (!bShowAll) criteria.Add(new Filter(x => x.System).IsEqualTo(false)); if (!DateTime.Equals(StartDate, DateTime.MinValue)) criteria.Add(new Filter(x => x.Date).IsGreaterThanOrEqualTo(StartDate)); if (!DateTime.Equals(EndDate, DateTime.MaxValue)) criteria.Add(new Filter(x => x.Date).IsLessThan(EndDate.Date.AddDays(1))); sort = new SortOrder(x => x.Date, SortDirection.Descending).ThenBy(x => x.System); base.Reload(criteria, columns, ref sort, action); } protected override StockMovement CreateItem() { var result = base.CreateItem(); result.Location.ID = Location != null ? Location.ID : Guid.Empty; return result; } protected override BaseEditor? GetEditor(object item, DynamicGridColumn column) { if (column.ColumnName.Equals("Location.ID")) return new NullEditor(); return base.GetEditor(item, column); } protected override void DeleteItems(params CoreRow[] rows) { if (!rows.Any()) { MessageBox.Show("Please select an item first!"); return; } var txnid = rows.First().Get(x => x.Transaction); var allrecords = new Client().Query( new Filter(x => x.Transaction).IsEqualTo(txnid), new Columns(x => x.ID) ); base.DeleteItems(allrecords.Rows.ToArray()); } //int typecolumn = -1; protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style) { var result = base.GetRowStyle(row, style); if (syscolumn == -1) { var col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("System")); syscolumn = row.Table.Columns.IndexOf(col); } if (row.Values[syscolumn].Equals(true)) //row.Get(x => x.System)) { result = new DynamicGridRowStyle { Foreground = new SolidColorBrush(Colors.Gray), FontStyle = FontStyles.Italic, Background = new SolidColorBrush(Colors.Gainsboro) }; return result; } //if (typecolumn == -1) //{ // CoreColumn col = row.Table.Columns.FirstOrDefault(x => x.ColumnName.Equals("Batch.Type")); // typecolumn = row.Table.Columns.IndexOf(col); //} //var type = (StockMovementBatchType)row.Values[typecolumn]; //result = new NewDynamicGridStyle() //{ // Background = new SolidColorBrush( // type == StockMovementBatchType.Stocktake // ? Colors.LightGreen // : type == StockMovementBatchType.Receipt // ? Colors.LightYellow // : Colors.LightSalmon // ) //}; return result; } } }