AWGMappingWindowViewModel.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Input;
  8. using System.Windows.Media.Imaging;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.Integration.Awg;
  14. using InABox.Integration.Logikal;
  15. using InABox.WPF;
  16. using Microsoft.Xaml.Behaviors.Core;
  17. using PRSDesktop.Integrations.Logikal;
  18. namespace PRSDesktop.Integrations.Common;
  19. public class AWGMappingWindowViewModel : DependencyObject
  20. {
  21. private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register(
  22. nameof(SourceType),
  23. typeof(IntegrationSourceType),
  24. typeof(AWGMappingWindowViewModel)
  25. );
  26. public IntegrationSourceType SourceType
  27. {
  28. get => (IntegrationSourceType)GetValue(SourceTypeProperty);
  29. set => SetValue(SourceTypeProperty, value);
  30. }
  31. // public static DependencyProperty JobIDProperty = DependencyProperty.Register(
  32. // nameof(JobID),
  33. // typeof(Guid),
  34. // typeof(IntegrationBOMWindowViewModel)
  35. // );
  36. //
  37. // public Guid JobID
  38. // {
  39. // get => (Guid)GetValue(JobIDProperty);
  40. // set => SetValue(JobIDProperty, value);
  41. // }
  42. // public static DependencyProperty BOMProperty = DependencyProperty.Register(
  43. // nameof(BOM),
  44. // typeof(IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>),
  45. // typeof(IntegrationBOMWindowViewModel),
  46. // new FrameworkPropertyMetadata(BOMChanged));
  47. //
  48. // private static void BOMChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. // {
  50. //
  51. // if (d is not IntegrationBOMWindowViewModel model)
  52. // return;
  53. //
  54. // var bom =
  55. // e.NewValue as IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass,
  56. // IAwgLabour>;
  57. //
  58. // var styles = model.ExtractMappings<ProductStyleIntegrationSource, IAwgFinish, ProductStyle, ProductStyleLink, ProductStyleIntegrationSource>(
  59. // bom?.Finishes, x => x.Code, x => x.Description, x => x.Code);
  60. //
  61. // var profiles = model.ExtractMappings<ProductIntegrationSource,IAwgProfile,Product,ProductLink, ProductIntegrationSource>(
  62. // bom?.Profiles, x => x.Code, x => x.Description, x => x.Code);
  63. //
  64. // var gaskets = model.ExtractMappings<ProductIntegrationSource,IAwgGasket,Product,ProductLink, ProductIntegrationSource>(
  65. // bom?.Gaskets, x => x.Code, x => x.Description, x => x.Code);
  66. //
  67. // var components = model.ExtractMappings<ProductIntegrationSource,IAwgComponent,Product,ProductLink, ProductIntegrationSource>(
  68. // bom?.Components, x => x.Code, x => x.Description, x => x.Code);
  69. //
  70. // var glass = model.ExtractMappings<ProductIntegrationSource,IAwgGlass,Product,ProductLink, ProductIntegrationSource>(
  71. // bom?.Glass, x => x.Code, x => x.Description, x => x.Code);
  72. //
  73. // var labour = model.ExtractMappings<ActivityIntegrationSource,IAwgLabour,Activity,ActivityLink, ActivityIntegrationSource>(
  74. // bom?.Labour, x => x.Code, x => x.Description, x => x.Code);
  75. //
  76. // Task.WaitAll(styles, profiles, gaskets, components, glass, labour);
  77. // model.Styles = styles.Result;
  78. // model.Profiles = profiles.Result;
  79. // model.Gaskets = gaskets.Result;
  80. // model.Components = components.Result;
  81. // model.Glass = glass.Result;
  82. // model.Labour = labour.Result;
  83. // model.CheckChanged();
  84. // }
  85. //
  86. // public IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>? BOM
  87. // {
  88. // get => GetValue(BOMProperty) as IAwgBOM<IAwgFinish, IAwgProfile, IAwgGasket, IAwgComponent, IAwgGlass, IAwgLabour>;
  89. // set => SetValue(BOMProperty, value);
  90. // }
  91. private static readonly DependencyProperty FinishesProperty = DependencyProperty.Register(
  92. nameof(Finishes),
  93. typeof(IEnumerable<IAwgFinish>),
  94. typeof(AWGMappingWindowViewModel),
  95. new FrameworkPropertyMetadata(FinishesChanged)
  96. );
  97. private static void FinishesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  98. {
  99. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgFinish> finishes)
  100. return;
  101. var mappings =
  102. model.ExtractMappings<ProductStyleIntegrationSource, IAwgFinish, ProductStyle, ProductStyleLink>(
  103. finishes, x => x.Code, x => x.Description, x => x.Code);
  104. mappings.Wait();
  105. model.FinishMappings = mappings.Result;
  106. model.CheckChanged();
  107. }
  108. public IEnumerable<IAwgFinish>? Finishes
  109. {
  110. get => GetValue(FinishesProperty) as IEnumerable<IAwgFinish>;
  111. set => SetValue(FinishesProperty, value);
  112. }
  113. private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register(
  114. nameof(Profiles),
  115. typeof(IEnumerable<IAwgProfile>),
  116. typeof(AWGMappingWindowViewModel),
  117. new FrameworkPropertyMetadata(ProfilesChanged)
  118. );
  119. private static void ProfilesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  120. {
  121. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgProfile> profiles)
  122. return;
  123. var mappings = model.ExtractMappings<ProductIntegrationSource,IAwgProfile,Product,ProductLink>(
  124. profiles, x => x.Code, x => x.Description, x => x.Code);
  125. mappings.Wait();
  126. model.ProfileMappings = mappings.Result;
  127. model.CheckChanged();
  128. }
  129. public IEnumerable<IAwgProfile>? Profiles
  130. {
  131. get => GetValue(ProfilesProperty) as IEnumerable<IAwgProfile>;
  132. set => SetValue(ProfilesProperty, value);
  133. }
  134. private static readonly DependencyProperty GasketsProperty = DependencyProperty.Register(
  135. nameof(Gaskets),
  136. typeof(IEnumerable<IAwgGasket>),
  137. typeof(AWGMappingWindowViewModel),
  138. new FrameworkPropertyMetadata(GasketsChanged)
  139. );
  140. private static void GasketsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  141. {
  142. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGasket> gaskets)
  143. return;
  144. var mappings = model.ExtractMappings<ProductIntegrationSource,IAwgGasket,Product,ProductLink>(
  145. gaskets, x => x.Code, x => x.Description, x => x.Code);
  146. mappings.Wait();
  147. model.GasketMappings = mappings.Result;
  148. model.CheckChanged();
  149. }
  150. public IEnumerable<IAwgGasket>? Gaskets
  151. {
  152. get => GetValue(GasketsProperty) as IEnumerable<IAwgGasket>;
  153. set => SetValue(GasketsProperty, value);
  154. }
  155. private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register(
  156. nameof(Components),
  157. typeof(IEnumerable<IAwgComponent>),
  158. typeof(AWGMappingWindowViewModel),
  159. new FrameworkPropertyMetadata(ComponentsChanged)
  160. );
  161. private static void ComponentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  162. {
  163. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgComponent> components)
  164. return;
  165. var mappings = model.ExtractMappings<ProductIntegrationSource,IAwgComponent,Product,ProductLink>(
  166. components, x => x.Code, x => x.Description, x => x.Code);
  167. mappings.Wait();
  168. model.ComponentMappings = mappings.Result;
  169. model.CheckChanged();
  170. }
  171. public IEnumerable<IAwgComponent>? Components
  172. {
  173. get => GetValue(ComponentsProperty) as IEnumerable<IAwgComponent>;
  174. set => SetValue(ComponentsProperty, value);
  175. }
  176. private static readonly DependencyProperty GlassProperty = DependencyProperty.Register(
  177. nameof(Glass),
  178. typeof(IEnumerable<IAwgGlass>),
  179. typeof(AWGMappingWindowViewModel),
  180. new FrameworkPropertyMetadata(GlassChanged)
  181. );
  182. private static void GlassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  183. {
  184. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGlass> glass)
  185. return;
  186. var mappings = model.ExtractMappings<ProductIntegrationSource,IAwgGlass,Product,ProductLink>(
  187. glass, x => x.Code, x => x.Description, x => x.Code);
  188. mappings.Wait();
  189. model.GlassMappings = mappings.Result;
  190. model.CheckChanged();
  191. }
  192. public IEnumerable<IAwgGlass>? Glass
  193. {
  194. get => GetValue(GlassProperty) as IEnumerable<IAwgGlass>;
  195. set => SetValue(GlassProperty, value);
  196. }
  197. private static readonly DependencyProperty LabourProperty = DependencyProperty.Register(
  198. nameof(Labour),
  199. typeof(IEnumerable<IAwgLabour>),
  200. typeof(AWGMappingWindowViewModel),
  201. new FrameworkPropertyMetadata(LabourChanged)
  202. );
  203. private static void LabourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  204. {
  205. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgLabour> labour)
  206. return;
  207. var mappings = model.ExtractMappings<ActivityIntegrationSource,IAwgLabour,Activity,ActivityLink>(
  208. labour, x => x.Code, x => x.Description, x => x.Code);
  209. mappings.Wait();
  210. model.LabourMappings = mappings.Result;
  211. model.CheckChanged();
  212. }
  213. public IEnumerable<IAwgLabour>? Labour
  214. {
  215. get => GetValue(LabourProperty) as IEnumerable<IAwgLabour>;
  216. set => SetValue(LabourProperty, value);
  217. }
  218. private static readonly DependencyProperty FinishMappingsProperty = DependencyProperty.Register(
  219. nameof(FinishMappings),
  220. typeof(List<ProductStyleIntegrationSource>),
  221. typeof(AWGMappingWindowViewModel)
  222. );
  223. public List<ProductStyleIntegrationSource>? FinishMappings
  224. {
  225. get => GetValue(FinishMappingsProperty) as List<ProductStyleIntegrationSource>;
  226. set => SetValue(FinishMappingsProperty, value);
  227. }
  228. private static readonly DependencyProperty ProfileMappingsProperty = DependencyProperty.Register(
  229. nameof(ProfileMappings),
  230. typeof(List<ProductIntegrationSource>),
  231. typeof(AWGMappingWindowViewModel)
  232. );
  233. public List<ProductIntegrationSource>? ProfileMappings
  234. {
  235. get => GetValue(ProfileMappingsProperty) as List<ProductIntegrationSource>;
  236. set => SetValue(ProfileMappingsProperty, value);
  237. }
  238. private static readonly DependencyProperty GasketMappingsProperty = DependencyProperty.Register(
  239. nameof(GasketMappings),
  240. typeof(List<ProductIntegrationSource>),
  241. typeof(AWGMappingWindowViewModel)
  242. );
  243. public List<ProductIntegrationSource>? GasketMappings
  244. {
  245. get => GetValue(GasketMappingsProperty) as List<ProductIntegrationSource>;
  246. set => SetValue(GasketMappingsProperty, value);
  247. }
  248. private static readonly DependencyProperty ComponentMappingsProperty = DependencyProperty.Register(
  249. nameof(ComponentMappings),
  250. typeof(List<ProductIntegrationSource>),
  251. typeof(AWGMappingWindowViewModel)
  252. );
  253. public List<ProductIntegrationSource>? ComponentMappings
  254. {
  255. get => GetValue(ComponentMappingsProperty) as List<ProductIntegrationSource>;
  256. set => SetValue(ComponentMappingsProperty, value);
  257. }
  258. private static readonly DependencyProperty GlassMappingsProperty = DependencyProperty.Register(
  259. nameof(GlassMappings),
  260. typeof(List<ProductIntegrationSource>),
  261. typeof(AWGMappingWindowViewModel)
  262. );
  263. public List<ProductIntegrationSource>? GlassMappings
  264. {
  265. get => GetValue(GlassMappingsProperty) as List<ProductIntegrationSource>;
  266. set => SetValue(GlassMappingsProperty, value);
  267. }
  268. private static readonly DependencyProperty LabourMappingsProperty = DependencyProperty.Register(
  269. nameof(LabourMappings),
  270. typeof(List<ActivityIntegrationSource>),
  271. typeof(AWGMappingWindowViewModel)
  272. );
  273. public List<ActivityIntegrationSource>? LabourMappings
  274. {
  275. get => GetValue(LabourMappingsProperty) as List<ActivityIntegrationSource>;
  276. set => SetValue(LabourMappingsProperty, value);
  277. }
  278. private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register(
  279. nameof(Sections),
  280. typeof(Dictionary<string, BitmapImage>),
  281. typeof(AWGMappingWindowViewModel)
  282. );
  283. private Dictionary<string,BitmapImage> _sections = new()
  284. {
  285. { "Finishes", PRSDesktop.Resources.palette.AsBitmapImage(64, 64) },
  286. { "Profiles", PRSDesktop.Resources.profile.AsBitmapImage(64, 64) },
  287. { "Gaskets", PRSDesktop.Resources.gasket.AsBitmapImage(64, 64) },
  288. { "Components", PRSDesktop.Resources.fixings.AsBitmapImage(64, 64) },
  289. { "Glass", PRSDesktop.Resources.glass.AsBitmapImage(64, 64) },
  290. { "Labour", PRSDesktop.Resources.quality.AsBitmapImage(64, 64) },
  291. };
  292. public Dictionary<string, BitmapImage> Sections
  293. {
  294. get => (Dictionary<string, BitmapImage>)GetValue(SectionsProperty);
  295. set => SetValue(SectionsProperty, value);
  296. }
  297. private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register(
  298. nameof(SelectedSection),
  299. typeof(KeyValuePair<string, BitmapImage>),
  300. typeof(AWGMappingWindowViewModel)
  301. );
  302. public KeyValuePair<string, BitmapImage> SelectedSection
  303. {
  304. get => (KeyValuePair<string, BitmapImage>)GetValue(SelectedSectionProperty);
  305. set => SetValue(SelectedSectionProperty, value);
  306. }
  307. private static readonly DependencyProperty MappingsCompleteProperty = DependencyProperty.Register(
  308. nameof(MappingsComplete),
  309. typeof(bool),
  310. typeof(AWGMappingWindowViewModel)
  311. );
  312. public bool MappingsComplete
  313. {
  314. get => (bool)GetValue(MappingsCompleteProperty);
  315. set => SetValue(MappingsCompleteProperty, value);
  316. }
  317. private LogikalSettings _settings;
  318. public AWGMappingWindowViewModel()
  319. {
  320. Sections = _sections;
  321. SelectedSection = Sections.First();
  322. _settings = new GlobalConfiguration<LogikalSettings>().Load();
  323. }
  324. public Task<List<TCode>> ExtractMappings<TCode,TType,TEntity,TLink>(
  325. IEnumerable<TType>? items,
  326. Func<TType,string?> logikalcode,
  327. Func<TType,string?> logikaldescription,
  328. Expression<Func<TEntity,object?>> entitycode)
  329. where TType : IAwgBOMItem
  330. where TCode : BaseIntegrationSource<TEntity,TLink>, IRemotable, IPersistent, new()
  331. where TEntity : Entity, IRemotable,IPersistent, new()
  332. where TLink :EntityLink<TEntity>
  333. {
  334. return Task.Run(() =>
  335. {
  336. var f = entitycode.Compile();
  337. var results = new List<TCode>();
  338. if (items == null)
  339. return results;
  340. var sourceitems = new Dictionary<string, string>();
  341. foreach (var item in items)
  342. sourceitems[logikalcode(item) ?? ""] = logikaldescription(item) ?? "";
  343. MultiQuery query = new();
  344. query.Add<TEntity>(
  345. new Filter<TEntity>(entitycode).InList(sourceitems.Keys.ToArray()),
  346. Columns.Required<TEntity>().Add(x => x.ID).Add(entitycode)
  347. );
  348. var entitycodecol = $"Entity.{CoreUtils.GetFullPropertyName(entitycode, ".")}";
  349. query.Add<TCode>(
  350. new Filter<TCode>(x => x.Code).InList(sourceitems.Keys.ToArray()),
  351. Columns.Required<TCode>()
  352. .Add(x => x.ID)
  353. .Add(x => x.Code)
  354. .Add(x=>x.Entity.ID)
  355. .Add(entitycodecol)
  356. );
  357. query.Query();
  358. var mappings = query.Get<TCode>().ToObjects<TCode>().ToArray();
  359. var entities = query.Get<TEntity>();
  360. foreach (var sourceitem in sourceitems)
  361. {
  362. var result = new TCode()
  363. {
  364. Code = sourceitem.Key,
  365. Description = sourceitem.Value
  366. };
  367. var mapping = mappings.FirstOrDefault(x => string.Equals(x.Code, sourceitem.Key));
  368. if (mapping != null)
  369. result.Entity.CopyFrom(mapping.Entity);
  370. else
  371. {
  372. var entity = entities.Rows.FirstOrDefault(r => string.Equals(sourceitem.Key,r.Get<TEntity,object?>(entitycode)));
  373. if (entity != null)
  374. result.Entity.CopyFrom(entity.ToObject<TEntity>());
  375. }
  376. results.Add(result);
  377. result.PropertyChanged += (s, e) =>
  378. {
  379. // TMapping mapping = mappingtable.Rows.FirstOrDefault(r =>
  380. // string.Equals(r.Get<TMapping, String>(c => c.Code), result.Code))?.ToObject<TMapping>();
  381. // if (mapping == null)
  382. // mapping = new TMapping() { Code = result.Code };
  383. // mapping.Entity.ID = result.Entity.ID;
  384. // new Client<TMapping>().Save(mapping, "Created from BOM Integration Window");
  385. CheckChanged();
  386. };
  387. }
  388. return results;
  389. });
  390. }
  391. private void CheckChanged()
  392. {
  393. Dispatcher.BeginInvoke(() =>
  394. {
  395. var styles = FinishMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false;
  396. var profiles = ProfileMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false;
  397. var glass = GlassMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false;
  398. var components = ComponentMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false;
  399. var labour = LabourMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false;
  400. MappingsComplete = styles && profiles && glass && components && labour;
  401. });
  402. }
  403. public ICommand CreateStyle => new ActionCommand(
  404. o =>
  405. {
  406. if (o is IntegrationGridCreateEntityArgs<ProductStyle, ProductStyleIntegrationSource> args)
  407. {
  408. args.Entity.Code = args.Mapping.Code ?? "";
  409. args.Entity.Description = args.Mapping.Description ?? "";
  410. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  411. }
  412. }
  413. );
  414. public ICommand CreateProfile => new ActionCommand(
  415. o =>
  416. {
  417. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  418. {
  419. args.Entity.Code = args.Mapping.Code ?? "";
  420. args.Entity.Name = args.Mapping.Description ?? "";
  421. args.Entity.UnitOfMeasure.CopyFrom(_settings.ProfileUom);
  422. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  423. }
  424. }
  425. );
  426. public ICommand CreateGasket => new ActionCommand(
  427. o =>
  428. {
  429. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  430. {
  431. args.Entity.Code = args.Mapping.Code ?? "";
  432. args.Entity.Name = args.Mapping.Description ?? "";
  433. args.Entity.UnitOfMeasure.CopyFrom(_settings.GasketUom);
  434. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  435. }
  436. }
  437. );
  438. public ICommand CreateComponent => new ActionCommand(
  439. o =>
  440. {
  441. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  442. {
  443. args.Entity.Code = args.Mapping.Code ?? "";
  444. args.Entity.Name = args.Mapping.Description ?? "";
  445. args.Entity.UnitOfMeasure.CopyFrom(_settings.ComponentUom);
  446. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  447. }
  448. }
  449. );
  450. public ICommand CreateGlass => new ActionCommand(
  451. o =>
  452. {
  453. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  454. {
  455. args.Entity.Code = args.Mapping.Code ?? "";
  456. args.Entity.Name = args.Mapping.Description ?? "";
  457. args.Entity.UnitOfMeasure.CopyFrom(_settings.GlassUom);
  458. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  459. }
  460. }
  461. );
  462. public ICommand CreateActivity => new ActionCommand(
  463. o =>
  464. {
  465. if (o is IntegrationGridCreateEntityArgs<Activity, ActivityIntegrationSource> args)
  466. {
  467. args.Entity.Code = args.Mapping.Code ?? "";
  468. args.Entity.Description = args.Mapping.Description ?? "";
  469. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  470. }
  471. }
  472. );
  473. private class RawDimensions : IBaseDimensions
  474. {
  475. public double Quantity { get; set; }
  476. public double Length { get; set; }
  477. public double Width { get; set; }
  478. public double Height { get; set; }
  479. public double Weight { get; set; }
  480. }
  481. public void GetParts(
  482. Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
  483. Action<ActivityLink, TimeSpan, double>? labourCallback)
  484. {
  485. GetParts(Profiles,Gaskets,Components,Glass,Labour,productCallback,labourCallback);
  486. }
  487. public void GetParts<TProfile, TGasket, TComponent, TGlass, TLabour>(
  488. IEnumerable<TProfile>? profiles,
  489. IEnumerable<TGasket>? gaskets,
  490. IEnumerable<TComponent>? components,
  491. IEnumerable<TGlass>? glasses,
  492. IEnumerable<TLabour>? labour,
  493. Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
  494. Action<ActivityLink, TimeSpan, double>? labourCallback)
  495. where TProfile : IAwgProfile
  496. where TGasket : IAwgGasket
  497. where TComponent : IAwgComponent
  498. where TGlass : IAwgGlass
  499. where TLabour : IAwgLabour
  500. {
  501. if (profiles != null)
  502. {
  503. foreach (var profile in profiles)
  504. {
  505. var profilemapping = ProfileMappings?.FirstOrDefault(x => x.Code == profile.Code);
  506. var stylemapping = FinishMappings?.FirstOrDefault(x => string.Equals(x.Code, profile.Finish));
  507. if (profilemapping != null && productCallback is not null)
  508. {
  509. productCallback(
  510. profilemapping.Entity,
  511. stylemapping?.Entity,
  512. new RawDimensions() { Length = profile.Length },
  513. profile.Quantity,
  514. profile.Cost);
  515. }
  516. }
  517. }
  518. if (gaskets != null)
  519. {
  520. foreach (var gasket in gaskets)
  521. {
  522. var gasketmapping = GasketMappings?.FirstOrDefault(x => x.Code == gasket.Code);
  523. if (gasketmapping != null && productCallback is not null)
  524. {
  525. productCallback(
  526. gasketmapping.Entity,
  527. null,
  528. new RawDimensions() { Length = gasket.Length },
  529. gasket.Quantity,
  530. gasket.Cost);
  531. }
  532. }
  533. }
  534. if (components != null)
  535. {
  536. foreach (var component in components)
  537. {
  538. var componentmapping = ComponentMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Code));
  539. if (componentmapping != null && productCallback is not null)
  540. {
  541. productCallback(
  542. componentmapping.Entity,
  543. null,
  544. new RawDimensions() { Quantity = component.PackSize },
  545. component.Quantity,
  546. component.Cost);
  547. }
  548. }
  549. }
  550. if (glasses != null)
  551. {
  552. foreach (var glass in glasses)
  553. {
  554. var glassmapping = GlassMappings?.FirstOrDefault(x => string.Equals(x.Code, glass.Code));
  555. if (glassmapping != null && productCallback is not null)
  556. {
  557. productCallback(
  558. glassmapping.Entity,
  559. null,
  560. new RawDimensions() { Height = glass.Height, Width = glass.Width },
  561. glass.Quantity,
  562. glass.Cost);
  563. }
  564. }
  565. }
  566. if (labour != null)
  567. {
  568. foreach (var activity in labour)
  569. {
  570. var activitymapping = LabourMappings?.FirstOrDefault(x => string.Equals(x.Code, activity.Code));
  571. if (activitymapping != null && labourCallback is not null)
  572. {
  573. labourCallback(
  574. activitymapping.Entity,
  575. TimeSpan.FromHours(activity.Quantity),
  576. activity.Cost);
  577. }
  578. }
  579. }
  580. }
  581. // public void CreateBOM()
  582. // {
  583. // if (Finishes == null || Profiles == null || Gaskets == null || Components == null || Glass == null || Labour == null)
  584. // return;
  585. //
  586. // List<JobBillOfMaterialsItem> items = new List<JobBillOfMaterialsItem>();
  587. //
  588. // foreach (var profile in Profiles)
  589. // {
  590. // var profilemapping = ProfileMappings?.FirstOrDefault(x => x.Code == profile.Code);
  591. // var stylemapping = FinishMappings?.FirstOrDefault(x => string.Equals(x.Code, profile.Finish));
  592. // if (profilemapping != null)
  593. // {
  594. // JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
  595. // newitem.Product.CopyFrom(profilemapping.Entity);
  596. // newitem.Dimensions.Unit.CopyFrom(profilemapping.Entity.UnitOfMeasure);
  597. // newitem.Dimensions.Length = profile.Length;
  598. // if (stylemapping != null)
  599. // newitem.Style.CopyFrom(stylemapping.Entity);
  600. // newitem.Quantity = profile.Quantity;
  601. // newitem.UnitCost = profile.Cost;
  602. // items.Add(newitem);
  603. // }
  604. // }
  605. //
  606. // foreach (var component in Components)
  607. // {
  608. // var componentmapping = ComponentMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Code));
  609. // if (componentmapping != null)
  610. // {
  611. // JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
  612. // newitem.Product.CopyFrom(componentmapping.Entity);
  613. // newitem.Dimensions.Unit.CopyFrom(componentmapping.Entity.UnitOfMeasure);
  614. // newitem.Dimensions.Quantity = component.PackSize;
  615. // newitem.Quantity = component.Quantity;
  616. // newitem.UnitCost = component.Cost;
  617. // items.Add(newitem);
  618. // }
  619. // }
  620. //
  621. // foreach (var glass in Glass)
  622. // {
  623. // var glassmapping = GlassMappings?.FirstOrDefault(x => string.Equals(x.Code, glass.Code));
  624. // if (glassmapping != null)
  625. // {
  626. // JobBillOfMaterialsItem newitem = new JobBillOfMaterialsItem();
  627. // newitem.Product.CopyFrom(glassmapping.Entity);
  628. // newitem.Dimensions.Unit.CopyFrom(glassmapping.Entity.UnitOfMeasure);
  629. // newitem.Dimensions.Height = glass.Height;
  630. // newitem.Dimensions.Height = glass.Width;
  631. // newitem.Quantity = glass.Quantity;
  632. // newitem.UnitCost = glass.Cost;
  633. // items.Add(newitem);
  634. // }
  635. // }
  636. //
  637. // List<JobBillOfMaterialsActivity> activities = new List<JobBillOfMaterialsActivity>();
  638. //
  639. // foreach (var activity in Labour)
  640. // {
  641. // var activitymapping = LabourMappings?.FirstOrDefault(x => string.Equals(x.Code, activity.Code));
  642. // if (activitymapping != null)
  643. // {
  644. // JobBillOfMaterialsActivity newactivity = new JobBillOfMaterialsActivity();
  645. // newactivity.ActivityLink.CopyFrom(activitymapping.Entity);
  646. // newactivity.Duration = TimeSpan.FromHours(activity.Quantity);
  647. // newactivity.HourlyCost = activity.Cost;
  648. // activities.Add(newactivity);
  649. // }
  650. // }
  651. //
  652. // var _jobbom = new JobBillOfMaterials();
  653. // _jobbom.Job.ID = JobID;
  654. // _jobbom.Description = $"BOM Imported {DateTime.Now}";
  655. //
  656. // Progress.ShowModal("Creating BOM...", progress =>
  657. // {
  658. // Client.Save(_jobbom, "Imported From Logikal");
  659. //
  660. // progress.Report("Updating Items");
  661. // foreach (var item in items)
  662. // {
  663. // item.BillOfMaterials.ID = _jobbom.ID;
  664. // item.Job.ID = _jobbom.Job.ID;
  665. // }
  666. //
  667. // Client.Save(items, "Imported From Logikal");
  668. //
  669. // progress.Report("Updating Labour");
  670. // foreach (var activity in activities)
  671. // {
  672. // activity.BillOfMaterials.ID = _jobbom.ID;
  673. // activity.JobLink.ID = _jobbom.Job.ID;
  674. // }
  675. //
  676. // Client.Save(activities, "Imported From Logikal");
  677. //
  678. //
  679. // });
  680. // }
  681. }