123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- 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;
- using Syncfusion.UI.Xaml.Diagram.Controls;
- namespace PRSDesktop;
- public class StockMovementGrid : DynamicDataGrid<StockMovement>, IDataModelSource, ISpecificGrid
- {
- 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 Button AllButton;
- private bool bShowAll = true;
- private readonly BitmapImage docs = PRSDesktop.Resources.doc_png.AsBitmapImage();
- private int syscolumn = -1;
- private static readonly BitmapImage? post = PRSDesktop.Resources.post.AsBitmapImage();
- private static readonly BitmapImage? tick = PRSDesktop.Resources.tick.AsBitmapImage();
- private static readonly BitmapImage? warning = PRSDesktop.Resources.warning.AsBitmapImage();
- private static readonly BitmapImage? refresh = PRSDesktop.Resources.refresh.AsBitmapImage();
- public StockMovementGrid()
- {
- ColumnsTag = "StockMovementGrid";
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.FilterRows = true;
- }
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x => x.System);
- HiddenColumns.Add(x => x.Transaction);
- HiddenColumns.Add(x=>x.Product.ID);
- HiddenColumns.Add(x=>x.Location.ID);
- HiddenColumns.Add(x=>x.Style.ID);
- HiddenColumns.Add(x=>x.Job.ID);
- HiddenColumns.Add(x=>x.Dimensions.Unit.ID);
- HiddenColumns.Add(x=>x.Dimensions.UnitSize);
- 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);
- HiddenColumns.Add(x => x.JobRequisitionItem.Requisition.Number);
- AllButton = AddButton("Hide System", null, ToggleAllTransations);
- HiddenColumns.Add(x => x.PostedStatus);
- HiddenColumns.Add(x => x.PostedNote);
- BeforeRefresh += StockMovementGrid_BeforeRefresh;
- ActionColumns.Add(new DynamicImageColumn(Posted_Image, null)
- {
- ToolTip = Posted_ToolTip
- });
- }
- 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<StockMovement>(new Filter<StockMovement>(x => x.ID).InList(ids));
- }
- private FrameworkElement? Posted_ToolTip(DynamicActionColumn column, CoreRow? row)
- {
- if (row is null)
- {
- return column.TextToolTip("Stock Movement Processed Status");
- }
- return column.TextToolTip(row.Get<StockMovement, PostedStatus>(x => x.PostedStatus) switch
- {
- PostedStatus.PostFailed => "Post failed: " + row.Get<StockMovement, string>(x => x.PostedNote),
- PostedStatus.RequiresRepost => "Repost required: " + row.Get<StockMovement, string>(x => x.PostedNote),
- PostedStatus.Posted => "Processed",
- PostedStatus.NeverPosted or _ => "Not posted yet",
- });
- }
- private BitmapImage? Posted_Image(CoreRow? row)
- {
- if (row is null)
- return post;
- return row.Get<StockMovement, PostedStatus>(x => x.PostedStatus) switch
- {
- PostedStatus.PostFailed => warning,
- PostedStatus.Posted => tick,
- PostedStatus.RequiresRepost => refresh,
- PostedStatus.NeverPosted or _ => null,
- };
- }
- private bool DocumentsClick(CoreRow? arg)
- {
- if (arg == null || arg.Get<StockMovement, int>(x => x.Documents) == 0)
- return false;
- var docs = new List<IEntityDocument>();
- using (new WaitCursor())
- {
- var batchid = arg.Get<StockMovement, Guid>(x => x.Batch.ID);
- var table = new Client<StockMovementBatchDocument>().Query(
- new Filter<StockMovementBatchDocument>(x => x.EntityLink.ID).IsEqualTo(batchid)
- );
- foreach (var row in table.Rows)
- docs.Add(row.ToObject<StockMovementBatchDocument>());
- }
- if (docs.Any())
- {
- var editor = new DocumentEditor(docs.ToArray());
- //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
- editor.SaveAllowed = Security.IsAllowed<CanSaveFactoryFloorDrawings>();
- editor.ShowDialog();
- }
- else
- {
- MessageBox.Show("No Documents Available!");
- }
- return false;
- }
- private BitmapImage? DocumentsImage(CoreRow? arg)
- {
- if (arg == null)
- return docs;
- return arg.Get<Delivery, int>(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<StockMovement> criteria, Columns<StockMovement> columns, ref SortOrder<StockMovement>? sort,
- Action<CoreTable, Exception> action)
- {
- if (!AllowNullLocation && (Location == null || Location.ID == Guid.Empty))
- {
- criteria.Add(new Filter<StockMovement>().None());
- }
- else
- {
- if (Location != null)
- criteria.Add(new Filter<StockMovement>(x => x.Location.ID).IsEqualTo(Location.ID));
- }
- if (!AllowNullBatch && (Batch == null || Batch.ID == Guid.Empty))
- {
- criteria.Add(new Filter<StockMovement>().None());
- }
- else
- {
- if (Batch != null)
- criteria.Add(new Filter<StockMovement>(x => x.Batch.ID).IsEqualTo(Batch.ID));
- }
- if (!bShowAll)
- criteria.Add(new Filter<StockMovement>(x => x.System).IsEqualTo(false));
- if (!DateTime.Equals(StartDate, DateTime.MinValue))
- criteria.Add(new Filter<StockMovement>(x => x.Date).IsGreaterThanOrEqualTo(StartDate));
- if (!DateTime.Equals(EndDate, DateTime.MaxValue))
- criteria.Add(new Filter<StockMovement>(x => x.Date).IsLessThan(EndDate.Date.AddDays(1)));
- sort = new SortOrder<StockMovement>(x => x.Date, SortDirection.Descending).ThenBy(x => x.System);
- base.Reload(criteria, columns, ref sort, action);
- }
- public 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);
- }
- public override void DeleteItems(params CoreRow[] rows)
- {
- if (!rows.Any())
- {
- MessageBox.Show("Please select an item first!");
- return;
- }
- var txnid = rows.First().Get<StockMovement, Guid>(x => x.Transaction);
- var allrecords = new Client<StockMovement>().Query(
- new Filter<StockMovement>(x => x.Transaction).IsEqualTo(txnid),
- Columns.None<StockMovement>().Add(x => x.ID)
- );
- base.DeleteItems(allrecords.Rows.ToArray());
- }
- private void StockMovementGrid_BeforeRefresh(object sender, BeforeRefreshEventArgs args)
- {
- syscolumn = -1;
- }
- //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 (object.Equals(row.Values[syscolumn], true)) //row.Get<StockMovement, bool>(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;
- }
- }
|