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

Wersja 116, 108.0 KB (wprowadzona przez dorota, 17 years temu)

dodany rok i miesiac emisji w specyfikacjach reklam na fakturach
poprawiony blad z 0 w OrderDetails?
zablokowanie starych wydrukow przy wyswietlaniu nowych faktur

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