AWGMappingWindowViewModel.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Input;
  9. using System.Windows.Media.Imaging;
  10. using Comal.Classes;
  11. using InABox.Clients;
  12. using InABox.Configuration;
  13. using InABox.Core;
  14. using InABox.Integration;
  15. using InABox.Integration.Awg;
  16. using InABox.WPF;
  17. using Inflector;
  18. using Microsoft.CodeAnalysis.CSharp.Syntax;
  19. using Microsoft.Xaml.Behaviors.Core;
  20. using PRSDimensionUtils;
  21. using sun.text.resources.ro;
  22. namespace PRSDesktop.Integrations.Common;
  23. public class AWGMappingWindowViewModel : DependencyObject
  24. {
  25. private readonly LogikalSettings _settings;
  26. public LogikalSettings Settings => _settings;
  27. public AWGMappingWindowViewModel() : base()
  28. {
  29. _settings = new GlobalConfiguration<LogikalSettings>().Load();
  30. StylesChecked = _settings.UpdateStyles != LogikalUpdateType.None;
  31. GroupsChecked = _settings.UpdateGroups != LogikalUpdateType.None;
  32. SuppliersChecked = _settings.UpdateSuppliers != LogikalUpdateType.None;
  33. DiscountsChecked = _settings.UpdateDiscounts != LogikalUpdateType.None;
  34. ProfilesChecked = _settings.UpdateProfiles != LogikalUpdateType.None;
  35. GasketsChecked = _settings.UpdateGaskets != LogikalUpdateType.None;
  36. ComponentsChecked = _settings.UpdateComponents != LogikalUpdateType.None;
  37. GlassChecked = _settings.UpdateGlass != LogikalUpdateType.None;
  38. LabourChecked = _settings.UpdateLabour != LogikalUpdateType.None;
  39. }
  40. private static readonly DependencyProperty SourceTypeProperty = DependencyProperty.Register(
  41. nameof(SourceType),
  42. typeof(IntegrationSourceType),
  43. typeof(AWGMappingWindowViewModel)
  44. );
  45. public IntegrationSourceType SourceType
  46. {
  47. get => (IntegrationSourceType)GetValue(SourceTypeProperty);
  48. set => SetValue(SourceTypeProperty, value);
  49. }
  50. private static readonly DependencyProperty StylesProperty = DependencyProperty.Register(
  51. nameof(Styles),
  52. typeof(IEnumerable<IAwgStyle>),
  53. typeof(AWGMappingWindowViewModel),
  54. new FrameworkPropertyMetadata(StylesChanged)
  55. );
  56. private static void StylesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgStyle> styles)
  59. return;
  60. var mappings =
  61. model.ExtractMappings<ProductStyleIntegrationSource, IAwgStyle, ProductStyle, ProductStyleLink>(
  62. styles,
  63. (logikal, mapping) =>
  64. {
  65. mapping.Cost = logikal.Cost;
  66. mapping.StyleType = logikal.StyleType;
  67. },
  68. x => x.Code
  69. );
  70. mappings.Wait();
  71. model.StyleMappings = mappings.Result;
  72. model.CheckChanged();
  73. }
  74. public IEnumerable<IAwgStyle>? Styles
  75. {
  76. get => GetValue(StylesProperty) as IEnumerable<IAwgStyle>;
  77. set => SetValue(StylesProperty, value);
  78. }
  79. private static readonly DependencyProperty GroupsProperty = DependencyProperty.Register(
  80. nameof(Groups),
  81. typeof(IEnumerable<IAwgGroup>),
  82. typeof(AWGMappingWindowViewModel),
  83. new FrameworkPropertyMetadata(GroupsChanged)
  84. );
  85. private static void GroupsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  86. {
  87. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGroup> groups)
  88. return;
  89. var mappings =
  90. model.ExtractMappings<ProductGroupIntegrationSource, IAwgGroup, ProductGroup, ProductGroupLink>(
  91. groups,
  92. (logikal, mapping) =>
  93. {
  94. mapping.Parent = logikal.Parent;
  95. },
  96. x => x.Code
  97. );
  98. mappings.Wait();
  99. model.GroupMappings = mappings.Result;
  100. model.CheckChanged();
  101. }
  102. public IEnumerable<IAwgGroup>? Groups
  103. {
  104. get => GetValue(GroupsProperty) as IEnumerable<IAwgGroup>;
  105. set => SetValue(GroupsProperty, value);
  106. }
  107. private static readonly DependencyProperty SuppliersProperty = DependencyProperty.Register(
  108. nameof(Suppliers),
  109. typeof(IEnumerable<IAwgSupplier>),
  110. typeof(AWGMappingWindowViewModel),
  111. new FrameworkPropertyMetadata(SuppliersChanged)
  112. );
  113. private static void SuppliersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  114. {
  115. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgSupplier> suppliers)
  116. return;
  117. var mappings =
  118. model.ExtractMappings<SupplierIntegrationSource, IAwgSupplier, Supplier, SupplierLink>(
  119. suppliers,
  120. null,
  121. x => x.Code
  122. );
  123. mappings.Wait();
  124. model.SupplierMappings = mappings.Result;
  125. model.CheckChanged();
  126. }
  127. public IEnumerable<IAwgSupplier>? Suppliers
  128. {
  129. get => GetValue(SuppliersProperty) as IEnumerable<IAwgSupplier>;
  130. set => SetValue(SuppliersProperty, value);
  131. }
  132. private static readonly DependencyProperty DiscountsProperty = DependencyProperty.Register(
  133. nameof(Discounts),
  134. typeof(IEnumerable<IAwgDiscount>),
  135. typeof(AWGMappingWindowViewModel),
  136. new FrameworkPropertyMetadata(DiscountsChanged)
  137. );
  138. private static void DiscountsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  139. {
  140. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgDiscount> discounts)
  141. return;
  142. var mappings =
  143. model.ExtractMappings<SupplierDiscountGroupIntegrationSource, IAwgDiscount, SupplierDiscountGroup, SupplierDiscountGroupLink>(
  144. discounts,
  145. (logikal, mapping) =>
  146. {
  147. mapping.Supplier = logikal.SupplierCode;
  148. mapping.Discount = logikal.Discount;
  149. },
  150. x => x.Code
  151. );
  152. mappings.Wait();
  153. model.DiscountMappings = mappings.Result;
  154. model.CheckChanged();
  155. }
  156. public IEnumerable<IAwgDiscount>? Discounts
  157. {
  158. get => GetValue(DiscountsProperty) as IEnumerable<IAwgDiscount>;
  159. set => SetValue(DiscountsProperty, value);
  160. }
  161. private static readonly DependencyProperty ProfilesProperty = DependencyProperty.Register(
  162. nameof(Profiles),
  163. typeof(IEnumerable<IAwgProfile>),
  164. typeof(AWGMappingWindowViewModel),
  165. new FrameworkPropertyMetadata(ProfilesChanged)
  166. );
  167. private static void ProfilesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  168. {
  169. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgProfile> profiles)
  170. return;
  171. var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgProfile, Product, ProductLink>(
  172. profiles,
  173. (logikal, mapping) =>
  174. {
  175. mapping.Style = logikal.Finish;
  176. mapping.Dimensions.Unit.CopyFrom(model.Settings.ProfileUom);
  177. mapping.Dimensions.Length = logikal.Length;
  178. mapping.Group = logikal.Group;
  179. mapping.Supplier = logikal.Supplier;
  180. mapping.Cost = logikal.Cost;
  181. mapping.MillCost = logikal.MillCost;
  182. mapping.Quantity = logikal.Quantity;
  183. mapping.TreatmentParameters[AwgStyleType.Powdercoated] = logikal.PaintPerimeter;
  184. mapping.TreatmentParameters[AwgStyleType.Anodised] = logikal.AnodizePerimeter;
  185. },
  186. x => x.Code
  187. );
  188. mappings.Wait();
  189. model.ProfileMappings = mappings.Result;
  190. model.CheckChanged();
  191. }
  192. public IEnumerable<IAwgProfile>? Profiles
  193. {
  194. get => GetValue(ProfilesProperty) as IEnumerable<IAwgProfile>;
  195. set => SetValue(ProfilesProperty, value);
  196. }
  197. private static readonly DependencyProperty GasketsProperty = DependencyProperty.Register(
  198. nameof(Gaskets),
  199. typeof(IEnumerable<IAwgGasket>),
  200. typeof(AWGMappingWindowViewModel),
  201. new FrameworkPropertyMetadata(GasketsChanged)
  202. );
  203. private static void GasketsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  204. {
  205. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGasket> gaskets)
  206. return;
  207. var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgGasket, Product, ProductLink>(
  208. gaskets,
  209. (logikal, mapping) =>
  210. {
  211. mapping.Group = logikal.Group;
  212. mapping.Supplier = logikal.Supplier;
  213. mapping.Style = logikal.Finish;
  214. mapping.Dimensions.Unit.CopyFrom(model.Settings.GasketUom);
  215. mapping.Dimensions.Length = logikal.Length;
  216. mapping.Cost = logikal.Cost;
  217. mapping.Quantity = logikal.Quantity;
  218. },
  219. x => x.Code
  220. );
  221. mappings.Wait();
  222. model.GasketMappings = mappings.Result;
  223. model.CheckChanged();
  224. }
  225. public IEnumerable<IAwgGasket>? Gaskets
  226. {
  227. get => GetValue(GasketsProperty) as IEnumerable<IAwgGasket>;
  228. set => SetValue(GasketsProperty, value);
  229. }
  230. private static readonly DependencyProperty ComponentsProperty = DependencyProperty.Register(
  231. nameof(Components),
  232. typeof(IEnumerable<IAwgComponent>),
  233. typeof(AWGMappingWindowViewModel),
  234. new FrameworkPropertyMetadata(ComponentsChanged)
  235. );
  236. private static void ComponentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  237. {
  238. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgComponent> components)
  239. return;
  240. var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgComponent, Product, ProductLink>(
  241. components,
  242. (logikal, mapping) =>
  243. {
  244. mapping.Dimensions.Unit.CopyFrom(model.Settings.ComponentUom);
  245. mapping.Dimensions.Quantity = logikal.PackSize;
  246. mapping.Quantity = logikal.Quantity;
  247. mapping.Cost = logikal.Cost;
  248. mapping.Group = logikal.Group;
  249. mapping.Supplier = logikal.Supplier;
  250. mapping.Style = logikal.Finish;
  251. },
  252. x => x.Code
  253. );
  254. mappings.Wait();
  255. model.ComponentMappings = mappings.Result;
  256. model.CheckChanged();
  257. }
  258. public IEnumerable<IAwgComponent>? Components
  259. {
  260. get => GetValue(ComponentsProperty) as IEnumerable<IAwgComponent>;
  261. set => SetValue(ComponentsProperty, value);
  262. }
  263. private static readonly DependencyProperty GlassProperty = DependencyProperty.Register(
  264. nameof(Glass),
  265. typeof(IEnumerable<IAwgGlass>),
  266. typeof(AWGMappingWindowViewModel),
  267. new FrameworkPropertyMetadata(GlassChanged)
  268. );
  269. private static void GlassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  270. {
  271. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgGlass> glass)
  272. return;
  273. var mappings = model.ExtractMappings<ProductIntegrationSource, IAwgGlass, Product, ProductLink>(
  274. glass,
  275. (logikal, mapping) =>
  276. {
  277. mapping.Group = logikal.Group;
  278. mapping.Supplier = logikal.Supplier;
  279. mapping.Dimensions.Unit.CopyFrom(model.Settings.GlassUom);
  280. mapping.Dimensions.Height = logikal.Height;
  281. mapping.Dimensions.Width = logikal.Width;
  282. mapping.Style = logikal.Treatment;
  283. mapping.Quantity = logikal.Quantity;
  284. mapping.Cost = logikal.Cost;
  285. },
  286. x => x.Code
  287. );
  288. mappings.Wait();
  289. model.GlassMappings = mappings.Result;
  290. model.CheckChanged();
  291. }
  292. public IEnumerable<IAwgGlass>? Glass
  293. {
  294. get => GetValue(GlassProperty) as IEnumerable<IAwgGlass>;
  295. set => SetValue(GlassProperty, value);
  296. }
  297. private static readonly DependencyProperty LabourProperty = DependencyProperty.Register(
  298. nameof(Labour),
  299. typeof(IEnumerable<IAwgLabour>),
  300. typeof(AWGMappingWindowViewModel),
  301. new FrameworkPropertyMetadata(LabourChanged)
  302. );
  303. private static void LabourChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  304. {
  305. if (d is not AWGMappingWindowViewModel model || e.NewValue is not IEnumerable<IAwgLabour> labour)
  306. return;
  307. var mappings = model.ExtractMappings<ActivityIntegrationSource, IAwgLabour, Activity, ActivityLink>(
  308. labour,
  309. (logikal, mapping) =>
  310. {
  311. mapping.Quantity = logikal.Quantity;
  312. },
  313. x => x.Code
  314. );
  315. mappings.Wait();
  316. model.LabourMappings = mappings.Result;
  317. model.CheckChanged();
  318. }
  319. public IEnumerable<IAwgLabour>? Labour
  320. {
  321. get => GetValue(LabourProperty) as IEnumerable<IAwgLabour>;
  322. set => SetValue(LabourProperty, value);
  323. }
  324. private static void SectionCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  325. {
  326. if (d is AWGMappingWindowViewModel model)
  327. model.CheckChanged();
  328. }
  329. private static readonly DependencyProperty StyleMappingsProperty = DependencyProperty.Register(
  330. nameof(StyleMappings),
  331. typeof(List<ProductStyleIntegrationSource>),
  332. typeof(AWGMappingWindowViewModel)
  333. );
  334. public List<ProductStyleIntegrationSource>? StyleMappings
  335. {
  336. get => GetValue(StyleMappingsProperty) as List<ProductStyleIntegrationSource>;
  337. set => SetValue(StyleMappingsProperty, value);
  338. }
  339. private static readonly DependencyProperty StylesCheckedProperty = DependencyProperty.Register(
  340. nameof(StylesChecked),
  341. typeof(bool),
  342. typeof(AWGMappingWindowViewModel),
  343. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  344. );
  345. public bool StylesChecked
  346. {
  347. get => (bool)GetValue(StylesCheckedProperty);
  348. set => SetValue(StylesCheckedProperty, value);
  349. }
  350. private static readonly DependencyProperty GroupMappingsProperty = DependencyProperty.Register(
  351. nameof(GroupMappings),
  352. typeof(List<ProductGroupIntegrationSource>),
  353. typeof(AWGMappingWindowViewModel)
  354. );
  355. public List<ProductGroupIntegrationSource>? GroupMappings
  356. {
  357. get => GetValue(GroupMappingsProperty) as List<ProductGroupIntegrationSource>;
  358. set => SetValue(GroupMappingsProperty, value);
  359. }
  360. private static readonly DependencyProperty GroupsCheckedProperty = DependencyProperty.Register(
  361. nameof(GroupsChecked),
  362. typeof(bool),
  363. typeof(AWGMappingWindowViewModel),
  364. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  365. );
  366. public bool GroupsChecked
  367. {
  368. get => (bool)GetValue(GroupsCheckedProperty);
  369. set => SetValue(GroupsCheckedProperty, value);
  370. }
  371. private static readonly DependencyProperty SupplierMappingsProperty = DependencyProperty.Register(
  372. nameof(SupplierMappings),
  373. typeof(List<SupplierIntegrationSource>),
  374. typeof(AWGMappingWindowViewModel)
  375. );
  376. public List<SupplierIntegrationSource>? SupplierMappings
  377. {
  378. get => GetValue(SupplierMappingsProperty) as List<SupplierIntegrationSource>;
  379. set => SetValue(SupplierMappingsProperty, value);
  380. }
  381. private static readonly DependencyProperty DiscountMappingsProperty = DependencyProperty.Register(
  382. nameof(DiscountMappings),
  383. typeof(List<SupplierDiscountGroupIntegrationSource>),
  384. typeof(AWGMappingWindowViewModel)
  385. );
  386. public List<SupplierDiscountGroupIntegrationSource>? DiscountMappings
  387. {
  388. get => GetValue(DiscountMappingsProperty) as List<SupplierDiscountGroupIntegrationSource>;
  389. set => SetValue(DiscountMappingsProperty, value);
  390. }
  391. private static readonly DependencyProperty SuppliersCheckedProperty = DependencyProperty.Register(
  392. nameof(SuppliersChecked),
  393. typeof(bool),
  394. typeof(AWGMappingWindowViewModel),
  395. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  396. );
  397. public bool SuppliersChecked
  398. {
  399. get => (bool)GetValue(SuppliersCheckedProperty);
  400. set => SetValue(SuppliersCheckedProperty, value);
  401. }
  402. private static readonly DependencyProperty DiscountsCheckedProperty = DependencyProperty.Register(
  403. nameof(DiscountsChecked),
  404. typeof(bool),
  405. typeof(AWGMappingWindowViewModel),
  406. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  407. );
  408. public bool DiscountsChecked
  409. {
  410. get => (bool)GetValue(DiscountsCheckedProperty);
  411. set => SetValue(DiscountsCheckedProperty, value);
  412. }
  413. private static readonly DependencyProperty ProfileMappingsProperty = DependencyProperty.Register(
  414. nameof(ProfileMappings),
  415. typeof(List<ProductIntegrationSource>),
  416. typeof(AWGMappingWindowViewModel)
  417. );
  418. public List<ProductIntegrationSource>? ProfileMappings
  419. {
  420. get => GetValue(ProfileMappingsProperty) as List<ProductIntegrationSource>;
  421. set => SetValue(ProfileMappingsProperty, value);
  422. }
  423. private static readonly DependencyProperty ProfilesCheckedProperty = DependencyProperty.Register(
  424. nameof(ProfilesChecked),
  425. typeof(bool),
  426. typeof(AWGMappingWindowViewModel),
  427. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  428. );
  429. public bool ProfilesChecked
  430. {
  431. get => (bool)GetValue(ProfilesCheckedProperty);
  432. set => SetValue(ProfilesCheckedProperty, value);
  433. }
  434. private static readonly DependencyProperty GasketMappingsProperty = DependencyProperty.Register(
  435. nameof(GasketMappings),
  436. typeof(List<ProductIntegrationSource>),
  437. typeof(AWGMappingWindowViewModel)
  438. );
  439. public List<ProductIntegrationSource>? GasketMappings
  440. {
  441. get => GetValue(GasketMappingsProperty) as List<ProductIntegrationSource>;
  442. set => SetValue(GasketMappingsProperty, value);
  443. }
  444. private static readonly DependencyProperty GasketsCheckedProperty = DependencyProperty.Register(
  445. nameof(GasketsChecked),
  446. typeof(bool),
  447. typeof(AWGMappingWindowViewModel),
  448. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  449. );
  450. public bool GasketsChecked
  451. {
  452. get => (bool)GetValue(GasketsCheckedProperty);
  453. set => SetValue(GasketsCheckedProperty, value);
  454. }
  455. private static readonly DependencyProperty ComponentMappingsProperty = DependencyProperty.Register(
  456. nameof(ComponentMappings),
  457. typeof(List<ProductIntegrationSource>),
  458. typeof(AWGMappingWindowViewModel)
  459. );
  460. public List<ProductIntegrationSource>? ComponentMappings
  461. {
  462. get => GetValue(ComponentMappingsProperty) as List<ProductIntegrationSource>;
  463. set => SetValue(ComponentMappingsProperty, value);
  464. }
  465. private static readonly DependencyProperty ComponentsCheckedProperty = DependencyProperty.Register(
  466. nameof(ComponentsChecked),
  467. typeof(bool),
  468. typeof(AWGMappingWindowViewModel),
  469. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  470. );
  471. public bool ComponentsChecked
  472. {
  473. get => (bool)GetValue(ComponentsCheckedProperty);
  474. set => SetValue(ComponentsCheckedProperty, value);
  475. }
  476. private static readonly DependencyProperty GlassMappingsProperty = DependencyProperty.Register(
  477. nameof(GlassMappings),
  478. typeof(List<ProductIntegrationSource>),
  479. typeof(AWGMappingWindowViewModel)
  480. );
  481. public List<ProductIntegrationSource>? GlassMappings
  482. {
  483. get => GetValue(GlassMappingsProperty) as List<ProductIntegrationSource>;
  484. set => SetValue(GlassMappingsProperty, value);
  485. }
  486. private static readonly DependencyProperty GlassCheckedProperty = DependencyProperty.Register(
  487. nameof(GlassChecked),
  488. typeof(bool),
  489. typeof(AWGMappingWindowViewModel),
  490. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  491. );
  492. public bool GlassChecked
  493. {
  494. get => (bool)GetValue(GlassCheckedProperty);
  495. set => SetValue(GlassCheckedProperty, value);
  496. }
  497. private static readonly DependencyProperty LabourMappingsProperty = DependencyProperty.Register(
  498. nameof(LabourMappings),
  499. typeof(List<ActivityIntegrationSource>),
  500. typeof(AWGMappingWindowViewModel)
  501. );
  502. public List<ActivityIntegrationSource>? LabourMappings
  503. {
  504. get => GetValue(LabourMappingsProperty) as List<ActivityIntegrationSource>;
  505. set => SetValue(LabourMappingsProperty, value);
  506. }
  507. private static readonly DependencyProperty LabourCheckedProperty = DependencyProperty.Register(
  508. nameof(LabourChecked),
  509. typeof(bool),
  510. typeof(AWGMappingWindowViewModel),
  511. new FrameworkPropertyMetadata(defaultValue: true, propertyChangedCallback: SectionCheckedChanged)
  512. );
  513. public bool LabourChecked
  514. {
  515. get => (bool)GetValue(LabourCheckedProperty);
  516. set => SetValue(LabourCheckedProperty, value);
  517. }
  518. private static readonly DependencyProperty SectionsProperty = DependencyProperty.Register(
  519. nameof(Sections),
  520. typeof(CoreObservableCollection<KeyValuePair<string, BitmapImage>>),
  521. typeof(AWGMappingWindowViewModel),
  522. new PropertyMetadata(new CoreObservableCollection<KeyValuePair<string, BitmapImage>>())
  523. );
  524. public CoreObservableCollection<KeyValuePair<string, BitmapImage>> Sections
  525. {
  526. get => (CoreObservableCollection<KeyValuePair<string, BitmapImage>>)GetValue(SectionsProperty);
  527. set => SetValue(SectionsProperty, value);
  528. }
  529. private static readonly DependencyProperty SelectedSectionProperty = DependencyProperty.Register(
  530. nameof(SelectedSection),
  531. typeof(KeyValuePair<string, BitmapImage>?),
  532. typeof(AWGMappingWindowViewModel)
  533. );
  534. public KeyValuePair<string, BitmapImage>? SelectedSection
  535. {
  536. get => (KeyValuePair<string, BitmapImage>?)GetValue(SelectedSectionProperty);
  537. set => SetValue(SelectedSectionProperty, value);
  538. }
  539. private static readonly DependencyProperty MappingsCompleteProperty = DependencyProperty.Register(
  540. nameof(MappingsComplete),
  541. typeof(bool),
  542. typeof(AWGMappingWindowViewModel)
  543. );
  544. public bool MappingsComplete
  545. {
  546. get => (bool)GetValue(MappingsCompleteProperty);
  547. set => SetValue(MappingsCompleteProperty, value);
  548. }
  549. private Task<List<TCode>> ExtractMappings<TCode, TType, TEntity, TLink>(
  550. IEnumerable<TType>? items,
  551. Action<TType, TCode>? populate,
  552. Expression<Func<TEntity, object?>> entitycode
  553. )
  554. where TType : IAwgItem
  555. where TCode : BaseIntegrationSource<TEntity, TLink>, IRemotable, IPersistent, new()
  556. where TEntity : Entity, IRemotable, IPersistent, new()
  557. where TLink : EntityLink<TEntity>
  558. {
  559. return Task.Run(() =>
  560. {
  561. var results = new List<TCode>();
  562. if (items == null)
  563. return results;
  564. //var sourceitems = new Dictionary<string, string>();
  565. //foreach (var item in items)
  566. // sourceitems[item.Code] = item.Description;
  567. var keys = items.Select(x => x.Code).Distinct().ToArray();
  568. MultiQuery query = new();
  569. query.Add(
  570. new Filter<TEntity>(entitycode).InList(keys),
  571. Columns.Required<TEntity>().Add(x => x.ID).Add(entitycode)
  572. );
  573. var entitycodecol = $"Entity.{CoreUtils.GetFullPropertyName(entitycode, ".")}";
  574. query.Add(
  575. new Filter<TCode>(x => x.Code).InList(keys),
  576. Columns.Required<TCode>()
  577. .Add(x => x.ID)
  578. .Add(x => x.Code)
  579. .Add(x => x.Entity.ID)
  580. .Add(entitycodecol)
  581. );
  582. query.Query();
  583. var mappings = query.Get<TCode>().ToObjects<TCode>().ToArray();
  584. var entities = query.Get<TEntity>();
  585. foreach (var item in items)
  586. {
  587. var result = new TCode()
  588. {
  589. Code = item.Code,
  590. Description = item.Description
  591. };
  592. populate?.Invoke(item, result);
  593. var mapping = mappings.FirstOrDefault(x => string.Equals(x.Code, item.Code));
  594. if (mapping != null)
  595. result.Entity.CopyFrom(mapping.Entity);
  596. else
  597. {
  598. var entity = entities.Rows.FirstOrDefault(r => Equals(item.Code, r.Get(entitycode)));
  599. if (entity != null)
  600. {
  601. result.Entity.CopyFrom(entity.ToObject<TEntity>());
  602. result.DirectMatch = true;
  603. }
  604. }
  605. results.Add(result);
  606. // result.PropertyChanged += (_, _) =>
  607. // {
  608. // // TMapping mapping = mappingtable.Rows.FirstOrDefault(r =>
  609. // // string.Equals(r.Get<TMapping, String>(c => c.Code), result.Code))?.ToObject<TMapping>();
  610. // // if (mapping == null)
  611. // // mapping = new TMapping() { Code = result.Code };
  612. // // mapping.Entity.ID = result.Entity.ID;
  613. // // new Client<TMapping>().Save(mapping, "Created from BOM Integration Window");
  614. //
  615. // CheckChanged();
  616. // };
  617. }
  618. return results;
  619. });
  620. }
  621. public void CheckChanged()
  622. {
  623. Dispatcher.BeginInvoke(() =>
  624. {
  625. var curSel = SelectedSection?.Key ?? "";
  626. SelectedSection = null;
  627. Sections.Clear();
  628. if (StylesChecked && StyleMappings?.Any() == true)
  629. Sections.Add(new KeyValuePair<string, BitmapImage>("Styles", Resources.palette.AsBitmapImage(64, 64)));
  630. if (GroupsChecked && GroupMappings?.Any() == true)
  631. Sections.Add(
  632. new KeyValuePair<string, BitmapImage>("Groups", Resources.productgroup.AsBitmapImage(64, 64)));
  633. if (SuppliersChecked && SupplierMappings?.Any() == true)
  634. Sections.Add(
  635. new KeyValuePair<string, BitmapImage>("Suppliers", Resources.supplier.AsBitmapImage(64, 64)));
  636. if (DiscountsChecked && DiscountMappings?.Any() == true)
  637. Sections.Add(
  638. new KeyValuePair<string, BitmapImage>("Discounts", Resources.receipt.AsBitmapImage(64, 64)));
  639. if (ProfilesChecked && ProfileMappings?.Any() == true)
  640. Sections.Add(
  641. new KeyValuePair<string, BitmapImage>("Profiles", Resources.profile.AsBitmapImage(64, 64)));
  642. if (GasketsChecked && GasketMappings?.Any() == true)
  643. Sections.Add(new KeyValuePair<string, BitmapImage>("Gaskets", Resources.gasket.AsBitmapImage(64, 64)));
  644. if (ComponentsChecked && ComponentMappings?.Any() == true)
  645. Sections.Add(
  646. new KeyValuePair<string, BitmapImage>("Components", Resources.fixings.AsBitmapImage(64, 64)));
  647. if (GlassChecked && GlassMappings?.Any() == true)
  648. Sections.Add(new KeyValuePair<string, BitmapImage>("Glass", Resources.glass.AsBitmapImage(64, 64)));
  649. if (LabourChecked && LabourMappings?.Any() == true)
  650. Sections.Add(new KeyValuePair<string, BitmapImage>("Labour", Resources.quality.AsBitmapImage(64, 64)));
  651. SelectedSection = Sections.Any(x => string.Equals(x.Key, curSel))
  652. ? Sections.First(x => string.Equals(x.Key, curSel))
  653. : Sections.FirstOrDefault();
  654. var styles = !StylesChecked || (StyleMappings?.Any() != true) || (StyleMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  655. var groups = !GroupsChecked || (GroupMappings?.Any() != true) || (GroupMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  656. var suppliers = !SuppliersChecked || (SupplierMappings?.Any() != true) || (SupplierMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  657. var discounts = !DiscountsChecked || (DiscountMappings?.Any() != true) || (DiscountMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  658. var profiles = !ProfilesChecked || (ProfileMappings?.Any() != true) || (ProfileMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  659. var gaskets = !GasketsChecked || (GasketMappings?.Any() != true) || (GasketMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  660. var components = !ComponentsChecked || (ComponentMappings?.Any() != true) || (ComponentMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  661. var glass = !GlassChecked || (GlassMappings?.Any() != true) || (GlassMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  662. var labour = !LabourChecked || (LabourMappings?.Any() != true) || (LabourMappings?.All(x => x.Entity.ID != Guid.Empty) ?? false);
  663. MappingsComplete = styles && groups && suppliers && discounts && profiles && gaskets && components && glass && labour;
  664. });
  665. }
  666. public ICommand CreateStyle => new ActionCommand(
  667. o =>
  668. {
  669. if (o is IntegrationGridCreateEntityArgs<ProductStyle, ProductStyleIntegrationSource> args)
  670. {
  671. PopulateStyle(args.Entity, args.Mapping);
  672. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  673. }
  674. }
  675. );
  676. private void PopulateStyle(ProductStyle entity, ProductStyleIntegrationSource mapping)
  677. {
  678. entity.Code = mapping.Code ?? "";
  679. entity.Description = mapping.Description ?? "";
  680. TreatmentTypeLink? type = mapping.StyleType == AwgStyleType.Powdercoated
  681. ? _settings.PowdercoatedType
  682. : mapping.StyleType == AwgStyleType.Anodised
  683. ? _settings.AnodisedType
  684. : mapping.StyleType == AwgStyleType.Varnished
  685. ? _settings.VarnishedType
  686. : mapping.StyleType == AwgStyleType.Taped
  687. ? _settings.TapedType
  688. : new TreatmentTypeLink();
  689. entity.TreatmentType.ID = type.ID;
  690. if (entity.TreatmentType.ID != Guid.Empty)
  691. {
  692. var product = new Client<Product>().Query(
  693. new Filter<Product>(x => x.Code).IsEqualTo(entity.Code),
  694. Columns.Local<Product>()
  695. ).ToArray<Product>().FirstOrDefault();
  696. if (product == null)
  697. {
  698. product = new Product();
  699. product.Problem.Notes = ["Created from BOM Integration Window"];
  700. }
  701. product.Code = entity.Code;
  702. product.Name = entity.Description;
  703. product.TreatmentType.ID = entity.TreatmentType.ID;
  704. if (product.IsChanged())
  705. Client.Save(product, "Created from AWG Mapping Window");
  706. entity.StockTreatmentProduct.ID = product.ID;
  707. entity.ManufacturingTreatmentProduct.ID = product.ID;
  708. }
  709. }
  710. public ICommand CreateGroup => new ActionCommand(
  711. o =>
  712. {
  713. if (o is IntegrationGridCreateEntityArgs<ProductGroup, ProductGroupIntegrationSource> args)
  714. {
  715. PopulateGroup(args.Entity, args.Mapping);
  716. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  717. }
  718. }
  719. );
  720. public void PopulateGroup(ProductGroup entity, ProductGroupIntegrationSource mapping)
  721. {
  722. Guid parentid = Guid.Empty;
  723. var parentcodes = mapping.Parent
  724. .Split('/')
  725. .Select(x => x.Trim().ToUpper())
  726. .Where(x=>!string.IsNullOrWhiteSpace(x))
  727. .ToArray();
  728. var parents = Client.Query(
  729. new Filter<ProductGroup>(x => x.Code).InList(parentcodes),
  730. Columns.None<ProductGroup>().Add(x => x.ID).Add(x => x.Code)
  731. ).ToArray<ProductGroup>();
  732. foreach (var parentcode in parentcodes)
  733. {
  734. var parent = parents.FirstOrDefault(x => Equals(x.Code,parentcode));
  735. if (parent == null)
  736. {
  737. parent = new ProductGroup();
  738. parent.Code = parentcode;
  739. parent.Description = !string.IsNullOrWhiteSpace(parentcode) ? parentcode.Titleize() : "";
  740. parent.Parent.ID = parentid;
  741. parent.Problem.Notes = ["Created from BOM Integration Window"];
  742. Client.Save(parent, "Created from AWG Mapping Window");
  743. }
  744. parentid = parent.ID;
  745. }
  746. entity.Parent.ID = parentid;
  747. entity.Code = mapping.Code ?? "";
  748. entity.Description = mapping.Description ?? "";
  749. }
  750. public ICommand CreateSupplier => new ActionCommand(
  751. o =>
  752. {
  753. if (o is IntegrationGridCreateEntityArgs<Supplier, SupplierIntegrationSource> args)
  754. {
  755. PopulateSupplier(args.Entity, args.Mapping);
  756. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  757. }
  758. }
  759. );
  760. private void PopulateSupplier(Supplier entity, SupplierIntegrationSource mapping)
  761. {
  762. entity.Code = mapping.Code ?? "";
  763. entity.Name = mapping.Description ?? "";
  764. }
  765. public ICommand CreateDiscount => new ActionCommand(
  766. o =>
  767. {
  768. if (o is IntegrationGridCreateEntityArgs<SupplierDiscountGroup, SupplierDiscountGroupIntegrationSource> args)
  769. {
  770. PopulateDiscountGroup(args.Entity, args.Mapping);
  771. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  772. }
  773. }
  774. );
  775. private void PopulateDiscountGroup(SupplierDiscountGroup entity, SupplierDiscountGroupIntegrationSource mapping)
  776. {
  777. entity.Code = mapping.Code ?? "";
  778. entity.Description = mapping.Description ?? "";
  779. entity.Type = SupplierDiscountGroupType.Discount;
  780. var _supplier = SupplierMappings?.FirstOrDefault(x => string.Equals(x.Code, mapping.Supplier))?.Entity.ID;
  781. if (_supplier == null)
  782. _supplier = new Client<Supplier>()
  783. .Query(new Filter<Supplier>(x => x.Code).IsEqualTo(entity.Code),
  784. Columns.None<Supplier>().Add(x => x.ID))
  785. .ToArray<Supplier>()
  786. .FirstOrDefault()?.ID ?? Guid.Empty;
  787. entity.Supplier.ID = _supplier.Value;
  788. }
  789. public ICommand AfterCreateDiscountGroup => new ActionCommand(
  790. o =>
  791. {
  792. if (o is IntegrationGridCreateEntityArgs<SupplierDiscountGroup, SupplierDiscountGroupIntegrationSource> args)
  793. {
  794. PopulateDiscount(args.Entity,args.Mapping);
  795. }
  796. }
  797. );
  798. private void PopulateDiscount(SupplierDiscountGroup entity, SupplierDiscountGroupIntegrationSource mapping)
  799. {
  800. var _discount = new Client<SupplierDiscount>().Query(
  801. new Filter<SupplierDiscount>(x => x.Group.ID).IsEqualTo(entity.ID),
  802. Columns.Local<SupplierDiscount>()
  803. ).ToObjects<SupplierDiscount>().FirstOrDefault();
  804. if (_discount == null)
  805. {
  806. _discount = new();
  807. _discount.Group.ID = entity.ID;
  808. }
  809. _discount.Value = mapping.Discount;
  810. if (_discount.IsChanged())
  811. Client.Save(_discount, "Updated from AWG Mapping Window");
  812. }
  813. public ICommand CreateProfile => new ActionCommand(
  814. o =>
  815. {
  816. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  817. {
  818. PopulateProfile(args.Entity,args.Mapping);
  819. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  820. //CreateImage(args);
  821. }
  822. }
  823. );
  824. private void PopulateProfile(Product entity, ProductIntegrationSource mapping)
  825. {
  826. entity.Code = mapping.Code ?? "";
  827. entity.Name = mapping.Description ?? "";
  828. entity.UnitOfMeasure.ID = _settings.ProfileUom.ID;
  829. var _group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, mapping.Group));
  830. if (_group != null)
  831. entity.Group.ID = _group.Entity.ID;
  832. }
  833. public ICommand AfterCreateProduct => new ActionCommand(
  834. o =>
  835. {
  836. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  837. {
  838. PopulateProduct(args.Entity,args.Mapping);
  839. }
  840. }
  841. );
  842. private void PopulateProduct(Product entity, ProductIntegrationSource mapping)
  843. {
  844. CreateTreatmentParams(entity, mapping, AwgStyleType.Powdercoated, Settings.PowdercoatedType);
  845. CreateTreatmentParams(entity, mapping, AwgStyleType.Anodised, Settings.AnodisedType);
  846. CreateTreatmentParams(entity, mapping, AwgStyleType.Taped, Settings.TapedType);
  847. CreateTreatmentParams(entity, mapping, AwgStyleType.Varnished, Settings.VarnishedType);
  848. CreateSupplierProducts(entity, mapping);
  849. }
  850. // private void CreateImage(IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  851. // {
  852. // if (!string.IsNullOrWhiteSpace(_settings.ImageUrl))
  853. // {
  854. // var model = new LogikalCodeModel() { Code = args.Mapping.Code };
  855. // var _expression = new CoreExpression<LogikalCodeModel, string>(_settings.ImageUrl);
  856. // if (_expression.Evaluate(model).Get(out var eval, out var e))
  857. // {
  858. // var tcs = new TaskCompletionSource<byte[]>();
  859. // new HttpClient().GetByteArrayAsync(eval)
  860. // .ContinueWith(
  861. // t =>
  862. // {
  863. // if (t.IsFaulted)
  864. // tcs.SetException(t.Exception);
  865. // else
  866. // tcs.SetResult(t.Result);
  867. // });
  868. // try
  869. // {
  870. // var data = tcs.Task.Result;
  871. // var document = new Document()
  872. // {
  873. // Data = tcs.Task.Result,
  874. // CRC = CoreUtils.CalculateCRC(data),
  875. // FileName = args.Mapping.Code
  876. // };
  877. // Client.Save(document,"Created from AWG Mapping Window");
  878. // args.Entity.Image.ID = document.ID;
  879. // }
  880. // catch (Exception exception)
  881. // {
  882. // Logger.Send(LogType.Error,"",$"Exception in CreateImage(): {exception.Message}");
  883. // }
  884. // }
  885. // }
  886. // }
  887. private void CreateTreatmentParams(Product entity, ProductIntegrationSource mapping, AwgStyleType type, TreatmentTypeLink link)
  888. {
  889. if (mapping.TreatmentParameters.ContainsKey(type) && !Equals(link.ID,Guid.Empty))
  890. {
  891. var _treatment = new Client<ProductTreatment>().Query(
  892. new Filter<ProductTreatment>(x=>x.Product.ID).IsEqualTo(entity.ID)
  893. .And(x=>x.TreatmentType.ID).IsEqualTo(link.ID),
  894. Columns.Local<ProductTreatment>()
  895. ).ToObjects<ProductTreatment>().FirstOrDefault();
  896. if (_treatment == null)
  897. {
  898. _treatment = new();
  899. _treatment.Product.ID = entity.ID;
  900. _treatment.TreatmentType.ID = link.ID;
  901. }
  902. _treatment.Parameter = mapping.TreatmentParameters[type];
  903. if (_treatment.IsChanged())
  904. Client.Save(_treatment,"Updated from AWG Mapping Window");
  905. }
  906. }
  907. private void CreateSupplierProducts(Product entity, ProductIntegrationSource mapping)
  908. {
  909. var supplier = SupplierMappings?.FirstOrDefault(x => Equals(x.Code, mapping.Supplier));
  910. if (supplier != null)
  911. {
  912. List<SupplierProduct> products = new();
  913. if (!mapping.MillCost.IsEffectivelyEqual(mapping.Cost))
  914. {
  915. var _mill = CreateSupplierProduct(entity, mapping, supplier, "", mapping.MillCost, false);
  916. products.Add(_mill);
  917. }
  918. var _treated = CreateSupplierProduct(entity, mapping, supplier, mapping.Style, mapping.Cost, false);
  919. products.Add(_treated);
  920. var _updates = products.Where(x => x.IsChanged()).ToArray();
  921. if (_updates.Any())
  922. Client.Save(_updates,"Updated from AWG Mapping Window");
  923. }
  924. }
  925. private SupplierProduct CreateSupplierProduct(Product entity, ProductIntegrationSource mapping, SupplierIntegrationSource supplier,
  926. string stylecode, double cost, bool save)
  927. {
  928. var _sp = new Client<SupplierProduct>().Query(
  929. new Filter<SupplierProduct>(x => x.Product.ID).IsEqualTo(entity.ID).And(x=>x.SupplierLink.ID).IsEqualTo(supplier.Entity.ID),
  930. Columns.Local<SupplierProduct>()
  931. ).ToObjects<SupplierProduct>().FirstOrDefault();
  932. if (_sp == null)
  933. {
  934. _sp = new();
  935. _sp.Product.ID = entity.ID;
  936. _sp.SupplierLink.ID = supplier.Entity.ID;
  937. }
  938. _sp.SupplierCode = mapping.Code ?? "";
  939. _sp.SupplierDescription = mapping.Description ?? "";
  940. _sp.Dimensions.Unit.ID = mapping.Dimensions.Unit.ID;
  941. _sp.Dimensions.Height = mapping.Dimensions.Height;
  942. _sp.Dimensions.Width = mapping.Dimensions.Width;
  943. _sp.Dimensions.Length = mapping.Dimensions.Length;
  944. _sp.Dimensions.Quantity = mapping.Dimensions.Quantity;
  945. _sp.Dimensions.Weight = mapping.Dimensions.Weight;
  946. _sp.Dimensions.Value = mapping.Dimensions.Value;
  947. _sp.Dimensions.UnitSize = mapping.Dimensions.UnitSize;
  948. var _style = StyleMappings?.FirstOrDefault(x => Equals(x.Code, stylecode));
  949. _sp.Style.ID = _style?.Entity.ID ?? Guid.Empty;
  950. _sp.TradePrice = cost;
  951. if (save && _sp.IsChanged())
  952. new Client<SupplierProduct>().Save(_sp, "Updated by Awg Mapping Window");
  953. return _sp;
  954. }
  955. public ICommand CreateGasket => new ActionCommand(
  956. o =>
  957. {
  958. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  959. {
  960. PopulateGasket(args.Entity, args.Mapping);
  961. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  962. }
  963. }
  964. );
  965. private void PopulateGasket(Product argsEntity, ProductIntegrationSource argsMapping)
  966. {
  967. argsEntity.Code = argsMapping.Entity.Code ?? "";
  968. argsEntity.Name = argsMapping.Description ?? "";
  969. argsEntity.UnitOfMeasure.ID = _settings.GasketUom.ID;
  970. var _group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, argsMapping.Group));
  971. if (_group != null)
  972. argsEntity.Group.ID = _group.Entity.ID;
  973. }
  974. public ICommand CreateComponent => new ActionCommand(
  975. o =>
  976. {
  977. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  978. {
  979. PopulateComponent(args.Entity, args.Mapping);
  980. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  981. }
  982. }
  983. );
  984. private void PopulateComponent(Product entity, ProductIntegrationSource mapping)
  985. {
  986. entity.Code = mapping.Entity.Code ?? "";
  987. entity.Name = mapping.Description ?? "";
  988. entity.UnitOfMeasure.ID = _settings.ComponentUom.ID;
  989. var _group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, mapping.Group));
  990. if (_group != null)
  991. entity.Group.ID = _group.Entity.ID;
  992. }
  993. public ICommand CreateGlass => new ActionCommand(
  994. o =>
  995. {
  996. if (o is IntegrationGridCreateEntityArgs<Product, ProductIntegrationSource> args)
  997. {
  998. PopulateGlass(args.Entity, args.Mapping);
  999. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  1000. }
  1001. }
  1002. );
  1003. private void PopulateGlass(Product entity, ProductIntegrationSource mapping)
  1004. {
  1005. entity.Code = mapping.Entity.Code ?? "";
  1006. entity.Name = mapping.Description ?? "";
  1007. entity.UnitOfMeasure.ID = _settings.GlassUom.ID;
  1008. var _group = GroupMappings?.FirstOrDefault(x => Equals(x.Code, mapping.Group));
  1009. if (_group != null)
  1010. entity.Group.ID = _group.Entity.ID;
  1011. }
  1012. public ICommand CreateActivity => new ActionCommand(
  1013. o =>
  1014. {
  1015. if (o is IntegrationGridCreateEntityArgs<Activity, ActivityIntegrationSource> args)
  1016. {
  1017. PopulateActivity(args.Entity, args.Mapping);
  1018. args.Entity.Problem.Notes = ["Created from BOM Integration Window"];
  1019. }
  1020. }
  1021. );
  1022. private void PopulateActivity(Activity entity, ActivityIntegrationSource mapping)
  1023. {
  1024. entity.Code = mapping.Entity.Code ?? "";
  1025. entity.Description = mapping.Description ?? "";
  1026. }
  1027. private class RawDimensions : IBaseDimensions
  1028. {
  1029. public double Quantity { get; set; }
  1030. public double Length { get; set; }
  1031. public double Width { get; set; }
  1032. public double Height { get; set; }
  1033. public double Weight { get; set; }
  1034. }
  1035. public void GetDiscounts(Action<SupplierDiscountGroupLink, double>? discountCallback)
  1036. {
  1037. if (DiscountsChecked && Discounts != null)
  1038. {
  1039. foreach (var discount in Discounts)
  1040. {
  1041. var discountmapping = DiscountMappings?.FirstOrDefault(x => x.Code == discount.Code);
  1042. if (discountmapping != null && discountCallback != null)
  1043. discountCallback?.Invoke(discountmapping.Entity,discountmapping.Discount);
  1044. }
  1045. }
  1046. }
  1047. public void GetParts(
  1048. Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
  1049. Action<ActivityLink, TimeSpan, double>? labourCallback)
  1050. {
  1051. GetParts(Profiles,Gaskets,Components,Glass,Labour,productCallback,labourCallback);
  1052. }
  1053. public void GetParts<TProfile, TGasket, TComponent, TGlass, TLabour>(
  1054. IEnumerable<TProfile>? profiles,
  1055. IEnumerable<TGasket>? gaskets,
  1056. IEnumerable<TComponent>? components,
  1057. IEnumerable<TGlass>? glasses,
  1058. IEnumerable<TLabour>? labour,
  1059. Action<ProductLink, ProductStyleLink?, IBaseDimensions, double, double>? productCallback,
  1060. Action<ActivityLink, TimeSpan, double>? labourCallback)
  1061. where TProfile : IAwgProfile
  1062. where TGasket : IAwgGasket
  1063. where TComponent : IAwgComponent
  1064. where TGlass : IAwgGlass
  1065. where TLabour : IAwgLabour
  1066. {
  1067. if (ProfilesChecked && profiles != null)
  1068. {
  1069. foreach (var profile in profiles)
  1070. {
  1071. var profilemapping = ProfileMappings?.FirstOrDefault(x => x.Code == profile.Code);
  1072. var stylemapping = StyleMappings?.FirstOrDefault(x => string.Equals(x.Code, profile.Finish));
  1073. if (profilemapping != null && productCallback is not null)
  1074. {
  1075. productCallback(
  1076. profilemapping.Entity,
  1077. stylemapping?.Entity,
  1078. new RawDimensions() { Length = profile.Length },
  1079. profile.Quantity,
  1080. profile.Cost * profile.Quantity);
  1081. }
  1082. }
  1083. }
  1084. if (GasketsChecked && gaskets != null)
  1085. {
  1086. foreach (var gasket in gaskets)
  1087. {
  1088. var _mapping = GasketMappings?.FirstOrDefault(x => x.Code == gasket.Code);
  1089. var stylemapping = StyleMappings?.FirstOrDefault(x => string.Equals(x.Code, gasket.Finish));
  1090. if (_mapping != null && productCallback is not null)
  1091. {
  1092. _mapping.ConvertDimensions(
  1093. x => x.Dimensions,
  1094. x => x.Quantity,
  1095. x => x.Cost,
  1096. Client<ProductDimensionUnit>.Provider
  1097. );
  1098. productCallback(
  1099. _mapping.Entity,
  1100. stylemapping?.Entity,
  1101. new RawDimensions() { Length = _mapping.Dimensions.Length },
  1102. _mapping.Quantity,
  1103. _mapping.Quantity * _mapping.Cost);
  1104. }
  1105. }
  1106. }
  1107. if (ComponentsChecked && components != null)
  1108. {
  1109. foreach (var component in components)
  1110. {
  1111. var _mapping = ComponentMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Code));
  1112. var stylemapping = StyleMappings?.FirstOrDefault(x => string.Equals(x.Code, component.Finish));
  1113. if (_mapping != null && productCallback is not null)
  1114. {
  1115. _mapping.ConvertDimensions(
  1116. x => x.Dimensions,
  1117. x => x.Quantity,
  1118. x => x.Cost,
  1119. Client<ProductDimensionUnit>.Provider
  1120. );
  1121. productCallback(
  1122. _mapping.Entity,
  1123. stylemapping?.Entity,
  1124. new RawDimensions() { Quantity = _mapping.Dimensions.Quantity },
  1125. _mapping.Quantity,
  1126. _mapping.Quantity * _mapping.Cost);
  1127. }
  1128. }
  1129. }
  1130. if (GlassChecked && glasses != null)
  1131. {
  1132. foreach (var glass in glasses)
  1133. {
  1134. var _mapping = GlassMappings?.FirstOrDefault(x => string.Equals(x.Code, glass.Code));
  1135. if (_mapping != null && productCallback is not null)
  1136. {
  1137. productCallback(
  1138. _mapping.Entity,
  1139. null,
  1140. new RawDimensions() { Height = glass.Height, Width = glass.Width },
  1141. glass.Quantity,
  1142. glass.Quantity * glass.Cost);
  1143. }
  1144. }
  1145. }
  1146. if (LabourChecked && labour != null)
  1147. {
  1148. foreach (var activity in labour)
  1149. {
  1150. var _mapping = LabourMappings?.FirstOrDefault(x => string.Equals(x.Code, activity.Code));
  1151. if (_mapping != null && labourCallback is not null)
  1152. {
  1153. labourCallback(
  1154. _mapping.Entity,
  1155. TimeSpan.FromHours(activity.Quantity),
  1156. activity.Quantity * activity.Cost);
  1157. }
  1158. }
  1159. }
  1160. }
  1161. private void CheckUpdate<TEntity,TItem,TMapping>(bool isChecked, LogikalUpdateType updateType, IEnumerable<TItem>? items,
  1162. IEnumerable<TMapping>? mappings,
  1163. Action<TEntity,TMapping> callback)
  1164. where TEntity : Entity, IRemotable, IPersistent, new()
  1165. where TItem : IIntegrationItem
  1166. where TMapping : class, IBaseIntegrationSource
  1167. {
  1168. var _items = items as TItem[] ?? items?.ToArray();
  1169. var _mappings = mappings as TMapping[] ?? mappings?.ToArray();
  1170. if (isChecked && updateType.Equals(LogikalUpdateType.AlwaysUpdate) && _items?.Any()==true && _mappings?.Any() == true)
  1171. {
  1172. var _ids = _mappings?.Select(x => x.Entity.ID).Distinct().ToArray() ?? [];
  1173. var _entities = new Client<TEntity>().Query(
  1174. new Filter<TEntity>(x=>x.ID).InList(_ids),
  1175. Columns.Local<TEntity>().Add(Columns.Required<TEntity>())
  1176. ).ToArray<TEntity>();
  1177. foreach (var _item in _items)
  1178. {
  1179. var _mapping = _mappings?.FirstOrDefault(x => string.Equals(x.Code, _item.Code));
  1180. if (_mapping != null)
  1181. {
  1182. var _entity = _entities.FirstOrDefault(x => x.ID == _mapping.Entity.ID);
  1183. if (_entity != null)
  1184. callback(_entity, _mapping);
  1185. }
  1186. }
  1187. var _updates = _entities.Where(x => x.IsChanged()).ToArray();
  1188. if (_updates.Any())
  1189. new Client<TEntity>().Save(_updates,"Updated by AWG Mapping Window");
  1190. }
  1191. }
  1192. public void CheckUpdates()
  1193. {
  1194. CheckUpdate<ProductStyle,IAwgStyle,ProductStyleIntegrationSource>(StylesChecked, Settings.UpdateStyles, Styles,
  1195. StyleMappings, PopulateStyle);
  1196. CheckUpdate<ProductGroup,IAwgGroup,ProductGroupIntegrationSource>(GroupsChecked, Settings.UpdateGroups, Groups,
  1197. GroupMappings, PopulateGroup);
  1198. CheckUpdate<Supplier, IAwgSupplier, SupplierIntegrationSource>(SuppliersChecked, Settings.UpdateSuppliers,
  1199. Suppliers, SupplierMappings, PopulateSupplier);
  1200. CheckUpdate<SupplierDiscountGroup, IAwgDiscount,SupplierDiscountGroupIntegrationSource>(DiscountsChecked,
  1201. Settings.UpdateDiscounts, Discounts, DiscountMappings, (entity,mapping) =>
  1202. {
  1203. PopulateDiscountGroup(entity,mapping);
  1204. PopulateDiscount(entity,mapping);
  1205. });
  1206. CheckUpdate<Product, IAwgProfile, ProductIntegrationSource>(ProfilesChecked, Settings.UpdateProfiles,
  1207. Profiles, ProfileMappings, (entity, mapping) =>
  1208. {
  1209. PopulateProfile(entity,mapping);
  1210. PopulateProduct(entity,mapping);
  1211. });
  1212. CheckUpdate<Product, IAwgGasket, ProductIntegrationSource>(GasketsChecked, Settings.UpdateGaskets,
  1213. Gaskets, GasketMappings, (entity, mapping) =>
  1214. {
  1215. PopulateGasket(entity,mapping);
  1216. PopulateProduct(entity,mapping);
  1217. });
  1218. CheckUpdate<Product, IAwgComponent, ProductIntegrationSource>(ComponentsChecked, Settings.UpdateComponents,
  1219. Components, ComponentMappings, (entity, mapping) =>
  1220. {
  1221. PopulateComponent(entity,mapping);
  1222. PopulateProduct(entity,mapping);
  1223. });
  1224. CheckUpdate<Product, IAwgGlass, ProductIntegrationSource>(GlassChecked, Settings.UpdateGlass,
  1225. Glass, GlassMappings, (entity, mapping) =>
  1226. {
  1227. PopulateGlass(entity,mapping);
  1228. PopulateProduct(entity,mapping);
  1229. });
  1230. CheckUpdate<Activity, IAwgLabour, ActivityIntegrationSource>(LabourChecked, Settings.UpdateLabour,
  1231. Labour, LabourMappings, PopulateActivity);
  1232. }
  1233. }