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

Wersja 28, 107.6 KB (wprowadzona przez dorota, 17 years temu)

dodawanie wplat przy wystawianiu faktury

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
1162                    FactureViewer fv = new FactureViewer(idFaktury, true, 0);
1163                    fv.ShowDialog();
1164
1165                    this.Cursor = Cursors.Default;     
1166                }
1167                else {
1168                    MessageBox.Show("Nie wystawiono faktury dla zlecenia");
1169                }
1170            }
1171        }
1172
1173        private void pokazZamButton_Click(object sender, EventArgs e)
1174        {
1175            if (rEKLAMABindingSource.Current != null)
1176            {
1177                this.Cursor = Cursors.WaitCursor;
1178
1179                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1180                int idRek = Convert.ToInt32(row["reklamaId"]);
1181                OrderViewer ov = new OrderViewer(idRek);
1182
1183                this.Cursor = Cursors.Default;
1184
1185                ov.ShowDialog();
1186            }
1187        }
1188
1189        private void zapiszDaneToolStripButton_Click(object sender, EventArgs e)
1190        {
1191            //zmiana modify user i modify date
1192            if (this.kLIENCIBindingSource.Current != null)
1193            {
1194                kLIENCIBindingSource.EndEdit();
1195                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
1196                REKLAMADataSet.KLIENCIRow klient = (REKLAMADataSet.KLIENCIRow)row.Row;
1197               
1198                if (!Utils.kodKlientaUnikalny(kodKlientaTextBox.Text, userNameComboBox.Text,klient.CustomerID))
1199                {
1200                    MessageBox.Show(
1201                        String.Format("Dla agenta {0} istnieje klient o kodzie {1}.\nZmieñ kod klienta",
1202                        userNameComboBox.Text, kodKlientaTextBox.Text),"",MessageBoxButtons.OK,MessageBoxIcon.Stop);
1203                    return;
1204                }
1205
1206                kLIENCIBindingSource.EndEdit();
1207                row["Last_Modify"] = DateTime.Now;
1208
1209                Validate();
1210                kLIENCIBindingSource.EndEdit();
1211                kLIENCITableAdapter.Update(rEKLAMADataSet.KLIENCI);
1212               
1213                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1214                kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1215                MessageBox.Show("Dane zapisane");
1216            }
1217        }
1218
1219        private void zapiszKontaktToolStripButton_Click(object sender, EventArgs e)
1220        {
1221            kONTAKTYBindingSource.EndEdit();
1222            kONTAKTYTableAdapter.Update(rEKLAMADataSet.KONTAKTY);
1223            MessageBox.Show("Dane zapisane");               
1224        }
1225
1226        private void JPGbutton_Click(object sender, EventArgs e)
1227        {
1228            if (rEKLAMABindingSource.Current != null)
1229            {
1230                this.Cursor = Cursors.WaitCursor;
1231
1232                DataRowView row = (DataRowView) rEKLAMABindingSource.Current;
1233                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1234                string idReklamy = row["id reklamy"].ToString();
1235
1236                string tytul;
1237                int grzbiet;
1238                string miasto;
1239                byte staryNowyArchiwum;
1240               
1241                Produkcja.argumentyDoSciezki(reklamaId,idReklamy,out tytul, out grzbiet,out miasto,out staryNowyArchiwum);               
1242               
1243                string sciezka = Produkcja.GetReklamaFileName(idReklamy,tytul,grzbiet,miasto,staryNowyArchiwum,"jpg");
1244
1245                this.Cursor = Cursors.Default;
1246
1247                if (File.Exists(sciezka))
1248                {
1249                    System.Diagnostics.Process.Start(sciezka);
1250                }
1251                else
1252                {
1253                    MessageBox.Show("Nie znaleziono pliku:" + sciezka);
1254                }   
1255            }
1256
1257        }
1258
1259        private void EPSbutton_Click(object sender, EventArgs e)
1260        {
1261            if (rEKLAMABindingSource.Current != null)
1262            {
1263                this.Cursor = Cursors.WaitCursor;
1264
1265                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1266               
1267                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1268                string idReklamy = row["id reklamy"].ToString();
1269               
1270                string tytul;
1271                int grzbiet;
1272                string miasto;
1273                byte staryNowyArchiwum;
1274
1275                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);
1276
1277                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "eps");
1278             
1279                this.Cursor = Cursors.Default;
1280
1281                if (File.Exists(sciezka))
1282                {
1283                    System.Diagnostics.Process.Start(sciezka);
1284                }
1285                else
1286                {
1287                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
1288                }
1289
1290            }
1291        }
1292
1293
1294        private void PDFbutton_Click(object sender, EventArgs e)
1295        {
1296            if (rEKLAMABindingSource.Current != null)
1297            {
1298                this.Cursor = Cursors.WaitCursor;
1299
1300                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1301                int reklamaId = Convert.ToInt32(row["reklamaId"]);
1302                string idReklamy = row["id reklamy"].ToString();
1303
1304                string tytul;
1305                int grzbiet;
1306                string miasto;
1307                byte staryNowyArchiwum;
1308
1309                Produkcja.argumentyDoSciezki(reklamaId,idReklamy, out tytul, out grzbiet, out miasto, out staryNowyArchiwum);
1310
1311                string sciezka = Produkcja.GetReklamaFileName(idReklamy, tytul, grzbiet, miasto, staryNowyArchiwum, "pdf");
1312
1313                this.Cursor = Cursors.Default;
1314
1315                if (File.Exists(sciezka))
1316                {
1317                    System.Diagnostics.Process.Start(sciezka);
1318                }
1319                else
1320                {
1321                    MessageBox.Show("Nie znaleziono pliku: " + sciezka);
1322                }
1323
1324            }
1325        }
1326
1327        private void clientsDataGridView_Leave(object sender, EventArgs e)
1328        {
1329            clientsDataGridView.EndEdit();
1330        }
1331
1332        private void kLIENCIBindingSource_CurrentChanged(object sender, EventArgs e)
1333        {
1334            wyczyscDaneKlienta();
1335
1336            if (kLIENCIBindingSource.Current != null) {
1337
1338                this.Cursor = Cursors.WaitCursor;
1339
1340                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1341                int custId = Convert.ToInt32(row["customerId"]);
1342                decimal zal = (decimal)zaleglosciKlienta(custId);
1343                zalegaTextBox.Text = String.Format("{0:C}", zal);
1344                if (zal > 0)
1345                {
1346                    zalegaTextBox.BackColor = Color.LightYellow;
1347                    zalegaTextBox.ForeColor = Color.Red;
1348                }
1349                else {
1350                    zalegaTextBox.BackColor = Color.White;
1351                    zalegaTextBox.ForeColor = Color.Black;
1352                }
1353
1354               agencjaLabel.Text = Utils.nazwaAgencji(row["username"].ToString());
1355
1356               userNameComboBox.Text = row["username"].ToString();
1357              // userNameComboBox.Enabled = User.getUser().St_kierownik & (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1358
1359               if (!ConnString.getConnString().Value.Contains("truck"))
1360               {
1361                       userNameComboBox.Enabled = (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1362               }
1363               else if (!User.getUser().St_kierownik)
1364               {
1365                   userNameComboBox.Enabled = (Utils.idAgencji(row["username"].ToString()) == User.getUser().IdAgencji);
1366               }
1367
1368               if (((REKLAMADataSet.KLIENCIRow)row.Row).IsCountryNull())
1369               {
1370                   countryComboBox.SelectedIndex = -1;
1371               }
1372               
1373               this.Cursor = Cursors.Default;
1374            }
1375        }
1376
1377        private void zamDataGridView_Leave(object sender, EventArgs e)
1378        {
1379            zamDataGridView.EndEdit();
1380        }
1381
1382        private void fakturyDataGridView_Leave(object sender, EventArgs e)
1383        {
1384            fakturyDataGridView.EndEdit();
1385        }
1386
1387        private void kryteriumWyszukiwania_KeyPress(object sender, KeyPressEventArgs e)
1388        {
1389            if (e.KeyChar == 13)
1390            {
1391                szukajToolStripButton.PerformClick();
1392            } 
1393        }
1394               
1395        private void wyczyscPolaToolStripButton_Click(object sender, EventArgs e)
1396        {
1397            rEKLAMADataSet.KLIENCI.Clear();
1398
1399            nazwaToolStripTextBox.Text = "";
1400            ulicaToolStripTextBox.Text = "";
1401            kodToolStripTextBox.Text = "";
1402            miastoToolStripTextBox.Text = "";
1403            NIPStripTextBox.Text = "";
1404            panstwoToolStripTextBox.Text = "";
1405            kodToolStripTextBox.Text = string.Empty;
1406
1407            agencjaToolStripComboBox.SelectedIndex = -1;
1408            agentToolStripComboBox.Text = "";
1409
1410            agentToolStripComboBox.SelectedIndex = -1;
1411            agentToolStripComboBox.Text = "";
1412        }
1413       
1414        private void zestawienieFakturBindingSource_ListChanged(object sender, ListChangedEventArgs e)
1415        {
1416            podsumujFaktury();
1417        }
1418
1419        private void zapiszOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1420        {           
1421            kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1422            kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1423            MessageBox.Show("Dane zapisane");
1424        }
1425
1426        private void dodajOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1427        {
1428            if (kLIENCIBindingSource.Current != null)
1429            {
1430                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1431
1432                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1433                int custId = Int32.Parse(row["CustomerId"].ToString());
1434
1435                DataView datatable = (DataView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List;
1436                DataRowView r = datatable.AddNew();
1437
1438                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.MoveLast();
1439                kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1440
1441                groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
1442            }
1443        }
1444
1445        private void usunOsobeDoKontaktuToolStripButton_Click(object sender, EventArgs e)
1446        {
1447            if (kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current != null)
1448            {
1449                //int id = Convert.ToInt32(((DataRowView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current)["id_klienci_os_kontakt"]);
1450                if (MessageBox.Show("Czy na pewno chcesz usun¹æ rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
1451                {
1452                    kLIENCI_OSOBY_DO_KONTAKTUBindingSource.RemoveCurrent();
1453                    kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1454                }
1455                 // MessageBox.Show("Dane usuniête");
1456
1457                groupBox6.Enabled = kLIENCI_OSOBY_DO_KONTAKTUBindingSource.List.Count == 0 ? false : true;
1458            }
1459        }
1460
1461        private void pokazFaktureButton_Click(object sender, EventArgs e)
1462        {
1463            if (zestawienieFakturBindingSource.Current != null)
1464            {
1465                DataRowView row = (DataRowView)zestawienieFakturBindingSource.Current;
1466
1467                if (row["id_faktury"] != DBNull.Value)
1468                {
1469                    this.Cursor = Cursors.WaitCursor;
1470
1471                    int idFaktury = Convert.ToInt32(row["id_faktury"]);
1472
1473                    FactureViewer fv = new FactureViewer(idFaktury, true, 0);
1474                    fv.ShowDialog();
1475
1476                    this.Cursor = Cursors.Default;     
1477                }
1478            }
1479        }
1480
1481        private void daneKlientaTab_Leave(object sender, EventArgs e)
1482        {
1483            this.kLIENCIBindingSource.EndEdit();
1484
1485            REKLAMADataSet.KLIENCIDataTable changes =
1486                this.rEKLAMADataSet.KLIENCI.GetChanges(
1487                DataRowState.Added | DataRowState.Modified)
1488                as REKLAMADataSet.KLIENCIDataTable;           
1489
1490            if (changes != null)
1491            {
1492                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1493                    MessageBoxIcon.Question) == DialogResult.Yes)
1494                {
1495                    foreach (DataRow r in changes)
1496                    {
1497                        r["Last_Modify"] = DateTime.Now;
1498                        r["Modify_User"] = User.getUser().Login.ToString();
1499                    }
1500
1501                    this.kLIENCITableAdapter.Update(this.rEKLAMADataSet.KLIENCI);
1502                }
1503                else
1504                {
1505                    this.rEKLAMADataSet.KLIENCI.RejectChanges();
1506                    daneKlientaTab.Select();
1507                }
1508            }         
1509        }
1510
1511        private void tabControl2_Leave(object sender, EventArgs e)
1512        {
1513            kLKLIENCIBindingSource.EndEdit();
1514
1515            REKLAMADataSet.KL_KLIENCIDataTable changes = rEKLAMADataSet.KL_KLIENCI.GetChanges(
1516              DataRowState.Added | DataRowState.Modified)
1517              as REKLAMADataSet.KL_KLIENCIDataTable;
1518
1519            if (changes != null)
1520            {
1521                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1522                    MessageBoxIcon.Question) == DialogResult.Yes)
1523                {
1524                    DataRowView row = (DataRowView)this.kLKLIENCIBindingSource.Current;
1525                    row["username"] = User.getUser().Login;
1526
1527                    kL_KLIENCITableAdapter.Update(rEKLAMADataSet.KL_KLIENCI);
1528                }
1529                else
1530                {
1531                    rEKLAMADataSet.KONTAKTY.RejectChanges();
1532                }
1533            }
1534        }
1535
1536        private void kontaktyTabPage_Leave(object sender, EventArgs e)
1537        {
1538            kONTAKTYBindingSource.EndEdit();
1539
1540            REKLAMADataSet.KONTAKTYDataTable changes =
1541                rEKLAMADataSet.KONTAKTY.GetChanges(DataRowState.Added | DataRowState.Modified)
1542              as REKLAMADataSet.KONTAKTYDataTable;
1543
1544            if (changes != null)
1545            {
1546                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1547                    MessageBoxIcon.Question) == DialogResult.Yes)
1548                {
1549                    kONTAKTYTableAdapter.Update(rEKLAMADataSet.KONTAKTY);
1550                }
1551                else
1552                {
1553                    rEKLAMADataSet.KONTAKTY.RejectChanges();
1554                }
1555            }
1556
1557        }
1558
1559        private void osobyDoKontaktuTabPage_Leave(object sender, EventArgs e)
1560        {
1561            kLIENCI_OSOBY_DO_KONTAKTUBindingSource.EndEdit();
1562
1563            REKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTUDataTable changes =
1564               rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.GetChanges(
1565               DataRowState.Added | DataRowState.Modified)
1566               as REKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTUDataTable;
1567
1568            if (changes != null)
1569            {
1570                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1571                    MessageBoxIcon.Question) == DialogResult.Yes)
1572                {                   
1573                    kLIENCI_OSOBY_DO_KONTAKTUTableAdapter.Update(rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU);
1574                }
1575                else
1576                {
1577                    rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.RejectChanges();
1578                }
1579            }
1580        }
1581
1582
1583        private void doZalatwienia_Leave(object sender, EventArgs e)
1584        {
1585            shedulerBindingSource.EndEdit();
1586
1587            REKLAMADataSet.shedulerDataTable changes = rEKLAMADataSet.sheduler.GetChanges(
1588                DataRowState.Added | DataRowState.Modified)
1589                as REKLAMADataSet.shedulerDataTable;
1590
1591            if (changes != null)
1592            {
1593                if (MessageBox.Show("Czy zapisaæ zmiany?", "", MessageBoxButtons.YesNo,
1594                    MessageBoxIcon.Question) == DialogResult.Yes)
1595                {
1596                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);                   
1597                }
1598                else
1599                {
1600                    rEKLAMADataSet.sheduler.RejectChanges();
1601                }
1602            }
1603        }
1604
1605        private void klasyfikacjaTreeView_AfterExpand(object sender, TreeViewEventArgs e)
1606        {
1607            if (e.Node != null)
1608            {
1609                SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
1610                SqlCommand comand = new SqlCommand();
1611                comand.Connection = conn;
1612                comand.CommandType = CommandType.Text;
1613                SqlDataReader reader;
1614                TreeNode node;
1615
1616                switch (e.Node.Level)
1617                {
1618                    case 0:
1619                        e.Node.Nodes.Clear();
1620
1621                        comand.CommandText = "select KL_2,ID_KL_2 from dbo.KL_2 where ID_KL_1=@param  order by KL_2";
1622                        comand.Parameters.Clear();
1623                        comand.Parameters.AddWithValue("@param", e.Node.Name);
1624
1625                        conn.Open();
1626                        reader = comand.ExecuteReader();
1627                        while (reader.Read())
1628                        {
1629                            node = new TreeNode(reader.GetValue(0).ToString());
1630                            node.Name = reader.GetValue(1).ToString();
1631                            node.Nodes.Add(new TreeNode());
1632                            e.Node.Nodes.Add(node);
1633                        }
1634                        conn.Close();
1635                        break;
1636
1637                    case 1:
1638                        e.Node.Nodes.Clear();
1639
1640                        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";
1641                        comand.Parameters.Clear();
1642                        comand.Parameters.AddWithValue("@param1", e.Node.Parent.Name);
1643                        comand.Parameters.AddWithValue("@param2", e.Node.Name);
1644
1645                        conn.Open();
1646                        reader = comand.ExecuteReader();
1647                        while (reader.Read())
1648                        {
1649                            node = new TreeNode(reader.GetValue(0).ToString());
1650                            node.Name = reader.GetValue(1).ToString();
1651                            e.Node.Nodes.Add(node);
1652                        }
1653                        conn.Close();
1654
1655                        break;
1656                    default:
1657                        break;
1658                }
1659            }
1660        }
1661
1662        private void dodKlasKlientaButton_Click(object sender, EventArgs e)
1663        {
1664            if (kLIENCIBindingSource.Current == null)
1665            {
1666                return;
1667            }
1668
1669            if (klasyfikacjaTreeView.SelectedNode == null)
1670            {
1671                MessageBox.Show("Wybierz klasyfikacjê.");
1672                return;
1673            }
1674
1675            DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
1676            int custId = Int32.Parse(row["CustomerId"].ToString());
1677
1678            if (klasyfikacjaTreeView.SelectedNode.Level != 2)
1679            {
1680                MessageBox.Show("Wybierz klasyfikacjê szczegó³ow¹.");
1681            }
1682            else
1683            {
1684                TreeNode n = klasyfikacjaTreeView.SelectedNode;
1685                int kl1 = Convert.ToInt32(n.Parent.Parent.Name);
1686                int kl2 = Convert.ToInt32(n.Parent.Name);
1687                int kl3 = Convert.ToInt32(n.Name);
1688
1689                kL_KLIENCITableAdapter.Insert(kl1, kl2, kl3, custId, User.getUser().Login, DateTime.Now);
1690                kL_KLIENCITableAdapter.FillByCustomerId(rEKLAMADataSet.KL_KLIENCI, custId);
1691            } 
1692
1693        }
1694
1695        private void usunKlasKlientaButton_Click(object sender, EventArgs e)
1696        {
1697            if (kLIENCIBindingSource.Current != null)
1698            {
1699                if (kLKLIENCIBindingSource.Current != null)
1700                {
1701                    kLKLIENCIBindingSource.RemoveCurrent();
1702                    kL_KLIENCITableAdapter.Update(rEKLAMADataSet.KL_KLIENCI);
1703                }
1704            }
1705        }
1706
1707        private void dodajKlButton_Click(object sender, EventArgs e)
1708        {
1709            if (klasyfikacjaTreeView.SelectedNode == null) {
1710                if (klTextBox.Text != "") {                   
1711                    kL_1TableAdapter.Insert(klTextBox.Text);
1712                    DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);
1713                    klTextBox.Clear();
1714                    klasyfikacjaTreeView.SelectedNode = null;
1715                    return;
1716                }               
1717            }
1718
1719            switch (klasyfikacjaTreeView.SelectedNode.Level)
1720            {
1721                case 0:
1722                    if (klTextBox.Text != "")
1723                    {
1724                        string node = klasyfikacjaTreeView.SelectedNode.Name;
1725                        kL_2TableAdapter.Insert(Convert.ToInt32(node), klTextBox.Text);
1726                        DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);
1727                        klasyfikacjaTreeView.Nodes[node].Expand();
1728                        klTextBox.Clear();
1729                    }             
1730                    break;
1731                case 1:
1732                    if (klTextBox.Text != "")
1733                    {
1734                        string parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1735                        string node = klasyfikacjaTreeView.SelectedNode.Name;
1736                        kL_3TableAdapter.Insert(Convert.ToInt32(parentNode), Convert.ToInt32(node), klTextBox.Text);                   
1737                        klasyfikacjaTreeView.Nodes[parentNode].Expand();
1738                        klasyfikacjaTreeView.Nodes[parentNode].Nodes[node].Collapse();
1739                        klasyfikacjaTreeView.Nodes[parentNode].Nodes[node].Expand();
1740                        klTextBox.Clear();
1741                    }       
1742                    break;
1743                case 2:
1744                    MessageBox.Show("Nie mo¿na dodac kolejnej podkategorii.");
1745                    break;
1746            }
1747            klasyfikacjaTreeView.SelectedNode = null;
1748        }
1749
1750        private void usunKlButton_Click(object sender, EventArgs e)
1751        {
1752            string parentParentNode;
1753            string parentNode;
1754
1755            if (klasyfikacjaTreeView.SelectedNode != null) {
1756                switch (klasyfikacjaTreeView.SelectedNode.Level) {
1757                    case 0:
1758                        klasyfikacjaTreeView.SelectedNode.Expand();
1759
1760                        if (klasyfikacjaTreeView.SelectedNode.Nodes.Count == 0)
1761                        {
1762                            kL_1TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1763
1764                            DBBindings.dodajKlasyfikacjePoziom1(klasyfikacjaTreeView);                       
1765                        }
1766                        else
1767                        {
1768                            MessageBox.Show("Nie mo¿na usun¹æ kategorii, je¿eli istniej¹ podkategorie.");
1769                        }
1770                        break;
1771                    case 1:
1772                        klasyfikacjaTreeView.SelectedNode.Expand();
1773
1774                        if (klasyfikacjaTreeView.SelectedNode.Nodes.Count == 0)
1775                        {
1776                             parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1777
1778                            kL_2TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1779
1780                            klasyfikacjaTreeView.Nodes[parentNode].Collapse();
1781                            klasyfikacjaTreeView.Nodes[parentNode].Expand();
1782                        }
1783                        else {
1784                            MessageBox.Show("Nie mo¿na usun¹æ kategorii, je¿eli istniej¹ podkategorie.");
1785                        }
1786                        break;
1787                    case 2:
1788                        parentParentNode = klasyfikacjaTreeView.SelectedNode.Parent.Parent.Name;
1789                        parentNode = klasyfikacjaTreeView.SelectedNode.Parent.Name;
1790
1791                        kL_3TableAdapter.Delete(Convert.ToInt32(klasyfikacjaTreeView.SelectedNode.Name));
1792
1793                        klasyfikacjaTreeView.Nodes[parentParentNode].Nodes[parentNode].Collapse();
1794                        klasyfikacjaTreeView.Nodes[parentParentNode].Nodes[parentNode].Expand();
1795                        break;
1796                }
1797            }
1798            klasyfikacjaTreeView.SelectedNode = null;
1799        }
1800
1801        private void klasyfikacjaTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
1802        {
1803            klasyfikacjaTreeView.SelectedNode = null;
1804        }
1805
1806        private void klasyfikacjaDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
1807        {
1808            //MessageBox.Show("Wybrana klasyfikacja klienta nie istnieje.");
1809        }
1810       
1811        /*
1812        private void klasyfikacjaWezlyGlowne()
1813        {
1814            klasyfikacjaTreeView.Nodes.Clear();
1815
1816            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
1817            SqlCommand comand = new SqlCommand();
1818            comand.Connection = conn;
1819            comand.CommandType = CommandType.Text;
1820            comand.CommandText = "select KL_1,ID_KL_1 from dbo.KL_1  order by KL_1";
1821            conn.Open();
1822            TreeNode node;
1823            TreeNode node2;
1824            SqlDataReader reader = comand.ExecuteReader();
1825
1826            while (reader.Read())
1827            {
1828                node = new TreeNode(reader.GetValue(0).ToString());
1829                node.Name = reader.GetValue(1).ToString();
1830                node.Nodes.Add(new TreeNode());
1831                klasyfikacjaTreeView.Nodes.Add(node);
1832
1833                node2 = (TreeNode)node.Clone();
1834                treeView1.Nodes["Klasyfikacja"].Nodes.Add(node2);
1835            }
1836            conn.Close();
1837        }*/
1838
1839        private void generateNodes()
1840        {
1841            TreeNode node;
1842
1843            node = new TreeNode("***");
1844            node.Name = "test";
1845            treeView1.Nodes.Add(node);
1846
1847            if (User.getUser().St_handlowiec)
1848            {
1849                node = new TreeNode("Moi klienci");
1850                node.Name = "MoiKlienci";
1851                node.Nodes.Add(new TreeNode());
1852                treeView1.Nodes.Add(node);
1853            }
1854
1855            node = new TreeNode("Biura");
1856            node.Name = "Biura";
1857            node.Nodes.Add(new TreeNode());
1858            treeView1.Nodes.Add(node);
1859
1860            node = new TreeNode("Klasyfikacja");
1861            node.Name = "Klasyfikacja";
1862            node.Nodes.Add(new TreeNode());       
1863            treeView1.Nodes.Add(node);
1864
1865            node = new TreeNode("Niesklasyfikowani");
1866            node.Name = "Niesklasyfikowani";
1867            node.Nodes.Add(new TreeNode());
1868            treeView1.Nodes.Add(node);
1869
1870            node = new TreeNode("Niesklasyfikowani - tytu³y");
1871            node.Name = "NiesklasyfikowaniT";
1872            node.Nodes.Add(new TreeNode());
1873            treeView1.Nodes.Add(node);
1874        }
1875
1876        private void podpowiedzi() {
1877           
1878            ToolTip toolTip = new ToolTip();
1879
1880            toolTip.AutoPopDelay = 5000;
1881            toolTip.InitialDelay = 1000;
1882            toolTip.ReshowDelay = 500;
1883            toolTip.ShowAlways = true;
1884
1885            toolTip.SetToolTip(this.pokazZamButton, "Wydruk zamówienia");
1886            toolTip.SetToolTip(this.pokFaktureButton, "Wydruk faktury");
1887            toolTip.SetToolTip(this.dodKlasKlientaButton, "Dodaj klasyfikacjê klienta");
1888            toolTip.SetToolTip(this.usunKlasKlientaButton, "Usuñ klasyfikacjê klienta");
1889            toolTip.SetToolTip(this.usunKlButton, "Usuñ now¹ kategoriê");
1890            toolTip.SetToolTip(this.dodajKlButton, "Dodaj now¹ kategoriê");
1891            toolTip.SetToolTip(this.openWWWbutton, "Otwórz stronê");
1892        }
1893
1894        private void uprawnienia()
1895        {
1896            //usuwanie faktury
1897            usunFakture.Enabled = User.getUser().St_kierownik;
1898
1899            //dodawanie klasyfikacji
1900            klTextBox.Visible = User.getUser().St_kierownik;
1901            dodajKlButton.Visible = User.getUser().St_kierownik;
1902            usunKlButton.Visible = User.getUser().St_kierownik;
1903
1904            dodajLogoToolStripButton.Enabled = User.getUser().St_kierownik | User.getUser().St_produkcja;
1905        }
1906
1907        private void podsumujFaktury()
1908        {
1909            if (zestawienieFakturBindingSource.List.Count != 0)
1910            {
1911                decimal brutto = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(BRUTTO)", ""));
1912                decimal zaplata = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(suma_zaplat)", ""));
1913
1914                bruttoTextBox.Text = String.Format("{0:C}", brutto);
1915                zaplataTextBox.Text = String.Format("{0:C}", zaplata);
1916            }
1917            else
1918            {
1919                bruttoTextBox.Clear();
1920                zaplataTextBox.Clear();
1921            }
1922        }
1923
1924        private decimal zaleglosciKlienta(int customerId)
1925        {
1926            decimal zalega = 0;
1927
1928            SqlCommand command = new SqlCommand();
1929            command.Connection = new SqlConnection(ConnString.getConnString().Value);
1930            command.CommandText = "select zalega from dbo.view_klient_winien_ma_zalega where customerid=@custId";
1931            command.Parameters.AddWithValue("@custId", customerId);
1932
1933            command.Connection.Open();
1934            zalega = (decimal)(command.ExecuteScalar() ?? zalega);
1935            command.Connection.Close();
1936            return zalega;
1937        }
1938
1939        private void openWWWbutton_Click(object sender, EventArgs e)
1940        {
1941           string adres = httpTextBox.Text.Trim();
1942
1943           if (adres != "")
1944           {
1945               adres = adres.Contains("http://") ? adres : "http://" + adres;
1946
1947               if (Uri.IsWellFormedUriString(adres, UriKind.Absolute))
1948               {
1949                   System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
1950                   n.CreateNoWindow = false;
1951                   n.FileName = adres;
1952                   System.Diagnostics.Process.Start(n);
1953               }
1954               else
1955               {
1956                   MessageBox.Show("Podany adres jest niepoprawny");
1957               }
1958           }
1959        }
1960
1961        private void pokZam_Click(object sender, EventArgs e)
1962        {
1963            if (zestawienieFakturBindingSource.Current != null)
1964            {
1965                DataRowView row = (DataRowView)zestawienieFakturBindingSource.Current;
1966
1967                if (row["reklamaId"] != DBNull.Value)
1968                {
1969                    int idRek = Convert.ToInt32(row["reklamaId"]);
1970                   
1971                    OrderDetails.getOrderDetails().pokazSzczegolyZamowienia(idRek);
1972                    OrderDetails.getOrderDetails().ShowDialog();
1973
1974                    /*
1975                    OrderDetails od = new OrderDetails();
1976                    od.pokazSzczegolyZamowienia(idRek);
1977                    DialogResult result = od.ShowDialog();*/
1978                }
1979            }
1980        }
1981
1982        private void dodajSprDoZalatButton_Click(object sender, EventArgs e)
1983        {
1984            if (kLIENCIBindingSource.Current != null)
1985            {
1986                shedulerBindingSource.EndEdit();
1987
1988                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
1989                int custId = Int32.Parse(row["CustomerId"].ToString());
1990
1991                DataView datatable = (DataView)this.shedulerBindingSource.List;
1992
1993                DataRowView r = datatable.AddNew();
1994                r["customerId"] = custId;
1995                r["CustomerName"] = row["FirstName"].ToString();
1996                r["Akwizytor"] = User.getUser().Login.ToString();
1997                r["DataWprowadzenia"] = DateTime.Now;
1998                r["DataPrzypomnienia"] = DateTime.Now.AddDays(7);
1999
2000                shedulerBindingSource.MoveLast();
2001                shedulerBindingSource.EndEdit();
2002
2003                groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
2004
2005            }
2006        }
2007
2008        private void usunSprDoZalatButton_Click(object sender, EventArgs e)
2009        {
2010            if (shedulerBindingSource.Current != null)
2011            {
2012                if (MessageBox.Show("Czy na pewno chcesz usun¹æ rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
2013                {
2014                    shedulerBindingSource.RemoveCurrent();
2015                    shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2016                    // MessageBox.Show("Dane usuniête");
2017                    groupBox15.Enabled = shedulerBindingSource.List.Count == 0 ? false : true;
2018                }
2019            }
2020        }
2021
2022        private void zapiszSprDoZalatButton_Click(object sender, EventArgs e)
2023        {
2024            shedulerBindingSource.EndEdit();
2025            shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2026            MessageBox.Show("Dane zapisane");
2027        }
2028
2029        private void wplata2Button_Click(object sender, EventArgs e)
2030        {
2031            if (zestawienieFakturBindingSource.Current != null)
2032            {
2033                DataRowView row = (DataRowView)this.zestawienieFakturBindingSource.Current;
2034                int idReklamy = Convert.ToInt32(row["reklamaId"]);
2035                int custId = Convert.ToInt32(row["ID_NABYWCY"]);
2036                PaymentForm p = new PaymentForm(idReklamy);
2037                if (p.ShowDialog() == DialogResult.OK)
2038                {
2039                    zestawienieFakturTableAdapter.ClearBeforeFill = true;
2040                    zestawienieFakturTableAdapter.FillByCustomerID(this.rEKLAMADataSet.ZestawienieFaktur, custId);
2041                };
2042            }
2043        }
2044
2045        private void nowaOfertaButton_Click(object sender, EventArgs e)
2046        {
2047            if (kLIENCIBindingSource.Current != null)
2048            {
2049                this.Cursor = Cursors.WaitCursor;
2050
2051                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2052                int custId = Convert.ToInt32(row["customerId"]);
2053
2054                OferForm of = new OferForm(custId, 0, true);
2055
2056
2057                this.Cursor = Cursors.Default;
2058
2059                of.ShowDialog();
2060
2061                this.rEKLAMADataSet.OFERTY_MAIN.Clear();
2062                this.oFERTY_MAINTableAdapter.FillByCustomerId(this.rEKLAMADataSet.OFERTY_MAIN, custId);
2063            }                 
2064        }
2065
2066        private void edycjaOfertyButton_Click(object sender, EventArgs e)
2067        {
2068            if ((kLIENCIBindingSource.Current != null) & (oFERTY_MAINBindingSource.Current != null))
2069            {
2070                this.Cursor = Cursors.WaitCursor;
2071
2072                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2073                int custId = Convert.ToInt32(row["customerId"]);
2074
2075                DataRowView r = (DataRowView)oFERTY_MAINBindingSource.Current;
2076                int ofertaId = Convert.ToInt32(r["id_oferty"]);
2077
2078                OferForm of = new OferForm(custId, ofertaId, false);
2079
2080                this.Cursor = Cursors.Default;
2081               
2082                of.ShowDialog();
2083            }
2084        }
2085
2086        private void ClientsForm_Resize(object sender, EventArgs e)
2087        {
2088            if (this.MdiParent != null)
2089            {
2090                if (this.MdiParent.WindowState == FormWindowState.Maximized)
2091                {
2092                    double h = this.MdiParent.Height;
2093
2094                    int ph = (int)Math.Floor((65 * h) / 100);
2095
2096                    panel1.Height = ph;
2097
2098                }
2099                else
2100                {
2101                    splitContainer1.Height = splitConteiner1Height;
2102                    panel1.Height = Panel1Height;
2103                }
2104            }
2105        }
2106
2107        private void przeniesAdresButton_Click(object sender, EventArgs e)
2108        {
2109            string[] adres = new string[4];
2110
2111            adres[0] = firstNameTextBox.Text;
2112            adres[1] = addressTextBox.Text;
2113            adres[2] = postalCodeTextBox.Text + " " + cityTextBox.Text;
2114            adres[3] = stateComboBox.Text + " " + countryComboBox.Text;
2115
2116            adres_FkaturaTextBox.Lines = adres;
2117            adres_KorTextBox.Lines = adres;
2118        }
2119
2120        private void wyczyscDaneKlienta() {
2121            this.rEKLAMADataSet.KLIENCI_OSOBY_DO_KONTAKTU.Clear();
2122            this.rEKLAMADataSet.sheduler.Clear();
2123            this.rEKLAMADataSet.REKLAMA.Clear();
2124            this.rEKLAMADataSet.ZestawienieFaktur.Clear();
2125            this.rEKLAMADataSet.KL_KLIENCI.Clear();
2126            this.rEKLAMADataSet.KONTAKTY.Clear();
2127            this.rEKLAMADataSet.OFERTY_MAIN.Clear();
2128
2129            zalegaTextBox.Clear();
2130            agencjaLabel.Text = "";
2131        }
2132
2133        private void EmailButton_Click(object sender, EventArgs e)
2134        {
2135
2136            if ((kLIENCIBindingSource.Current != null) & (oFERTY_MAINBindingSource.Current != null))
2137            {
2138                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2139                int custId = Convert.ToInt32(row["customerId"]);
2140
2141                DataRowView r = (DataRowView)oFERTY_MAINBindingSource.Current;
2142                int ofertaId = Convert.ToInt32(r["id_oferty"]);
2143
2144                string temat = "Oferta nr: " + ofertaId + "/" + User.getUser().Kod_agenta.ToUpper() + "/" + DateTime.Today.Year;
2145
2146                string ofertaFile = @"g:\reklamafiles\oferty\" + r["id_oferty"].ToString() + ".doc";
2147
2148                if (File.Exists(ofertaFile))
2149                {
2150                    MailForm mf = new MailForm(custId, temat ,ofertaFile);
2151                    mf.ShowDialog();
2152                }
2153                else
2154                {
2155                    MessageBox.Show("Nie mo¿na odnaleŸæ oferty: " + ofertaFile);
2156                }
2157
2158            }         
2159        }
2160
2161        private void stateComboBox_Leave(object sender, EventArgs e)
2162        {
2163            kLIENCIBindingSource.EndEdit();
2164        }
2165
2166        private void countryComboBox_Leave(object sender, EventArgs e)
2167        {
2168            kLIENCIBindingSource.EndEdit();
2169        }
2170
2171        private void emailToolStripButton_Click(object sender, EventArgs e)
2172        {
2173            if (kLIENCIBindingSource.Current != null)
2174            {
2175                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2176                int custId = Convert.ToInt32(row["customerId"]);
2177
2178                MailForm mf = new MailForm(custId, "", "");
2179                mf.ShowDialog();   
2180            }         
2181        }
2182
2183        private void smsToolStripButton_Click(object sender, EventArgs e)
2184        {
2185            //format +48602315947
2186            if (kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current != null)
2187            {
2188                DataRowView row = (DataRowView)kLIENCI_OSOBY_DO_KONTAKTUBindingSource.Current;
2189                string nrTel = row["Tel"].ToString();
2190                int  customerId = Convert.ToInt32(row["customerId"]);
2191
2192                nrTel = nrTel.Replace("-", "");
2193                nrTel = nrTel.Replace(" ", "");
2194                nrTel = nrTel.Replace("+48", "");
2195
2196                if ((nrTel.Length == 10) & (nrTel[0] == Char.Parse("0")))
2197                {
2198                    nrTel = nrTel.Substring(1, 9);
2199                }
2200
2201                if (!Regex.IsMatch(nrTel, "^[1-9]{1}[0-9]{8}"))
2202                {
2203                    MessageBox.Show("Podany nr telefony jest z z³ym formacie. Prawid³owy nr musi byæ w formacie +48xxxxxxxxx lub 0xxxxxxxxx");
2204                }
2205                else
2206                {
2207                    SMSform sf = new SMSform("+48" + nrTel, customerId);
2208                    sf.ShowDialog();
2209                }
2210            }
2211        }
2212
2213        private void userNameComboBox_TextChanged(object sender, EventArgs e)
2214        {
2215            agencjaLabel.Text = Utils.nazwaAgencji(userNameComboBox.Text);
2216        }
2217
2218
2219        private void zrobioneToolStripButton_Click(object sender, EventArgs e)
2220        {
2221            if (shedulerBindingSource.Current != null)
2222            {
2223                DataRowView row = (DataRowView)shedulerBindingSource.Current;
2224                row["zalatwione"] = false;
2225                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2226                shedulerDataGridView.Refresh();
2227            }
2228        }
2229
2230        private void niezrobioneToolStripButton_Click(object sender, EventArgs e)
2231        {
2232            if (shedulerBindingSource.Current != null)
2233            {
2234                DataRowView row = (DataRowView)shedulerBindingSource.Current;
2235                row["zalatwione"] = true;
2236                shedulerTableAdapter.Update(rEKLAMADataSet.sheduler);
2237                shedulerDataGridView.Refresh();
2238            }
2239        }
2240
2241        private void ECARDbutton_Click(object sender, EventArgs e)
2242        {
2243            System.Diagnostics.ProcessStartInfo n = new System.Diagnostics.ProcessStartInfo();
2244            n.CreateNoWindow = false;
2245            n.FileName = "http://platnosci.admoto.pl/";
2246            System.Diagnostics.Process.Start(n);
2247        }
2248
2249        private void wyszukiwaniePoKlasyfikacji(TreeViewEventArgs e)
2250        {           
2251            SqlCommand commmand = new SqlCommand();
2252            command.Connection = new SqlConnection(ConnString.getConnString().Value);
2253
2254            SqlDataAdapter adapter = new SqlDataAdapter();
2255            adapter.SelectCommand = command;
2256
2257            switch (e.Node.Level)
2258            {
2259                case 1:
2260                    this.Cursor = Cursors.WaitCursor;
2261                    this.rEKLAMADataSet.KLIENCI.Clear();
2262                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2263                    command.CommandText += " K.CustomerID = K2.Customerid where K2.ID_KL_1=@idkl1 ";
2264                    command.Parameters.AddWithValue("@idkl1", e.Node.Name);
2265                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2266                    clientsDataGridView.Refresh();
2267                    this.Cursor = Cursors.Default;
2268                    break;
2269                case 2:
2270                    this.Cursor = Cursors.WaitCursor;
2271                    this.rEKLAMADataSet.KLIENCI.Clear();
2272                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2273                    command.CommandText += " K.CustomerID = K2.Customerid where K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2  ";
2274                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Name);
2275                    command.Parameters.AddWithValue("@idkl2", e.Node.Name);
2276                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2277                    clientsDataGridView.Refresh();
2278                    this.Cursor = Cursors.Default;
2279                    break;
2280                case 3:
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 AND K2.ID_KL_3=@idkl3";
2285                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Parent.Name);
2286                    command.Parameters.AddWithValue("@idkl2", e.Node.Parent.Name);
2287                    command.Parameters.AddWithValue("@idkl3", e.Node.Name);
2288                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2289                    clientsDataGridView.Refresh();
2290                    this.Cursor = Cursors.Default;
2291                    break;
2292            }
2293        }
2294
2295        private void wyszukiwaniePoKlasyfikacjiWgAgencji(TreeViewEventArgs e, int idAgencji)
2296        {
2297            SqlCommand commmand = new SqlCommand();
2298            command.Connection = new SqlConnection(ConnString.getConnString().Value);
2299
2300            SqlDataAdapter adapter = new SqlDataAdapter();
2301            adapter.SelectCommand = command;
2302
2303            switch (e.Node.Level)
2304            {
2305                case 3:
2306                    this.Cursor = Cursors.WaitCursor;
2307                    this.rEKLAMADataSet.KLIENCI.Clear();
2308                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2309                    command.CommandText += " K.CustomerID = K2.Customerid ";
2310                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2311                    command.CommandText += " where A.Id_agencji = @idAgencji AND K2.ID_KL_1=@idkl1 ";
2312                    command.Parameters.AddWithValue("@idkl1", e.Node.Name);
2313                    command.Parameters.AddWithValue("@idAgencji", idAgencji);
2314                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2315                    clientsDataGridView.Refresh();
2316                    this.Cursor = Cursors.Default;
2317                    break;
2318                case 4:
2319                    this.Cursor = Cursors.WaitCursor;
2320                    this.rEKLAMADataSet.KLIENCI.Clear();
2321                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2322                    command.CommandText += " K.CustomerID = K2.Customerid ";
2323                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2324                    command.CommandText += " where A.Id_agencji = @idAgencji AND K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2 ";
2325                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Name);
2326                    command.Parameters.AddWithValue("@idkl2", e.Node.Name);
2327                    command.Parameters.AddWithValue("@idAgencji", idAgencji);                 
2328                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2329                    clientsDataGridView.Refresh();
2330                    this.Cursor = Cursors.Default;
2331                    break;
2332                case 5:
2333                    this.Cursor = Cursors.WaitCursor;
2334                    this.rEKLAMADataSet.KLIENCI.Clear();
2335                    command.CommandText = "select distinct top 2000 K.* from KLIENCI K left join KL_KLIENCI K2 on ";
2336                    command.CommandText += " K.CustomerID = K2.Customerid ";
2337                    command.CommandText += " left join dbo.AGENCI A on K.UserName = A.Symbol ";
2338                    command.CommandText += " where A.Id_agencji = @idAgencji";
2339                    command.CommandText += " and K2.ID_KL_1=@idkl1 AND K2.ID_KL_2=@idkl2 AND K2.ID_KL_3=@idkl3";
2340                    command.Parameters.AddWithValue("@idkl1", e.Node.Parent.Parent.Name);
2341                    command.Parameters.AddWithValue("@idkl2", e.Node.Parent.Name);
2342                    command.Parameters.AddWithValue("@idkl3", e.Node.Name);
2343                    command.Parameters.AddWithValue("@idAgencji", idAgencji);                 
2344                    adapter.Fill(rEKLAMADataSet.KLIENCI);
2345                    clientsDataGridView.Refresh();
2346                    this.Cursor = Cursors.Default;
2347                    break;
2348            }
2349        }
2350
2351        private void wydrukDanychKlientaButton_Click(object sender, EventArgs e)
2352        {
2353            if (kLIENCIBindingSource.Current != null)
2354            {
2355                this.Cursor = Cursors.WaitCursor;
2356
2357                DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2358                int custId = Convert.ToInt32(row["customerId"]);
2359
2360                DaneKlientaViewer dnv = new DaneKlientaViewer(custId);
2361                dnv.ShowDialog();
2362
2363                this.Cursor = Cursors.Default;
2364            }
2365        }
2366
2367        private void excelToolStripButton_Click(object sender, EventArgs e)
2368        {
2369            this.Cursor = Cursors.WaitCursor;
2370
2371            ExcelHandler ex = new ExcelHandler();
2372            ex.exportToExcel(clientsDataGridView);
2373
2374            this.Cursor = Cursors.Default;
2375        }
2376
2377        private void dodajTytulButton_Click(object sender, EventArgs e)
2378        {
2379            if (kLIENCIBindingSource.Current == null)
2380            {
2381                return;
2382            }
2383
2384            if (tytulyListBox.SelectedValue == null)
2385            {
2386                MessageBox.Show("Wybierz tytu³.");
2387                return;
2388            }
2389
2390            DataRowView row = (DataRowView)kLIENCIBindingSource.Current;
2391            int custId = Int32.Parse(row["CustomerId"].ToString());
2392           
2393            this.kLIENCI_TYTULTableAdapter.Insert(tytulyListBox.SelectedValue.ToString(),custId, User.getUser().Login, DateTime.Now);         
2394            this.kLIENCI_TYTULTableAdapter.FillByCustomerId(this.rEKLAMADataSet.KLIENCI_TYTUL,custId);         
2395           
2396        }
2397   
2398        private void usunTytulButton_Click(object sender, EventArgs e)
2399        {
2400              if (kLIENCITYTULBindingSource.Current != null)
2401            {
2402                if (kLIENCITYTULBindingSource.Current != null)
2403                {
2404                   this.kLIENCITYTULBindingSource.RemoveCurrent();
2405                   this.kLIENCI_TYTULTableAdapter.Update(rEKLAMADataSet.KLIENCI_TYTUL);
2406                }
2407            }
2408       
2409
2410        }
2411
2412        private void agencjaToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
2413        {
2414            if (((ComboBox)this.agencjaToolStripComboBox.Control).SelectedValue != null)
2415            {
2416                ((BindingSource)((ComboBox)this.agentToolStripComboBox.Control).DataSource).Filter = "ID_AGENCJI = " + ((ComboBox)this.agencjaToolStripComboBox.Control).SelectedValue.ToString();
2417                this.agentToolStripComboBox.SelectedIndex = -1;
2418                this.agentToolStripComboBox.SelectedIndex = -1;
2419            }
2420        }
2421
2422        private void wstecznyKontaktToolStripButton_Click(object sender, EventArgs e)
2423        {
2424            if (Utils.iloscKontaktowAgenta(User.getUser().Login) == 0)
2425            {
2426                if (kLIENCIBindingSource.Current != null)
2427                {
2428                    kONTAKTYBindingSource.EndEdit();
2429
2430                    DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2431                    int custId = Int32.Parse(row["CustomerId"].ToString());
2432
2433                    DataView datatable = (DataView)this.kONTAKTYBindingSource.List;
2434
2435                    DataRowView r = datatable.AddNew();
2436                    r["customerId"] = custId;
2437                    r["data"] = DateTime.Now.AddMonths(-1);
2438                    r["symbol_agenta"] = User.getUser().Login.ToString();
2439
2440                    kONTAKTYBindingSource.MoveLast();
2441                    kONTAKTYBindingSource.EndEdit();
2442
2443                    groupBox7.Enabled = kONTAKTYBindingSource.List.Count == 0 ? false : true;
2444                }
2445            }
2446            else
2447            {
2448                MessageBox.Show("Nie mo¿na dodaæ kontaktu");
2449            }
2450        }
2451
2452        private void dodajLogoToolStripButton_Click(object sender, EventArgs e)
2453        {
2454            if (kLIENCIBindingSource.Current != null)
2455            {
2456                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2457                int custId = Int32.Parse(row["CustomerId"].ToString());
2458
2459                LogaKlientowForm logaForm = new LogaKlientowForm(custId);
2460                logaForm.ShowDialog();               
2461            }
2462        }
2463
2464        private void toolStripButton1_Click(object sender, EventArgs e)
2465        {
2466            if (kLIENCIBindingSource.Current != null)
2467            {
2468                DataRowView row = (DataRowView)this.kLIENCIBindingSource.Current;
2469                REKLAMADataSet.KLIENCIRow klient = (REKLAMADataSet.KLIENCIRow)row.Row;
2470             
2471                ZamowieniaForm zam = new ZamowieniaForm(klient);
2472                zam.ShowDialog();
2473            }
2474        }
2475
2476   
2477       
2478        //dodanie CTR + A do textboxow
2479        /*
2480        private void test() {
2481            foreach (Control c in this.Controls) {
2482                if (c is TextBox)
2483                {
2484                    ((TextBox)c).KeyDown += new KeyEventHandler(obslugaCTR_A);
2485                }
2486            }
2487        }
2488
2489        private void obslugaCTR_A(object sender, System.Windows.Forms.KeyEventArgs e)
2490        {
2491            MessageBox.Show("d");
2492            if (e.Control && e.KeyCode == Keys.A)
2493            {
2494                ((TextBox)sender).SelectAll();
2495               
2496            }
2497        }*/
2498
2499   }
2500}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.