12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- 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 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<LogikalSettings>().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);
- }
- // public static DependencyProperty JobIDProperty = DependencyProperty.Register(
- // nameof(JobID),
- // typeof(Guid),
- // typeof(IntegrationBOMWindowViewModel)
- // );
- //
- // public Guid JobID
- // {
- // get => (Guid)GetValue(JobIDProperty);
- // set => SetValue(JobIDProperty, value);
- // }
- // public static DependencyProperty BOMProperty = DependencyProperty.Register(
- // nameof(BOM),
- // typeof(IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>),
- // typeof(IntegrationBOMWindowViewModel),
- // new FrameworkPropertyMetadata(BOMChanged));
- //
- // private static void BOMChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- // {
- //
- // if (d is not IntegrationBOMWindowViewModel model)
- // return;
- //
- // var bom =
- // e.NewValue as IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass,
- // IAwgLabour>;
- //
- // var styles = model.ExtractMappings<ProductStyleIntegrationSource, IAwgFinish, ProductStyle, ProductStyleLink, ProductStyleIntegrationSource>(
- // bom?.Finishes, x => x.Code, x => x.Description, x => x.Code);
- //
- // var profiles = model.ExtractMappings<ProductIntegrationSource,IAwgProfile,Product,ProductLink, ProductIntegrationSource>(
- // bom?.Profiles, x => x.Code, x => x.Description, x => x.Code);
- //
- // var gaskets = model.ExtractMappings<ProductIntegrationSource,IAwgGasket,Product,ProductLink, ProductIntegrationSource>(
- // bom?.Gaskets, x => x.Code, x => x.Description, x => x.Code);
- //
- // var components = model.ExtractMappings<ProductIntegrationSource,IAwgComponent,Product,ProductLink, ProductIntegrationSource>(
- // bom?.Components, x => x.Code, x => x.Description, x => x.Code);
- //
- // var glass = model.ExtractMappings<ProductIntegrationSource,IAwgGlass,Product,ProductLink, ProductIntegrationSource>(
- // bom?.Glass, x => x.Code, x => x.Description, x => x.Code);
- //
- // var labour = model.ExtractMappings<ActivityIntegrationSource,IAwgLabour,Activity,ActivityLink, ActivityIntegrationSource>(
- // bom?.Labour, x => x.Code, x => x.Description, x => x.Code);
- //
- // Task.WaitAll(styles, profiles, gaskets, components, glass, labour);
- // model.Styles = styles.Result;
- // model.Profiles = profiles.Result;
- // model.Gaskets = gaskets.Result;
- // model.Components = components.Result;
- // model.Glass = glass.Result;
- // model.Labour = labour.Result;
- // model.CheckChanged();
- // }
- //
- // public IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>? BOM
- // {
- // get => GetValue(BOMProperty) as IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>;
- // set => SetValue(BOMProperty, value);
- // }
- private static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
- nameof(Styles),
- typeof(IEnumerable<IAwgStyle>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(StylesChanged)
- );
- private static void StylesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgStyle> styles)
- return;
- var mappings =
- model.ExtractMappings<ProductStyleIntegrationSource, IAwgStyle, ProductStyle, ProductStyleLink>(
- styles,
- (logikal, mapping) =>
- {
- mapping.Cost = logikal.Cost;
- mapping.StyleType = logikal.StyleType;
- },
- x => x.Code
- );
- mappings.Wait();
- model.StyleMappings = mappings.Result;
- model.CheckChanged();
- }
- public IEnumerable<IAwgStyle>? Styles
- {
- get => GetValue(StylesProperty) as IEnumerable<IAwgStyle>;
- set => SetValue(StylesProperty, value);
- }
- private static readonly DependencyProperty GroupsProperty = DependencyProperty.Register(
- nameof(Groups),
- typeof(IEnumerable<IAwgGroup>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(GroupsChanged)
- );
- private static void GroupsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGroup> groups)
- return;
- var mappings =
- model.ExtractMappings<ProductGroupIntegrationSource, IAwgGroup, ProductGroup, ProductGroupLink>(
- groups,
- (logikal, mapping) =>
- {
- mapping.Parent = logikal.Parent;
- },
- x => x.Code
- );
- mappings.Wait();
- model.GroupMappings = mappings.Result;
- model.CheckChanged();
- }
- public IEnumerable<IAwgGroup>? Groups
- {
- get => GetValue(GroupsProperty) as IEnumerable<IAwgGroup>;
- set => SetValue(GroupsProperty, value);
- }
- private static readonly DependencyProperty SuppliersProperty = DependencyProperty.Register(
- nameof(Suppliers),
- typeof(IEnumerable<IAwgSupplier>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(SuppliersChanged)
- );
- private static void SuppliersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgSupplier> suppliers)
- return;
- var mappings =
- model.ExtractMappings<SupplierIntegrationSource, IAwgSupplier, Supplier, SupplierLink>(
- suppliers,
- null,
- x => x.Code
- );
- mappings.Wait();
- model.SupplierMappings = mappings.Result;
- model.CheckChanged();
- }
- public IEnumerable<IAwgSupplier>? Suppliers
- {
- get => GetValue(SuppliersProperty) as IEnumerable<IAwgSupplier>;
- set => SetValue(SuppliersProperty, value);
- }
- private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register(
- nameof(Profiles),
- typeof(IEnumerable<IAwgProfile>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(ProfilesChanged)
- );
- private static void ProfilesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgProfile> profiles)
- return;
- var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgProfile, Product, ProductLink>(
- 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<IAwgProfile>? Profiles
- {
- get => GetValue(ProfilesProperty) as IEnumerable<IAwgProfile>;
- set => SetValue(ProfilesProperty, value);
- }
- private static readonly DependencyProperty GasketsProperty = DependencyProperty.Register(
- nameof(Gaskets),
- typeof(IEnumerable<IAwgGasket>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(GasketsChanged)
- );
- private static void GasketsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGasket> gaskets)
- return;
- var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgGasket, Product, ProductLink>(
- 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<IAwgGasket>? Gaskets
- {
- get => GetValue(GasketsProperty) as IEnumerable<IAwgGasket>;
- set => SetValue(GasketsProperty, value);
- }
- private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register(
- nameof(Components),
- typeof(IEnumerable<IAwgComponent>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(ComponentsChanged)
- );
- private static void ComponentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgComponent> components)
- return;
- var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgComponent, Product, ProductLink>(
- 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<IAwgComponent>? Components
- {
- get => GetValue(ComponentsProperty) as IEnumerable<IAwgComponent>;
- set => SetValue(ComponentsProperty, value);
- }
- private static readonly DependencyProperty GlassProperty = DependencyProperty.Register(
- nameof(Glass),
- typeof(IEnumerable<IAwgGlass>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(GlassChanged)
- );
- private static void GlassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGlass> glass)
- return;
- var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgGlass, Product, ProductLink>(
- 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<IAwgGlass>? Glass
- {
- get => GetValue(GlassProperty) as IEnumerable<IAwgGlass>;
- set => SetValue(GlassProperty, value);
- }
- private static readonly DependencyProperty LabourProperty = DependencyProperty.Register(
- nameof(Labour),
- typeof(IEnumerable<IAwgLabour>),
- typeof(AWGMappingWindowViewModel),
- new FrameworkPropertyMetadata(LabourChanged)
- );
- private static void LabourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgLabour> labour)
- return;
- var mappings = model.ExtractMappings<ActivityIntegrationSource, IAwgLabour, Activity, ActivityLink>(
- labour,
- (logikal, mapping) =>
- {
- mapping.Quantity = logikal.Quantity;
- },
- x => x.Code
- );
- mappings.Wait();
- model.LabourMappings = mappings.Result;
- model.CheckChanged();
- }
- public IEnumerable<IAwgLabour>? Labour
- {
- get => GetValue(LabourProperty) as IEnumerable<IAwgLabour>;
- 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<ProductStyleIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductStyleIntegrationSource>? StyleMappings
- {
- get => GetValue(StyleMappingsProperty) as List<ProductStyleIntegrationSource>;
- 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<ProductGroupIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductGroupIntegrationSource>? GroupMappings
- {
- get => GetValue(GroupMappingsProperty) as List<ProductGroupIntegrationSource>;
- 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<SupplierIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<SupplierIntegrationSource>? SupplierMappings
- {
- get => GetValue(SupplierMappingsProperty) as List<SupplierIntegrationSource>;
- set => SetValue(SupplierMappingsProperty, 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 ProfileMappingsProperty = DependencyProperty.Register(
- nameof(ProfileMappings),
- typeof(List<ProductIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductIntegrationSource>? ProfileMappings
- {
- get => GetValue(ProfileMappingsProperty) as List<ProductIntegrationSource>;
- 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<ProductIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductIntegrationSource>? GasketMappings
- {
- get => GetValue(GasketMappingsProperty) as List<ProductIntegrationSource>;
- 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<ProductIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductIntegrationSource>? ComponentMappings
- {
- get => GetValue(ComponentMappingsProperty) as List<ProductIntegrationSource>;
- 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<ProductIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ProductIntegrationSource>? GlassMappings
- {
- get => GetValue(GlassMappingsProperty) as List<ProductIntegrationSource>;
- 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<ActivityIntegrationSource>),
- typeof(AWGMappingWindowViewModel)
- );
- public List<ActivityIntegrationSource>? LabourMappings
- {
- get => GetValue(LabourMappingsProperty) as List<ActivityIntegrationSource>;
- 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<KeyValuePair<string, BitmapImage>>),
- typeof(AWGMappingWindowViewModel),
- new PropertyMetadata(new CoreObservableCollection<KeyValuePair<string, BitmapImage>>())
- );
- public CoreObservableCollection<KeyValuePair<string, BitmapImage>> Sections
- {
- get => (CoreObservableCollection<KeyValuePair<string, BitmapImage>>)GetValue(SectionsProperty);
- set => SetValue(SectionsProperty, value);
- }
- private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register(
- nameof(SelectedSection),
- typeof(KeyValuePair<string, BitmapImage>?),
- typeof(AWGMappingWindowViewModel)
- );
- public KeyValuePair<string, BitmapImage>? SelectedSection
- {
- get => (KeyValuePair<string, BitmapImage>?)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<List<TCode>> ExtractMappings<TCode, TType, TEntity, TLink>(
- IEnumerable<TType>? items,
- Action<TType, TCode>? populate,
- Expression<Func<TEntity, object?>> entitycode
- )
- where TType : IAwgItem
- where TCode : BaseIntegrationSource<TEntity, TLink>, IRemotable, IPersistent, new()
- where TEntity : Entity, IRemotable, IPersistent, new()
- where TLink : EntityLink<TEntity>
- {
- return Task.Run(() =>
- {
- var results = new List<TCode>();
- if (items == null)
- return results;
- //var sourceitems = new Dictionary<string, string>();
- //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<TEntity>(entitycode).InList(keys),
- Columns.Required<TEntity>().Add(x => x.ID).Add(entitycode)
- );
- var entitycodecol = $"Entity.{CoreUtils.GetFullPropertyName(entitycode, ".")}";
- query.Add(
- new Filter<TCode>(x => x.Code).InList(keys),
- Columns.Required<TCode>()
- .Add(x => x.ID)
- .Add(x => x.Code)
- .Add(x => x.Entity.ID)
- .Add(entitycodecol)
- );
- query.Query();
- var mappings = query.Get<TCode>().ToObjects<TCode>().ToArray();
- var entities = query.Get<TEntity>();
- 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<TEntity>());
- result.DirectMatch = true;
- }
- }
- results.Add(result);
- // result.PropertyChanged += (_, _) =>
- // {
- // // TMapping mapping = mappingtable.Rows.FirstOrDefault(r =>
- // // string.Equals(r.Get<TMapping, String>(c => c.Code), result.Code))?.ToObject<TMapping>();
- // // if (mapping == null)
- // // mapping = new TMapping() { Code = result.Code };
- // // mapping.Entity.ID = result.Entity.ID;
- // // new Client<TMapping>().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<string, BitmapImage>("Styles", Resources.palette.AsBitmapImage(64, 64)));
- if (GroupsChecked && GroupMappings?.Any() == true)
- Sections.Add(
- new KeyValuePair<string, BitmapImage>("Groups", Resources.productgroup.AsBitmapImage(64, 64)));
- if (SuppliersChecked && SupplierMappings?.Any() == true)
- Sections.Add(
- new KeyValuePair<string, BitmapImage>("Suppliers", Resources.supplier.AsBitmapImage(64, 64)));
- if (ProfilesChecked && ProfileMappings?.Any() == true)
- Sections.Add(
- new KeyValuePair<string, BitmapImage>("Profiles", Resources.profile.AsBitmapImage(64, 64)));
- if (GasketsChecked && GasketMappings?.Any() == true)
- Sections.Add(new KeyValuePair<string, BitmapImage>("Gaskets", Resources.gasket.AsBitmapImage(64, 64)));
- if (ComponentsChecked && ComponentMappings?.Any() == true)
- Sections.Add(
- new KeyValuePair<string, BitmapImage>("Components", Resources.fixings.AsBitmapImage(64, 64)));
- if (GlassChecked && GlassMappings?.Any() == true)
- Sections.Add(new KeyValuePair<string, BitmapImage>("Glass", Resources.glass.AsBitmapImage(64, 64)));
- if (LabourChecked && LabourMappings?.Any() == true)
- Sections.Add(new KeyValuePair<string, BitmapImage>("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 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 && profiles && gaskets && components && glass && labour;
- });
- }
- public ICommand CreateStyle => new ActionCommand(
- o =>
- {
- if (o is IntegrationGridCreateEntityArgs<ProductStyle, ProductStyleIntegrationSource> 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<Product>().Query(
- new Filter<Product>(x => x.Code).IsEqualTo(args.Entity.Code),
- Columns.Local<Product>()
- ).ToArray<Product>().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<ProductGroup, ProductGroupIntegrationSource> args)
- {
- Guid parentid = Guid.Empty;
- var parentcodes = args.Mapping.Parent.Split('/').Select(x => x.Trim().ToUpper()).ToArray();
- var parents = Client.Query(
- new Filter<ProductGroup>(x => x.Code).InList(parentcodes),
- Columns.None<ProductGroup>().Add(x => x.ID).Add(x => x.Code)
- ).ToArray<ProductGroup>();
- 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<Supplier, SupplierIntegrationSource> args)
- {
- args.Entity.Code = args.Mapping.Code ?? "";
- args.Entity.Name = args.Mapping.Description ?? "";
- args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
- }
- }
- );
- public ICommand CreateProfile => new ActionCommand(
- o =>
- {
- if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> args)
- {
- if (!string.IsNullOrWhiteSpace(_settings.ImageUrl))
- {
- var model = new LogikalCodeModel() { Code = args.Mapping.Code };
- var _expression = new CoreExpression<LogikalCodeModel, string>(_settings.ImageUrl);
- if (_expression.Evaluate(model).Get(out var eval, out var e))
- {
- var tcs = new TaskCompletionSource<byte[]>();
- 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<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> 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<Product, ProductIntegrationSource> 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<Activity, ActivityIntegrationSource> 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 GetParts(
- Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
- Action<ActivityLink, TimeSpan, double>? labourCallback)
- {
- GetParts(Profiles,Gaskets,Components,Glass,Labour,productCallback,labourCallback);
- }
-
- public void GetParts<TProfile, TGasket, TComponent, TGlass, TLabour>(
- IEnumerable<TProfile>? profiles,
- IEnumerable<TGasket>? gaskets,
- IEnumerable<TComponent>? components,
- IEnumerable<TGlass>? glasses,
- IEnumerable<TLabour>? labour,
- Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
- Action<ActivityLink, TimeSpan, double>? 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 gasketmapping = GasketMappings?.FirstOrDefault(x => x.Code == gasket.Code);
- if (gasketmapping != null && productCallback is not null)
- {
- var totalcost = gasket.Quantity * gasket.Cost;
- var length = PRSDimensionUtils.DimensionUtils.ConvertDimensions(
- gasketmapping.Dimensions,
- gasketmapping.Quantity,
- (f,c) => Client.Query(f,c)
- );
- productCallback(
- gasketmapping.Entity,
- null,
- new RawDimensions() { Length = gasketmapping.Dimensions.Length },
- length,
- totalcost);
- }
- }
- }
- if (ComponentsChecked && components != null)
- {
- foreach (var component in components)
- {
- var componentmapping = ComponentMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Code));
- if (componentmapping != null && productCallback is not null)
- {
- var totalcost = component.Cost * componentmapping.Quantity;
- var qty = PRSDimensionUtils.DimensionUtils.ConvertDimensions(
- componentmapping.Dimensions,
- componentmapping.Quantity,
- (f,c) => Client.Query(f,c)
- );
- productCallback(
- componentmapping.Entity,
- null,
- new RawDimensions() { Quantity = componentmapping.Dimensions.Quantity },
- qty,
- totalcost);
- }
- }
- }
- if (GlassChecked && glasses != null)
- {
- foreach (var glass in glasses)
- {
- var glassmapping = GlassMappings?.FirstOrDefault(x => string.Equals(x.Code, glass.Code));
- if (glassmapping != null && productCallback is not null)
- {
- productCallback(
- glassmapping.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 activitymapping = LabourMappings?.FirstOrDefault(x => string.Equals(x.Code, activity.Code));
- if (activitymapping != null && labourCallback is not null)
- {
- labourCallback(
- activitymapping.Entity,
- TimeSpan.FromHours(activity.Quantity),
- activity.Quantity * activity.Cost);
- }
- }
- }
- }
-
- }
|