| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- 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.Logikal;
- using InABox.WPF;
- using Microsoft.Xaml.Behaviors.Core;
- using PRSDesktop.Integrations.Logikal;
- namespace PRSDesktop.Integrations.Common;
- public class IntegrationBOMWindowViewModel : DependencyObject
- {
-
- 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(ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>),
- 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 ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass,
- LogikalLabour>;
-
- var styles = model.ExtractMappings<ProductStyleIntegrationSource, LogikalFinish, ProductStyle, ProductStyleLink, ProductStyleIntegrationSource>(
- bom?.Finishes, x => x.Code, x => x.Description, x => x.Code);
-
- var profiles = model.ExtractMappings<ProductIntegrationSource,LogikalProfile,Product,ProductLink, ProductIntegrationSource>(
- bom?.Profiles, x => x.Code, x => x.Description, x => x.Code);
-
- var components = model.ExtractMappings<ProductIntegrationSource,LogikalComponent,Product,ProductLink, ProductIntegrationSource>(
- bom?.Components, x => x.Code, x => x.Description, x => x.Code);
-
- var glass = model.ExtractMappings<ProductIntegrationSource,LogikalGlass,Product,ProductLink, ProductIntegrationSource>(
- bom?.Glass, x => x.Code, x => x.Description, x => x.Code);
-
- var labour = model.ExtractMappings<ActivityIntegrationSource,LogikalLabour,Activity,ActivityLink, ActivityIntegrationSource>(
- bom?.Labour, x => x.Code, x => x.Description, x => x.Code);
- Task.WaitAll(styles, profiles, components, glass, labour);
- model.Styles = styles.Result;
- model.Profiles = profiles.Result;
- model.Components = components.Result;
- model.Glass = glass.Result;
- model.Labour = labour.Result;
- model.CheckChanged();
- }
-
- public ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>? BOM
- {
- get => GetValue(BOMProperty) as ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour>;
- set => SetValue(BOMProperty, value);
- }
-
-
- private static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
- nameof(Styles),
- typeof(List<ProductStyleIntegrationSource>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductStyleIntegrationSource>? Styles
- {
- get => GetValue(StylesProperty) as List<ProductStyleIntegrationSource>;
- set => SetValue(StylesProperty, value);
- }
-
-
- private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register(
- nameof(Profiles),
- typeof(List<ProductIntegrationSource>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationSource>? Profiles
- {
- get => GetValue(ProfilesProperty) as List<ProductIntegrationSource>;
- set => SetValue(ProfilesProperty, value);
- }
-
-
- private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register(
- nameof(Components),
- typeof(List<ProductIntegrationSource>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationSource>? Components
- {
- get => GetValue(ComponentsProperty) as List<ProductIntegrationSource>;
- set => SetValue(ComponentsProperty, value);
- }
-
-
- private static readonly DependencyProperty GlassProperty = DependencyProperty.Register(
- nameof(Glass),
- typeof(List<ProductIntegrationSource>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ProductIntegrationSource>? Glass
- {
- get => GetValue(GlassProperty) as List<ProductIntegrationSource>;
- set => SetValue(GlassProperty, value);
- }
-
-
- private static readonly DependencyProperty LabourProperty = DependencyProperty.Register(
- nameof(Labour),
- typeof(List<ActivityIntegrationSource>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- public List<ActivityIntegrationSource>? Labour
- {
- get => GetValue(LabourProperty) as List<ActivityIntegrationSource>;
- set => SetValue(LabourProperty, value);
- }
-
- private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register(
- nameof(Sections),
- typeof(Dictionary<string, BitmapImage>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- private Dictionary<string,BitmapImage> _sections = new()
- {
- { nameof(Styles), PRSDesktop.Resources.palette.AsBitmapImage(64, 64) },
- { nameof(Profiles), PRSDesktop.Resources.profile.AsBitmapImage(64, 64) },
- { nameof(Components), PRSDesktop.Resources.fixings.AsBitmapImage(64, 64) },
- { nameof(Glass), PRSDesktop.Resources.glass.AsBitmapImage(64, 64) },
- { nameof(Labour), PRSDesktop.Resources.quality.AsBitmapImage(64, 64) },
- };
- public Dictionary<string, BitmapImage> Sections
- {
- get => (Dictionary<string, BitmapImage>)GetValue(SectionsProperty);
- set => SetValue(SectionsProperty, value);
- }
-
- private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register(
- nameof(SelectedSection),
- typeof(KeyValuePair<string, BitmapImage>),
- typeof(IntegrationBOMWindowViewModel)
- );
-
- 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(IntegrationBOMWindowViewModel)
- );
- public bool MappingsComplete
- {
- get => (bool)GetValue(MappingsCompleteProperty);
- set => SetValue(MappingsCompleteProperty, value);
- }
- private LogikalSettings _settings;
- private ProductDimensionUnit _profileUom;
- private ProductDimensionUnit _componentUom;
- private ProductDimensionUnit _glassUom;
-
- public IntegrationBOMWindowViewModel()
- {
- Sections = _sections;
- SelectedSection = Sections.First();
- _settings = new GlobalConfiguration<LogikalSettings>().Load();
- var uoms = new Client<ProductDimensionUnit>().Query(
- new Filter<ProductDimensionUnit>(x => x.Code).IsEqualTo(_settings.ProfileUom)
- .Or(x => x.Code).IsEqualTo(_settings.ComponentUom)
- .Or(x => x.Code).IsEqualTo(_settings.GlassUom)
- ).ToObjects<ProductDimensionUnit>().ToArray();
- _profileUom = uoms.FirstOrDefault(x=>x.Code == _settings.ProfileUom) ?? new ProductDimensionUnit();
- _componentUom = uoms.FirstOrDefault(x=>x.Code == _settings.ComponentUom) ?? new ProductDimensionUnit();
- _glassUom = uoms.FirstOrDefault(x=>x.Code == _settings.GlassUom) ?? new ProductDimensionUnit();
- }
-
- public Task<List<TCode>> ExtractMappings<TCode,TLogikal,TEntity,TLink,TMapping>(
- IEnumerable<TLogikal>? items,
- Func<TLogikal,string?> logikalcode,
- Func<TLogikal,string?> logikaldescription,
- Expression<Func<TEntity,object?>> entitycode)
- where TCode : BaseIntegrationSource<TEntity,TLink>, new()
- where TEntity : Entity, IRemotable,IPersistent, new()
- where TLink :EntityLink<TEntity>
- where TMapping : BaseIntegrationSource<TEntity,TLink>, IRemotable, IPersistent, new()
- {
- return Task.Run(() =>
- {
- var f = entitycode.Compile();
- var results = new List<TCode>();
- if (items == null)
- return results;
- var sourceitems = new Dictionary<string, string>();
- foreach (var item in items)
- sourceitems[logikalcode(item) ?? ""] = logikaldescription(item) ?? "";
- MultiQuery query = new();
- query.Add<TEntity>(
- new Filter<TEntity>(entitycode).InList(sourceitems.Keys.ToArray()),
- Columns.Required<TEntity>().Add(x => x.ID).Add(entitycode)
- );
- var entitycodecol = $"Entity.{CoreUtils.GetFullPropertyName(entitycode, ".")}";
- query.Add<TMapping>(
- new Filter<TMapping>(x => x.Code).InList(sourceitems.Keys.ToArray()),
- Columns.Required<TMapping>()
- .Add(x => x.ID)
- .Add(x => x.Code)
- .Add(x=>x.Entity.ID)
- .Add(entitycodecol)
- );
- query.Query();
- var mappings = query.Get<TMapping>().ToObjects<TMapping>().ToArray();
- var entities = query.Get<TEntity>().ToDictionary(entitycode, x => x.ID);
- foreach (var sourceitem in sourceitems)
- {
- var result = new TCode()
- {
- Code = sourceitem.Key,
- Description = sourceitem.Value
- };
- var mapping = mappings.FirstOrDefault(x => string.Equals(x.Code, sourceitem.Key));
- if (mapping != null)
- {
- result.Entity.ID = mapping.Entity.ID;
- CoreUtils.SetPropertyValue(result, entitycodecol,
- CoreUtils.GetPropertyValue(mapping, entitycodecol));
- }
- else if (entities.ContainsKey(sourceitem.Key))
- {
- result.Entity.ID = entities[sourceitem.Key];
- CoreUtils.SetPropertyValue(result.Entity, CoreUtils.GetFullPropertyName(entitycode, "."),
- sourceitem.Key);
- }
- results.Add(result);
- result.PropertyChanged += (s, e) =>
- {
- // 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;
- });
- }
- private void CheckChanged()
- {
- Dispatcher.BeginInvoke(() =>
- {
- var styles = Styles?.All(x => x.Entity.ID != Guid.Empty) ?? false;
- var profiles = Profiles?.All(x => x.Entity.ID != Guid.Empty) ?? false;
- var glass = Glass?.All(x => x.Entity.ID != Guid.Empty) ?? false;
- var components = Components?.All(x => x.Entity.ID != Guid.Empty) ?? false;
- var labour = Labour?.All(x => x.Entity.ID != Guid.Empty) ?? false;
- MappingsComplete = styles && profiles && glass && components && 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"];
- }
- }
- );
- 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(_profileUom);
- 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(_componentUom);
- 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(_glassUom);
- 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"];
- }
- }
- );
- public void CreateBOM()
- {
- if (BOM == null)
- return;
-
- List<JobBillOfMaterialsItem> items = new List<JobBillOfMaterialsItem>();
- foreach (var profile in BOM.Profiles)
- {
- var profilemapping = Profiles?.FirstOrDefault(x => x.Code == profile.Code);
- var stylemapping = Styles?.FirstOrDefault(x => string.Equals(x.Code, profile.Finish));
- if (profilemapping != null)
- {
- JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
- newitem.Product.CopyFrom(profilemapping.Entity);
- newitem.Dimensions.Unit.CopyFrom(profilemapping.Entity.UnitOfMeasure);
- newitem.Dimensions.Length = profile.Length;
- if (stylemapping != null)
- newitem.Style.CopyFrom(stylemapping.Entity);
- newitem.Quantity = profile.Quantity;
- newitem.UnitCost = profile.Cost;
- items.Add(newitem);
- }
- }
- foreach (var component in BOM.Components)
- {
- var componentmapping = Components?.FirstOrDefault(x => string.Equals(x.Code, component.Code));
- if (componentmapping != null)
- {
- JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
- newitem.Product.CopyFrom(componentmapping.Entity);
- newitem.Dimensions.Unit.CopyFrom(componentmapping.Entity.UnitOfMeasure);
- newitem.Dimensions.Quantity = component.PackSize;
- newitem.Quantity = component.Quantity;
- newitem.UnitCost = component.Cost;
- items.Add(newitem);
- }
- }
- foreach (var glass in BOM.Glass)
- {
- var glassmapping = Glass?.FirstOrDefault(x => string.Equals(x.Code, glass.Code));
- if (glassmapping != null)
- {
- JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
- newitem.Product.CopyFrom(glassmapping.Entity);
- newitem.Dimensions.Unit.CopyFrom(glassmapping.Entity.UnitOfMeasure);
- newitem.Dimensions.Height = glass.Height;
- newitem.Dimensions.Height = glass.Width;
- newitem.Quantity = glass.Quantity;
- newitem.UnitCost = glass.Cost;
- items.Add(newitem);
- }
- }
-
- List<JobBillOfMaterialsActivity> activities = new List<JobBillOfMaterialsActivity>();
-
- foreach (var activity in BOM.Labour)
- {
- var activitymapping = Labour?.FirstOrDefault(x => string.Equals(x.Code, activity.Code));
- if (activitymapping != null)
- {
- JobBillOfMaterialsActivity newactivity = new JobBillOfMaterialsActivity();
- newactivity.ActivityLink.CopyFrom(activitymapping.Entity);
- newactivity.Duration = TimeSpan.FromHours(activity.Quantity);
- newactivity.HourlyCost = activity.Cost;
- activities.Add(newactivity);
- }
- }
-
- var _jobbom = new JobBillOfMaterials();
- _jobbom.Job.ID = JobID;
- _jobbom.Description = $"BOM Imported {DateTime.Now}";
-
- Progress.ShowModal("Creating BOM...", progress =>
- {
- Client.Save(_jobbom, "Imported From Logikal");
-
- progress.Report("Updating Items");
- foreach (var item in items)
- {
- item.BillOfMaterials.ID = _jobbom.ID;
- item.Job.ID = _jobbom.Job.ID;
- }
- Client.Save(items, "Imported From Logikal");
-
- progress.Report("Updating Labour");
- foreach (var activity in activities)
- {
- activity.BillOfMaterials.ID = _jobbom.ID;
- activity.JobLink.ID = _jobbom.Job.ID;
- }
- Client.Save(activities, "Imported From Logikal");
-
- });
- }
- }
|