root/Baza Reklam 2 - Faktury/FacturesFormNEW.cs @ 10

Wersja 10, 23.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;
9
10namespace Baza_Reklam
11{
12    public partial class FacturesFormNEW : Form
13    {
14        string query = "SELECT top 2000 * from VIEW_ZESTAWIENIE_FAKTUR_NOWE ";
15
16        private static FacturesFormNEW fakturesForm;
17        private SqlCommand command;
18        private SqlDataAdapter sqlDataAdapter;
19
20        private bool commandExecuted = false;
21
22        public static FacturesFormNEW getFacturesForm(MDIBazaReklam parent)
23        {
24            if (fakturesForm == null){
25                fakturesForm = new FacturesFormNEW(parent);
26            }
27            return fakturesForm;
28        }
29
30        private FacturesFormNEW(MDIBazaReklam parent)
31        {
32            InitializeComponent();
33
34            this.MdiParent = parent;
35
36            //podmiana connstringa
37            zestawienieFakturNEWTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
38            reklamaTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
39         
40            //obiekty wykorzytywane przy wyszukiwaniu
41            SqlConnection conn = new SqlConnection(ConnString.getConnString().Value);
42           
43            command = new SqlCommand();
44            command.CommandType = CommandType.Text;
45            command.Connection = conn;
46           
47            this.sqlDataAdapter = new SqlDataAdapter();
48
49            //generuje piersze wêz³y w drzewie
50            generateNodes();
51
52            //ustawia domyslny rok i miesi¹c w pasku wyszukiwania
53            rokToolStripTextBox.Text = DateTime.Today.Year.ToString();
54            miesiacToolStripTextBox.Text = DateTime.Today.Month.ToString();
55
56            wyszukajToolStrip.Items.Insert(10, new ToolStripLabel("nieuregulowane:"));
57            wyszukajToolStrip.Items.Insert(11, new ToolStripControlHost(new CheckBox(), "zalegleCheckBox"));
58            ((CheckBox)((ToolStripControlHost)wyszukajToolStrip.Items["zalegleCheckBox"]).Control).ThreeState = true;
59            ((CheckBox)((ToolStripControlHost)wyszukajToolStrip.Items["zalegleCheckBox"]).Control).CheckState = CheckState.Indeterminate;           
60        }
61
62        private void FacturesForm_Load(object sender, EventArgs e)
63        {
64            this.WindowState = FormWindowState.Maximized;
65
66            DBBindings.bindujAgencje(agencjaToolStripComboBox);
67         
68            command.CommandText = this.query;
69            sqlDataAdapter.SelectCommand = command;
70        }
71
72        private void generateNodes()
73        {
74            TreeNode node;
75
76            node = new TreeNode("Wp³aty - ksiêgowoœæ");
77            node.Name = "WplatyKsiegowosc";
78            treeView1.Nodes.Add(node);
79
80            node = new TreeNode("Ró¿nice wp³at");
81            node.Nodes.Add(new TreeNode());
82            node.Name = "BrakWKsiegowosci";
83
84            treeView1.Nodes["WplatyKsiegowosc"].Nodes.Add(node);
85
86            node = new TreeNode("Brak w reklamie");
87            node.Nodes.Add(new TreeNode());
88            node.Name = "BrakWReklamie";
89
90            treeView1.Nodes["WplatyKsiegowosc"].Nodes.Add(node);
91                       
92            command.Connection.Close();                         
93        }
94
95        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
96        {
97            if (e.Node != null)
98            {
99                command.CommandText = query;
100                command.Parameters.Clear();
101
102                switch (e.Node.Level)
103                {
104                    case 0:
105                        break;
106                    case 1:
107                        switch (e.Node.Parent.Name)
108                        {
109                            case "WplatyKsiegowosc":
110                                break;
111                            default:
112                                break;
113                        }
114                        break;
115                    case 2:
116                        switch (e.Node.Parent.Parent.Name)
117                        {
118                            case "WplatyKsiegowosc":
119                                switch (e.Node.Parent.Name)
120                                {
121                                    case "BrakWKsiegowosci":
122                                        this.Cursor = Cursors.WaitCursor;
123
124                                        rEKLAMADataSet.ZestawienieFakturNEW.Clear();
125
126                                        command.CommandText += " WHERE YEAR(dataWplaty) = @rok AND (suma_zaplat is null OR (CONVERT(int, suma_zaplat * 100) <> CONVERT(int, BRUTTO * 100))) order by dataWplaty";
127
128                                        command.Parameters.AddWithValue("@rok", e.Node.Name);
129                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.ZestawienieFakturNEW);
130                                        commandExecuted = true;
131                                        fakturyDataGridView.Refresh();
132
133                                        this.Cursor = Cursors.Default;
134
135                                        break;
136
137                                    case "BrakWReklamie":
138                                        this.Cursor = Cursors.WaitCursor;
139
140                                        rEKLAMADataSet.ZestawienieFakturNEW.Clear();
141                                        command.CommandText += " WHERE YEAR(DATA_SPRZEDAZY) = @rok AND (suma_zaplat is not null AND suma_zaplat<>0) AND (dataWplaty is null) order by DATA_SPRZEDAZY";
142                                        command.Parameters.AddWithValue("@rok", e.Node.Name);
143                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.ZestawienieFakturNEW);
144                                        commandExecuted = true;
145                                        fakturyDataGridView.Refresh();
146
147                                        this.Cursor = Cursors.Default;
148
149                                        break;
150                                    default:
151                                        break;
152                                }
153                                break;
154                            default:
155                                break;
156                        }
157                        break;
158                    case 3:
159                        switch (e.Node.Parent.Parent.Parent.Name)
160                        {
161                            case "WplatyKsiegowosc":
162                                switch (e.Node.Parent.Parent.Name)
163                                {
164                                    case "BrakWKsiegowosci":
165                                        this.Cursor = Cursors.WaitCursor;
166
167                                        rEKLAMADataSet.ZestawienieFakturNEW.Clear();
168                                        command.CommandText += " WHERE YEAR([dataWplaty]) = @rok AND MONTH(dataWplaty) = @miesiac AND (suma_zaplat is null OR (CONVERT(int, suma_zaplat * 100) <> CONVERT(int, BRUTTO * 100))) order by dataWplaty";
169                                        command.Parameters.AddWithValue("@rok", e.Node.Parent.Name);
170                                        command.Parameters.AddWithValue("@miesiac", e.Node.Name);
171                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.ZestawienieFakturNEW);
172                                        commandExecuted = true;
173                                        fakturyDataGridView.Refresh();
174
175                                        this.Cursor = Cursors.Default;
176                                        break;
177
178                                    case "BrakWReklamie":
179                                        this.Cursor = Cursors.WaitCursor;
180
181                                        rEKLAMADataSet.ZestawienieFakturNEW.Clear();
182                                        command.CommandText += " WHERE YEAR(DATA_SPRZEDAZY) = @rok AND MONTH(DATA_SPRZEDAZY) = @miesiac AND (suma_zaplat is not null AND suma_zaplat<>0) AND (dataWplaty is null) order by DATA_SPRZEDAZY";
183                                        command.Parameters.AddWithValue("@rok", e.Node.Parent.Name);
184                                        command.Parameters.AddWithValue("@miesiac", e.Node.Name);
185                                        sqlDataAdapter.Fill(this.rEKLAMADataSet.ZestawienieFakturNEW);
186                                        commandExecuted = true;
187                                        fakturyDataGridView.Refresh();
188
189                                        this.Cursor = Cursors.Default;
190                                        break;
191                                }
192                                break;
193                            default:
194                                break;
195                        }
196                        break;
197                    default:
198                        break;
199                }
200
201            }
202        }
203
204        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
205        {
206            TreeNode node;
207            SqlDataReader reader;
208
209            if (e.Node != null)
210            {
211               switch (e.Node.Level)
212                {
213                    case 0:                       
214                        break;
215                    case 1:
216                        switch (e.Node.Parent.Name)
217                        {
218                            case "WplatyKsiegowosc":
219                                switch (e.Node.Name)
220                                {
221                                    case "BrakWKsiegowosci":
222                                        e.Node.Nodes.Clear();
223
224                                        command.CommandText = "select distinct datepart(year,DATA_W) as rok from dbo.NR where datepart(year,DATA_W) is not null order by rok desc";
225                                        command.Connection.Open();
226                                        reader = command.ExecuteReader();
227
228                                        while (reader.Read())
229                                        {
230                                            node = new TreeNode(reader.GetValue(0).ToString());
231                                            node.Name = reader.GetValue(0).ToString();
232
233                                            for (int i = 12; i >= 1; i--)
234                                            {
235                                                TreeNode node2 = new TreeNode(i.ToString());
236                                                node2.Name = i.ToString();
237                                                //node2.Nodes.Add(new TreeNode());
238                                                node.Nodes.Add(node2);
239                                            }
240                                            e.Node.Nodes.Add(node);
241                                        }
242
243                                        command.Connection.Close();
244
245                                        break;
246                                    case "BrakWReklamie":
247                                        e.Node.Nodes.Clear();
248
249                                        command.CommandText = "select distinct datepart(year,DATA_W) as rok from dbo.NR where datepart(year,DATA_W) is not null order by rok desc";
250                                        command.Connection.Open();
251                                        reader = command.ExecuteReader();
252
253                                        while (reader.Read())
254                                        {
255                                            node = new TreeNode(reader.GetValue(0).ToString());
256                                            node.Name = reader.GetValue(0).ToString();
257
258                                            for (int i = 12; i >= 1; i--)
259                                            {
260                                                TreeNode node2 = new TreeNode(i.ToString());
261                                                node2.Name = i.ToString();
262                                                //node2.Nodes.Add(new TreeNode());
263                                                node.Nodes.Add(node2);
264                                            }
265                                            e.Node.Nodes.Add(node);
266                                        }
267
268                                        command.Connection.Close();
269
270                                        break;
271                                }
272                                break;
273                            default:
274                                e.Node.Nodes.Clear();
275
276                                node = new TreeNode("Biura");
277                                node.Name = "Biura";
278                                node.Nodes.Add(new TreeNode());
279                                e.Node.Nodes.Add(node);
280                                break;                     
281                        }
282                        break;
283                    case 2:
284               
285                        switch (e.Node.Name) {
286                            case "Biura":
287                                DBBindings.dodajAgencjeDoWezla(e.Node);
288                                break;
289                            default:
290                                break;
291                        }
292
293                        break;
294                    case 3:
295                        switch (e.Node.Parent.Name)
296                        {
297                            case "Biura":
298                                DBBindings.dodajAgentowDoWezla(e.Node, e.Node.Name);
299                                break;
300                            default:
301                                break;
302                        }
303                        break;
304                   case 4:
305                       break;                           
306                    default:
307                        break;
308                }
309            }
310        }
311
312        private void fakturyDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
313        {
314            zestawienieFakturNEWBindingSource.CancelEdit();
315        }
316       
317        private void wyszukajToolStripButton_Click(object sender, EventArgs e)
318        {
319            wyszukajToolStrip.Visible = wyszukajToolStrip.Visible ? false : true;
320        }
321
322        private void szukajToolStripButton_Click(object sender, EventArgs e)
323        {
324            command.CommandText = query;
325
326            this.rEKLAMADataSet.ZestawienieFakturNEW.Clear();
327
328            command.CommandText += " where 1=1 ";
329
330            command.Parameters.Clear();
331
332            if (nrFakturyToolStripTextBox.Text.Trim() != "")
333            {
334                int i;
335                if (!Int32.TryParse(nrFakturyToolStripTextBox.Text, out i))
336                {
337                    MessageBox.Show("Podaj prawid³owy numer faktury.");
338                    return;
339                }
340
341                command.CommandText += " AND NUMER=@nr";
342                command.Parameters.AddWithValue("@nr", nrFakturyToolStripTextBox.Text.Trim());
343            }
344
345            if (kodRozliczeniowyToolStripTextBox.Text.Trim() != "")
346            {
347                command.CommandText += " AND NR_FK like '%' + @kod + '%'";
348                command.Parameters.AddWithValue("@kod", kodRozliczeniowyToolStripTextBox.Text.Trim());
349            }
350
351            if (rokToolStripTextBox.Text.Trim() != "")
352            {
353                int i;
354                if (!Int32.TryParse(rokToolStripTextBox.Text.Trim(), out i))
355                {
356                    MessageBox.Show("Podaj prawid³owy rok.");
357                    return;
358                }
359
360                command.CommandText += " AND YEAR(DATA_SPRZEDAZY)=@rok";
361                command.Parameters.AddWithValue("@rok", rokToolStripTextBox.Text.Trim());
362            }
363
364            if (miesiacToolStripTextBox.Text.Trim() != "")
365            {
366                int i;
367                if (!Int32.TryParse(miesiacToolStripTextBox.Text.Trim(), out i))
368                {
369                    MessageBox.Show("Podaj prawid³owy miesi¹c.");
370                    return;
371                }
372
373                command.CommandText += " AND MONTH(DATA_SPRZEDAZY)=@miesiac";
374                command.Parameters.AddWithValue("@miesiac", miesiacToolStripTextBox.Text.Trim());
375            }
376
377            if (agencjaToolStripComboBox.Text.Trim() != "")
378            {
379                command.CommandText += " AND Agencja=@agencja";
380                command.Parameters.AddWithValue("@agencja", agencjaToolStripComboBox.Text.Trim());
381            }
382           
383            if (((CheckBox)((ToolStripControlHost)wyszukajToolStrip.Items["zalegleCheckBox"]).Control).CheckState == CheckState.Checked)
384            {
385                command.CommandText += " AND (BRUTTO - suma_zaplat) > 0";
386            }
387            else if (((CheckBox)((ToolStripControlHost)wyszukajToolStrip.Items["zalegleCheckBox"]).Control).CheckState == CheckState.Unchecked)
388            {
389                command.CommandText += " AND (BRUTTO - suma_zaplat) <= 0 ";
390            }
391         
392            //MessageBox.Show(command.CommandText + nrFakturyToolStripTextBox.Text);
393            sqlDataAdapter.SelectCommand = command;
394
395            this.Cursor = Cursors.WaitCursor;
396
397            try
398            {
399                int t = sqlDataAdapter.Fill(this.rEKLAMADataSet.ZestawienieFakturNEW);
400                commandExecuted = true;
401            }
402            catch (Exception e1)
403            {
404                MessageBox.Show(e1.Message);
405            }
406
407            treeView1.CollapseAll();
408            treeView1.SelectedNode = null;
409
410            this.Cursor = Cursors.Default;
411        }
412
413        private void fakturyDataGridView_Leave(object sender, EventArgs e)
414        {
415            fakturyDataGridView.EndEdit();
416        }
417
418        private void kryteriumWyszukiwania_KeyPress(object sender, KeyPressEventArgs e)
419        {
420            if (e.KeyChar == 13)
421            {
422                wyszukajToolStripButton.PerformClick();
423            }
424        }
425
426        /// <summary>
427        /// Podsumowuje brutto i zaplate z wyswietlonych faktur
428        /// </summary>
429        private void podsumuj() {
430
431            if (zestawienieFakturNEWBindingSource.List.Count != 0)
432            {
433            decimal brutto = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(BRUTTO)",""));
434            decimal netto = Convert.ToDecimal(rEKLAMADataSet.ZestawienieFaktur.Compute("Sum(netto)", ""));
435
436            bruttoTextBox.Text = String.Format("{0:C}", brutto);
437            zaplataTextBox.Text = String.Format("{0:C}", netto);
438            }
439            else
440            {
441            bruttoTextBox.Clear();
442            zaplataTextBox.Clear();           
443            }
444        }
445
446        private void zestawienieFakturBindingSource_ListChanged(object sender, ListChangedEventArgs e)
447        {         
448           podsumuj();
449        }
450
451        private void toolStripButton1_Click(object sender, EventArgs e)
452        {
453            PrintDGV.Print_DataGridView(fakturyDataGridView,50);
454        }
455
456        private void wyczyscPolaToolStripButton_Click(object sender, EventArgs e)
457        {
458            rEKLAMADataSet.ZestawienieFaktur.Clear();
459
460            nrFakturyToolStripTextBox.Clear();
461            kodRozliczeniowyToolStripTextBox.Clear();
462            rokToolStripTextBox.Clear();
463            miesiacToolStripTextBox.Clear();
464            agencjaToolStripComboBox.SelectedIndex = -1;
465            agencjaToolStripComboBox.Text = "";
466            ((CheckBox)((ToolStripControlHost)wyszukajToolStrip.Items["zalegleCheckBox"]).Control).CheckState = CheckState.Indeterminate;
467        }
468
469        private void podgladToolStripButton_Click(object sender, EventArgs e)
470        {
471            if (zestawienieFakturNEWBindingSource.Current != null)
472            {
473                DataRowView row = (DataRowView)zestawienieFakturNEWBindingSource.Current;
474                REKLAMADataSet.ZestawienieFakturNEWRow faktura = (REKLAMADataSet.ZestawienieFakturNEWRow)row.Row;
475
476                this.Cursor = Cursors.WaitCursor;
477                FactureViewer fv = new FactureViewer(faktura.ID_FAKTURY);
478                fv.ShowDialog();
479               
480                this.Cursor = Cursors.Default;
481            }
482        }
483
484        /// <summary>
485        /// Przechodzi do okna KLIENCI i wyœwietla dane klienta zwi¹zanego z  faktur¹.
486        /// </summary>
487        private void klientToolStripButton_Click(object sender, EventArgs e)
488        {
489            if (zestawienieFakturNEWBindingSource.Current != null)
490            {
491                DataRowView row = (DataRowView)zestawienieFakturNEWBindingSource.Current;
492
493                int custID = Convert.ToInt32(row["ID_NABYWCY"]);
494
495                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).pokazKlienta(custID);
496
497                this.Hide();
498
499                ClientsForm.getClientsForm((MDIBazaReklam)this.MdiParent).Show();
500
501            }   
502        }
503
504        private void FacturesForm_Shown(object sender, EventArgs e)
505        {
506            if (User.getUser().St_kierownik)
507            {
508                agencjaToolStripComboBox.Text = User.getUser().SymbolAgencji;
509            }
510            else if (User.getUser().St_handlowiec | User.getUser().St_subhandlowiec)
511            {
512                kodRozliczeniowyToolStripTextBox.Text = User.getUser().Kod_agenta;
513            }
514
515         
516        }
517
518        private void excelToolStripButton_Click(object sender, EventArgs e)
519        {
520            this.Cursor = Cursors.WaitCursor;
521
522            ExcelHandler ex = new ExcelHandler();
523            ex.exportToExcel(fakturyDataGridView);
524
525            this.Cursor = Cursors.Default;
526        }
527
528        private void fakturyDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
529        {
530            if (e.ListChangedType == ListChangedType.Reset)
531            {
532                foreach (DataGridViewRow r in fakturyDataGridView.Rows)
533                {
534                    if (r.Cells[7].Value != DBNull.Value && r.Cells[8].Value != DBNull.Value)
535                    {
536                        if (Convert.ToDecimal(r.Cells[7].Value) != Convert.ToDecimal(r.Cells[8].Value))
537                        {
538                            r.DefaultCellStyle.ForeColor = Color.Red;
539                        }
540                    }
541                }
542            }
543        }
544
545        private void wplata2toolStripButton_Click(object sender, EventArgs e)
546        {
547            if (zestawienieFakturNEWBindingSource.Current != null)
548            {
549                DataRowView row = (DataRowView)zestawienieFakturNEWBindingSource.Current;
550                REKLAMADataSet.ZestawienieFakturNEWRow f = (REKLAMADataSet.ZestawienieFakturNEWRow)row.Row;
551               
552                PaymentForm2 pf2;
553
554                if (f.IsidWplatyNull())
555                {
556                    pf2 = new PaymentForm2(0,f.ID_FAKTURY);
557                }
558                else
559                {
560                    pf2 = new PaymentForm2(f.idWplaty, f.ID_FAKTURY);
561                }
562
563                if (pf2.ShowDialog() == DialogResult.OK)
564                {
565                    f.idWplaty = pf2.IdWplaty;
566                    f.EndEdit();
567                    fakturyDataGridView.Refresh();
568                }
569            }
570   
571        }
572
573        private void zamowieniaToolStripButton_Click(object sender, EventArgs e)
574        {
575            if (zestawienieFakturNEWBindingSource.Current != null)
576            {
577                DataRowView row = (DataRowView)zestawienieFakturNEWBindingSource.Current;
578                REKLAMADataSet.ZestawienieFakturNEWRow f = (REKLAMADataSet.ZestawienieFakturNEWRow)row.Row;
579
580                ZamowieniaForm zf = new ZamowieniaForm(f.ID_NABYWCY,f.idZamowienia);
581                zf.ShowDialog();
582            }
583        }
584    }
585}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.