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

Wersja 12, 108.1 KB (wprowadzona przez dorota, 17 years temu)

kod klienta jako nickname przy dodawaniu nowej reklamy

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