using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Net.Http; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Configuration; using InABox.Core; using InABox.Integration.Awg; using InABox.WPF; using Inflector; using Microsoft.Xaml.Behaviors.Core; using PRSDimensionUtils; using sun.text.resources.ro; namespace PRSDesktop.Integrations.Common; public class AWGMappingWindowViewModel : DependencyObject { private readonly LogikalSettings _settings; public LogikalSettings Settings => _settings; public AWGMappingWindowViewModel() : base() { _settings = new GlobalConfiguration().Load(); } private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register( nameof(SourceType), typeof(IntegrationSourceType), typeof(AWGMappingWindowViewModel) ); public IntegrationSourceType SourceType { get => (IntegrationSourceType)GetValue(SourceTypeProperty); set => SetValue(SourceTypeProperty, value); } private static readonly DependencyProperty StylesProperty = DependencyProperty.Register( nameof(Styles), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(StylesChanged) ); private static void StylesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable styles) return; var mappings = model.ExtractMappings( styles, (logikal, mapping) => { mapping.Cost = logikal.Cost; mapping.StyleType = logikal.StyleType; }, x => x.Code ); mappings.Wait(); model.StyleMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Styles { get => GetValue(StylesProperty) as IEnumerable; set => SetValue(StylesProperty, value); } private static readonly DependencyProperty GroupsProperty = DependencyProperty.Register( nameof(Groups), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(GroupsChanged) ); private static void GroupsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable groups) return; var mappings = model.ExtractMappings( groups, (logikal, mapping) => { mapping.Parent = logikal.Parent; }, x => x.Code ); mappings.Wait(); model.GroupMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Groups { get => GetValue(GroupsProperty) as IEnumerable; set => SetValue(GroupsProperty, value); } private static readonly DependencyProperty SuppliersProperty = DependencyProperty.Register( nameof(Suppliers), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(SuppliersChanged) ); private static void SuppliersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable suppliers) return; var mappings = model.ExtractMappings( suppliers, null, x => x.Code ); mappings.Wait(); model.SupplierMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Suppliers { get => GetValue(SuppliersProperty) as IEnumerable; set => SetValue(SuppliersProperty, value); } private static readonly DependencyProperty DiscountsProperty = DependencyProperty.Register( nameof(Discounts), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(DiscountsChanged) ); private static void DiscountsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable discounts) return; var mappings = model.ExtractMappings( discounts, (logikal, mapping) => { mapping.Supplier = logikal.SupplierCode; mapping.Discount = logikal.Discount; }, x => x.Code ); mappings.Wait(); model.DiscountMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Discounts { get => GetValue(DiscountsProperty) as IEnumerable; set => SetValue(DiscountsProperty, value); } private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register( nameof(Profiles), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(ProfilesChanged) ); private static void ProfilesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable profiles) return; var mappings = model.ExtractMappings( profiles, (logikal, mapping) => { mapping.Style = logikal.Finish; mapping.Dimensions.Unit.CopyFrom(model.Settings.ProfileUom); mapping.Dimensions.Length = logikal.Length; mapping.Group = logikal.Group; mapping.Supplier = logikal.Supplier; mapping.Cost = logikal.Cost; mapping.Quantity = logikal.Quantity; mapping.TreatmentParameters[AwgStyleType.Powdercoated] = logikal.PaintPerimeter; mapping.TreatmentParameters[AwgStyleType.Anodised] = logikal.AnodizePerimeter; }, x => x.Code ); mappings.Wait(); model.ProfileMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Profiles { get => GetValue(ProfilesProperty) as IEnumerable; set => SetValue(ProfilesProperty, value); } private static readonly DependencyProperty GasketsProperty = DependencyProperty.Register( nameof(Gaskets), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(GasketsChanged) ); private static void GasketsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable gaskets) return; var mappings = model.ExtractMappings( gaskets, (logikal, mapping) => { mapping.Group = logikal.Group; mapping.Supplier = logikal.Supplier; mapping.Dimensions.Unit.CopyFrom(model.Settings.GasketUom); mapping.Dimensions.Length = logikal.Length; mapping.Cost = logikal.Cost; mapping.Quantity = logikal.Quantity; }, x => x.Code ); mappings.Wait(); model.GasketMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Gaskets { get => GetValue(GasketsProperty) as IEnumerable; set => SetValue(GasketsProperty, value); } private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register( nameof(Components), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(ComponentsChanged) ); private static void ComponentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable components) return; var mappings = model.ExtractMappings( components, (logikal, mapping) => { mapping.Dimensions.Unit.CopyFrom(model.Settings.ComponentUom); mapping.Dimensions.Quantity = logikal.PackSize; mapping.Quantity = logikal.Quantity; mapping.Cost = logikal.Cost; mapping.Group = logikal.Group; mapping.Supplier = logikal.Supplier; }, x => x.Code ); mappings.Wait(); model.ComponentMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Components { get => GetValue(ComponentsProperty) as IEnumerable; set => SetValue(ComponentsProperty, value); } private static readonly DependencyProperty GlassProperty = DependencyProperty.Register( nameof(Glass), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(GlassChanged) ); private static void GlassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable glass) return; var mappings = model.ExtractMappings( glass, (logikal, mapping) => { mapping.Group = logikal.Group; mapping.Supplier = logikal.Supplier; mapping.Dimensions.Unit.CopyFrom(model.Settings.GlassUom); mapping.Dimensions.Height = logikal.Height; mapping.Dimensions.Width = logikal.Width; mapping.Style = logikal.Treatment; mapping.Quantity = logikal.Quantity; mapping.Cost = logikal.Cost; }, x => x.Code ); mappings.Wait(); model.GlassMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Glass { get => GetValue(GlassProperty) as IEnumerable; set => SetValue(GlassProperty, value); } private static readonly DependencyProperty LabourProperty = DependencyProperty.Register( nameof(Labour), typeof(IEnumerable), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(LabourChanged) ); private static void LabourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable labour) return; var mappings = model.ExtractMappings( labour, (logikal, mapping) => { mapping.Quantity = logikal.Quantity; }, x => x.Code ); mappings.Wait(); model.LabourMappings = mappings.Result; model.CheckChanged(); } public IEnumerable? Labour { get => GetValue(LabourProperty) as IEnumerable; set => SetValue(LabourProperty, value); } private static void SectionCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is AWGMappingWindowViewModel model) model.CheckChanged(); } private static readonly DependencyProperty StyleMappingsProperty = DependencyProperty.Register( nameof(StyleMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? StyleMappings { get => GetValue(StyleMappingsProperty) as List; set => SetValue(StyleMappingsProperty, value); } private static readonly DependencyProperty StylesCheckedProperty = DependencyProperty.Register( nameof(StylesChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool StylesChecked { get => (bool)GetValue(StylesCheckedProperty); set => SetValue(StylesCheckedProperty, value); } private static readonly DependencyProperty GroupMappingsProperty = DependencyProperty.Register( nameof(GroupMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? GroupMappings { get => GetValue(GroupMappingsProperty) as List; set => SetValue(GroupMappingsProperty, value); } private static readonly DependencyProperty GroupsCheckedProperty = DependencyProperty.Register( nameof(GroupsChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool GroupsChecked { get => (bool)GetValue(GroupsCheckedProperty); set => SetValue(GroupsCheckedProperty, value); } private static readonly DependencyProperty SupplierMappingsProperty = DependencyProperty.Register( nameof(SupplierMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? SupplierMappings { get => GetValue(SupplierMappingsProperty) as List; set => SetValue(SupplierMappingsProperty, value); } private static readonly DependencyProperty DiscountMappingsProperty = DependencyProperty.Register( nameof(DiscountMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? DiscountMappings { get => GetValue(DiscountMappingsProperty) as List; set => SetValue(DiscountMappingsProperty, value); } private static readonly DependencyProperty SuppliersCheckedProperty = DependencyProperty.Register( nameof(SuppliersChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool SuppliersChecked { get => (bool)GetValue(SuppliersCheckedProperty); set => SetValue(SuppliersCheckedProperty, value); } private static readonly DependencyProperty DiscountsCheckedProperty = DependencyProperty.Register( nameof(DiscountsChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool DiscountsChecked { get => (bool)GetValue(DiscountsCheckedProperty); set => SetValue(DiscountsCheckedProperty, value); } private static readonly DependencyProperty ProfileMappingsProperty = DependencyProperty.Register( nameof(ProfileMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? ProfileMappings { get => GetValue(ProfileMappingsProperty) as List; set => SetValue(ProfileMappingsProperty, value); } private static readonly DependencyProperty ProfilesCheckedProperty = DependencyProperty.Register( nameof(ProfilesChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool ProfilesChecked { get => (bool)GetValue(ProfilesCheckedProperty); set => SetValue(ProfilesCheckedProperty, value); } private static readonly DependencyProperty GasketMappingsProperty = DependencyProperty.Register( nameof(GasketMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? GasketMappings { get => GetValue(GasketMappingsProperty) as List; set => SetValue(GasketMappingsProperty, value); } private static readonly DependencyProperty GasketsCheckedProperty = DependencyProperty.Register( nameof(GasketsChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool GasketsChecked { get => (bool)GetValue(GasketsCheckedProperty); set => SetValue(GasketsCheckedProperty, value); } private static readonly DependencyProperty ComponentMappingsProperty = DependencyProperty.Register( nameof(ComponentMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? ComponentMappings { get => GetValue(ComponentMappingsProperty) as List; set => SetValue(ComponentMappingsProperty, value); } private static readonly DependencyProperty ComponentsCheckedProperty = DependencyProperty.Register( nameof(ComponentsChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool ComponentsChecked { get => (bool)GetValue(ComponentsCheckedProperty); set => SetValue(ComponentsCheckedProperty, value); } private static readonly DependencyProperty GlassMappingsProperty = DependencyProperty.Register( nameof(GlassMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? GlassMappings { get => GetValue(GlassMappingsProperty) as List; set => SetValue(GlassMappingsProperty, value); } private static readonly DependencyProperty GlassCheckedProperty = DependencyProperty.Register( nameof(GlassChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool GlassChecked { get => (bool)GetValue(GlassCheckedProperty); set => SetValue(GlassCheckedProperty, value); } private static readonly DependencyProperty LabourMappingsProperty = DependencyProperty.Register( nameof(LabourMappings), typeof(List), typeof(AWGMappingWindowViewModel) ); public List? LabourMappings { get => GetValue(LabourMappingsProperty) as List; set => SetValue(LabourMappingsProperty, value); } private static readonly DependencyProperty LabourCheckedProperty = DependencyProperty.Register( nameof(LabourChecked), typeof(bool), typeof(AWGMappingWindowViewModel), new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged) ); public bool LabourChecked { get => (bool)GetValue(LabourCheckedProperty); set => SetValue(LabourCheckedProperty, value); } private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register( nameof(Sections), typeof(CoreObservableCollection>), typeof(AWGMappingWindowViewModel), new PropertyMetadata(new CoreObservableCollection>()) ); public CoreObservableCollection> Sections { get => (CoreObservableCollection>)GetValue(SectionsProperty); set => SetValue(SectionsProperty, value); } private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register( nameof(SelectedSection), typeof(KeyValuePair?), typeof(AWGMappingWindowViewModel) ); public KeyValuePair? SelectedSection { get => (KeyValuePair?)GetValue(SelectedSectionProperty); set => SetValue(SelectedSectionProperty, value); } private static readonly DependencyProperty MappingsCompleteProperty = DependencyProperty.Register( nameof(MappingsComplete), typeof(bool), typeof(AWGMappingWindowViewModel) ); public bool MappingsComplete { get => (bool)GetValue(MappingsCompleteProperty); set => SetValue(MappingsCompleteProperty, value); } private Task> ExtractMappings( IEnumerable? items, Action? populate, Expression> entitycode ) where TType : IAwgItem where TCode : BaseIntegrationSource, IRemotable, IPersistent, new() where TEntity : Entity, IRemotable, IPersistent, new() where TLink : EntityLink { return Task.Run(() => { var results = new List(); if (items == null) return results; //var sourceitems = new Dictionary(); //foreach (var item in items) // sourceitems[item.Code] = item.Description; var keys = items.Select(x => x.Code).Distinct().ToArray(); MultiQuery query = new(); query.Add( new Filter(entitycode).InList(keys), Columns.Required().Add(x => x.ID).Add(entitycode) ); var entitycodecol = $"Entity.{CoreUtils.GetFullPropertyName(entitycode, ".")}"; query.Add( new Filter(x => x.Code).InList(keys), Columns.Required() .Add(x => x.ID) .Add(x => x.Code) .Add(x => x.Entity.ID) .Add(entitycodecol) ); query.Query(); var mappings = query.Get().ToObjects().ToArray(); var entities = query.Get(); foreach (var item in items) { var result = new TCode() { Code = item.Code, Description = item.Description }; populate?.Invoke(item, result); var mapping = mappings.FirstOrDefault(x => string.Equals(x.Code, item.Code)); if (mapping != null) result.Entity.CopyFrom(mapping.Entity); else { var entity = entities.Rows.FirstOrDefault(r => Equals(item.Code, r.Get(entitycode))); if (entity != null) { result.Entity.CopyFrom(entity.ToObject()); result.DirectMatch = true; } } results.Add(result); // result.PropertyChanged += (_, _) => // { // // TMapping mapping = mappingtable.Rows.FirstOrDefault(r => // // string.Equals(r.Get(c => c.Code), result.Code))?.ToObject(); // // if (mapping == null) // // mapping = new TMapping() { Code = result.Code }; // // mapping.Entity.ID = result.Entity.ID; // // new Client().Save(mapping, "Created from BOM Integration Window"); // // CheckChanged(); // }; } return results; }); } public void CheckChanged() { Dispatcher.BeginInvoke(() => { var curSel = SelectedSection?.Key ?? ""; SelectedSection = null; Sections.Clear(); if (StylesChecked && StyleMappings?.Any() == true) Sections.Add(new KeyValuePair("Styles", Resources.palette.AsBitmapImage(64, 64))); if (GroupsChecked && GroupMappings?.Any() == true) Sections.Add( new KeyValuePair("Groups", Resources.productgroup.AsBitmapImage(64, 64))); if (SuppliersChecked && SupplierMappings?.Any() == true) Sections.Add( new KeyValuePair("Suppliers", Resources.supplier.AsBitmapImage(64, 64))); if (DiscountsChecked && DiscountMappings?.Any() == true) Sections.Add( new KeyValuePair("Discounts", Resources.receipt.AsBitmapImage(64, 64))); if (ProfilesChecked && ProfileMappings?.Any() == true) Sections.Add( new KeyValuePair("Profiles", Resources.profile.AsBitmapImage(64, 64))); if (GasketsChecked && GasketMappings?.Any() == true) Sections.Add(new KeyValuePair("Gaskets", Resources.gasket.AsBitmapImage(64, 64))); if (ComponentsChecked && ComponentMappings?.Any() == true) Sections.Add( new KeyValuePair("Components", Resources.fixings.AsBitmapImage(64, 64))); if (GlassChecked && GlassMappings?.Any() == true) Sections.Add(new KeyValuePair("Glass", Resources.glass.AsBitmapImage(64, 64))); if (LabourChecked && LabourMappings?.Any() == true) Sections.Add(new KeyValuePair("Labour", Resources.quality.AsBitmapImage(64, 64))); SelectedSection = Sections.Any(x => string.Equals(x.Key, curSel)) ? Sections.First(x => string.Equals(x.Key, curSel)) : Sections.FirstOrDefault(); var styles = !StylesChecked || (StyleMappings?.Any() != true) || (StyleMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var groups = !GroupsChecked || (GroupMappings?.Any() != true) || (GroupMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var suppliers = !SuppliersChecked || (SupplierMappings?.Any() != true) || (SupplierMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var discounts = !DiscountsChecked || (DiscountMappings?.Any() != true) || (DiscountMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var profiles = !ProfilesChecked || (ProfileMappings?.Any() != true) || (ProfileMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var gaskets = !GasketsChecked || (GasketMappings?.Any() != true) || (GasketMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var components = !ComponentsChecked || (ComponentMappings?.Any() != true) || (ComponentMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var glass = !GlassChecked || (GlassMappings?.Any() != true) || (GlassMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); var labour = !LabourChecked || (LabourMappings?.Any() != true) || (LabourMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false); MappingsComplete = styles && groups && suppliers && discounts && profiles && gaskets && components && glass && labour; }); } public ICommand CreateStyle => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Description = args.Mapping.Description ?? ""; args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; TreatmentTypeLink? type = args.Mapping.StyleType == AwgStyleType.Powdercoated ? _settings.PowdercoatedType : args.Mapping.StyleType == AwgStyleType.Anodised ? _settings.AnodisedType : args.Mapping.StyleType == AwgStyleType.Varnished ? _settings.VarnishedType : args.Mapping.StyleType == AwgStyleType.Taped ? _settings.TapedType : null; if (type != null) { args.Entity.TreatmentType.CopyFrom(type); var product = new Client().Query( new Filter(x => x.Code).IsEqualTo(args.Entity.Code), Columns.Local() ).ToArray().FirstOrDefault(); if (product == null) { product = new Product(); product.Code = args.Entity.Code; product.Name = args.Entity.Description; product.Problem.Notes = ["Created from BOM Integration Window"]; product.TreatmentType.CopyFrom(type); Client.Save(product, "Created from AWG Mapping Window"); } args.Entity.StockTreatmentProduct.CopyFrom(product); args.Entity.ManufacturingTreatmentProduct.CopyFrom(product); } } } ); public ICommand CreateGroup => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { Guid parentid = Guid.Empty; var parentcodes = args.Mapping.Parent.Split('/').Select(x => x.Trim().ToUpper()).ToArray(); var parents = Client.Query( new Filter(x => x.Code).InList(parentcodes), Columns.None().Add(x => x.ID).Add(x => x.Code) ).ToArray(); foreach (var parentcode in parentcodes) { var parent = parents.FirstOrDefault(x => Equals(x.Code,parentcode)); if (parent == null) { parent = new ProductGroup(); parent.Code = parentcode; parent.Description = parentcode.Titleize(); parent.Parent.ID = parentid; parent.Problem.Notes = ["Created from BOM Integration Window"]; Client.Save(parent, "Created from AWG Mapping Window"); } parentid = parent.ID; } args.Entity.Parent.ID = parentid; args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Description = args.Mapping.Description ?? ""; args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand CreateSupplier => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Name = args.Mapping.Description ?? ""; args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand CreateDiscount => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Description = args.Mapping.Description ?? ""; args.Entity.Type = SupplierDiscountGroupType.Discount; var supplier = SupplierMappings.FirstOrDefault(x => string.Equals(x.Code, args.Mapping.Supplier))?.Entity.ID; if (supplier == null) supplier = new Client() .Query(new Filter(x => x.Code).IsEqualTo(args.Entity.Code), Columns.None().Add(x => x.ID)) .ToArray() .FirstOrDefault()?.ID; if (supplier == null) throw new Exception($"Unable to locate Supplier [{args.Mapping.Supplier}]"); args.Entity.Supplier.ID = supplier.Value; args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand AfterCreateDiscount => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { CreateSupplierDiscountInstance(args); } } ); private void CreateSupplierDiscountInstance(IntegrationGridCreateEntityArgs args) { var discount = new SupplierDiscount(); discount.Group.CopyFrom(args.Entity); discount.Value = args.Mapping.Discount; discount.Job.ID = Guid.Empty; Client.Save(discount, "Created from AWG Mapping Window"); } public ICommand CreateProfile => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Name = args.Mapping.Description ?? ""; args.Entity.UnitOfMeasure.CopyFrom(_settings.ProfileUom); var group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Group)); if (group != null) args.Entity.Group.CopyFrom(group.Entity); args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; CreateImage(args); } } ); public ICommand AfterCreateProduct => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { CreateTreatmentParams(args, AwgStyleType.Powdercoated, Settings.PowdercoatedType); CreateTreatmentParams(args, AwgStyleType.Anodised, Settings.AnodisedType); CreateTreatmentParams(args, AwgStyleType.Taped, Settings.TapedType); CreateTreatmentParams(args, AwgStyleType.Varnished, Settings.VarnishedType); CreateSupplierProduct(args); } } ); private void CreateImage(IntegrationGridCreateEntityArgs args) { if (!string.IsNullOrWhiteSpace(_settings.ImageUrl)) { var model = new LogikalCodeModel() { Code = args.Mapping.Code }; var _expression = new CoreExpression(_settings.ImageUrl); if (_expression.Evaluate(model).Get(out var eval, out var e)) { var tcs = new TaskCompletionSource(); new HttpClient().GetByteArrayAsync(eval) .ContinueWith( t => { if (t.IsFaulted) tcs.SetException(t.Exception); else tcs.SetResult(t.Result); }); try { var data = tcs.Task.Result; var document = new Document() { Data = tcs.Task.Result, CRC = CoreUtils.CalculateCRC(data), FileName = args.Mapping.Code }; Client.Save(document,"Created from AWG Mapping Window"); args.Entity.Image.ID = document.ID; } catch (Exception exception) { Logger.Send(LogType.Error,"",$"Exception in CreateImage(): {exception.Message}"); } } } } private void CreateTreatmentParams(IntegrationGridCreateEntityArgs args, AwgStyleType type, TreatmentTypeLink link) { if (args.Mapping.TreatmentParameters.ContainsKey(type) && !Equals(link.ID,Guid.Empty)) { ProductTreatment treatment = new(); treatment.Product.CopyFrom(args.Entity); treatment.TreatmentType.CopyFrom(link); treatment.Parameter = args.Mapping.TreatmentParameters[type]; Client.Save(treatment,"Created from AWG Mapping Window"); } } private void CreateSupplierProduct(IntegrationGridCreateEntityArgs args) { var supplier = SupplierMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Supplier)); if (supplier != null) { SupplierProduct sp = new SupplierProduct(); sp.Product.ID = args.Entity.ID; sp.SupplierLink.ID = supplier.Entity.ID; sp.SupplierCode = args.Mapping.Code; sp.SupplierDescription = args.Mapping.Description; sp.Dimensions.CopyFrom(args.Mapping.Dimensions); var style = StyleMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Style)); if (style != null) sp.Style.CopyFrom(style.Entity); sp.TradePrice = args.Mapping.Cost; // * (args.Mapping.Quantity.IsEffectivelyEqual(0.0) ? 1.0 : args.Mapping.Dimensions.Quantity); Client.Save(sp,"Created from AWG Mapping Window"); } } public ICommand CreateGasket => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Name = args.Mapping.Description ?? ""; args.Entity.UnitOfMeasure.CopyFrom(_settings.GasketUom); var group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Group)); if (group != null) args.Entity.Group.CopyFrom(group.Entity); args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand CreateComponent => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Name = args.Mapping.Description ?? ""; args.Entity.UnitOfMeasure.CopyFrom(_settings.ComponentUom); var group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Group)); if (group != null) args.Entity.Group.CopyFrom(group.Entity); args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand CreateGlass => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Name = args.Mapping.Description ?? ""; args.Entity.UnitOfMeasure.CopyFrom(_settings.GlassUom); var group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, args.Mapping.Group)); if (group != null) args.Entity.Group.CopyFrom(group.Entity); args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); public ICommand CreateActivity => new ActionCommand( o => { if (o is IntegrationGridCreateEntityArgs args) { args.Entity.Code = args.Mapping.Code ?? ""; args.Entity.Description = args.Mapping.Description ?? ""; args.Entity.Problem.Notes = ["Created from BOM Integration Window"]; } } ); private class RawDimensions : IBaseDimensions { public double Quantity { get; set; } public double Length { get; set; } public double Width { get; set; } public double Height { get; set; } public double Weight { get; set; } } public void GetDiscounts(Action? discountCallback) { if (DiscountsChecked && Discounts != null) { foreach (var discount in Discounts) { var discountmapping = DiscountMappings?.FirstOrDefault(x => x.Code == discount.Code); if (discountmapping != null && discountCallback != null) discountCallback?.Invoke(discountmapping.Entity,discountmapping.Discount); } } } public void GetParts( Action? productCallback, Action? labourCallback) { GetParts(Profiles,Gaskets,Components,Glass,Labour,productCallback,labourCallback); } public void GetParts( IEnumerable? profiles, IEnumerable? gaskets, IEnumerable? components, IEnumerable? glasses, IEnumerable? labour, Action? productCallback, Action? labourCallback) where TProfile : IAwgProfile where TGasket : IAwgGasket where TComponent : IAwgComponent where TGlass : IAwgGlass where TLabour : IAwgLabour { if (ProfilesChecked && profiles != null) { foreach (var profile in profiles) { var profilemapping = ProfileMappings?.FirstOrDefault(x => x.Code == profile.Code); var stylemapping = StyleMappings?.FirstOrDefault(x => string.Equals(x.Code, profile.Finish)); if (profilemapping != null && productCallback is not null) { productCallback( profilemapping.Entity, stylemapping?.Entity, new RawDimensions() { Length = profile.Length }, profile.Quantity, profile.Cost * profile.Quantity); } } } if (GasketsChecked && gaskets != null) { foreach (var gasket in gaskets) { var _mapping = GasketMappings?.FirstOrDefault(x => x.Code == gasket.Code); if (_mapping != null && productCallback is not null) { _mapping.ConvertDimensions( x => x.Dimensions, x => x.Quantity, x => x.Cost, Client.Provider ); productCallback( _mapping.Entity, null, new RawDimensions() { Length = _mapping.Dimensions.Length }, _mapping.Quantity, _mapping.Quantity * _mapping.Cost); } } } if (ComponentsChecked && components != null) { foreach (var component in components) { var _mapping = ComponentMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Code)); if (_mapping != null && productCallback is not null) { _mapping.ConvertDimensions( x => x.Dimensions, x => x.Quantity, x => x.Cost, Client.Provider ); productCallback( _mapping.Entity, null, new RawDimensions() { Quantity = _mapping.Dimensions.Quantity }, _mapping.Quantity, _mapping.Quantity * _mapping.Cost); } } } if (GlassChecked && glasses != null) { foreach (var glass in glasses) { var _mapping = GlassMappings?.FirstOrDefault(x => string.Equals(x.Code, glass.Code)); if (_mapping != null && productCallback is not null) { productCallback( _mapping.Entity, null, new RawDimensions() { Height = glass.Height, Width = glass.Width }, glass.Quantity, glass.Quantity * glass.Cost); } } } if (LabourChecked && labour != null) { foreach (var activity in labour) { var _mapping = LabourMappings?.FirstOrDefault(x => string.Equals(x.Code, activity.Code)); if (_mapping != null && labourCallback is not null) { labourCallback( _mapping.Entity, TimeSpan.FromHours(activity.Quantity), activity.Quantity * activity.Cost); } } } } }