123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Logikal;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.CodeAnalysis;
- using PRSDesktop.Utils.LogikalUtils;
- namespace PRSDesktop;
- public partial class LogikalBillOfMaterialsImport : Window
- {
- private void UpdateStatus(LogikalResponse response)
- {
- Status.Content = response.ToString();
- }
- private LogikalClient GetClient(object sender) => client;
- private LogikalClient client;
- private Job _job;
- public LogikalBillOfMaterialsImport(Job job)
- {
- client = new LogikalClient();
- InitializeComponent();
- _job = job;
- Projects.Refresh(true, false);
- Phases.Refresh(true, false);
- Elevations.Refresh(true, false);
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- if (LogikalCommon.CheckSettings(client.Settings))
- {
- if (client.Settings.ImportJobs && Guid.TryParse(_job.SourceRef, out Guid _projectid))
- {
- Phases.ProjectID = _projectid;
- Elevations.ProjectID = _projectid;
- Phases.Get();
- }
- else
- {
- Projects.JobNumber = _job.JobNumber;
- Projects.Get();
- }
-
- }
- else
- Close();
- }
- private void Projects_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
- {
- var id = e.Rows?.FirstOrDefault()?.Get<LogikalProject, Guid>(x => x.ID) ?? Guid.Empty;
- GetPhases(id);
- }
- private void GetPhases(Guid id)
- {
- Phases.ProjectID = id;
- Elevations.ProjectID = id;
- Phases.Get();
- }
- private void Phases_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
- {
- var phase = e.Rows?.FirstOrDefault()?.Get<LogikalPhase, string>(x => x.ID) ?? "";
- GetElevations(phase);
- }
- private void GetElevations(string phase)
- {
- Elevations.Phase = phase;
- Elevations.Get();
- }
- private void Elevations_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
- {
- var id = e.Rows?.FirstOrDefault()?.Get<LogikalElevation, Guid>(x => x.ID) ?? Guid.Empty;
- OK.IsEnabled = id != Guid.Empty;
- }
- private void Projects_AfterRefresh(object sender, AfterRefreshEventArgs args)
- {
- Projects.Visibility = Projects.Items.Count > 1
- ? Visibility.Visible
- : Visibility.Collapsed;
- GetPhases(Projects.Items.FirstOrDefault()?.ID ?? Guid.Empty);
- }
- private void Phases_AfterRefresh(object sender, AfterRefreshEventArgs args)
- {
- Phases.Visibility = Phases.Items.Count > 1
- ? Visibility.Visible
- : Visibility.Collapsed;
- GetElevations(Phases.Items.FirstOrDefault()?.ID ?? "");
- }
- private void Cancel_Click(object sender, RoutedEventArgs e)
- {
- DialogResult = false;
- }
- private void OK_Click(object sender, RoutedEventArgs e)
- {
- bool close = false;
- // Create a BOM
- var ids = Elevations.SelectedRows.Select(r => r.Get<LogikalElevation, Guid>(c => c.ID)).ToArray();
- client.GetBillOfMaterials(Elevations.ProjectID, ids, true)
- .Always(UpdateStatus)
- .Error(error =>
- {
- MessageWindow.ShowMessage(error.Message,"Error");
- })
- .Success<LogikalBOMResponse<LogikalFinish,LogikalProfile,LogikalComponent,LogikalGlass,LogikalLabour>>(bom =>
- {
- var _fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BillOfMaterials.xlsx");
- File.WriteAllBytes(_fileName, bom.ExcelData);
- if (CreateBOM(bom))
- close = true;
- });
- if (close)
- DialogResult = true;
- }
- private bool CreateBOM(LogikalBOMResponse<LogikalFinish,LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour> bom)
- {
- LogikalPartsManager manager = new LogikalPartsManager();
- if (!manager.CheckMissingItems(bom))
- return false;
- var _jobbom = new JobBillOfMaterials();
- _jobbom.Job.ID = _job.ID;
- _jobbom.Description = $"BOM Imported {DateTime.Now}";
- Client.Save(_jobbom, "Imported From Logikal");
- var _bomitems = manager.CreateItems<JobBillOfMaterialsItem>(
- _jobbom,
- bom,
- (logikalitem, bomitem) =>
- {
- bomitem.BillOfMaterials.ID = _jobbom.ID;
- bomitem.Quantity = logikalitem.Quantity;
- }
- );
- Client.Save(_bomitems, "Imported From Logikal");
- return true;
- }
-
-
- }
|