-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
369 lines (318 loc) · 13 KB
/
MainWindow.xaml.cs
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Windows;
using System.Windows.Controls;
using System.Data;
using System.IO;
namespace Prosty_System_zamówień
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
DodajPolaczenie wnd = new DodajPolaczenie(); //przekazanie do nowego okna
wnd.Show();
InitializeComponent();
//Zarządzanie klientami
dataGrid.Visibility = Visibility.Hidden;
edytujKlienta.Visibility = Visibility.Hidden;
infCombobox.Visibility = Visibility.Hidden;
comboBox.Visibility = Visibility.Hidden;
//Zarządzanie towarami
listaTowarow.Visibility = Visibility.Hidden;
edytujTowar.Visibility = Visibility.Hidden;
infWybierzTowar.Visibility = Visibility.Hidden;
WybierzTowar.Visibility = Visibility.Hidden;
}
private void edytujKlienta_Click(object sender, RoutedEventArgs e)
{
string sqlString = null;
Klient kl = new Klient();
try
{
string wybranyKlient = comboBox.SelectedValue.ToString(); //wczyatnie wartości z comboboxa do stringa
String[] substrings = wybranyKlient.Split(' '); // podzielenie stringa na imie i nazwisko
kl.Imie = substrings[0];
kl.Nazwisko = substrings[1];
sqlString = "SELECT * FROM Klienci WHERE Imie Like '" + kl.Imie + "' AND Nazwisko Like '" + kl.Nazwisko + "';";
DataTable dt = new DataTable();
BazaDanych bd = new BazaDanych();
dt = bd.pobierzDane(sqlString);
foreach (DataRow row in dt.Rows)
{
kl.Id = (int)row.ItemArray[0];
}
Window2 wnd = new Window2(kl); //przekazanie do nowego okna
wnd.Show();
}
catch (System.NullReferenceException)
{
MessageBox.Show("Nie wybrano klienta do edycji! Prosze wybrać klienta z listy rozwijanej.");
}
}
private void WybierzTowar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void dodaj_towar(object sender, RoutedEventArgs e)
{
DodajTowar wnd = new DodajTowar(); //przekazanie do nowego okna
wnd.Show();
}
private void pobierz_towary(object sender, RoutedEventArgs e)
{
string sqlString = "SELECT * FROM Towary";
DataTable dt = new DataTable();
BazaDanych bd = new BazaDanych();
dt = bd.pobierzDane(sqlString);
listaTowarow.Visibility = Visibility.Visible;
listaTowarow.ItemsSource = dt.DefaultView;
WybierzTowar.Items.Clear();
foreach (DataRow row in dt.Rows)
{
string etykieta = row.ItemArray[1].ToString();
WybierzTowar.Items.Add(etykieta);
}
if (dt != null)
{
edytujTowar.Visibility = Visibility.Visible;
infWybierzTowar.Visibility = Visibility.Visible;
WybierzTowar.Visibility = Visibility.Visible;
}
}
private void edytujTowar_Click(object sender, RoutedEventArgs e)
{
string sqlString = null;
Towar tow = new Towar();
try
{
tow.Nazwa = WybierzTowar.SelectedValue.ToString(); //wczyatnie wartości z comboboxa do stringa
sqlString = "SELECT * FROM Towary WHERE NazwaTowaru Like '" + tow.Nazwa + "';";
DataTable dt = new DataTable();
BazaDanych bd = new BazaDanych();
dt = bd.pobierzDane(sqlString);
foreach (DataRow row in dt.Rows)
{
tow.Id = (int)row.ItemArray[0];
tow.Cena = Convert.ToDecimal(row.ItemArray[2].ToString()); //żeby użyć trzeba zmienić w klasie typ z string na int
tow.wielkoscPodatku = (int)row.ItemArray[4];
}
EdytujTowar wnd = new EdytujTowar(tow); //przekazanie do nowego okna
wnd.Show();
}
catch (System.NullReferenceException)
{
MessageBox.Show("Nie wybrano towaru do edycji! Prosze wybrać towar z listy rozwijanej.");
}
}
private void dodajKlientaPrzyZamawianiu_Click(object sender, RoutedEventArgs e)
{
DodajOsobe wnd = new DodajOsobe(); //przekazanie do nowego okna
wnd.Show();
}
private void comboKlienci_DropDownOpened(object sender, EventArgs e)
{
//pobierz dane klientów
string sqlStringKlienci = "SELECT * FROM Klienci";
DataTable klienci = new DataTable();
BazaDanych bd = new BazaDanych();
klienci = bd.pobierzDane(sqlStringKlienci);
comboKlienci.Items.Clear();
lKlientowDoWys.Items.Clear();
try
{
klienci = bd.pobierzDane(sqlStringKlienci);
foreach (DataRow row in klienci.Rows)
{
string etykieta = row.ItemArray[1].ToString() + " " + row.ItemArray[2].ToString();
comboKlienci.Items.Add(etykieta);
lKlientowDoWys.Items.Add(etykieta);
}
}
catch (Exception)
{
MessageBox.Show("Nie udało się załadować listy klientów!");
}
}
private void comboTowary_DropDownOpened(object sender, EventArgs e)
{
string sqlStringTowary = "SELECT * FROM Towary";
BazaDanych bd = new BazaDanych();
DataTable towary = new DataTable();
towary = bd.pobierzDane(sqlStringTowary);
comboTowary.Items.Clear();
lTowarowDoWys.Items.Clear();
try
{
towary = bd.pobierzDane(sqlStringTowary);
foreach (DataRow row in towary.Rows)
{
string etykieta = row.ItemArray[1].ToString();
comboTowary.Items.Add(etykieta);
lTowarowDoWys.Items.Add(etykieta);
}
}
catch (Exception)
{
MessageBox.Show("Nie udało się załadować listy towarów!");
}
}
private void ZlozZamowienie_Click(object sender, RoutedEventArgs e)
{
if (comboKlienci.SelectedItem == null && comboTowary.SelectedItem == null)
{
MessageBox.Show("Proszę wybrać towar oraz klienta, który go zamowił!");
}
else
{
//Tutaj dodawanie zamówienia do bazy danych.
Klient kt = new Klient();
Towar tow = new Towar();
Zamowienia zamowienie = new Zamowienia();
try
{
tow.Nazwa = comboTowary.SelectedValue.ToString();
String[] substrings = comboKlienci.SelectedValue.ToString().Split(' '); // podzielenie stringa na imie i nazwisko
kt.Imie = substrings[0];
kt.Nazwisko = substrings[1];
bool stanDodawania = zamowienie.zlozZamowienie(kt, tow);
try
{
if (stanDodawania == true)
MessageBox.Show("Dodawanie nowego zamówienia powiodło się!");
else
MessageBox.Show("Nowe zamówienie nie zostało dodane!");
}
catch (Exception)
{
MessageBox.Show("Nie udało się dodać zamówienia!");
}
}
catch (Exception)
{
MessageBox.Show("Proszę uzupełnić pola wybór klienta oraz wybór towaru!");
}
}
}
private void pobierzAll_Click(object sender, RoutedEventArgs e)
{
Klient kt = new Klient();
Zamowienia zam = new Zamowienia();
DataTable dt = new DataTable();
dt = zam.pobierzZamowienia(null);
dataZamowienia.ItemsSource = dt.DefaultView;
}
private void filtruj_Click(object sender, RoutedEventArgs e)
{
Klient kt = new Klient();
Zamowienia zam = new Zamowienia();
DataTable dt = new DataTable();
try
{
String[] substrings = lKlientowDoWys.SelectedValue.ToString().Split(' '); // podzielenie stringa na imie i nazwisko
kt.Imie = substrings[0];
kt.Nazwisko = substrings[1];
kt.pobierzIDKlienta();
}
catch (Exception)
{
MessageBox.Show("Proszę wybrać klienta z listy rozwijanej.");
}
dt = zam.pobierzZamowienia(kt);
dataZamowienia.ItemsSource = dt.DefaultView;
}
private void eksport_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "ListaZamowien"; // Domyślna nazwa pliku
dlg.DefaultExt = ".txt"; // Domyślne rozszerzenie pliku
dlg.Filter = "Text documents (.xml)|*.xml"; // Filtr plików
DataTable dt = new DataTable();
System.IO.StringWriter writer = new System.IO.StringWriter();
try // sprawdza czy pobrano dane z bazy danych
{
dt = ((DataView)dataZamowienia.ItemsSource).ToTable();
//Ustawienie ID Columny jako primary key
dt.PrimaryKey = new DataColumn[] { dt.Columns[1] };
dt.TableName = "Lista zamowień";
dt.AcceptChanges();
dt.WriteXml(writer, false);
// Pokarz okno wyboru plików
Nullable<bool> result = dlg.ShowDialog();
// Zrób gdy wybrano jakiś plik
if (result == true)
{
try
{
FileStream fs = new FileStream(dlg.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
using (StreamWriter swriter = new StreamWriter(fs))
{
swriter.Write(writer.ToString());
}
}
catch (Exception)
{
MessageBox.Show("Nie udało się zapisać pliku!");
}
}
}
catch (Exception)
{
MessageBox.Show("Nie pobrano danych o zamówieniach!");
}
}
private void pobierzKlienta_Click(object sender, RoutedEventArgs e)
{
string sqlString = "SELECT * FROM Klienci";
BazaDanych bd = new BazaDanych();
DataTable dt = new DataTable();
dt = bd.pobierzDane(sqlString);
dataGrid.Visibility = Visibility.Visible;
dataGrid.ItemsSource = dt.DefaultView;
comboBox.Items.Clear();
foreach (DataRow row in dt.Rows)
{
string etykieta = row.ItemArray[1].ToString() + " " + row.ItemArray[2].ToString();
comboBox.Items.Add(etykieta);
}
if (dt != null)
{
edytujKlienta.Visibility = Visibility.Visible;
infCombobox.Visibility = Visibility.Visible;
comboBox.Visibility = Visibility.Visible;
}
}
private void dodajKlienta_Click(object sender, RoutedEventArgs e)
{
DodajOsobe wnd = new DodajOsobe(); //przekazanie do nowego okna
wnd.Show();
}
private void filtrujTowary_Click(object sender, RoutedEventArgs e)
{
Towar tow = new Towar();
Zamowienia zam = new Zamowienia();
DataTable dt = new DataTable();
try
{
tow.Nazwa = lTowarowDoWys.SelectedValue.ToString();
tow.pobierzIDTowaru();
}
catch (Exception)
{
MessageBox.Show("Proszę wybrać klienta z listy rozwijanej.");
}
dt = zam.pobierzZamowieniaT(tow);
dataZamowienia.ItemsSource = dt.DefaultView;
}
private void Grid_DragEnter(object sender, DragEventArgs e)
{
}
private void lKlientowDoWys_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void lTowarowDoWys_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}