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

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