root/Baza Reklam 2 - Faktury/ClientsForm.cs @ 65

Wersja 65, 108.2 KB (wprowadzona przez dorota, 17 years temu)
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.SqlClient;
6using System.Drawing;
7using System.Text;
8using System.Windows.Forms;
9using System.IO;
10using System.Text.RegularExpressions;
11
12namespace Baza_Reklam
13{
14    public partial class ClientsForm : Form
15    {
16        private int Panel1Height = 380;
17        private int splitConteiner1Height = 230;
18
19        //zmienna pomocnicza do kontroli clientsDataGridView_CellLeave
20        private bool dodajKlienta = false;
21       
22        //obiekty wykorzytywane przy wyszukiwaniu
23        private SqlDataAdapter sqlDataAdapter;
24        private SqlCommand command;
25        private string query = "select top 2000 K.* from KLIENCI K ";
26
27        //sigleton
28        private static ClientsForm clientsForm;
29       
30        public static ClientsForm getClientsForm(MDIBazaReklam parent)
31        {
32            if (clientsForm == null)
33            {
34                clientsForm = new ClientsForm(parent);               
35            }
36            return clientsForm;
37        }
38
39        private ClientsForm(MDIBazaReklam parent)
40        {
41            InitializeComponent();
42
43            this.MdiParent = parent;
44
45            //podmiana connstringa
46            this.kL_1TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
47            this.kL_2TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
48            this.kL_3TableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
49            this.kL_KLIENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
50            this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
51            this.kLIENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
52            this.kONTAKTYTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
53            this.zestawienieFakturTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
54            this.rEKLAMATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
55            this.fakturA_DETAILSTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
56            this.fAKTURYTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
57            this.shedulerTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
58            this.oFERTY_MAINTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
59            this.aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
60            this.kLIENCI_TYTULTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
61            //this.li
62           
63            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
64           
65            this.command = new SqlCommand();
66            this.command.CommandType = CommandType.Text;
67            this.command.Connection = conn;
68           
69            this.sqlDataAdapter = new SqlDataAdapter();
70            this.sqlDataAdapter.SelectCommand = command;
71
72            //dodanie obrazkow do drzewka w zakladce KLASYFIKACJA KLIENTA
73            ImageList imageList = new ImageList();
74            imageList.Images.Add(Baza_Reklam.Properties.Resources.bullet_ball_glass_green);
75            imageList.Images.Add(Baza_Reklam.Properties.Resources.bullet_ball_glass_red);
76            klasyfikacjaTreeView.ImageList = imageList;
77            klasyfikacjaTreeView.ImageIndex = 0;
78            klasyfikacjaTreeView.SelectedImageIndex = 1;
79           
80            //generuje piersze wêz³y w drzewie
81            generateNodes();
82
83            //ustawia uprawnienia
84            uprawnienia();
85
86            //dodaje podpowiedzi do buttonow
87            podpowiedzi();
88      }
89
90        private void ClientsForm_Load(object sender, EventArgs e)
91        {
92             this.WindowState = FormWindowState.Maximized;
93
94            //bez tego zak³adki ZAMÓWIENIA i FAKTURY po wziêciu programu do paska i ponownym pokazaniu
95            //rozjezdzaja sie...
96            zamowieniaSplitContainer.Panel2MinSize = 200;           
97            fakturySplitContainer.Panel1MinSize = 180;
98
99            /*
100            //pobiera listy agentów i agencji do wyszukiwania - toolstrip nie obsluguje databindings
101            command.CommandText = "select ID_AGENCJI,Symbol from dbo.AGENCJE where aktywna = 1";
102            command.Connection.Open();
103            SqlDataReader reader = command.ExecuteReader();
104            while (reader.Read())
105            {
106                agencjaToolStripComboBox.Items.Add(reader.GetValue(1).ToString());
107            }
108            command.Connection.Close();
109
110            command.CommandText = "select Symbol from dbo.AGENCI where aktywny=1 order by Symbol";
111            command.Connection.Open();
112            reader = command.ExecuteReader();
113            while (reader.Read())
114            {
115                agentToolStripComboBox.Items.Add(reader.GetValue(0).ToString());
116            //    userNameComboBox.Items.Add(reader.GetValue(0).ToString());
117            }
118            command.Connection.Close();
119            */
120
121            DBBindings.bindujAgencje(agencjaToolStripComboBox);
122            DBBindings.bindujAgentow(agentToolStripComboBox);
123            agencjaToolStripComboBox.SelectedIndexChanged += new EventHandler(agencjaToolStripComboBox_SelectedIndexChanged);
124 
125            this.aGENCITableAdapter.Fill(this.sLOWNIKDataSet.AGENCI);
126          //  if (User.getUser().St_kierownik)
127          //  {
128
129            if (!ConnString.getConnString().Value.Contains("truck"))
130            {
131                if (!User.getUser().St_kierownik)
132                {
133                    aGENCIBindingSource.Filter = "aktywny=1 AND id_agencji=" + User.getUser().IdAgencji;
134                }
135            }
136            else if (!User.getUser().St_kierownik)
137            {
138                aGENCIBindingSource.Filter = "aktywny=1 AND id_agencji=" + User.getUser().IdAgencji;
139            }
140         
141
142            //pobiera pierwsze wezly w drzewku z klasyfikacji
143            //klasyfikacjaWezlyGlowne();
144            DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);
145
146            SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter ta = new SLOWNIKDataSetTableAdapters.LISTA_TYTULOWTableAdapter();
147            ta.Connection.ConnectionString = ConnString.getConnString().Value;
148            tytulyListBox.DataSource = ta.GetData();
149            tytulyListBox.DisplayMember = "SYMB";
150            tytulyListBox.ValueMember = "SYMB";
151
152        }
153
154       private void clientsDataGridView_SelectionChanged(object sender, EventArgs e)
155        {
156            if (this.kLIENCIBindingSource.Current != null)
157            {
158                this.Cursor = Cursors.WaitCursor;
159
160                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
161                int customerId = Int32.Parse(row["CustomerId"].ToString());
162
163                switch (tabControl2.SelectedTab.Name)
164                {
165                    case "daneKlientaTab":
166                        groupBox1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
167                        groupBox2.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
168                        groupBox3.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
169                        groupBox4.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
170                        groupBox5.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
171                        tabControl1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
172                        this.kLIENCI_TYTULTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_TYTUL, customerId);
173                        break;
174                    case "zamowieniaTab":
175                        this.rEKLAMADataSet.REKLAMA.Clear();
176                        this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA, customerId);
177                        this.zamDataGridView.Sort(dATAZAMÓWIENIADataGridViewTextBoxColumn, ListSortDirection.Descending);
178                        this.zamDataGridView.Refresh();
179                        break;
180                    case "fakturyTab":
181                        this.rEKLAMADataSet.FAKTURY.Clear();
182                        this.zestawienieFakturTableAdapter.FillByCustomerID(this.rEKLAMADataSet.ZestawienieFaktur, customerId);
183                        this.fakturyDataGridView.Refresh();
184                        break;
185                    case "klasyfikacjaTabPage":
186                        this.rEKLAMADataSet.KL_KLIENCI.Clear();
187                        kL_KLIENCITableAdapter.ClearBeforeFill = true;
188                        kL_KLIENCITableAdapter.FillByCustomerId(rEKLAMADataSet.KL_KLIENCI, customerId);
189                        kL_1TableAdapter.Fill(sLOWNIKDataSet.KL_1);
190                        kL_2TableAdapter.Fill(sLOWNIKDataSet.KL_2);
191                        kL_3TableAdapter.Fill(sLOWNIKDataSet.KL_3);
192                        break;
193                    case "kontaktyTabPage":
194                        this.rEKLAMADataSet.KONTAKTY.Clear();
195                        this.kONTAKTYTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KONTAKTY, customerId);
196                        groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
197                        break;
198                    case "osobyDoKontaktuTabPage":
199                        this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.Clear();
200                        this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU, customerId);
201                        groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
202                        break;
203                    case "doZalatwienia":
204                        this.rEKLAMADataSet.sheduler.Clear();
205                        this.shedulerTableAdapter.FillByCustomerId(this.rEKLAMADataSet.sheduler, customerId);
206                        groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
207                        break;
208                    case "ofertyTabPage":
209                        this.rEKLAMADataSet.OFERTY_MAIN.Clear();
210                        this.oFERTY_MAINTableAdapter.FillByCustomerId(this.rEKLAMADataSet.OFERTY_MAIN, customerId);
211                        break;
212                    default:
213                        break;
214                }
215
216                if (((REKLAMADataSet.KLIENCIRow)row.Row).IsCountryNull())
217                {
218                    countryComboBox.SelectedIndex = -1;
219                }
220                               
221                this.Cursor = Cursors.Default;
222            }
223        }
224
225        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
226        {               
227            if (tabControl2.SelectedTab.Name != "daneKlientaTab")
228            {
229                dodajToolStripButton.Enabled = false;
230                zapiszDaneToolStripButton.Enabled = false;
231                usunToolStripButton.Enabled = false;
232            }
233            else {
234                dodajToolStripButton.Enabled = true;
235                zapiszDaneToolStripButton.Enabled = true;
236                usunToolStripButton.Enabled = true;
237            }
238
239            if (this.kLIENCIBindingSource.Current != null)
240            {
241                this.Cursor = Cursors.WaitCursor;
242
243                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
244                int customerId = Int32.Parse(row["CustomerId"].ToString());
245
246                switch (tabControl2.SelectedTab.Name)
247                {
248                    case "daneKlientaTab":
249                        groupBox1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
250                        groupBox2.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
251                        groupBox3.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
252                        groupBox4.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
253                        groupBox5.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
254                        tabControl1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
255                        this.kLIENCI_TYTULTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_TYTUL,customerId);       
256                        break;
257                    case "zamowieniaTab":
258                        this.rEKLAMADataSet.REKLAMA.Clear();
259                        this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA, customerId);
260                        this.zamDataGridView.Sort(dATAZAMÓWIENIADataGridViewTextBoxColumn, ListSortDirection.Descending);
261                        this.zamDataGridView.Refresh();
262                        break;
263                    case "fakturyTab":
264                        this.rEKLAMADataSet.FAKTURY.Clear();
265                        this.zestawienieFakturTableAdapter.FillByCustomerID(this.rEKLAMADataSet.ZestawienieFaktur, customerId);
266                        this.fakturyDataGridView.Refresh();
267                        break;
268                    case "klasyfikacjaTabPage":
269                        this.rEKLAMADataSet.KL_KLIENCI.Clear();
270                        kL_KLIENCITableAdapter.ClearBeforeFill = true;
271                        kL_KLIENCITableAdapter.FillByCustomerId(rEKLAMADataSet.KL_KLIENCI, customerId);
272                        kL_1TableAdapter.Fill(sLOWNIKDataSet.KL_1);
273                        kL_2TableAdapter.Fill(sLOWNIKDataSet.KL_2);
274                        kL_3TableAdapter.Fill(sLOWNIKDataSet.KL_3);
275                        break;                       
276                    case "kontaktyTabPage":
277                        this.rEKLAMADataSet.KONTAKTY.Clear();
278                        this.kONTAKTYTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KONTAKTY, customerId);
279                        groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
280                        break;
281                    case "osobyDoKontaktuTabPage":
282                        this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.Clear();
283                        this.kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU, customerId);
284                        groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
285                        break;
286                    case "doZalatwienia":
287                        this.rEKLAMADataSet.sheduler.Clear();
288                        this.shedulerTableAdapter.FillByCustomerId(this.rEKLAMADataSet.sheduler, customerId);
289                        groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
290                        break;
291                    case "ofertyTabPage":
292                        this.rEKLAMADataSet.OFERTY_MAIN.Clear();
293                        this.oFERTY_MAINTableAdapter.FillByCustomerId(this.rEKLAMADataSet.OFERTY_MAIN, customerId);
294                        break;                       
295                    default:
296                        break;
297                }
298                this.Cursor = Cursors.Default;
299            }
300
301        }
302
303        private void pokSzczegButton_Click(object sender, EventArgs e)
304        {
305            if (rEKLAMABindingSource.Current != null)
306            {               
307                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
308                int idRek = Int32.Parse(row["reklamaId"].ToString());
309                int custId = Int32.Parse(row["customerId"].ToString());
310
311                /*
312                OrderDetails od = new OrderDetails();
313                od.pokazSzczegolyZamowienia(idRek);
314                DialogResult result = od.ShowDialog();
315                */
316
317                OrderDetails.getOrderDetails().pokazSzczegolyZamowienia(idRek);
318
319                if (OrderDetails.getOrderDetails().ShowDialog() == DialogResult.OK)
320                {
321                    this.rEKLAMADataSet.REKLAMA.Clear();
322                    this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA, custId);
323                }
324            }
325        }
326
327        private void nowaButton_Click(object sender, EventArgs e)
328        {
329            if (kLIENCIBindingSource.Current != null)
330            {
331                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
332                int custId = Int32.Parse(row["CustomerId"].ToString());
333
334                OrderDetails.getOrderDetails().dodajNoweZamowienie(custId);
335
336                /*
337                OrderDetails od = new OrderDetails();
338                od.dodajNoweZamowienie(custId);
339                DialogResult result = od.ShowDialog();*/
340
341                if (OrderDetails.getOrderDetails().ShowDialog() == DialogResult.OK)
342                {
343                    this.rEKLAMADataSet.REKLAMA.Clear();
344                    this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA,custId);
345                }
346            }
347        }
348
349        private void usunButton_Click(object sender, EventArgs e)
350        {
351            if (rEKLAMABindingSource.Current != null)
352            {
353                bool ok = MessageBox.Show("Czyna pewno chcesz usun¹æ zlecenie?", "", MessageBoxButtons.YesNo) == DialogResult.Yes ? true : false;
354
355                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
356                if (row["id_faktury"] != DBNull.Value)
357                {
358                    MessageBox.Show("Wystawiono ju¿ fakturê.Nie mo¿na usun¹æ zlecenia.");
359                    return;
360                }
361
362                if (row["ZATWIERDZONO DO DRUKU"] != DBNull.Value)
363                {
364                    if (Convert.ToBoolean(row["ZATWIERDZONO DO DRUKU"]))
365                    {
366                        MessageBox.Show("Zamówienie zatwierdzone do druku.Nie mo¿na usun¹æ zlecenia.");
367                        return;
368                    }
369                }
370
371                if (row["ZAP£ACONO"] != DBNull.Value)
372                {
373                    if (Convert.ToBoolean(row["ZAP£ACONO"]))
374                    {
375                        MessageBox.Show("Zamówienie zap³acone.Nie mo¿na usun¹æ zlecenia.");
376                        return;
377                    }
378                }
379
380                if (ok)
381                {
382                    int idRek = Int32.Parse(row["reklamaId"].ToString());
383                    this.rEKLAMABindingSource.RemoveCurrent();
384                    this.rEKLAMATableAdapter.Update(this.rEKLAMADataSet.REKLAMA);
385
386                    SqlCommand command = new SqlCommand("DELETE FROM [UKA¯E SIÊ W NR] WHERE (ReklamaId = @rekID)");
387                    command.Parameters.AddWithValue("@rekID", idRek);
388                    command.Connection = new SqlConnection(ConnString.getConnString().Value);
389
390                    command.Connection.Open();
391                    try
392                    {
393                        command.ExecuteNonQuery();
394                    }
395                    catch (Exception e1)
396                    {
397                        MessageBox.Show(e1.Message);
398                    }
399                    command.Connection.Close();
400                }
401            }
402        }
403
404        private void wnowButton_Click(object sender, EventArgs e)
405        {           
406            if (rEKLAMABindingSource.Current != null)
407            {               
408                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
409                int idRek = Int32.Parse(row["reklamaId"].ToString());
410                int custId = Int32.Parse(row["customerId"].ToString());
411               
412                OrderDetails.getOrderDetails().wznowZamowienie(idRek);
413
414                /*
415                OrderDetails od = new OrderDetails();
416                od.wznowZamowienie(idRek);
417                DialogResult result = od.ShowDialog();*/
418
419                if (OrderDetails.getOrderDetails().ShowDialog() == DialogResult.OK)
420                {
421                    this.rEKLAMADataSet.REKLAMA.Clear();
422                    this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA, custId);
423                }
424            }           
425        }
426
427        private void usunToolStripButton_Click(object sender, EventArgs e)
428        {
429            if (kLIENCIBindingSource.Current != null)
430            {
431                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
432                int custId = Int32.Parse(row["CustomerId"].ToString());
433                             
434                if (kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.GetDataByCustomerId(custId).Count != 0
435                    || rEKLAMATableAdapter.GetDataByCustomerId(custId).Count != 0
436                    || kL_KLIENCITableAdapter.GetDataByCustomerId(custId).Count !=0)
437                {
438                    if (MessageBox.Show("S¹ inne dane zwi¹zane z klientem! Usunac klienta?", "", MessageBoxButtons.YesNo,
439                          MessageBoxIcon.Question) == DialogResult.Yes)
440                    {
441                        kLIENCIBindingSource.RemoveCurrent();
442                        kLIENCIBindingSource.EndEdit();
443                        this.kLIENCITableAdapter.Update(this.rEKLAMADataSet.KLIENCI);
444                        MessageBox.Show("Dane usuniêto");
445                    }
446                }
447                else {
448                    kLIENCIBindingSource.RemoveCurrent();
449                    kLIENCIBindingSource.EndEdit();
450                    this.kLIENCITableAdapter.Update(this.rEKLAMADataSet.KLIENCI);
451                    MessageBox.Show("Dane usuniêto");                   
452                }
453
454                groupBox1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
455                groupBox2.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
456                groupBox3.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
457                groupBox4.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
458                groupBox5.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
459                tabControl1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true; 
460            }
461        }
462
463        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
464        {
465            if (e.Node != null)
466            {
467                this.command.CommandText = query;
468                this.command.Parameters.Clear();
469
470                this.sqlDataAdapter.SelectCommand = command;
471
472                switch (e.Node.Level)
473                {
474                    case 0:
475                        switch (e.Node.Name)
476                        {
477                            case "MoiKlienci":
478                                this.Cursor = Cursors.WaitCursor;
479
480                                this.rEKLAMADataSet.KLIENCI.Clear();
481                                command.CommandText += " where K.UserName=@agent order by data desc";
482                                command.Parameters.AddWithValue("@agent", User.getUser().Symbol_agenta);
483                                sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
484                                clientsDataGridView.Refresh();
485
486                                this.Cursor = Cursors.Default;
487                                break;                         
488                            default:
489                                break;
490                        }
491                        break;
492                    case 1:
493                        switch (e.Node.Parent.Name)
494                        {
495                            case "MoiKlienci":
496                                switch (e.Node.Name)
497                                {
498                                    case "Aktywni":
499
500                                        this.Cursor = Cursors.WaitCursor;
501
502                                        this.rEKLAMADataSet.KLIENCI.Clear();
503                                        command.CommandText += " where K.UserName=@agent AND K.Aktywny=1 order by K.data desc";
504                                        command.Parameters.AddWithValue("@agent", User.getUser().Symbol_agenta);
505                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
506                                        clientsDataGridView.Refresh();
507
508                                        this.Cursor = Cursors.Default;
509                                        break;
510                                    case "Nieaktywni":
511
512                                        this.Cursor = Cursors.WaitCursor;
513
514                                        this.rEKLAMADataSet.KLIENCI.Clear();
515                                        command.CommandText += " where K.UserName=@agent AND K.Aktywny=0 order by K.data desc";
516                                        command.Parameters.AddWithValue("@agent", User.getUser().Symbol_agenta);
517                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
518                                        clientsDataGridView.Refresh();
519
520                                        this.Cursor = Cursors.Default;
521                                        break;
522                                    default:
523                                        break;
524                                }
525                                break;
526                            case "Biura":
527                                this.Cursor = Cursors.WaitCursor;
528
529                                this.rEKLAMADataSet.KLIENCI.Clear();
530                                command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol where A.Id_agencji = @idAgencji";
531                                command.Parameters.AddWithValue("@idAgencji", e.Node.Name);
532                                sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
533                                clientsDataGridView.Refresh();
534
535                                this.Cursor = Cursors.Default;
536                                break;
537                            case "Niesklasyfikowani":
538                                this.Cursor = Cursors.WaitCursor;
539
540                                this.rEKLAMADataSet.KLIENCI.Clear();
541                                command.CommandText += " left join KL_KLIENCI K2 on K.CustomerID = K2.Customerid  left join dbo.AGENCI A";
542                                command.CommandText += " on K.UserName = A.Symbol where K2.ID_KL_KLIENCI is null  AND A.Id_agencji = @idAgencji";
543                                command.Parameters.AddWithValue("@idAgencji", e.Node.Name);
544                                sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
545                                clientsDataGridView.Refresh();
546
547                                this.Cursor = Cursors.Default;
548                                break;
549                            case "NiesklasyfikowaniT":
550                                this.Cursor = Cursors.WaitCursor;
551
552                                this.rEKLAMADataSet.KLIENCI.Clear();
553                                command.CommandText += " left join KLIENCI_TYTUL KT on K.customerId = KT.customerId left join dbo.AGENCI A";
554                                command.CommandText += " on K.UserName = A.Symbol where K.aktywny=1 AND KT.id_klienci_tytul is null  AND A.Id_agencji = @idAgencji";
555                                command.Parameters.AddWithValue("@idAgencji", e.Node.Name);
556                                sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
557                                clientsDataGridView.Refresh();
558
559                                this.Cursor = Cursors.Default;
560                                break;
561                            case "Klasyfikacja":
562                                wyszukiwaniePoKlasyfikacji(e);
563                                break;
564                            default:
565                                break;
566                        }
567                        break;
568                    case 2:
569                        if (e.Node.Parent.Parent.Name == "Klasyfikacja")
570                        {
571                            wyszukiwaniePoKlasyfikacji(e);
572                        }
573                        else
574                        {
575                            switch (e.Node.Parent.Parent.Name)
576                            {
577                                case "Niesklasyfikowani":
578                                    this.Cursor = Cursors.WaitCursor;
579
580                                    this.rEKLAMADataSet.KLIENCI.Clear();
581                                    command.CommandText += " left join KL_KLIENCI K2 on K.CustomerID = K2.Customerid  left join dbo.AGENCI A";
582                                    command.CommandText += " on K.UserName = A.Symbol where K2.ID_KL_KLIENCI is null  AND A.Symbol = @symAgenta";
583                                    command.Parameters.AddWithValue("@symAgenta", e.Node.Name);
584                                    sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
585                                    clientsDataGridView.Refresh();
586
587                                    this.Cursor = Cursors.Default;
588                                    break;
589                                case "NiesklasyfikowaniT":
590                                    this.Cursor = Cursors.WaitCursor;
591
592                                    this.rEKLAMADataSet.KLIENCI.Clear();
593                                    command.CommandText += " left join KLIENCI_TYTUL KT on K.customerId = KT.customerId left join dbo.AGENCI A";
594                                    command.CommandText += " on K.UserName = A.Symbol where K.aktywny=1 AND KT.id_klienci_tytul is null  AND A.Symbol = @symAgenta";
595                                    command.Parameters.AddWithValue("@symAgenta", e.Node.Name);
596                                    sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
597                                    clientsDataGridView.Refresh();
598
599                                    this.Cursor = Cursors.Default;
600                                    break;
601                            }
602                        }
603                        break;
604                    case 3:
605                        if (e.Node.Parent.Parent.Parent.Name == "Klasyfikacja")
606                        {
607                            wyszukiwaniePoKlasyfikacji(e);
608                        }
609                        else
610                        {
611                            switch (e.Node.Parent.Name)
612                            {
613                                case "Agenci":
614                                    this.Cursor = Cursors.WaitCursor;
615
616                                    this.rEKLAMADataSet.KLIENCI.Clear();
617                                    command.CommandText += " where UserName=@agent";
618                                    command.Parameters.AddWithValue("@agent", e.Node.Name);
619                                    sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
620                                    clientsDataGridView.Refresh();
621
622                                    this.Cursor = Cursors.Default;
623                                    break;
624                                case "BrakKontaktu":
625                                    this.Cursor = Cursors.WaitCursor;
626
627                                    this.rEKLAMADataSet.KLIENCI.Clear();
628                                    command.CommandText += "left join dbo.KONTAKTY K2 on K.CustomerId = K2.CustomerID " +
629                                        "left join dbo.AGENCI A on K.UserName = A.Symbol " +
630                                        "group by K.CustomerID, K.FirstName, K.LastName, K.OrganizationName, " +
631                                        "K.Address, K.City, K.State, K.PostalCode, K.Country, K.Nip, K.ContactName, K.PhoneNumber, K.FaxNumber," +
632                                        "K.Note, K.data, K.Adres_Fkatura, K.Adres_Kor, K.Platnik_VAT, K.Aktywny,  " +
633                                        "K.Email, K.UserName, K.Last_Modify, K.Modify_User, K.http, K.VIES, K.regon, K.krs, K.osw_nr,K.osw_wazne_do,K.Old_ID, K.NipKraj, A.Id_agencji " +
634                                        "HAVING datediff(day,max(K2.data),getdate()) > @param1 AND datediff(day,max(K2.data),getdate()) <@param2 " +
635                                        "AND A.Id_agencji = @idAgencji order by datediff(day,max(K2.data),getdate()) desc";
636                                    command.Parameters.AddWithValue("@idAgencji", e.Node.Parent.Parent.Name);
637                                    command.Parameters.AddWithValue("@param1", e.Node.Name);
638
639                                    if (e.Node.NextNode != null)
640                                    {
641                                        command.Parameters.AddWithValue("@param2", e.Node.NextNode.Name);
642                                    }
643                                    else
644                                    {
645                                        command.Parameters.AddWithValue("@param2", "9999");
646                                    }
647
648                                    sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
649                                    clientsDataGridView.Refresh();
650
651                                    this.Cursor = Cursors.Default;
652                                    break;
653                                case "Klasyfikacja":
654                                    wyszukiwaniePoKlasyfikacjiWgAgencji(e, Int32.Parse(e.Node.Parent.Parent.Name));                   
655                                    break;
656                                default:
657                                    break;
658                            }
659                        }
660                        break;
661                    case 4:
662                       if (e.Node.Parent.Parent.Name == "Klasyfikacja")
663                        {
664                            wyszukiwaniePoKlasyfikacjiWgAgencji(e, Int32.Parse(e.Node.Parent.Parent.Parent.Name));
665                        }                     
666                        break;
667                    case 5:
668                        if (e.Node.Parent.Parent.Parent.Name == "Klasyfikacja")
669                        {
670                            wyszukiwaniePoKlasyfikacjiWgAgencji(e, Int32.Parse(e.Node.Parent.Parent.Parent.Parent.Name));
671                        }                     
672                        break;
673                    default:
674                        break;
675                }
676            }
677        }
678       
679        private void szukajToolStripButton_Click(object sender, EventArgs e)
680        {
681            this.Cursor = Cursors.WaitCursor;
682
683            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);     
684   
685            SqlCommand command = new SqlCommand();
686            command.CommandType = CommandType.Text;
687            command.Connection = conn;
688            command.CommandText = "select top 2000 K.* from klienci K where 1=1 ";
689            command.Parameters.Clear();
690           
691            if (agencjaToolStripComboBox.Text.Trim() != "")
692            {
693                command.CommandText = "select  top 2000 K.* from klienci K left join dbo.AGENCI A on K.UserName = A.Symbol left join " +
694                " dbo.AGENCJE A2 on A.ID_AGENCJI = A2.Id_agencji where 1=1 AND A2.Symbol=@agencja ";
695                command.Parameters.AddWithValue("@agencja", agencjaToolStripComboBox.Text.Trim());
696            }
697
698            if (nazwaToolStripTextBox.Text.Trim() != "")
699            {
700                command.CommandText = command.CommandText +
701                    "AND (K.[firstname] like '%' + @nazwa + '%' " +
702                    "OR K.[lastname] like '%' + @nazwa + '%' " +
703                    "OR K.[organizationname] like '%' + @nazwa + '%') ";
704                command.Parameters.AddWithValue("@nazwa", nazwaToolStripTextBox.Text.Trim());               
705            }
706
707            if (ulicaToolStripTextBox.Text.Trim() != "")
708            {
709                command.CommandText = command.CommandText + "AND K.[address] like '%' + @ulica + '%' ";
710                command.Parameters.AddWithValue("@ulica", ulicaToolStripTextBox.Text.Trim());
711            }
712
713            if (kodToolStripTextBox.Text.Trim() != "")
714            {
715                command.CommandText = command.CommandText + "AND K.[postalCode] like '%' + @kod + '%' ";
716                command.Parameters.AddWithValue("@kod", kodToolStripTextBox.Text.Trim());
717            }
718
719            if (miastoToolStripTextBox.Text.Trim() != "")
720            {
721                command.CommandText = command.CommandText + "AND K.[city] like '%' + @miasto + '%' ";
722                command.Parameters.AddWithValue("@miasto", miastoToolStripTextBox.Text.Trim());   
723            }
724
725            if (NIPStripTextBox.Text.Trim() != "")
726            {
727                command.CommandText = command.CommandText + "AND K.[Nip]=@nip ";
728                command.Parameters.AddWithValue("@nip", NIPStripTextBox.Text.Trim());
729            }
730
731            if (agentToolStripComboBox.Text.Trim() != "")
732            {
733                command.CommandText = command.CommandText + "AND K.[UserName]=@agent ";
734                command.Parameters.AddWithValue("@agent", agentToolStripComboBox.Text.Trim());
735            }
736
737            if (panstwoToolStripTextBox.Text.Trim() != "")
738            {
739                command.CommandText = command.CommandText + "AND K.[country] like '%' + @panstwo + '%' ";
740                command.Parameters.AddWithValue("@panstwo", panstwoToolStripTextBox.Text.Trim());
741            }
742
743            if (kodKlientaToolStripTextBox.Text.Trim() != "")
744            {
745                command.CommandText = command.CommandText + "AND K.[kodKlienta] like '%' + @kodKlienta + '%' ";
746                command.Parameters.AddWithValue("@kodKlienta", kodKlientaToolStripTextBox.Text.Trim());
747            }
748
749            sqlDataAdapter.SelectCommand = command;
750
751            this.rEKLAMADataSet.KLIENCI.Clear();
752         
753            try
754            {
755                sqlDataAdapter.Fill(this.rEKLAMADataSet.KLIENCI);
756            }
757            catch (Exception e1)
758            {
759                MessageBox.Show(e1.Message);
760            }
761
762            this.clientsDataGridView.Refresh();
763
764            treeView1.CollapseAll();
765            treeView1.SelectedNode = null;
766
767            this.Cursor = Cursors.Default;
768           
769        }
770
771        private void fakturaButton_Click(object sender, EventArgs e)
772        {
773            if (rEKLAMABindingSource.Current != null)
774            {
775                DataRowView row = (DataRowView)this.rEKLAMABindingSource.Current;
776                int idReklamy = Convert.ToInt32(row["reklamaId"]);
777                Facturer f = new Facturer(idReklamy);
778
779                if (f.ShowDialog() == DialogResult.OK) {
780                    this.rEKLAMADataSet.REKLAMA.Clear();
781                    DataRowView r = (DataRowView)this.kLIENCIBindingSource.Current;
782                    int customerId = Int32.Parse(r["CustomerId"].ToString());
783                    this.rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA, customerId);
784                    this.zamDataGridView.Sort(dATAZAMÓWIENIADataGridViewTextBoxColumn, ListSortDirection.Descending);
785                    this.zamDataGridView.Refresh();                                                         
786                }
787            }
788        }
789
790        private void klButton_Click(object sender, EventArgs e)
791        {
792            if (kLIENCIBindingSource.Current != null){
793               
794                if (this.kLKLIENCIBindingSource.Current != null)
795                {
796                    kLKLIENCIBindingSource.EndEdit();
797
798                    DataRowView row = (DataRowView)this.kLKLIENCIBindingSource.Current;
799                    row["username"] = User.getUser().Login;
800
801                    kL_KLIENCITableAdapter.Update(rEKLAMADataSet.KL_KLIENCI);
802                    MessageBox.Show("Dane zapisane");
803                }
804            }       
805        }
806
807        private void button2_Click(object sender, EventArgs e)
808        {
809            if (rEKLAMABindingSource.Current != null)
810            {
811                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
812                int idRek = Int32.Parse(row["reklamaId"].ToString());
813                ProjectForm pf = new ProjectForm(idRek);
814                pf.ShowDialog();
815            }                       
816        }
817
818        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
819        {
820            if (e.Node != null)
821            {
822                TreeNode node;
823
824                switch (e.Node.Level)
825                {
826                    case 0:
827                        switch (e.Node.Name)
828                        {
829                            case "MoiKlienci":
830                                e.Node.Nodes.Clear();
831
832                                node = new TreeNode("Aktywni");
833                                node.Name = "Aktywni";
834                                treeView1.Nodes["MoiKlienci"].Nodes.Add(node);
835
836                                node = new TreeNode("Nieaktywni");
837                                node.Name = "Nieaktywni";
838                                treeView1.Nodes["MoiKlienci"].Nodes.Add(node);
839
840                                break;
841                            case "Biura":
842                                DBBindings.dodajAgencjeDoWezla(e.Node);
843                                break;
844                            case "Klasyfikacja":
845                                DBBindings.dodajKlasyfikacjePoziom1(e.Node);
846                                break;
847                            case "Niesklasyfikowani":
848                                DBBindings.dodajAgencjeDoWezla(e.Node);
849                                break;
850                            case "NiesklasyfikowaniT":
851                                DBBindings.dodajAgencjeDoWezla(e.Node);
852                                break;
853                            default:
854                                break;
855                        }
856                        break;
857                    case 1:                       
858                        switch (e.Node.Parent.Name) {
859                            case "MoiKlienci":                                                           
860                                break;
861                            case "Biura":
862                                e.Node.Nodes.Clear();
863
864                                node = new TreeNode("Agenci");
865                                node.Name = "Agenci";
866                                e.Node.Nodes.Add(node);
867
868                                //dodaje agentow do podwezla Agenci
869                                DBBindings.dodajAgentowDoWezla(node, e.Node.Name);
870
871                                node = new TreeNode("Brak kontaktu");
872                                node.Name = "BrakKontaktu";
873                                e.Node.Nodes.Add(node);                             
874
875                                node = new TreeNode("90 dni");
876                                node.Name = "90";
877                                e.Node.Nodes["BrakKontaktu"].Nodes.Add(node);
878
879                                node = new TreeNode("300 dni");
880                                node.Name = "300";
881                                e.Node.Nodes["BrakKontaktu"].Nodes.Add(node);
882
883                                node = new TreeNode("Klasyfikacja");
884                                node.Name = "Klasyfikacja";
885                                DBBindings.dodajKlasyfikacjePoziom1(node);
886                                e.Node.Nodes.Add(node);                             
887                                break;
888                            case "Klasyfikacja":
889                                DBBindings.dodajKlasyfikacjePoziom2(e.Node);
890                                break;
891                            case "Niesklasyfikowani":
892                                DBBindings.dodajAgentowDoWezla(e.Node, e.Node.Name);
893                                break;
894                            case "NiesklasyfikowaniT":
895                                DBBindings.dodajAgentowDoWezla(e.Node, e.Node.Name);
896                                break;
897                            default:
898                                break;
899                        }
900                        break;
901                    case 2:
902                         switch (e.Node.Parent.Parent.Name) {
903                             case "Klasyfikacja":
904                                 DBBindings.dodajKlasyfikacjePoziom3(e.Node);
905                                 break;
906                             default:
907                                 break;
908                         }
909                        break;
910                    case 3:
911                        switch (e.Node.Parent.Name)
912                        {
913                            case "Klasyfikacja":
914                                DBBindings.dodajKlasyfikacjePoziom2(e.Node);
915                                break;
916                            default:
917                                break;
918                        }
919                        break;
920                    case 4:
921                        switch (e.Node.Parent.Parent.Name)
922                        {
923                            case "Klasyfikacja":
924                                DBBindings.dodajKlasyfikacjePoziom3(e.Node);
925                                break;
926                            default:
927                                break;
928                        }
929                        break;
930                    default:
931                        break;
932                }
933            }
934        }
935       
936        private void clientsDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
937        {
938            if (!dodajKlienta)
939            {
940                kLIENCIBindingSource.CancelEdit();
941            } else {
942                dodajKlienta = false;
943            }
944           
945        }
946
947        private void usunFakture_Click(object sender, EventArgs e)
948        {           
949            if (zestawienieFakturBindingSource.Current != null)
950            {
951                DataRowView row = (DataRowView)zestawienieFakturBindingSource.Current;
952
953                if (row["id_faktury"] != DBNull.Value)
954                {
955                    bool ok = MessageBox.Show("Czyna pewno chcesz usun¹æ fakturê?", "", MessageBoxButtons.YesNo) == DialogResult.Yes ? true : false;
956
957                    if (ok)
958                    {
959                        this.Cursor = Cursors.WaitCursor;
960
961                        SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
962
963                        int idFaktury = Convert.ToInt32(row["id_faktury"]);
964
965                        //usuwanie faktury
966                        SqlCommand command3 = new SqlCommand();
967                        command3.CommandText = "delete from faktury where id_faktury = @param ";
968                        command3.CommandType = CommandType.Text;
969                        command3.Parameters.AddWithValue("@param", idFaktury);
970
971                        command3.Connection = conn;
972
973                        conn.Open();
974                        command3.ExecuteNonQuery();
975                        conn.Close();
976                       
977                        //usuwanie pozycji faktury
978                        command3 = new SqlCommand();
979                        command3.CommandText = "delete from faktura_details where id_faktury = @param ";
980                        command3.CommandType = CommandType.Text;
981                        command3.Parameters.AddWithValue("@param", idFaktury);
982
983                        command3.Connection = conn;
984
985                        conn.Open();
986                        command3.ExecuteNonQuery();
987                        conn.Close();
988
989                        //usuniecie informacji o fakturze z tabeli REKLAMA
990                        command3.CommandText = "update REKLAMA set ID_FAKTURY = null where ID_FAKTURY = @param ";
991
992                        conn.Open();
993                        command3.ExecuteNonQuery();
994                        conn.Close();
995
996                        zestawienieFakturBindingSource.RemoveCurrent();
997
998                        this.Cursor = Cursors.Default;
999                    }
1000                }                               
1001            } 
1002        }
1003
1004        private void ukryjToolStripButton_Click(object sender, EventArgs e)
1005        {
1006            if (panel1.Visible)
1007            {
1008                panel1.Visible = false;
1009                ukryjToolStripButton.ToolTipText = "Poka¿ szczegó³y";
1010                dodajToolStripButton.Enabled = false;
1011                usunToolStripButton.Enabled = false;
1012                zapiszDaneToolStripButton.Enabled = false;
1013            }
1014            else
1015            {
1016                panel1.Visible = true;
1017                ukryjToolStripButton.ToolTipText = "Ukryj szczegó³y";
1018                dodajToolStripButton.Enabled = true;
1019                usunToolStripButton.Enabled = true;
1020                zapiszDaneToolStripButton.Enabled = true;
1021            }
1022        }
1023
1024        private void wyszukajToolStripButton_Click(object sender, EventArgs e)
1025        {
1026           szukajToolStrip.Visible = szukajToolStrip.Visible ? false : true;
1027        }
1028
1029        private void dodajToolStripButton_Click(object sender, EventArgs e)
1030        {
1031            dodajKlienta = true;
1032
1033            kLIENCIBindingSource.EndEdit();
1034
1035            // pyta o zACHOWANIE zmian przed zalozeniem kolejnego rekordu
1036            REKLAMADataSet.KLIENCIDataTable changes =
1037              this.rEKLAMADataSet.KLIENCI.GetChanges(
1038              DataRowState.Added | DataRowState.Modified)
1039              as REKLAMADataSet.KLIENCIDataTable;
1040
1041            if (changes != null)
1042            {
1043                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1044                    MessageBoxIcon.Question) == DialogResult.Yes)
1045                {
1046                    foreach (DataRow r in changes)
1047                    {
1048                        r["Last_Modify"] = DateTime.Now;
1049                        r["Modify_User"] = User.getUser().Login.ToString();
1050                    }
1051
1052                    this.kLIENCITableAdapter.Update(this.rEKLAMADataSet.KLIENCI);
1053                }
1054                else
1055                {
1056                    this.rEKLAMADataSet.KLIENCI.RejectChanges();
1057                    daneKlientaTab.Select();
1058                }
1059            }
1060                   
1061
1062            //dodaje nowy rekord
1063            DataView datatable = (DataView)this.kLIENCIBindingSource.List;
1064            DataRowView row = datatable.AddNew();
1065            row["Last_Modify"] = DateTime.Now;
1066            row["data"] = DateTime.Now;
1067            // nie dziala, bo trigger w bazie zmienia
1068            row["Modify_User"] = User.getUser().Login.ToString();
1069            row["UserName"] = User.getUser().Login.ToString();
1070
1071            kLIENCIBindingSource.MoveLast();
1072            kLIENCIBindingSource.EndEdit();
1073            tabControl2.SelectedIndex = 0;
1074            countryComboBox.SelectedIndex = -1;
1075
1076            groupBox1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
1077            groupBox2.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
1078            groupBox3.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
1079            groupBox4.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
1080            groupBox5.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true;
1081            tabControl1.Enabled = kLIENCIBindingSource.List.Count == 0 ? false : true; 
1082
1083        }
1084
1085        public void pokazKlienta(int customerId) {
1086            treeView1.SelectedNode = treeView1.Nodes["test"];
1087            this.rEKLAMADataSet.KLIENCI.Clear();
1088            this.kLIENCITableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI, customerId);
1089            this.clientsDataGridView.Refresh();           
1090        }
1091
1092        private void dodajKontaktToolStripButton_Click(object sender, EventArgs e)
1093        {
1094            if (kLIENCIBindingSource.Current != null) {
1095
1096                kONTAKTYBindingSource.EndEdit();
1097               
1098                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
1099                int custId = Int32.Parse(row["CustomerId"].ToString());
1100
1101                DataView datatable = (DataView)this.kONTAKTYBindingSource.List;
1102               
1103                DataRowView r = datatable.AddNew();
1104                r["customerId"] = custId;
1105                r["data"] = DateTime.Now;
1106                r["symbol_agenta"] = User.getUser().Login.ToString();
1107
1108                kONTAKTYBindingSource.MoveLast();
1109                kONTAKTYBindingSource.EndEdit();
1110
1111                groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
1112               
1113            }
1114        }
1115
1116
1117        private void usunKontaktToolStripButton_Click(object sender, EventArgs e)
1118        {
1119            if (this.kONTAKTYBindingSource.Current != null)
1120            {
1121                if (MessageBox.Show("Czy na pewno chcesz usun¹æ rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
1122                {
1123                    kONTAKTYBindingSource.RemoveCurrent();
1124                    kONTAKTYTableAdapter.Update(rEKLAMADataSet.KONTAKTY);
1125                    //  MessageBox.Show("Dane usuniête");
1126                    groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
1127
1128                }
1129            }
1130        }
1131
1132        private void wplataButton_Click(object sender, EventArgs e)
1133        {
1134            if (rEKLAMABindingSource.Current != null) {
1135                DataRowView row = (DataRowView)this.rEKLAMABindingSource.Current;
1136                int idReklamy = Convert.ToInt32(row["reklamaId"]);
1137                int custId = Convert.ToInt32(row["customerId"]);
1138                PaymentForm p = new PaymentForm(idReklamy);
1139                if (p.ShowDialog() == DialogResult.OK) {
1140                    rEKLAMATableAdapter.ClearBeforeFill = true;
1141                    rEKLAMATableAdapter.FillByCustomerId(this.rEKLAMADataSet.REKLAMA,custId);
1142                };
1143            }
1144        }
1145
1146        private void zamDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
1147        {     
1148            rEKLAMABindingSource.CancelEdit();
1149        }
1150
1151        private void fakturyDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
1152        {
1153            zestawienieFakturBindingSource.CancelEdit();
1154        }
1155
1156        private void pokFaktureButton_Click(object sender, EventArgs e)
1157        {
1158            if (rEKLAMABindingSource.Current != null)
1159            {               
1160                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1161
1162                if (row["id_faktury"] != DBNull.Value)
1163                {
1164                    this.Cursor = Cursors.WaitCursor;
1165
1166                    int idFaktury = Convert.ToInt32(row["id_faktury"]);
1167                    if (User.getUser().IdAgencji == 6)
1168                    {
1169                        FactureViewer fv = new FactureViewer(idFaktury);
1170                        fv.ShowDialog();
1171                    }
1172                    else
1173                    {
1174                        FactureViewer fv = new FactureViewer(idFaktury, true);
1175                        fv.ShowDialog();
1176                    }
1177
1178                    this.Cursor = Cursors.Default;
1179                }
1180                else {
1181                    MessageBox.Show("Nie wystawiono faktury dla zlecenia");
1182                }
1183            }
1184        }
1185
1186        private void pokazZamButton_Click(object sender, EventArgs e)
1187        {
1188            if (rEKLAMABindingSource.Current != null)
1189            {
1190                this.Cursor = Cursors.WaitCursor;
1191
1192                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1193                int idRek = Convert.ToInt32(row["reklamaId"]);
1194                OrderViewer ov = new OrderViewer(idRek);
1195
1196                this.Cursor = Cursors.Default;
1197
1198                ov.ShowDialog();
1199            }
1200        }
1201
1202        private void zapiszDaneToolStripButton_Click(object sender, EventArgs e)
1203        {
1204            //zmiana modify user i modify date
1205            if (this.kLIENCIBindingSource.Current != null)
1206            {
1207                kLIENCIBindingSource.EndEdit();
1208                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
1209                REKLAMADataSet.KLIENCIRow klient = (REKLAMADataSet.KLIENCIRow)row.Row;
1210               
1211                if (!Utils.kodKlientaUnikalny(kodKlientaTextBox.Text, userNameComboBox.Text,klient.CustomerID))
1212                {
1213                    MessageBox.Show(
1214                        String.Format("Dla agenta {0} istnieje klient o kodzie {1}.\nZmieñ kod klienta",
1215                        userNameComboBox.Text, kodKlientaTextBox.Text),"",MessageBoxButtons.OK,MessageBoxIcon.Stop);
1216                    return;
1217                }
1218
1219                kLIENCIBindingSource.EndEdit();
1220                row["Last_Modify"] = DateTime.Now;
1221
1222                Validate();
1223                kLIENCIBindingSource.EndEdit();
1224                kLIENCITableAdapter.Update(rEKLAMADataSet.KLIENCI);
1225               
1226                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1227                kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1228                MessageBox.Show("Dane zapisane");
1229            }
1230        }
1231
1232        private void zapiszKontaktToolStripButton_Click(object sender, EventArgs e)
1233        {
1234            kONTAKTYBindingSource.EndEdit();
1235            kONTAKTYTableAdapter.Update(rEKLAMADataSet.KONTAKTY);
1236            MessageBox.Show("Dane zapisane");               
1237        }
1238
1239        private void JPGbutton_Click(object sender, EventArgs e)
1240        {
1241            if (rEKLAMABindingSource.Current != null)
1242            {
1243                this.Cursor = Cursors.WaitCursor;
1244
1245                DataRowView row = (DataRowView) rEKLAMABindingSource.Current;
1246                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1247                string idReklamy = row["id reklamy"].ToString();
1248
1249                string tytul;
1250                int grzbiet;
1251                string miasto;
1252                byte staryNowyArchiwum;
1253               
1254                Produkcja.argumentyDoSciezki(reklamaId,idReklamy,out tytul, out grzbiet,out miasto,out staryNowyArchiwum);               
1255               
1256                string sciezka = Produkcja.GetReklamaFileName(idReklamy,tytul,grzbiet,miasto,staryNowyArchiwum,"jpg");
1257
1258                this.Cursor = Cursors.Default;
1259
1260                if (File.Exists(sciezka))
1261                {
1262                    System.Diagnostics.Process.Start(sciezka);
1263                }
1264                else
1265                {
1266                    MessageBox.Show("Nie znaleziono pliku:" + sciezka);
1267                }   
1268            }
1269
1270        }
1271
1272        private void EPSbutton_Click(object sender, EventArgs e)
1273        {
1274            if (rEKLAMABindingSource.Current != null)
1275            {
1276                this.Cursor = Cursors.WaitCursor;
1277
1278                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1279               
1280                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1281                string idReklamy = row["id reklamy"].ToString();
1282               
1283                string tytul;
1284                int grzbiet;
1285                string miasto;
1286                byte staryNowyArchiwum;
1287
1288                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);
1289
1290                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "eps");
1291             
1292                this.Cursor = Cursors.Default;
1293
1294                if (File.Exists(sciezka))
1295                {
1296                    System.Diagnostics.Process.Start(sciezka);
1297                }
1298                else
1299                {
1300                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
1301                }
1302
1303            }
1304        }
1305
1306
1307        private void PDFbutton_Click(object sender, EventArgs e)
1308        {
1309            if (rEKLAMABindingSource.Current != null)
1310            {
1311                this.Cursor = Cursors.WaitCursor;
1312
1313                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1314                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1315                string idReklamy = row["id reklamy"].ToString();
1316
1317                string tytul;
1318                int grzbiet;
1319                string miasto;
1320                byte staryNowyArchiwum;
1321
1322                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);
1323
1324                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "pdf");
1325
1326                this.Cursor = Cursors.Default;
1327
1328                if (File.Exists(sciezka))
1329                {
1330                    System.Diagnostics.Process.Start(sciezka);
1331                }
1332                else
1333                {
1334                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
1335                }
1336
1337            }
1338        }
1339
1340        private void clientsDataGridView_Leave(object sender, EventArgs e)
1341        {
1342            clientsDataGridView.EndEdit();
1343        }
1344
1345        private void kLIENCIBindingSource_CurrentChanged(object sender, EventArgs e)
1346        {
1347            wyczyscDaneKlienta();
1348
1349            if (kLIENCIBindingSource.Current != null) {
1350
1351                this.Cursor = Cursors.WaitCursor;
1352
1353                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1354                int custId = Convert.ToInt32(row["customerId"]);
1355                decimal zal = (decimal)zaleglosciKlienta(custId);
1356                zalegaTextBox.Text = String.Format("{0:C}", zal);
1357                if (zal > 0)
1358                {
1359                    zalegaTextBox.BackColor = Color.LightYellow;
1360                    zalegaTextBox.ForeColor = Color.Red;
1361                }
1362                else {
1363                    zalegaTextBox.BackColor = Color.White;
1364                    zalegaTextBox.ForeColor = Color.Black;
1365                }
1366
1367               agencjaLabel.Text = Utils.nazwaAgencji(row["username"].ToString());
1368
1369               userNameComboBox.Text = row["username"].ToString();
1370              // userNameComboBox.Enabled = User.getUser().St_kierownik & (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1371
1372               if (!ConnString.getConnString().Value.Contains("truck"))
1373               {
1374                       userNameComboBox.Enabled = (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1375               }
1376               else if (!User.getUser().St_kierownik)
1377               {
1378                   userNameComboBox.Enabled = (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1379               }
1380
1381               if (((REKLAMADataSet.KLIENCIRow)row.Row).IsCountryNull())
1382               {
1383                   countryComboBox.SelectedIndex = -1;
1384               }
1385               
1386               this.Cursor = Cursors.Default;
1387            }
1388        }
1389
1390        private void zamDataGridView_Leave(object sender, EventArgs e)
1391        {
1392            zamDataGridView.EndEdit();
1393        }
1394
1395        private void fakturyDataGridView_Leave(object sender, EventArgs e)
1396        {
1397            fakturyDataGridView.EndEdit();
1398        }
1399
1400        private void kryteriumWyszukiwania_KeyPress(object sender, KeyPressEventArgs e)
1401        {
1402            if (e.KeyChar == 13)
1403            {
1404                szukajToolStripButton.PerformClick();
1405            } 
1406        }
1407               
1408        private void wyczyscPolaToolStripButton_Click(object sender, EventArgs e)
1409        {
1410            rEKLAMADataSet.KLIENCI.Clear();
1411
1412            nazwaToolStripTextBox.Text = "";
1413            ulicaToolStripTextBox.Text = "";
1414            kodToolStripTextBox.Text = "";
1415            miastoToolStripTextBox.Text = "";
1416            NIPStripTextBox.Text = "";
1417            panstwoToolStripTextBox.Text = "";
1418
1419            agencjaToolStripComboBox.SelectedIndex = -1;
1420            agentToolStripComboBox.Text = "";
1421
1422            agentToolStripComboBox.SelectedIndex = -1;
1423            agentToolStripComboBox.Text = "";
1424        }
1425       
1426        private void zestawienieFakturBindingSource_ListChanged(object sender, ListChangedEventArgs e)
1427        {
1428            podsumujFaktury();
1429        }
1430
1431        private void zapiszOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1432        {           
1433            kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1434            kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1435            MessageBox.Show("Dane zapisane");
1436        }
1437
1438        private void dodajOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1439        {
1440            if (kLIENCIBindingSource.Current != null)
1441            {
1442                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1443
1444                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1445                int custId = Int32.Parse(row["CustomerId"].ToString());
1446
1447                DataView datatable = (DataView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List;
1448                DataRowView r = datatable.AddNew();
1449
1450                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.MoveLast();
1451                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1452
1453                groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
1454            }
1455        }
1456
1457        private void usunOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1458        {
1459            if (kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current != null)
1460            {
1461                //int id = Convert.ToInt32(((DataRowView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current)["id_klienci_os_kontakt"]);
1462                if (MessageBox.Show("Czy na pewno chcesz usun¹æ rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
1463                {
1464                    kLIENCI_OSOBY_DO_KONTAKTUBindingSource.RemoveCurrent();
1465                    kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1466                }
1467                 // MessageBox.Show("Dane usuniête");
1468
1469                groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
1470            }
1471        }
1472
1473        private void pokazFaktureButton_Click(object sender, EventArgs e)
1474        {
1475            if (zestawienieFakturBindingSource.Current != null)
1476            {
1477                DataRowView row = (DataRowView)zestawienieFakturBindingSource.Current;
1478
1479                if (row["id_faktury"] != DBNull.Value)
1480                {
1481                    int idFaktury = Convert.ToInt32(row["id_faktury"]);
1482
1483                    if (User.getUser().IdAgencji == 6)
1484                    {
1485                        FactureViewer fv = new FactureViewer(idFaktury);
1486                        fv.ShowDialog();
1487                    }
1488                    else
1489                    {
1490                        FactureViewer fv = new FactureViewer(idFaktury, true);
1491                        fv.ShowDialog();
1492                    }
1493                }
1494            }
1495        }
1496
1497        private void daneKlientaTab_Leave(object sender, EventArgs e)
1498        {
1499            this.kLIENCIBindingSource.EndEdit();
1500
1501            REKLAMADataSet.KLIENCIDataTable changes =
1502                this.rEKLAMADataSet.KLIENCI.GetChanges(
1503                DataRowState.Added | DataRowState.Modified)
1504                as REKLAMADataSet.KLIENCIDataTable;           
1505
1506            if (changes != null)
1507            {
1508                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1509                    MessageBoxIcon.Question) == DialogResult.Yes)
1510                {
1511                    foreach (DataRow r in changes)
1512                    {
1513                        r["Last_Modify"] = DateTime.Now;
1514                        r["Modify_User"] = User.getUser().Login.ToString();
1515                    }
1516
1517                    this.kLIENCITableAdapter.Update(this.rEKLAMADataSet.KLIENCI);
1518                }
1519                else
1520                {
1521                    this.rEKLAMADataSet.KLIENCI.RejectChanges();
1522                    daneKlientaTab.Select();
1523                }
1524            }         
1525        }
1526
1527        private void tabControl2_Leave(object sender, EventArgs e)
1528        {
1529            kLKLIENCIBindingSource.EndEdit();
1530
1531            REKLAMADataSet.KL_KLIENCIDataTable changes = rEKLAMADataSet.KL_KLIENCI.GetChanges(
1532              DataRowState.Added | DataRowState.Modified)
1533              as REKLAMADataSet.KL_KLIENCIDataTable;
1534
1535            if (changes != null)
1536            {
1537                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1538                    MessageBoxIcon.Question) == DialogResult.Yes)
1539                {
1540                    DataRowView row = (DataRowView)this.kLKLIENCIBindingSource.Current;
1541                    row["username"] = User.getUser().Login;
1542
1543                    kL_KLIENCITableAdapter.Update(rEKLAMADataSet.KL_KLIENCI);
1544                }
1545                else
1546                {
1547                    rEKLAMADataSet.KONTAKTY.RejectChanges();
1548                }
1549            }
1550        }
1551
1552        private void kontaktyTabPage_Leave(object sender, EventArgs e)
1553        {
1554            kONTAKTYBindingSource.EndEdit();
1555
1556            REKLAMADataSet.KONTAKTYDataTable changes =
1557                rEKLAMADataSet.KONTAKTY.GetChanges(DataRowState.Added | DataRowState.Modified)
1558              as REKLAMADataSet.KONTAKTYDataTable;
1559
1560            if (changes != null)
1561            {
1562                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1563                    MessageBoxIcon.Question) == DialogResult.Yes)
1564                {
1565                    kONTAKTYTableAdapter.Update(rEKLAMADataSet.KONTAKTY);
1566                }
1567                else
1568                {
1569                    rEKLAMADataSet.KONTAKTY.RejectChanges();
1570                }
1571            }
1572
1573        }
1574
1575        private void osobyDoKontaktuTabPage_Leave(object sender, EventArgs e)
1576        {
1577            kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1578
1579            REKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTUDataTable changes =
1580               rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.GetChanges(
1581               DataRowState.Added | DataRowState.Modified)
1582               as REKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTUDataTable;
1583
1584            if (changes != null)
1585            {
1586                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1587                    MessageBoxIcon.Question) == DialogResult.Yes)
1588                {                   
1589                    kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1590                }
1591                else
1592                {
1593                    rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.RejectChanges();
1594                }
1595            }
1596        }
1597
1598
1599        private void doZalatwienia_Leave(object sender, EventArgs e)
1600        {
1601            shedulerBindingSource.EndEdit();
1602
1603            REKLAMADataSet.shedulerDataTable changes = rEKLAMADataSet.sheduler.GetChanges(
1604                DataRowState.Added | DataRowState.Modified)
1605                as REKLAMADataSet.shedulerDataTable;
1606
1607            if (changes != null)
1608            {
1609                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1610                    MessageBoxIcon.Question) == DialogResult.Yes)
1611                {
1612                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);                   
1613                }
1614                else
1615                {
1616                    rEKLAMADataSet.sheduler.RejectChanges();
1617                }
1618            }
1619        }
1620
1621        private void klasyfikacjaTreeView_AfterExpand(object sender, TreeViewEventArgs e)
1622        {
1623            if (e.Node != null)
1624            {
1625                SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
1626                SqlCommand comand = new SqlCommand();
1627                comand.Connection = conn;
1628                comand.CommandType = CommandType.Text;
1629                SqlDataReader reader;
1630                TreeNode node;
1631
1632                switch (e.Node.Level)
1633                {
1634                    case 0:
1635                        e.Node.Nodes.Clear();
1636
1637                        comand.CommandText = "select KL_2,ID_KL_2 from dbo.KL_2 where ID_KL_1=@param  order by KL_2";
1638                        comand.Parameters.Clear();
1639                        comand.Parameters.AddWithValue("@param", e.Node.Name);
1640
1641                        conn.Open();
1642                        reader = comand.ExecuteReader();
1643                        while (reader.Read())
1644                        {
1645                            node = new TreeNode(reader.GetValue(0).ToString());
1646                            node.Name = reader.GetValue(1).ToString();
1647                            node.Nodes.Add(new TreeNode());
1648                            e.Node.Nodes.Add(node);
1649                        }
1650                        conn.Close();
1651                        break;
1652
1653                    case 1:
1654                        e.Node.Nodes.Clear();
1655
1656                        comand.CommandText = "select KL_3,ID_KL_3 from dbo.KL_3 where ID_KL_1=@param1 and ID_KL_2=@param2  order by KL_3";
1657                        comand.Parameters.Clear();
1658                        comand.Parameters.AddWithValue("@param1", e.Node.Parent.Name);
1659                        comand.Parameters.AddWithValue("@param2", e.Node.Name);
1660
1661                        conn.Open();
1662                        reader = comand.ExecuteReader();
1663                        while (reader.Read())
1664                        {
1665                            node = new TreeNode(reader.GetValue(0).ToString());
1666                            node.Name = reader.GetValue(1).ToString();
1667                            e.Node.Nodes.Add(node);
1668                        }
1669                        conn.Close();
1670
1671                        break;
1672                    default:
1673                        break;
1674                }
1675            }
1676        }
1677
1678        private void dodKlasKlientaButton_Click(object sender, EventArgs e)
1679        {
1680            if (kLIENCIBindingSource.Current == null)
1681            {
1682                return;
1683            }
1684
1685            if (klasyfikacjaTreeView.SelectedNode == null)
1686            {
1687                MessageBox.Show("Wybierz klasyfikacjê.");
1688                return;
1689            }
1690
1691            DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1692            int custId = Int32.Parse(row["CustomerId"].ToString());
1693
1694            if (klasyfikacjaTreeView.SelectedNode.Level != 2)
1695            {
1696                MessageBox.Show("Wybierz klasyfikacjê szczegó³ow¹.");
1697            }
1698            else
1699            {
1700                TreeNode n = klasyfikacjaTreeView.SelectedNode;
1701                int kl1 = Convert.ToInt32(n.Parent.Parent.Name);
1702                int kl2 = Convert.ToInt32(n.Parent.Name);
1703                int kl3 = Convert.ToInt32(n.Name);
1704
1705                kL_KLIENCITableAdapter.Insert(kl1, kl2, kl3, custId, User.getUser().Login, DateTime.Now);
1706                kL_KLIENCITableAdapter.FillByCustomerId(rEKLAMADataSet.KL_KLIENCI, custId);
1707            } 
1708
1709        }
1710
1711        private void usunKlasKlientaButton_Click(object sender, EventArgs e)
1712        {
1713            if (kLIENCIBindingSource.Current != null)
1714            {
1715                if (kLKLIENCIBindingSource.Current != null)
1716                {
1717                    kLKLIENCIBindingSource.RemoveCurrent();
1718                    kL_KLIENCITableAdapter.Update(rEKLAMADataSet.KL_KLIENCI);
1719                }
1720            }
1721        }
1722
1723        private void dodajKlButton_Click(object sender, EventArgs e)
1724        {
1725            if (klasyfikacjaTreeView.SelectedNode == null) {
1726                if (klTextBox.Text != "") {                   
1727                    kL_1TableAdapter.Insert(klTextBox.Text);
1728                    DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);
1729                    klTextBox.Clear();
1730                    klasyfikacjaTreeView.SelectedNode = null;
1731                    return;
1732                }               
1733            }
1734
1735            switch (klasyfikacjaTreeView.SelectedNode.Level)
1736            {
1737                case 0:
1738                    if (klTextBox.Text != "")
1739                    {
1740                        string node = klasyfikacjaTreeView.SelectedNode.Name;
1741                        kL_2TableAdapter.Insert(Convert.ToInt32(node), klTextBox.Text);
1742                        DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);
1743                        klasyfikacjaTreeView.Nodes[node].Expand();
1744                        klTextBox.Clear();
1745                    }             
1746                    break;
1747                case 1:
1748                    if (klTextBox.Text != "")
1749                    {
1750                        string parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1751                        string node = klasyfikacjaTreeView.SelectedNode.Name;
1752                        kL_3TableAdapter.Insert(Convert.ToInt32(parentNode), Convert.ToInt32(node), klTextBox.Text);                   
1753                        klasyfikacjaTreeView.Nodes[parentNode].Expand();
1754                        klasyfikacjaTreeView.Nodes[parentNode].Nodes[node].Collapse();
1755                        klasyfikacjaTreeView.Nodes[parentNode].Nodes[node].Expand();
1756                        klTextBox.Clear();
1757                    }       
1758                    break;
1759                case 2:
1760                    MessageBox.Show("Nie mo¿na dodac kolejnej podkategorii.");
1761                    break;
1762            }
1763            klasyfikacjaTreeView.SelectedNode = null;
1764        }
1765
1766        private void usunKlButton_Click(object sender, EventArgs e)
1767        {
1768            string parentParentNode;
1769            string parentNode;
1770
1771            if (klasyfikacjaTreeView.SelectedNode != null) {
1772                switch (klasyfikacjaTreeView.SelectedNode.Level) {
1773                    case 0:
1774                        klasyfikacjaTreeView.SelectedNode.Expand();
1775
1776                        if (klasyfikacjaTreeView.SelectedNode.Nodes.Count == 0)
1777                        {
1778                            kL_1TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1779
1780                            DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);                       
1781                        }
1782                        else
1783                        {
1784                            MessageBox.Show("Nie mo¿na usun¹æ kategorii, je¿eli istniej¹ podkategorie.");
1785                        }
1786                        break;
1787                    case 1:
1788                        klasyfikacjaTreeView.SelectedNode.Expand();
1789
1790                        if (klasyfikacjaTreeView.SelectedNode.Nodes.Count == 0)
1791                        {
1792                             parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1793
1794                            kL_2TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1795
1796                            klasyfikacjaTreeView.Nodes[parentNode].Collapse();
1797                            klasyfikacjaTreeView.Nodes[parentNode].Expand();
1798                        }
1799                        else {
1800                            MessageBox.Show("Nie mo¿na usun¹æ kategorii, je¿eli istniej¹ podkategorie.");
1801                        }
1802                        break;
1803                    case 2:
1804                        parentParentNode = klasyfikacjaTreeView.SelectedNode.Parent.Parent.Name;
1805                        parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1806
1807                        kL_3TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1808
1809                        klasyfikacjaTreeView.Nodes[parentParentNode].Nodes[parentNode].Collapse();
1810                        klasyfikacjaTreeView.Nodes[parentParentNode].Nodes[parentNode].Expand();
1811                        break;
1812                }
1813            }
1814            klasyfikacjaTreeView.SelectedNode = null;
1815        }
1816
1817        private void klasyfikacjaTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
1818        {
1819            klasyfikacjaTreeView.SelectedNode = null;
1820        }
1821
1822        private void klasyfikacjaDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
1823        {
1824            //MessageBox.Show("Wybrana klasyfikacja klienta nie istnieje.");
1825        }
1826       
1827        /*
1828        private void klasyfikacjaWezlyGlowne()
1829        {
1830            klasyfikacjaTreeView.Nodes.Clear();
1831
1832            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
1833            SqlCommand comand = new SqlCommand();
1834            comand.Connection = conn;
1835            comand.CommandType = CommandType.Text;
1836            comand.CommandText = "select KL_1,ID_KL_1 from dbo.KL_1  order by KL_1";
1837            conn.Open();
1838            TreeNode node;
1839            TreeNode node2;
1840            SqlDataReader reader = comand.ExecuteReader();
1841
1842            while (reader.Read())
1843            {
1844                node = new TreeNode(reader.GetValue(0).ToString());
1845                node.Name = reader.GetValue(1).ToString();
1846                node.Nodes.Add(new TreeNode());
1847                klasyfikacjaTreeView.Nodes.Add(node);
1848
1849                node2 = (TreeNode)node.Clone();
1850                treeView1.Nodes["Klasyfikacja"].Nodes.Add(node2);
1851            }
1852            conn.Close();
1853        }*/
1854
1855        private void generateNodes()
1856        {
1857            TreeNode node;
1858
1859            node = new TreeNode("***");
1860            node.Name = "test";
1861            treeView1.Nodes.Add(node);
1862
1863            if (User.getUser().St_handlowiec)
1864            {
1865                node = new TreeNode("Moi klienci");
1866                node.Name = "MoiKlienci";
1867                node.Nodes.Add(new TreeNode());
1868                treeView1.Nodes.Add(node);
1869            }
1870
1871            node = new TreeNode("Biura");
1872            node.Name = "Biura";
1873            node.Nodes.Add(new TreeNode());
1874            treeView1.Nodes.Add(node);
1875
1876            node = new TreeNode("Klasyfikacja");
1877            node.Name = "Klasyfikacja";
1878            node.Nodes.Add(new TreeNode());       
1879            treeView1.Nodes.Add(node);
1880
1881            node = new TreeNode("Niesklasyfikowani");
1882            node.Name = "Niesklasyfikowani";
1883            node.Nodes.Add(new TreeNode());
1884            treeView1.Nodes.Add(node);
1885
1886            node = new TreeNode("Niesklasyfikowani - tytu³y");
1887            node.Name = "NiesklasyfikowaniT";
1888            node.Nodes.Add(new TreeNode());
1889            treeView1.Nodes.Add(node);
1890        }
1891
1892        private void podpowiedzi() {
1893           
1894            ToolTip toolTip = new ToolTip();
1895
1896            toolTip.AutoPopDelay = 5000;
1897            toolTip.InitialDelay = 1000;
1898            toolTip.ReshowDelay = 500;
1899            toolTip.ShowAlways = true;
1900
1901            toolTip.SetToolTip(this.pokazZamButton, "Wydruk zamówienia");
1902            toolTip.SetToolTip(this.pokFaktureButton, "Wydruk faktury");
1903            toolTip.SetToolTip(this.dodKlasKlientaButton, "Dodaj klasyfikacjê klienta");
1904            toolTip.SetToolTip(this.usunKlasKlientaButton, "Usuñ klasyfikacjê klienta");
1905            toolTip.SetToolTip(this.usunKlButton, "Usuñ now¹ kategoriê");
1906            toolTip.SetToolTip(this.dodajKlButton, "Dodaj now¹ kategoriê");
1907            toolTip.SetToolTip(this.openWWWbutton, "Otwórz stronê");
1908        }
1909
1910        private void uprawnienia()
1911        {
1912            //usuwanie faktury
1913            usunFakture.Enabled = User.getUser().St_kierownik;
1914
1915            //dodawanie klasyfikacji
1916            klTextBox.Visible = User.getUser().St_kierownik;
1917            dodajKlButton.Visible = User.getUser().St_kierownik;
1918            usunKlButton.Visible = User.getUser().St_kierownik;
1919
1920            dodajLogoToolStripButton.Enabled = User.getUser().St_kierownik | User.getUser().St_produkcja;
1921        }
1922
1923        private void podsumujFaktury()
1924        {
1925            if (zestawienieFakturBindingSource.List.Count != 0)
1926            {
1927                decimal brutto = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(BRUTTO)", ""));
1928                decimal zaplata = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(suma_zaplat)", ""));
1929
1930                bruttoTextBox.Text = String.Format("{0:C}", brutto);
1931                zaplataTextBox.Text = String.Format("{0:C}", zaplata);
1932            }
1933            else
1934            {
1935                bruttoTextBox.Clear();
1936                zaplataTextBox.Clear();
1937            }
1938        }
1939
1940        private decimal zaleglosciKlienta(int customerId)
1941        {
1942            decimal zalega = 0;
1943
1944            SqlCommand command = new SqlCommand();
1945            command.Connection = new SqlConnection(ConnString.getConnString().Value);
1946            command.CommandText = "select zalega from dbo.view_klient_winien_ma_zalega where customerid=@custId";
1947            command.Parameters.AddWithValue("@custId", customerId);
1948
1949            command.Connection.Open();
1950            zalega = (decimal)(command.ExecuteScalar() ?? zalega);
1951            command.Connection.Close();
1952            return zalega;
1953        }
1954
1955        private void openWWWbutton_Click(object sender, EventArgs e)
1956        {
1957           string adres = httpTextBox.Text.Trim();
1958
1959           if (adres != "")
1960           {
1961               adres = adres.Contains("http://") ? adres : "http://" + adres;
1962
1963               if (Uri.IsWellFormedUriString(adres, UriKind.Absolute))
1964               {
1965                   System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
1966                   n.CreateNoWindow = false;
1967                   n.FileName = adres;
1968                   System.Diagnostics.Process.Start(n);
1969               }
1970               else
1971               {
1972                   MessageBox.Show("Podany adres jest niepoprawny");
1973               }
1974           }
1975        }
1976
1977        private void pokZam_Click(object sender, EventArgs e)
1978        {
1979            if (zestawienieFakturBindingSource.Current != null)
1980            {
1981                DataRowView row = (DataRowView)zestawienieFakturBindingSource.Current;
1982
1983                if (row["reklamaId"] != DBNull.Value)
1984                {
1985                    int idRek = Convert.ToInt32(row["reklamaId"]);
1986                   
1987                    OrderDetails.getOrderDetails().pokazSzczegolyZamowienia(idRek);
1988                    OrderDetails.getOrderDetails().ShowDialog();
1989
1990                    /*
1991                    OrderDetails od = new OrderDetails();
1992                    od.pokazSzczegolyZamowienia(idRek);
1993                    DialogResult result = od.ShowDialog();*/
1994                }
1995            }
1996        }
1997
1998        private void dodajSprDoZalatButton_Click(object sender, EventArgs e)
1999        {
2000            if (kLIENCIBindingSource.Current != null)
2001            {
2002                shedulerBindingSource.EndEdit();
2003
2004                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2005                int custId = Int32.Parse(row["CustomerId"].ToString());
2006
2007                DataView datatable = (DataView)this.shedulerBindingSource.List;
2008
2009                DataRowView r = datatable.AddNew();
2010                r["customerId"] = custId;
2011                r["CustomerName"] = row["FirstName"].ToString();
2012                r["Akwizytor"] = User.getUser().Login.ToString();
2013                r["DataWprowadzenia"] = DateTime.Now;
2014                r["DataPrzypomnienia"] = DateTime.Now.AddDays(7);
2015
2016                shedulerBindingSource.MoveLast();
2017                shedulerBindingSource.EndEdit();
2018
2019                groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
2020
2021            }
2022        }
2023
2024        private void usunSprDoZalatButton_Click(object sender, EventArgs e)
2025        {
2026            if (shedulerBindingSource.Current != null)
2027            {
2028                if (MessageBox.Show("Czy na pewno chcesz usun¹æ rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
2029                {
2030                    shedulerBindingSource.RemoveCurrent();
2031                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2032                    // MessageBox.Show("Dane usuniête");
2033                    groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
2034                }
2035            }
2036        }
2037
2038        private void zapiszSprDoZalatButton_Click(object sender, EventArgs e)
2039        {
2040            shedulerBindingSource.EndEdit();
2041            shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2042            MessageBox.Show("Dane zapisane");
2043        }
2044
2045        private void wplata2Button_Click(object sender, EventArgs e)
2046        {
2047            if (zestawienieFakturBindingSource.Current != null)
2048            {
2049                DataRowView row = (DataRowView)this.zestawienieFakturBindingSource.Current;
2050                int idReklamy = Convert.ToInt32(row["reklamaId"]);
2051                int custId = Convert.ToInt32(row["ID_NABYWCY"]);
2052                PaymentForm p = new PaymentForm(idReklamy);
2053                if (p.ShowDialog() == DialogResult.OK)
2054                {
2055                    zestawienieFakturTableAdapter.ClearBeforeFill = true;
2056                    zestawienieFakturTableAdapter.FillByCustomerID(this.rEKLAMADataSet.ZestawienieFaktur, custId);
2057                };
2058            }
2059        }
2060
2061        private void nowaOfertaButton_Click(object sender, EventArgs e)
2062        {
2063            if (kLIENCIBindingSource.Current != null)
2064            {
2065                this.Cursor = Cursors.WaitCursor;
2066
2067                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2068                int custId = Convert.ToInt32(row["customerId"]);
2069
2070                OferForm of = new OferForm(custId, 0, true);
2071
2072
2073                this.Cursor = Cursors.Default;
2074
2075                of.ShowDialog();
2076
2077                this.rEKLAMADataSet.OFERTY_MAIN.Clear();
2078                this.oFERTY_MAINTableAdapter.FillByCustomerId(this.rEKLAMADataSet.OFERTY_MAIN, custId);
2079            }                 
2080        }
2081
2082        private void edycjaOfertyButton_Click(object sender, EventArgs e)
2083        {
2084            if ((kLIENCIBindingSource.Current != null) & (oFERTY_MAINBindingSource.Current != null))
2085            {
2086                this.Cursor = Cursors.WaitCursor;
2087
2088                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2089                int custId = Convert.ToInt32(row["customerId"]);
2090
2091                DataRowView r = (DataRowView)oFERTY_MAINBindingSource.Current;
2092                int ofertaId = Convert.ToInt32(r["id_oferty"]);
2093
2094                OferForm of = new OferForm(custId, ofertaId, false);
2095
2096                this.Cursor = Cursors.Default;
2097               
2098                of.ShowDialog();
2099            }
2100        }
2101
2102        private void ClientsForm_Resize(object sender, EventArgs e)
2103        {
2104            if (this.MdiParent != null)
2105            {
2106                if (this.MdiParent.WindowState == FormWindowState.Maximized)
2107                {
2108                    double h = this.MdiParent.Height;
2109
2110                    int ph = (int)Math.Floor((65 * h) / 100);
2111
2112                    panel1.Height = ph;
2113
2114                }
2115                else
2116                {
2117                    splitContainer1.Height = splitConteiner1Height;
2118                    panel1.Height = Panel1Height;
2119                }
2120            }
2121        }
2122
2123        private void przeniesAdresButton_Click(object sender, EventArgs e)
2124        {
2125            string[] adres = new string[4];
2126
2127            adres[0] = firstNameTextBox.Text;
2128            adres[1] = addressTextBox.Text;
2129            adres[2] = postalCodeTextBox.Text + " " + cityTextBox.Text;
2130            adres[3] = stateComboBox.Text + " " + countryComboBox.Text;
2131
2132            adres_FkaturaTextBox.Lines = adres;
2133            adres_KorTextBox.Lines = adres;
2134        }
2135
2136        private void wyczyscDaneKlienta() {
2137            this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.Clear();
2138            this.rEKLAMADataSet.sheduler.Clear();
2139            this.rEKLAMADataSet.REKLAMA.Clear();
2140            this.rEKLAMADataSet.ZestawienieFaktur.Clear();
2141            this.rEKLAMADataSet.KL_KLIENCI.Clear();
2142            this.rEKLAMADataSet.KONTAKTY.Clear();
2143            this.rEKLAMADataSet.OFERTY_MAIN.Clear();
2144
2145            zalegaTextBox.Clear();
2146            agencjaLabel.Text = "";
2147        }
2148
2149        private void EmailButton_Click(object sender, EventArgs e)
2150        {
2151
2152            if ((kLIENCIBindingSource.Current != null) & (oFERTY_MAINBindingSource.Current != null))
2153            {
2154                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2155                int custId = Convert.ToInt32(row["customerId"]);
2156
2157                DataRowView r = (DataRowView)oFERTY_MAINBindingSource.Current;
2158                int ofertaId = Convert.ToInt32(r["id_oferty"]);
2159
2160                string temat = "Oferta nr: " + ofertaId + "/" + User.getUser().Kod_agenta.ToUpper() + "/" + DateTime.Today.Year;
2161
2162                string ofertaFile = @"g:\reklamafiles\oferty\" + r["id_oferty"].ToString() + ".doc";
2163
2164                if (File.Exists(ofertaFile))
2165                {
2166                    MailForm mf = new MailForm(custId, temat ,ofertaFile);
2167                    mf.ShowDialog();
2168                }
2169                else
2170                {
2171                    MessageBox.Show("Nie mo¿na odnaleŸæ oferty: " + ofertaFile);
2172                }
2173
2174            }         
2175        }
2176
2177        private void stateComboBox_Leave(object sender, EventArgs e)
2178        {
2179            kLIENCIBindingSource.EndEdit();
2180        }
2181
2182        private void countryComboBox_Leave(object sender, EventArgs e)
2183        {
2184            kLIENCIBindingSource.EndEdit();
2185        }
2186
2187        private void emailToolStripButton_Click(object sender, EventArgs e)
2188        {
2189            if (kLIENCIBindingSource.Current != null)
2190            {
2191                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2192                int custId = Convert.ToInt32(row["customerId"]);
2193
2194                MailForm mf = new MailForm(custId, "", "");
2195                mf.ShowDialog();   
2196            }         
2197        }
2198
2199        private void smsToolStripButton_Click(object sender, EventArgs e)
2200        {
2201            //format +48602315947
2202            if (kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current != null)
2203            {
2204                DataRowView row = (DataRowView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current;
2205                string nrTel = row["Tel"].ToString();
2206                int  customerId = Convert.ToInt32(row["customerId"]);
2207
2208                nrTel = nrTel.Replace("-", "");
2209                nrTel = nrTel.Replace(" ", "");
2210                nrTel = nrTel.Replace("+48", "");
2211
2212                if ((nrTel.Length == 10) & (nrTel[0] == Char.Parse("0")))
2213                {
2214                    nrTel = nrTel.Substring(1, 9);
2215                }
2216
2217                if (!Regex.IsMatch(nrTel, "^[1-9]{1}[0-9]{8}"))
2218                {
2219                    MessageBox.Show("Podany nr telefony jest z z³ym formacie. Prawid³owy nr musi byæ w formacie +48xxxxxxxxx lub 0xxxxxxxxx");
2220                }
2221                else
2222                {
2223                    SMSform sf = new SMSform("+48" + nrTel, customerId);
2224                    sf.ShowDialog();
2225                }
2226            }
2227        }
2228
2229        private void userNameComboBox_TextChanged(object sender, EventArgs e)
2230        {
2231            agencjaLabel.Text = Utils.nazwaAgencji(userNameComboBox.Text);
2232        }
2233
2234
2235        private void zrobioneToolStripButton_Click(object sender, EventArgs e)
2236        {
2237            if (shedulerBindingSource.Current != null)
2238            {
2239                DataRowView row = (DataRowView)shedulerBindingSource.Current;
2240                row["zalatwione"] = false;
2241                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2242                shedulerDataGridView.Refresh();
2243            }
2244        }
2245
2246        private void niezrobioneToolStripButton_Click(object sender, EventArgs e)
2247        {
2248            if (shedulerBindingSource.Current != null)
2249            {
2250                DataRowView row = (DataRowView)shedulerBindingSource.Current;
2251                row["zalatwione"] = true;
2252                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2253                shedulerDataGridView.Refresh();
2254            }
2255        }
2256
2257        private void ECARDbutton_Click(object sender, EventArgs e)
2258        {
2259            System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
2260            n.CreateNoWindow = false;
2261            n.FileName = "http://platnosci.admoto.pl/";
2262            System.Diagnostics.Process.Start(n);
2263        }
2264
2265        private void wyszukiwaniePoKlasyfikacji(TreeViewEventArgs e)
2266        {           
2267            SqlCommand commmand = new SqlCommand();
2268            command.Connection = new SqlConnection(ConnString.getConnString().Value);
2269
2270            SqlDataAdapter adapter = new SqlDataAdapter();
2271            adapter.SelectCommand = command;
2272
2273            switch (e.Node.Level)
2274            {
2275                case 1:
2276                    this.Cursor = Cursors.WaitCursor;
2277                    this.rEKLAMADataSet.KLIENCI.Clear();
2278                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2279                    command.CommandText += " K.CustomerID = K2.Customerid where K2.ID_KL_1=@idkl1 ";
2280                    command.Parameters.AddWithValue("@idkl1", e.Node.Name);
2281                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2282                    clientsDataGridView.Refresh();
2283                    this.Cursor = Cursors.Default;
2284                    break;
2285                case 2:
2286                    this.Cursor = Cursors.WaitCursor;
2287                    this.rEKLAMADataSet.KLIENCI.Clear();
2288                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2289                    command.CommandText += " K.CustomerID = K2.Customerid where K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2  ";
2290                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Name);
2291                    command.Parameters.AddWithValue("@idkl2", e.Node.Name);
2292                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2293                    clientsDataGridView.Refresh();
2294                    this.Cursor = Cursors.Default;
2295                    break;
2296                case 3:
2297                    this.Cursor = Cursors.WaitCursor;
2298                    this.rEKLAMADataSet.KLIENCI.Clear();
2299                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2300                    command.CommandText += " K.CustomerID = K2.Customerid where K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2 AND K2.ID_KL_3=@idkl3";
2301                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Parent.Name);
2302                    command.Parameters.AddWithValue("@idkl2", e.Node.Parent.Name);
2303                    command.Parameters.AddWithValue("@idkl3", e.Node.Name);
2304                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2305                    clientsDataGridView.Refresh();
2306                    this.Cursor = Cursors.Default;
2307                    break;
2308            }
2309        }
2310
2311        private void wyszukiwaniePoKlasyfikacjiWgAgencji(TreeViewEventArgs e, int idAgencji)
2312        {
2313            SqlCommand commmand = new SqlCommand();
2314            command.Connection = new SqlConnection(ConnString.getConnString().Value);
2315
2316            SqlDataAdapter adapter = new SqlDataAdapter();
2317            adapter.SelectCommand = command;
2318
2319            switch (e.Node.Level)
2320            {
2321                case 3:
2322                    this.Cursor = Cursors.WaitCursor;
2323                    this.rEKLAMADataSet.KLIENCI.Clear();
2324                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2325                    command.CommandText += " K.CustomerID = K2.Customerid ";
2326                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2327                    command.CommandText += " where A.Id_agencji = @idAgencji AND K2.ID_KL_1=@idkl1 ";
2328                    command.Parameters.AddWithValue("@idkl1", e.Node.Name);
2329                    command.Parameters.AddWithValue("@idAgencji", idAgencji);
2330                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2331                    clientsDataGridView.Refresh();
2332                    this.Cursor = Cursors.Default;
2333                    break;
2334                case 4:
2335                    this.Cursor = Cursors.WaitCursor;
2336                    this.rEKLAMADataSet.KLIENCI.Clear();
2337                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2338                    command.CommandText += " K.CustomerID = K2.Customerid ";
2339                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2340                    command.CommandText += " where A.Id_agencji = @idAgencji AND K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2 ";
2341                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Name);
2342                    command.Parameters.AddWithValue("@idkl2", e.Node.Name);
2343                    command.Parameters.AddWithValue("@idAgencji", idAgencji);                 
2344                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2345                    clientsDataGridView.Refresh();
2346                    this.Cursor = Cursors.Default;
2347                    break;
2348                case 5:
2349                    this.Cursor = Cursors.WaitCursor;
2350                    this.rEKLAMADataSet.KLIENCI.Clear();
2351                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2352                    command.CommandText += " K.CustomerID = K2.Customerid ";
2353                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2354                    command.CommandText += " where A.Id_agencji = @idAgencji";
2355                    command.CommandText += " and K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2 AND K2.ID_KL_3=@idkl3";
2356                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Parent.Name);
2357                    command.Parameters.AddWithValue("@idkl2", e.Node.Parent.Name);
2358                    command.Parameters.AddWithValue("@idkl3", e.Node.Name);
2359                    command.Parameters.AddWithValue("@idAgencji", idAgencji);                 
2360                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2361                    clientsDataGridView.Refresh();
2362                    this.Cursor = Cursors.Default;
2363                    break;
2364            }
2365        }
2366
2367        private void wydrukDanychKlientaButton_Click(object sender, EventArgs e)
2368        {
2369            if (kLIENCIBindingSource.Current != null)
2370            {
2371                this.Cursor = Cursors.WaitCursor;
2372
2373                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2374                int custId = Convert.ToInt32(row["customerId"]);
2375
2376                DaneKlientaViewer dnv = new DaneKlientaViewer(custId);
2377                dnv.ShowDialog();
2378
2379                this.Cursor = Cursors.Default;
2380            }
2381        }
2382
2383        private void excelToolStripButton_Click(object sender, EventArgs e)
2384        {
2385            this.Cursor = Cursors.WaitCursor;
2386
2387            ExcelHandler ex = new ExcelHandler();
2388            ex.exportToExcel(clientsDataGridView);
2389
2390            this.Cursor = Cursors.Default;
2391        }
2392
2393        private void dodajTytulButton_Click(object sender, EventArgs e)
2394        {
2395            if (kLIENCIBindingSource.Current == null)
2396            {
2397                return;
2398            }
2399
2400            if (tytulyListBox.SelectedValue == null)
2401            {
2402                MessageBox.Show("Wybierz tytu³.");
2403                return;
2404            }
2405
2406            DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2407            int custId = Int32.Parse(row["CustomerId"].ToString());
2408           
2409            this.kLIENCI_TYTULTableAdapter.Insert(tytulyListBox.SelectedValue.ToString(),custId, User.getUser().Login, DateTime.Now);         
2410            this.kLIENCI_TYTULTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_TYTUL,custId);         
2411           
2412        }
2413   
2414        private void usunTytulButton_Click(object sender, EventArgs e)
2415        {
2416              if (kLIENCITYTULBindingSource.Current != null)
2417            {
2418                if (kLIENCITYTULBindingSource.Current != null)
2419                {
2420                   this.kLIENCITYTULBindingSource.RemoveCurrent();
2421                   this.kLIENCI_TYTULTableAdapter.Update(rEKLAMADataSet.KLIENCI_TYTUL);
2422                }
2423            }
2424       
2425
2426        }
2427
2428        private void agencjaToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
2429        {
2430            if (((ComboBox)this.agencjaToolStripComboBox.Control).SelectedValue != null)
2431            {
2432                ((BindingSource)((ComboBox)this.agentToolStripComboBox.Control).DataSource).Filter = "ID_AGENCJI = " + ((ComboBox)this.agencjaToolStripComboBox.Control).SelectedValue.ToString();
2433                this.agentToolStripComboBox.SelectedIndex = -1;
2434                this.agentToolStripComboBox.SelectedIndex = -1;
2435            }
2436        }
2437
2438        private void wstecznyKontaktToolStripButton_Click(object sender, EventArgs e)
2439        {
2440            if (Utils.iloscKontaktowAgenta(User.getUser().Login) == 0)
2441            {
2442                if (kLIENCIBindingSource.Current != null)
2443                {
2444                    kONTAKTYBindingSource.EndEdit();
2445
2446                    DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2447                    int custId = Int32.Parse(row["CustomerId"].ToString());
2448
2449                    DataView datatable = (DataView)this.kONTAKTYBindingSource.List;
2450
2451                    DataRowView r = datatable.AddNew();
2452                    r["customerId"] = custId;
2453                    r["data"] = DateTime.Now.AddMonths(-1);
2454                    r["symbol_agenta"] = User.getUser().Login.ToString();
2455
2456                    kONTAKTYBindingSource.MoveLast();
2457                    kONTAKTYBindingSource.EndEdit();
2458
2459                    groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
2460                }
2461            }
2462            else
2463            {
2464                MessageBox.Show("Nie mo¿na dodaæ kontaktu");
2465            }
2466        }
2467
2468        private void dodajLogoToolStripButton_Click(object sender, EventArgs e)
2469        {
2470            if (kLIENCIBindingSource.Current != null)
2471            {
2472                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2473                int custId = Int32.Parse(row["CustomerId"].ToString());
2474
2475                LogaKlientowForm logaForm = new LogaKlientowForm(custId);
2476                logaForm.ShowDialog();               
2477            }
2478        }
2479
2480        private void toolStripButton1_Click(object sender, EventArgs e)
2481        {
2482            if (kLIENCIBindingSource.Current != null)
2483            {
2484                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2485                REKLAMADataSet.KLIENCIRow klient = (REKLAMADataSet.KLIENCIRow)row.Row;
2486             
2487                ZamowieniaForm zam = new ZamowieniaForm(klient);
2488                zam.ShowDialog();
2489            }
2490        }
2491     
2492       
2493        //dodanie CTR + A do textboxow
2494        /*
2495        private void test() {
2496            foreach (Control c in this.Controls) {
2497                if (c is TextBox)
2498                {
2499                    ((TextBox)c).KeyDown += new KeyEventHandler(obslugaCTR_A);
2500                }
2501            }
2502        }
2503
2504        private void obslugaCTR_A(object sender, System.Windows.Forms.KeyEventArgs e)
2505        {
2506            MessageBox.Show("d");
2507            if (e.Control && e.KeyCode == Keys.A)
2508            {
2509                ((TextBox)sender).SelectAll();
2510               
2511            }
2512        }*/
2513
2514   }
2515}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.