-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineCheckForm.xaml.cs
More file actions
271 lines (235 loc) · 11.5 KB
/
MachineCheckForm.xaml.cs
File metadata and controls
271 lines (235 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using MachineCheck.Models;
using MachineCheck.Services;
using MachineCheck.ViewModels;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
namespace MachineCheck
{
public partial class MachineCheckForm : Window
{
private readonly long _machineId;
private readonly int _opeId;
private readonly DatabaseService _dbService;
private readonly MachineViewModel _machineVM;
public MachineCheckForm(long machineId, int opeId, DatabaseService dbService)
{
InitializeComponent();
var culture = new CultureInfo("en-CA");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
dpFirstCheck.Language = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
dpLastCheck.Language = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
_machineId = machineId;
_opeId = opeId;
_dbService = dbService;
_machineVM = new MachineViewModel();
this.DataContext = _machineVM;
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
await LoadMachineDataAsync();
}
catch (Exception ex)
{
MessageBox.Show($"Wystąpił nieznany błąd przy ładowaniu formularza. Poinformuj adminisratora. {Environment.NewLine}{ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async Task LoadMachineDataAsync()
{
try
{
var machine = await _dbService.GetMachineCheckDataAsync(_machineId);
if (machine == null)
{
MessageBox.Show("Nie znaleziono maszyny. Upewnij się, że maszyna jest dodana do środków trwałych.", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
}
_machineVM.MachineId = _machineId;
_machineVM.MachineName = machine.Name;
_machineVM.MachineCode = machine.Code;
_machineVM.MachineInternalNumber = machine.InternalCode;
_machineVM.MachineNumber = machine.NrEwidencyjny;
_machineVM.MachineSerialNumber = machine.SerialNumber;
_machineVM.MachineBuyDate = machine.BuyDate;
_machineVM.LastCheckDate = machine.LastCheckDate;
_machineVM.FirstCheckDate = machine.FirstCheckDate;
_machineVM.CheckInterval = machine.CheckInterval ?? 3;
_machineVM.DaysBeforeNotif = machine.DaysBeforeNotif ?? 14;
_machineVM.NotifEmail = machine.NotifEmail ?? "michal.g@gaska.com.pl";
_machineVM.IsSaved = machine.IsSaved;
if (_machineVM.LastCheckDate is null)
{
dpLastCheckLabel.Visibility = Visibility.Collapsed;
dpLastCheck.Visibility = Visibility.Collapsed;
dpFirstCheck.Visibility = Visibility.Visible;
dpFirstCheckLabel.Visibility = Visibility.Visible;
}
}
catch (Exception ex)
{
MessageBox.Show($"Wystąpił nieznany błąd przy ładowaniu formularza. Poinformuj administratora.{Environment.NewLine}{ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void GenerateProtocolButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (!_machineVM.IsSaved)
{
MessageBox.Show("Nie można wygenerować protokołu, ponieważ nie zapisano parametrów przeglądu maszyny. Uzupełnij parametry i kliknij przycisk 'Zapisz'.", "Uwaga", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var additionalParamsForm = new AdditionalPrintingParamsForm(_machineVM.MachineSerialNumber);
bool? result = additionalParamsForm.ShowDialog();
if (result != true)
return;
var additionalParams = additionalParamsForm.ToParameterDictionary();
string exe = @"\\Backup\k\DrukowanieMetekProdukcja\PrintReport\PrintReport.exe";
string rpt = @"\\Backup\k\DrukowanieMetekProdukcja\PrintReport\MachineProtocol.rpt";
// Start building args string
var argsBuilder = new StringBuilder();
argsBuilder.Append($"--rpt \"{rpt}\" ");
argsBuilder.Append("--copies 1 ");
argsBuilder.Append($"--param \"Nr Ewidencyjny={_machineVM.MachineNumber}\" ");
argsBuilder.Append($"--param \"Nazwa={_machineVM.MachineName}\" ");
argsBuilder.Append($"--param \"Data zakupu={_machineVM.MachineBuyDate}\" ");
argsBuilder.Append($"--param \"Numer Wewnętrzny={_machineVM.MachineInternalNumber}\" ");
argsBuilder.Append($"--param \"NextDate={_machineVM.NextCheckDate}\" ");
// Add additional params from the form
foreach (var kvp in additionalParams)
{
// Escape quotes just in case
string value = kvp.Value.Replace("\"", "\\\"");
argsBuilder.Append($"--param \"{kvp.Key}={value}\" ");
}
var psi = new ProcessStartInfo
{
FileName = exe,
Arguments = argsBuilder.ToString(),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (var proc = Process.Start(psi))
{
string stdout = proc.StandardOutput.ReadToEnd();
string stderr = proc.StandardError.ReadToEnd();
proc.WaitForExit();
int exitCode = proc.ExitCode;
if (exitCode == 0) MessageBox.Show("Przekazano wydruk do drukarki.", "Sukces", MessageBoxButton.OK, MessageBoxImage.Information);
else MessageBox.Show($"Błąd wydruku (kod {exitCode}).\n{stderr}\n{stdout}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
MessageBox.Show($"Wystąpił błąd przy generowaniu formularza. Poinformuj administratora.\n{ex}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void CheckButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (!_machineVM.IsSaved)
{
MessageBox.Show("Nie można wygenerować protokołu, ponieważ nie zapisano parametrów przeglądu maszyny. Uzupełnij parametry i kliknij przycisk 'Zapisz'.", "Uwaga", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// Let user choose file
var openFileDialog = new Microsoft.Win32.OpenFileDialog
{
Title = "Wybierz plik PDF do załączenia",
Filter = "Pliki PDF (*.pdf)|*.pdf",
CheckFileExists = true,
CheckPathExists = true
};
byte[] fileBytes = null;
string filePath = string.Empty;
bool? result = openFileDialog.ShowDialog();
if (result == true)
{
filePath = openFileDialog.FileName;
fileBytes = File.ReadAllBytes(filePath);
}
else
{
MessageBox.Show("Nie wybrano pliku. Operacja została anulowana.", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
// Execute machine check with file bytes
bool success = await _dbService.ExecuteMachineCheckAsync(_machineVM.MachineId, _opeId, Path.GetFileName(filePath), fileBytes, Path.GetExtension(filePath));
if (success)
{
_machineVM.LastCheckDate = DateTime.Now;
dpLastCheck.Visibility = Visibility.Visible;
dpLastCheckLabel.Visibility = Visibility.Visible;
dpFirstCheck.Visibility = Visibility.Hidden;
dpFirstCheckLabel.Visibility = Visibility.Hidden;
MessageBox.Show("Pomyślnie wykonano przegląd.", "Sukces", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Nie udało się wykonać przeglądu maszyny. Spróbuj ponownie.", "Uwaga", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show($"Wystąpił błąd przy wykonywaniu przeglądu danych. Poinformuj administratora.{Environment.NewLine}{ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
try
{
if (!_machineVM.FirstCheckDate.HasValue)
{
MessageBox.Show("Nie można zapisać danych, ponieważ data pierwszego przeglądu jest pusta.", "Uwaga", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var data = new MachineData
{
Id = _machineVM.MachineId,
Code = _machineVM.MachineCode,
FirstCheckDate = _machineVM.FirstCheckDate,
CheckInterval = _machineVM.CheckInterval,
DaysBeforeNotif = _machineVM.DaysBeforeNotif,
NotifEmail = _machineVM.NotifEmail,
SerialNumber = _machineVM.MachineSerialNumber
};
bool success = await _dbService.SaveMachineCheckDataAsync(data);
if (success)
{
MessageBox.Show("Dane przeglądu maszyny zostały zapisane pomyślnie.", "Sukces", MessageBoxButton.OK, MessageBoxImage.Information);
_machineVM.IsSaved = true;
}
else
MessageBox.Show("Nie zapisano żadnych danych. Sprawdź wprowadzone wartości.", "Uwaga", MessageBoxButton.OK, MessageBoxImage.Warning);
}
catch (Exception ex)
{
MessageBox.Show($"Wystąpił błąd przy zapisie danych. Poinformuj administratora.{Environment.NewLine}{ex.Message}", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void HistoryButton_Click(object sender, RoutedEventArgs e)
{
var historyWindow = new MachineCheckHistoryForm(_machineId, _dbService);
historyWindow.Owner = this;
historyWindow.ShowDialog();
}
}
}