root/Baza Reklam 2 - Faktury/OrderDetails.cs @ 38

Wersja 37, 61.7 KB (wprowadzona przez dorota, 17 years temu)

poprawka konta

Line 
1using System;
2using System.Collections.Specialized;
3using System.Collections.Generic;
4using System.ComponentModel;
5using System.Data;
6using System.Data.SqlClient;
7using System.Drawing;
8using System.Text;
9using System.Windows.Forms;
10using System.Text.RegularExpressions;
11using System.Diagnostics;
12
13namespace Baza_Reklam
14{
15    public partial class OrderDetails : Form
16    {
17        SLOWNIKDataSet.Kursy_WalutRow kurs;
18
19        //zmienna wy³¹czaj¹ca obs³ugê zdarzeñ, gdy formularz jest ukryty
20        private bool obslugaZdarzen;
21
22        //zmienna ustawiana na TRUE przy wznawianiu lub dodawnaniu nowego zamówienia
23        private bool nowyRekord;
24        private bool bylyZmiany;
25        private bool juzSieUkazaloWgazecie;
26        private double maksymalnyRabat = (double)0.3;
27
28        //numery reklam dla nowych zamówieñ       
29        StringDictionary numeryReklam = new StringDictionary();
30
31        private static OrderDetails orderDetails;
32
33        public static OrderDetails getOrderDetails()
34        {
35            if (orderDetails == null)
36            {
37                orderDetails = new OrderDetails();
38            }
39
40            return orderDetails;
41        }
42
43        private ErrorProvider errProvider;
44
45        // przechowuje id aktualnie wyœwietlanej reklamy
46        private int reklamaId;
47
48        // przechowuje id klienta , którego dotyczy aktualnie wyœwietlana reklama
49        private int customerId;
50
51
52        Baza_Reklam.REKLAMADataSetTableAdapters.zmianyEmisjiTableAdapter
53            zmianyEmisjiTableAdapter = new Baza_Reklam.REKLAMADataSetTableAdapters.zmianyEmisjiTableAdapter();
54
55        //private event ZmianaParametrowZamowienia
56
57        private OrderDetails()
58        {
59            InitializeComponent();
60
61            this.rEKLAMADataSet.UKAZE_SIE_W_NR.Constraints.Remove("FAKTURY_UKAZE_SIE_W_NR");
62
63            errProvider = new ErrorProvider();
64
65            // funckje formatuj¹ce Decimal <-> %
66            rABATTextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
67            rABATTextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
68
69            pROCENT_PROWIZJITextBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
70            pROCENT_PROWIZJITextBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
71
72            pVATComboBox.DataBindings[0].Format += new ConvertEventHandler(DecimalToProcent);
73            pVATComboBox.DataBindings[0].Parse += new ConvertEventHandler(ProcentToDecimal);
74
75            rEKLAMADataSet.UKAZE_SIE_W_NR.TableNewRow += nowaEmisja;
76
77            nAZWY_MODULOWTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
78            gRZBIETYTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
79            pROMOCJETableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
80            lISTA_TYPOW_REKLAMYTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
81            lISTA_TYTULOWTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
82            kursy_WalutTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
83            rEKLAMATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
84            uKAZE_SIE_W_NRTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
85            rEKLAMA_STRONATableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
86            reklama_Info_DodTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
87            aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
88            ogl_dzialyTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
89            ogl_rozdzialyTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
90            zmianyEmisjiTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
91
92            this.nAZWY_MODULOWTableAdapter.Fill(this.sLOWNIKDataSet.NAZWY_MODULOW);
93            this.gRZBIETYTableAdapter.Fill(this.sLOWNIKDataSet.GRZBIETY);
94            this.pROMOCJETableAdapter.Fill(this.sLOWNIKDataSet.PROMOCJE);
95
96            if (User.getUser().St_kierownik || User.getUser().St_produkcja)
97            {
98                this.lISTA_TYPOW_REKLAMYTableAdapter.Fill(this.sLOWNIKDataSet.LISTA_TYPOW_REKLAMY);
99            }
100            else
101            {
102                this.lISTA_TYPOW_REKLAMYTableAdapter.FillByBezBezplatnych(this.sLOWNIKDataSet.LISTA_TYPOW_REKLAMY);
103            }
104            this.lISTA_TYTULOWTableAdapter.Fill(this.sLOWNIKDataSet.LISTA_TYTULOW);
105            this.aGENCITableAdapter.FillByAktywny(this.sLOWNIKDataSet.AGENCI);
106            this.ogl_dzialyTableAdapter.Fill(this.sLOWNIKDataSet.ogl_dzialy);
107            this.ogl_rozdzialyTableAdapter.Fill(this.sLOWNIKDataSet.ogl_rozdzialy);
108
109            usunToolStripMenuItem.Click += usunEmisje;
110            anulujToolStripMenuItem.Click += anulujEmisje;
111            zmienToolStripMenuItem.Click += zmienEmisje;
112
113            walutyComboBox.SelectedIndex = 0;
114        }
115
116        private void OrderDetails_Load(object sender, EventArgs e)
117        {
118            wyroznienie_exportedCheckBox.Enabled = User.getUser().St_produkcja;
119        }
120
121        /// <summary>
122        /// Filtruje modu³y i grzbiety wg tytu³u gazety.
123        /// </summary>
124        private void tYTULComboBox_SelectedIndexChanged(object sender, EventArgs e)
125        {
126            if (obslugaZdarzen)
127            {
128                if (tYTULComboBox.SelectedValue != null)
129                {
130                    this.nAZWYMODULOWBindingSource.Filter = "tytul='" + tYTULComboBox.SelectedValue.ToString() + "'";
131                    this.iD_REKLAMYTextBox.Text = (nowyRekord) ? this.numeryReklam[tYTULComboBox.SelectedValue.ToString()] : this.iD_REKLAMYTextBox.Text;
132                    this.gRZBIETYBindingSource.Filter = "Nazwa like '%" + tYTULComboBox.SelectedValue.ToString() + "%'";
133                    int i = lISTATYTULOWBindingSource.Find("SYMB", tYTULComboBox.SelectedValue);
134                    this.maksymalnyRabat = Convert.ToDouble(((DataRowView)lISTATYTULOWBindingSource.List[i])["max_rabat"]);
135
136                    DataRowView row = (DataRowView)lISTATYTULOWBindingSource.Current;
137                    this.ogl_dzialyBindingSource.Filter = "TYTUL=" + row["id"].ToString();
138
139                    switch (tYTULComboBox.SelectedValue.ToString())
140                    {
141                        case "AMT":
142                            this.ogl_dzialyBindingSource.Filter = "TYTUL='2'";
143                            break;
144                        case "GS":
145                            this.ogl_dzialyBindingSource.Filter = "TYTUL='1'";
146                            break;
147                        case "AGRO":
148                            this.ogl_dzialyBindingSource.Filter = "TYTUL='7'";
149                            break;
150                        case "adMot":
151                            this.ogl_dzialyBindingSource.Filter = "TYTUL='6'";
152                            break;
153                        default:
154                            this.ogl_dzialyBindingSource.Filter = "";
155                            break;
156                    }
157                }
158            }
159        }
160
161        private void grzbietComboBox_SelectedIndexChanged(object sender, EventArgs e)
162        {
163            if (obslugaZdarzen)
164            {
165                if ((int)grzbietComboBox.SelectedValue == 9)
166                {
167                    this.iD_REKLAMYTextBox.Text = (nowyRekord) ? this.numeryReklam["slaski"] : this.iD_REKLAMYTextBox.Text;
168                }
169                else
170                {
171                    this.iD_REKLAMYTextBox.Text = (nowyRekord) ? this.numeryReklam[tYTULComboBox.SelectedValue.ToString()] : this.iD_REKLAMYTextBox.Text;
172                }
173            }
174        }
175
176        private void anulujButton_Click(object sender, EventArgs e)
177        {
178            if (nowyRekord)
179            {
180                //usuwa dodany wczesniej do bazy wiersz   
181                if (rEKLAMABindingSource.Current != null)
182                {
183                    rEKLAMABindingSource.RemoveCurrent();
184                    rEKLAMABindingSource.EndEdit();
185                    rEKLAMATableAdapter.Update(this.rEKLAMADataSet.REKLAMA);
186                }
187            }
188            else
189            {
190                //usuwa zmiany w zleceniu           
191                rEKLAMADataSet.REKLAMA.RejectChanges();
192                rEKLAMADataSet.UKAZE_SIE_W_NR.RejectChanges();
193            }
194
195            this.obslugaZdarzen = false;
196            liczbaEmisjiLabel.Text = "-";
197
198            this.DialogResult = bylyZmiany ? DialogResult.OK : DialogResult.Cancel;
199        }
200
201        /// <summary>
202        /// Zapisuje zmiany w wierszu oraz dodaje rekordy do "UKAZE SIE W NR" i "InfoDod"
203        /// </summary>
204        private void dodajButton_Click(object sender, EventArgs e)
205        {
206            this.Cursor = Cursors.WaitCursor;
207
208            this.Validate();
209            rEKLAMABindingSource.EndEdit();
210
211            DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
212           
213            /*
214            if (!(brutto_EuroTextBox.Text != "0" ^ walutyComboBox.SelectedIndex == -1))
215            {
216                MessageBox.Show("Popraw walutê!");
217                this.Cursor = Cursors.Default;
218                return;
219            }*/
220
221            if (wyroznienieCheckBox.Checked ^ Convert.ToDecimal(row["wyroznienie_kwota"]) != 0)
222            {
223                MessageBox.Show("Zosta³a zmieniona opcja wyró¿nienia. Zamówienie nale¿y ponownie przeliczyæ");
224                this.Cursor = Cursors.Default;
225                return;
226            }
227
228            row["USERID"] = User.getUser().Login;
229
230            rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
231            rEKLAMAReklamaInfoDodBindingSource.EndEdit();
232
233            //MessageBox.Show(rEKLAMAUKAZESIEWNRBindingSource.List.Count.ToString() + " " + rEKLAMADataSet.UKAZE_SIE_W_NR.Count.ToString());
234
235            this.rEKLAMATableAdapter.Update(this.rEKLAMADataSet.REKLAMA);
236            this.uKAZE_SIE_W_NRTableAdapter.Update(this.rEKLAMADataSet.UKAZE_SIE_W_NR);
237            this.reklama_Info_DodTableAdapter.Update(this.rEKLAMADataSet.Reklama_Info_Dod);
238            this.zmianyEmisjiTableAdapter.Update(this.rEKLAMADataSet.zmianyEmisji);
239            MessageBox.Show("Zmiany zapisane");
240
241            zmiany();
242
243            nowyRekord = false;
244            bylyZmiany = true;
245            //this.obslugaZdarzen = false;
246
247            this.Cursor = Cursors.Default;
248        }
249
250        private void sZERTextBox_Validating(object sender, CancelEventArgs e)
251        {
252            if (!sZERTextBox.Text.Equals(""))
253            {
254                if (!Regex.IsMatch(sZERTextBox.Text, "^[0-8]{1}$", RegexOptions.IgnoreCase))
255                {
256                    e.Cancel = true;
257
258                    errProvider.SetError((Control)sender, "1-8");
259                    return;
260                }
261
262            }
263            errProvider.SetError((Control)sender, "");
264        }
265
266        private void wYSTextBox_Validating(object sender, CancelEventArgs e)
267        {
268            if (!sZERTextBox.Text.Equals(""))
269            {
270                if (!Regex.IsMatch(sZERTextBox.Text, "^[0-8]{1}$", RegexOptions.IgnoreCase))
271                {
272                    e.Cancel = true;
273
274                    errProvider.SetError((Control)sender, "1-8");
275                    return;
276                }
277
278            }
279            errProvider.SetError((Control)sender, "");
280        }
281
282        private void promocjaComboBox_TextChanged(object sender, EventArgs e)
283        {
284            if (obslugaZdarzen)
285            {
286                if (promocjaComboBox.Text == "")
287                {
288                    if (rEKLAMABindingSource.Current != null)
289                    {
290                        DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
291                        row["PROMOCJA"] = DBNull.Value;
292                        rEKLAMABindingSource.EndEdit();
293                    }
294                }
295            }
296        }
297
298        private void zATWIERDZONO_DO_DRUKUCheckBox_Click(object sender, EventArgs e)
299        {
300            if (zATWIERDZONO_DO_DRUKUCheckBox.Checked)
301            {
302                if (rEKLAMABindingSource.Current != null)
303                {
304                    DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
305
306                    sprawdzBledy();
307
308                    if (!User.getUser().St_kierownik)
309                    {
310                        if (Convert.ToDouble(row["rabat"]) > this.maksymalnyRabat)
311                        {
312                            MessageBox.Show("Rabat wy¿szy ni¿ " + this.maksymalnyRabat.ToString("P") + ". Zamówienie musi byæ zatwierdzone do druku przez kierownika");
313                            zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
314                        }
315
316                        if (nAZWYMODULOWBindingSource.Current != null)
317                        {
318                            DataRowView r = (DataRowView)nAZWYMODULOWBindingSource.Current;
319                            //decimal cenaJedn = Convert.ToDecimal(row["CENA JEDN"]);
320                            decimal cenaMinMod = Convert.ToDecimal(r["CENA_MIN"]);
321
322                            decimal cenaJedn = Convert.ToDecimal(row["NETTO"]) / (Convert.ToDecimal(row["SZER"]) * Convert.ToDecimal(row["WYS"]) * Convert.ToDecimal(row["KROTNOŒÆ"]));
323                            cenaJedn = Math.Round(cenaJedn, 2);
324
325                            if (cenaJedn < cenaMinMod)
326                            {
327                                MessageBox.Show("Cena modu³u ni¿sza ni¿ cena cennikowa. Zamówienie musi byæ zatwierdzone do druku przez kierownika");
328                                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
329                            }
330                        }
331
332                        string typ = Convert.ToString(row["typ"]);
333                        if ((typ == "BEZP£ATNA") || (typ == "BARTER") || (typ == "REKLAMACJA"))
334                        {
335                            MessageBox.Show("Zamówienie musi byæ zatwierdzone do druku przez kierownika");
336                            zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
337                        }
338                    }
339                }
340            }
341        }
342
343        private void pROCENT_PROWIZJITextBox_Leave(object sender, EventArgs e)
344        {
345            rEKLAMABindingSource.EndEdit();
346        }
347       
348        /// <summary>
349        /// Blokuje czêœæ formuklarze w zale¿noœci od stanu zamówienia i praw u¿ytkownika...
350        /// </summary>
351        private void OrderDetails_Shown(object sender, EventArgs e)
352        {
353            // BindingSource sa resetowane, by ustawily sie wszytskie filtry
354            kurs = null;
355
356            obslugaZdarzen = true;
357
358            tYTULComboBox.SelectedIndex = -1;
359            rEKLAMABindingSource.ResetBindings(false);
360           
361            mOD_TYPComboBox.SelectedIndex = -1;
362            rEKLAMABindingSource.ResetBindings(false);
363           
364            ogl_dzialComboBox.SelectedIndex = -1;
365            rEKLAMABindingSource.ResetBindings(false);
366           
367            ogl_rozdzialComboBox.SelectedIndex = -1;
368            rEKLAMABindingSource.ResetBindings(false);
369           
370            blokowanieZrealizowanychEmisji();
371            zmiany();
372            init();
373        }
374
375        private void ukazeSieDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
376        {
377            if (rEKLAMAUKAZESIEWNRBindingSource.Current != null)
378            {
379                rEKLAMAUKAZESIEWNRBindingSource.RemoveCurrent();
380            }
381            blokowanieZrealizowanychEmisji();
382        }
383
384        private void ukazeSieDataGridView_Leave(object sender, EventArgs e)
385        {
386            Validate();
387
388            rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
389
390            liczbaEmisjiLabel.Text = rEKLAMAUKAZESIEWNRBindingSource.List.Count.ToString();
391
392            if (rEKLAMABindingSource.Current != null)
393            {
394                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
395
396                if (row["ID_FAKTURY"] == DBNull.Value)
397                {
398                    kROTNOSCTextBox.Text = rEKLAMAUKAZESIEWNRBindingSource.List.Count.ToString();
399                }
400            }
401        }
402
403        /// <summary>
404        /// Blokuje emisje,które ju¿ siê ukaza³y.
405        /// </summary>
406        private void blokowanieZrealizowanychEmisji()
407        {
408            //  ukazeSieDataGridView.Enabled = true;
409            foreach (DataGridViewRow r in ukazeSieDataGridView.Rows)
410            {
411                REKLAMADataSet.UKAZE_SIE_W_NRRow em =
412                    (REKLAMADataSet.UKAZE_SIE_W_NRRow)((DataRowView)r.DataBoundItem).Row;
413
414                //anulowana
415                if (em.status == 2)
416                {
417                    r.ReadOnly = true;
418                    r.DefaultCellStyle.ForeColor = Color.Red;
419                }
420               
421                foreach (DataGridViewRow s in ukazaloSieDataGridView.Rows)
422                {
423                    if (em.Nr_Wydania.ToString() == s.Cells["NR_WYDANIA"].Value.ToString())
424                    {
425                        r.ReadOnly = true;
426                        r.DefaultCellStyle.BackColor = Color.MintCream;
427                        this.juzSieUkazaloWgazecie = true;
428                        break;
429                    }
430                }
431            }
432
433        }
434       
435
436        public void zablokujGroupBoxa(GroupBox g)
437        {
438            foreach (Control c in g.Controls)
439            {
440                if (c is TextBox)
441                {
442                    ((TextBox)c).ReadOnly = true;
443                }
444
445                if (c is ComboBox)
446                {
447                    ((ComboBox)c).Enabled = false;
448                }
449            }
450        }
451
452        public void odblokujGroupBoxa(GroupBox g)
453        {
454            foreach (Control c in g.Controls)
455            {
456                if (c is TextBox)
457                {
458                    ((TextBox)c).ReadOnly = false;
459                }
460
461                if (c is ComboBox)
462                {
463                    ((ComboBox)c).Enabled = true;
464                }
465            }
466        }
467
468        /// <summary>
469        /// Blokuje kontrolki zwi¹zane z danymi zamówienia
470        /// </summary>
471        private void zablokujGroupBoxy()
472        {
473            zablokujGroupBoxa(groupBox1);
474
475            if (!(User.getUser().St_produkcja | User.getUser().St_kierownik))
476            {
477                zablokujGroupBoxa(groupBox2);
478            }
479            zablokujGroupBoxa(groupBox3);
480            zablokujGroupBoxa(groupBox4);
481
482            //lokalizacje mo¿na zmieniaæ
483            sTRONATextBox.ReadOnly = false;
484
485        }
486
487        /// <summary>
488        /// Odblokuje kontrolki zwi¹zane z danymi zamówienia
489        /// </summary>
490        private void odblokujGroupBoxy()
491        {
492            odblokujGroupBoxa(groupBox1);
493            odblokujGroupBoxa(groupBox2);
494            odblokujGroupBoxa(groupBox3);
495            odblokujGroupBoxa(groupBox4);
496        }
497
498        /// <summary>
499        /// Buduje nr zamowienia dla danego handlowca
500        /// </summary>
501        private string nrRek(string symAgenta, string tagAgenta)
502        {
503            if (User.getUser().St_handlowiec)
504            {
505                string nr = Baza_Reklam.Utils.numerNowejReklamy(symAgenta, tagAgenta).ToString();
506                switch (nr.Length)
507                {
508                    case 1:
509                        nr = tagAgenta + "00" + nr + "_00";
510                        break;
511                    case 2:
512                        nr = tagAgenta + "0" + nr + "_00";
513                        break;
514                    case 3:
515                        nr = tagAgenta + nr + "_00";
516                        break;
517                }
518                return nr;
519            }
520            return "";
521        }
522
523        /// <summary>
524        /// Sprawdza poprawnoœæ zamówienia... pewnie niekompletnie
525        /// </summary>
526        private void sprawdzBledy()
527        {
528            if (ogl_dzialComboBox.SelectedValue == null)
529            {
530                MessageBox.Show("Wybierz dzia³.");
531                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
532                return;
533            }
534
535
536            if (mOD_TYPComboBox.Text == "")
537            {
538                MessageBox.Show("Wybierz rodzaj modu³u.");
539                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
540                return;
541            }
542
543            string grzbiet = grzbietComboBox.Text;
544            string tytul = tYTULComboBox.Text;
545
546            if (grzbiet == "")
547            {
548                MessageBox.Show("Wybierz grzbiet.");
549                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
550                return;
551            }
552            else
553            {
554                if (!grzbiet.Contains(tytul))
555                {
556                    MessageBox.Show("Wybierz poprawny grzbiet.");
557                    zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
558                    return;
559                }
560            }
561
562            //lokalizacja
563            if (sTRONATextBox.Text == "")
564            {
565                MessageBox.Show("Podaj lokalizacjê.");
566                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
567                return;
568            }
569
570            if (rEKLAMAUKAZESIEWNRBindingSource.List.Count == 0)
571            {
572                MessageBox.Show("Brak wyznaczonych emisji.");
573                zATWIERDZONO_DO_DRUKUCheckBox.CheckState = CheckState.Unchecked;
574                return;
575            }
576        }
577
578        /// <summary>
579        /// Przelicza wartoœæ zamówienia.
580        /// </summary>
581        private void przelicz()
582        {
583            DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
584
585            if ((row["KROTNOŒÆ"] != null) && (row["CENA JEDN"] != null) && (row["VAT"] != null)
586              && (row["PROCENT PROWIZJI"] != null) && (row["RABAT"] != null)
587              && (row["SZER"] != null) && (row["WYS"] != null))
588            {
589                int szer = Convert.ToInt32(row["SZER"]);
590                int wys = Convert.ToInt32(row["WYS"]);
591                int krotnosc = Convert.ToInt32(row["KROTNOŒÆ"]);
592
593                if (krotnosc == 0)
594                {
595                    MessageBox.Show("Iloœæ emisji równa siê  0");
596                    return;
597                }
598
599                decimal cenaJednostkowa = Convert.ToDecimal(row["CENA JEDN"]);
600                decimal vatProcent = Convert.ToDecimal(row["VAT"]);
601                decimal prowizjaProcent = Convert.ToDecimal(row["PROCENT PROWIZJI"]);
602                decimal rabatProcent = Convert.ToDecimal(row["RABAT"]);
603
604
605                //rabatWartosc
606                decimal rabatWartosc = rabatProcent * cenaJednostkowa;
607                rabatWartosc = Math.Round(rabatWartosc, 2);
608
609                //netto
610                decimal netto = (cenaJednostkowa - rabatWartosc) * krotnosc;
611                netto = Math.Round(netto, 2);
612
613                nettoBezBOTextBox.Text = String.Format("{0:C}", netto);
614
615                //cena modulu
616                decimal cenaModulu = netto / (szer * wys * krotnosc);
617                cenaModulu = Math.Round(cenaModulu, 2);
618
619                if (Convert.ToBoolean(row["wyroznienie"]) == true)
620                {
621                    row["wyroznienie_procent"] = 0.15M;
622                    decimal kwotaWyroznienia = Math.Round(netto * 0.15M, 2);
623                    row["wyroznienie_kwota"] = kwotaWyroznienia;
624                    netto = netto + kwotaWyroznienia;
625                }
626                else
627                {
628                    row["wyroznienie_procent"] = 0;
629                    row["wyroznienie_kwota"] = 0;
630                }
631
632                //vatWartosc
633                decimal vatWartosc = vatProcent * netto;
634                vatWartosc = Math.Round(vatWartosc, 2);
635
636                //brutto
637                decimal brutto = netto + vatWartosc;
638                brutto = Math.Round(brutto, 2);
639
640                //prowizja
641                decimal prowizjaWartosc = prowizjaProcent * netto;
642                prowizjaWartosc = Math.Round(prowizjaWartosc, 2);
643
644                cenaModuluLabel.Text = String.Format("{0:C}", cenaModulu);
645                row["RABAT WARTOŒÆ"] = rabatWartosc.ToString();
646                row["NETTO"] = netto.ToString();
647                row["BRUTTO"] = brutto.ToString();
648                row["PVAT"] = vatWartosc.ToString();
649                row["PROWIZJA"] = prowizjaWartosc.ToString();
650
651                rEKLAMABindingSource.EndEdit();
652            }
653
654        }
655
656        /// <summary>
657        /// Przelicza kwotê w z³otówkach na wybran¹ walutê.
658        /// </summary>
659        private void przeliczNaWalute(SLOWNIKDataSet.Kursy_WalutRow kurs)
660        {
661            if (rEKLAMABindingSource.Current != null)
662            {
663                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
664
665                if (row["BRUTTO"] != DBNull.Value)
666                {
667
668                    decimal brutto = Convert.ToDecimal(row["BRUTTO"]);
669                    brutto = Math.Round(brutto, 2);
670
671                    // waluty
672                    row["waluta_tabela_nr"] = kurs.Numer_Tabeli;
673                    row["waluta_Przelicznik"] = kurs.przelicznik;
674                    row["waluta_kurs_z_dnia"] = kurs.Data_Publikacji;
675                    row["Brutto_Euro_Miano"] = kurs.Kod_Waluty;
676                       
677                    //zmiana kursu dla poznania
678                    if (User.getUser().IdAgencji == 6)
679                    {
680                        row["waluta_kurs"] = kurs.Kurs_Sredni_Poznan;
681                        row["Brutto_Euro"] = Math.Round(brutto / Convert.ToDecimal(kurs.Kurs_Sredni_Poznan) * Convert.ToInt32(kurs.przelicznik), 2);
682                    }
683                    else
684                    {
685                        row["waluta_kurs"] = kurs.Kurs_Sredni;
686                        row["Brutto_Euro"] = Math.Round(brutto / Convert.ToDecimal(kurs.Kurs_Sredni) * Convert.ToInt32(kurs.przelicznik), 2);
687                    }
688
689                    // zmiana domyœlnego konta do faktury dla EUR
690                    if (User.getUser().IdAgencji != 4 & (row["Brutto_Euro_Miano"].ToString() == "EUR"))
691                    {
692                        if (User.getUser().IdAgencji == 1223940396 | User.getUser().IdAgencji == 1223940398)
693                        {
694                            row["FAKTURA ID KONTA"] = 3;
695                        }
696                        else
697                        {
698                            row["FAKTURA ID KONTA"] = 5;
699                        }
700                    }
701
702                    rEKLAMABindingSource.EndEdit();
703                }
704            }
705        }
706
707        /// <summary>
708        /// Przelicza kwotê w walucie na z³otówki.
709        /// </summary>
710        private void przeliczWaluteNaZlotowki(SLOWNIKDataSet.Kursy_WalutRow kurs)
711        {
712            DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
713
714            if (row["Brutto_Euro"] != DBNull.Value)
715            {
716                // waluty
717                row["waluta_tabela_nr"] = kurs.Numer_Tabeli;
718                row["waluta_Przelicznik"] = kurs.przelicznik;
719                row["waluta_kurs"] = kurs.Kurs_Sredni;
720                row["waluta_kurs_z_dnia"] = kurs.Data_Publikacji;
721                row["Brutto_Euro_Miano"] = kurs.Kod_Waluty;
722
723                //zmiana kursu dla poznania
724                if (User.getUser().IdAgencji == 6)
725                {
726                    row["waluta_kurs"] = kurs.Kurs_Sredni_Poznan;
727                }
728             
729                int szer = Convert.ToInt32(row["SZER"]);
730                int wys = Convert.ToInt32(row["WYS"]);
731                int krotnosc = Convert.ToInt32(row["KROTNOŒÆ"]);
732
733                if (krotnosc == 0)
734                {
735                    MessageBox.Show("Iloœæ emisji równa siê  0");
736                    return;
737                }
738
739                decimal vatProcent = Convert.ToDecimal(row["VAT"]);
740                decimal prowizjaProcent = Convert.ToDecimal(row["PROCENT PROWIZJI"]);
741                decimal rabatProcent = Convert.ToDecimal(row["RABAT"]);
742
743                //przeliczanie z waluty na zlotowki   
744                decimal brutto = Math.Round(Convert.ToDecimal(row["Brutto_Euro"]) * Convert.ToDecimal(row["waluta_kurs"]) / Convert.ToInt32(row["waluta_Przelicznik"]), 2);
745
746                decimal netto = brutto / (1 + vatProcent);
747                netto = Math.Round(netto, 2);
748
749                decimal nettoZBO = netto;
750
751                if (Convert.ToBoolean(row["wyroznienie"]) == true)
752                {
753                    row["wyroznienie_procent"] = 0.15M;
754                    decimal nettoBezBO = Math.Round(netto / 1.15M, 2);
755                    row["wyroznienie_kwota"] = Math.Round(netto - nettoBezBO, 2);
756                    netto = nettoBezBO;
757                }
758                else
759                {
760                    row["wyroznienie_procent"] = 0;
761                    row["wyroznienie_kwota"] = 0;
762                }
763
764                nettoBezBOTextBox.Text = String.Format("{0:C}", netto);
765
766                decimal vatWartosc = vatProcent * nettoZBO;
767                vatWartosc = Math.Round(vatWartosc, 2);
768
769                decimal prowizjaWartosc = prowizjaProcent * nettoZBO;
770                prowizjaWartosc = Math.Round(prowizjaWartosc, 2);
771
772                decimal rabatWartosc = netto * rabatProcent / (krotnosc * (1 - rabatProcent));
773                rabatWartosc = Math.Round(rabatWartosc, 2);
774
775                decimal cenaJednostkowa = (netto / (decimal)krotnosc) + rabatWartosc;
776                cenaJednostkowa = Math.Round(cenaJednostkowa, 2);
777
778                //cena modulu
779                decimal cenaModulu = netto / (szer * wys * krotnosc);
780                cenaModulu = Math.Round(cenaModulu, 2);
781
782                cenaModuluLabel.Text = String.Format("{0:C}", cenaModulu);
783                row["RABAT WARTOŒÆ"] = rabatWartosc.ToString();
784                row["NETTO"] = nettoZBO.ToString();
785                row["BRUTTO"] = brutto.ToString();
786                row["PVAT"] = vatWartosc.ToString();
787                row["PROWIZJA"] = prowizjaWartosc.ToString();
788                row["CENA JEDN"] = cenaJednostkowa.ToString();
789
790
791                // zmiana domyœlnego konta do faktury dla EUR
792                if (User.getUser().IdAgencji != 4 & (row["Brutto_Euro_Miano"].ToString() == "EUR"))
793                {
794                    if (User.getUser().IdAgencji == 1223940396 | User.getUser().IdAgencji == 1223940398)
795                    {
796                        row["FAKTURA ID KONTA"] = 3;
797                    }
798                    else
799                    {
800                        row["FAKTURA ID KONTA"] = 5;
801                    }
802                }
803
804                rEKLAMABindingSource.EndEdit();
805
806            }
807        }
808
809        public void dodajNoweZamowienie(int custId, string kodKlienta)
810        {           
811            this.nowyRekord = true;
812            this.bylyZmiany = false;
813            this.juzSieUkazaloWgazecie = false;
814
815            this.numeryReklam.Clear();
816
817            string nr;
818            foreach (DataRowView r in lISTATYTULOWBindingSource.List)
819            {
820                nr = nrRek(User.getUser().Symbol_agenta, r["SYMB2"].ToString().Trim() + User.getUser().Kod_agenta);
821                this.numeryReklam.Add((string)r["SYMB"], nr);
822            }
823            //autosalon œl¹ski
824            nr = nrRek(User.getUser().Symbol_agenta, "G" + User.getUser().Kod_agenta);
825            this.numeryReklam.Add("slaski", nr);
826
827            this.rEKLAMADataSet.REKLAMA.Clear();
828            this.rEKLAMADataSet.UKAZE_SIE_W_NR.Clear();
829            this.rEKLAMADataSet.Reklama_Info_Dod.Clear();
830
831            this.customerId = custId;
832
833            DataView datatable = (DataView)this.rEKLAMABindingSource.List;
834            DataRowView row = datatable.AddNew();
835           
836            row["CustomerId"] = this.customerId;
837            row["DATA ZAMÓWIENIA"] = DateTime.Now;
838            row["nick name"] = kodKlienta;
839            if (Utils.czyKlientMaNrVIES(this.customerId))
840            {
841                row["VAT"] = 0;
842            }
843
844            if (User.getUser().St_handlowiec | User.getUser().St_subhandlowiec)
845            {
846                row["symbol akwizytora"] = User.getUser().Symbol_agenta;
847                row["ID Reklamy"] = this.numeryReklam["AMT"];
848
849                //Katowice
850                if (User.getUser().IdAgencji == 4)
851                {
852                    row["customerId_Sub"] = 678;
853                }
854
855                //Poznan
856                if (User.getUser().IdAgencji == 6)
857                {
858                    row["customerId_Sub"] = 18845;
859                }
860            }
861
862            row["USERID"] = User.getUser().Login;
863
864            Validate();
865         
866            rEKLAMABindingSource.EndEdit();
867            rEKLAMATableAdapter.Update(rEKLAMADataSet.REKLAMA);
868            rEKLAMABindingSource.MoveLast();
869        }
870
871        public void pokazSzczegolyZamowienia(int rekId)
872        {
873            this.nowyRekord = false;
874            this.bylyZmiany = false;
875            this.reklamaId = rekId;
876
877            this.rEKLAMATableAdapter.ClearBeforeFill = true;
878            this.rEKLAMATableAdapter.FillByReklamaId(this.rEKLAMADataSet.REKLAMA, reklamaId);
879            this.reklama_Info_DodTableAdapter.FillByReklamaId(this.rEKLAMADataSet.Reklama_Info_Dod, reklamaId);
880            this.rEKLAMA_STRONATableAdapter.FillByReklamaId(this.rEKLAMADataSet.REKLAMA_STRONA, this.reklamaId);
881            this.uKAZE_SIE_W_NRTableAdapter.FillByReklamaId(this.rEKLAMADataSet.UKAZE_SIE_W_NR, this.reklamaId);
882        }
883
884        public void wznowZamowienie(int rekId)
885        {
886            this.nowyRekord = true;
887            this.bylyZmiany = false;
888            this.juzSieUkazaloWgazecie = false;
889
890            REKLAMADataSet.REKLAMADataTable old = this.rEKLAMATableAdapter.GetDataByReklamaId(rekId);
891            DataRow oldRow = old.Rows[0];
892
893            this.rEKLAMADataSet.REKLAMA.Clear();
894            this.rEKLAMADataSet.UKAZE_SIE_W_NR.Clear();
895            this.rEKLAMADataSet.Reklama_Info_Dod.Clear();
896
897            DataView datatable = (DataView)this.rEKLAMABindingSource.List;
898            DataRowView newRow = datatable.AddNew();
899
900            this.customerId = Convert.ToInt32(oldRow["CustomerID"]);
901
902            newRow["CustomerID"] = oldRow["CustomerID"];
903            newRow["DATA ZAMÓWIENIA"] = DateTime.Now;
904            newRow["SYMBOL AKWIZYTORA"] = oldRow["SYMBOL AKWIZYTORA"];
905            newRow["ID REKLAMY"] = oldRow["ID REKLAMY"];
906            newRow["TYTU£"] = oldRow["TYTU£"];
907            newRow["TYP"] = oldRow["TYP"];
908            newRow["NICK NAME"] = oldRow["NICK NAME"];
909            newRow["SZER"] = oldRow["SZER"];
910            newRow["WYS"] = oldRow["WYS"];
911            newRow["MOD_TYP"] = oldRow["MOD_TYP"];
912            newRow["KROTNOŒÆ"] = oldRow["KROTNOŒÆ"];
913            newRow["ODSTÊP"] = oldRow["ODSTÊP"];
914            newRow["KOLOR"] = oldRow["KOLOR"];
915            newRow["RABAT"] = oldRow["RABAT"];
916            newRow["CENA JEDN"] = oldRow["CENA JEDN"];
917            newRow["VAT"] = oldRow["VAT"];
918            newRow["PROCENT PROWIZJI"] = oldRow["PROCENT PROWIZJI"];
919            newRow["PROMOCJA"] = oldRow["PROMOCJA"];
920            newRow["GRZBIET"] = oldRow["GRZBIET"];
921            newRow["STRONA"] = oldRow["STRONA"];
922            newRow["customerId_Sub"] = oldRow["customerId_Sub"];
923            newRow["ogl_dzial"] = oldRow["ogl_dzial"];
924            newRow["ogl_rozdzial"] = oldRow["ogl_rozdzial"];
925
926            //zmiana numerka
927            try
928            {
929                string starySym = newRow["Id reklamy"].ToString();
930                string symPart1 = starySym.Substring(0, starySym.Length - 2);
931                string symPart2 = starySym.Substring(starySym.Length - 2, 2);
932                int nowyNr = Int32.Parse(symPart2);
933                nowyNr++;
934                switch (nowyNr.ToString().Length)
935                {
936                    case 1:
937                        newRow["Id reklamy"] = symPart1 + "0" + nowyNr.ToString();
938                        break;
939                    default:
940                        newRow["Id reklamy"] = symPart1 + nowyNr.ToString();
941                        break;
942                }
943            }
944            catch (Exception e1)
945            {
946                newRow["Id reklamy"] = oldRow["Id reklamy"];
947            }
948
949            this.numeryReklam.Clear();
950            string nr;
951            foreach (DataRowView r in lISTATYTULOWBindingSource.List)
952            {
953                nr = nrRek(User.getUser().Symbol_agenta, r["SYMB2"].ToString().Trim() + User.getUser().Kod_agenta);
954                this.numeryReklam.Add((string)r["SYMB"], nr);
955            }
956            //autosalon œl¹ski
957            nr = nrRek(User.getUser().Symbol_agenta, "G" + User.getUser().Kod_agenta);
958            this.numeryReklam.Add("slaski", nr);
959
960            Validate();
961
962            rEKLAMABindingSource.EndEdit();
963            rEKLAMATableAdapter.Update(rEKLAMADataSet.REKLAMA);
964           // rEKLAMABindingSource.MoveLast();
965        }
966
967        private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
968        {
969            try
970            {
971                DataRowView rowView = dataGridView1[e.ColumnIndex, e.RowIndex].OwningRow.DataBoundItem as DataRowView;
972
973                if ((rowView != null) && rowView.IsNew)
974                {
975                    rowView["Data"] = DateTime.Now;
976                }
977            }
978            catch (Exception e1)
979            {
980                throw e1;
981            }
982
983            dataGridView1.Refresh();
984        }
985
986        private void dataGridView1_Leave(object sender, EventArgs e)
987        {
988            this.dataGridView1.EndEdit();
989
990            Validate();
991
992            this.rEKLAMAReklamaInfoDodBindingSource.EndEdit();
993        }
994
995        private void OrderDetails_FormClosing(object sender, FormClosingEventArgs e)
996        {
997            if (nowyRekord)
998            {
999                if (rEKLAMABindingSource.Current != null)
1000                {
1001                    rEKLAMABindingSource.RemoveCurrent();
1002                    rEKLAMABindingSource.EndEdit();
1003                    rEKLAMATableAdapter.Update(this.rEKLAMADataSet.REKLAMA);
1004                }
1005
1006            }
1007            else
1008            {
1009                //usuwa zmiany w zleceniu           
1010                rEKLAMADataSet.REKLAMA.RejectChanges();
1011                rEKLAMADataSet.UKAZE_SIE_W_NR.RejectChanges();
1012            }
1013
1014            this.obslugaZdarzen = false;
1015            liczbaEmisjiLabel.Text = "-";
1016            this.nettoBezBOTextBox.Clear();
1017            this.kurs = null;
1018            this.DialogResult = bylyZmiany ? DialogResult.OK : DialogResult.Cancel;
1019        }
1020
1021        /// <summary>
1022        /// Koloruje formularz, blokuje pola etc.
1023        /// </summary>
1024        private void zmiany()
1025        {
1026            if (this.rEKLAMABindingSource.Current != null)
1027            {
1028                DataRowView reklamRow = (DataRowView)this.rEKLAMABindingSource.Current;
1029
1030                bool zablokujProwizje = Convert.ToBoolean(reklamRow["zablokuj_prowizje"]);
1031                bool zatwiedzonoDoDruku = reklamRow["ZATWIERDZONO DO DRUKU"] == DBNull.Value ? false : Convert.ToBoolean(reklamRow["ZATWIERDZONO DO DRUKU"]);
1032                bool kierownikZatwierdzil = reklamRow["kier_zatwierdzil"] == DBNull.Value ? false : Convert.ToBoolean(reklamRow["kier_zatwierdzil"]);
1033                bool rabatPonad30 = (bool)(Convert.ToDouble(reklamRow["rabat"]) > this.maksymalnyRabat);
1034                bool wystawionoFakture = reklamRow["FAKTURA WYSTAWIONO"] == DBNull.Value ? false : Convert.ToBoolean(reklamRow["FAKTURA WYSTAWIONO"]);
1035
1036                //blokowanie prowizji
1037                pROWIZJATextBox.ReadOnly = zablokujProwizje;
1038                pROCENT_PROWIZJITextBox.ReadOnly = zablokujProwizje;
1039
1040                //oznaczenie ZD
1041                ZDpanel.BackColor = zatwiedzonoDoDruku ? Color.LightGreen : Color.Red;
1042               
1043
1044
1045                //UPRAWnienia
1046                kier_notatkaTextBox.ReadOnly = !User.getUser().St_kierownik;
1047                kier_zatwierdzilCheckBox.Enabled = User.getUser().St_kierownik;
1048                zablokuj_prowizjeCheckBox.Enabled = User.getUser().St_kierownik;
1049
1050                if (!User.getUser().St_kierownik)
1051                {
1052                    cENA_JEDNTextBox.ReadOnly = zatwiedzonoDoDruku;
1053                    rABATTextBox.ReadOnly = zatwiedzonoDoDruku;
1054
1055                    if (wystawionoFakture)
1056                    {
1057                        zablokujGroupBoxy();
1058                    }
1059                    else
1060                    {
1061                        odblokujGroupBoxy();
1062                    }
1063                }
1064
1065                blokowanieZrealizowanychEmisji();
1066            }
1067        }
1068
1069        private void ukazeSieDataGridView_KeyUp(object sender, KeyEventArgs e)
1070        {
1071            if (e.Control && e.KeyCode == Keys.C)
1072            {
1073                if (this.ukazeSieDataGridView.GetCellCount(DataGridViewElementStates.Selected) > 0)
1074                {
1075                    try
1076                    {
1077                        // Add the selection to the clipboard.
1078                        Clipboard.SetDataObject(
1079                            this.ukazeSieDataGridView.GetClipboardContent());
1080                    }
1081                    catch (System.Runtime.InteropServices.ExternalException)
1082                    {
1083
1084                    }
1085                }
1086            }
1087
1088            if (e.Control && e.KeyCode == Keys.V)
1089            {
1090                Validate();
1091
1092                rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
1093
1094                //czy user ma uprawnienia do dodawania wierszy
1095                if (ukazeSieDataGridView.AllowUserToAddRows)
1096                {
1097                    char[] rowSplitter = { '\r', '\n' };
1098                    char[] columnSplitter = { '\t' };
1099
1100                    IDataObject dataInClipboard = Clipboard.GetDataObject();
1101                    string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
1102
1103                    string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
1104
1105                    string nrWydania = "0";
1106
1107                    for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
1108                    {
1109                        string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
1110
1111                        for (int i = 0; i <= valuesInRow.Length - 1; i++)
1112                        {
1113                            if (valuesInRow[i] != "")
1114                            {
1115                                nrWydania = valuesInRow[i];
1116
1117                                try
1118                                {
1119                                    DataView datatable = (DataView)this.rEKLAMAUKAZESIEWNRBindingSource.List;
1120                                    DataRowView row = datatable.AddNew();
1121                                    row["Nr Wydania"] = nrWydania;
1122
1123                                    rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
1124                                }
1125                                catch (Exception e1)
1126                                {
1127                                    Debugger.Log(0, "ttt", e1.Message);
1128                                }
1129                            }
1130                        }
1131                    }
1132                }
1133            }
1134        }
1135
1136        private void ogl_dzialComboBox_SelectedValueChanged(object sender, EventArgs e)
1137        {
1138            if (obslugaZdarzen)
1139            {
1140                if ((ogl_dzialComboBox.SelectedValue != null) && (lISTATYTULOWBindingSource.Current != null))
1141                {
1142                    DataRowView row = (DataRowView)lISTATYTULOWBindingSource.Current;
1143                    string filter = "dzial=" + ogl_dzialComboBox.SelectedValue.ToString();
1144
1145                    switch (tYTULComboBox.SelectedValue.ToString())
1146                    {
1147                        case "AMT":
1148                            filter += " AND TYTU£='2'";
1149                            break;
1150                        case "GS":
1151                            filter += " AND TYTU£='1'";
1152                            break;
1153                        case "AGRO":
1154                            filter += " AND TYTU£='7'";
1155                            break;
1156                        case "adMot":
1157                            filter += " AND TYTU£='6'";
1158                            break;
1159                        default:
1160                            break;
1161                    }
1162
1163                    oglrozdzialyBindingSource.Filter = filter;
1164
1165                    DataRowView Rekrow = (DataRowView)rEKLAMABindingSource.Current;
1166
1167                    if (Rekrow["ogl_rozdzial"] == DBNull.Value)
1168                    {
1169                        ogl_rozdzialComboBox.SelectedIndex = -1;
1170                        ogl_rozdzialComboBox.SelectedIndex = -1;
1171                    }
1172                }
1173            }
1174        }
1175
1176        private void EmailButton_Click(object sender, EventArgs e)
1177        {
1178            StringBuilder tresc = new StringBuilder();
1179
1180            tresc.Append("Potwierdzamy przyjêcie podpisanego zamówienia na publikacjê reklamy w ");
1181
1182            if (tYTULComboBox.Text != "adMoto")
1183            {
1184                tresc.Append(" gazecie \"" + tYTULComboBox.Text + "\"");
1185            }
1186            else
1187            {
1188                tresc.Append(" portalu internetowym " + tYTULComboBox.Text);
1189            }
1190
1191            tresc.Append(" o wartoœci " + bRUTTOTextBox.Text + ".");
1192            //String.Format("{0:P}", cevent.Value);
1193
1194            tresc.AppendLine("");
1195            tresc.AppendLine("Reklamy zostan¹ wyemitowane zgodnie z umow¹ w wydaniu(-ach) nr: \n");
1196
1197            foreach (DataGridViewRow r in ukazeSieDataGridView.Rows)
1198            {
1199                if (r.Cells["nrWydaniaDataGridViewTextBoxColumn"].Value != null)
1200                {
1201                    tresc.Append(r.Cells["nrWydaniaDataGridViewTextBoxColumn"].Value.ToString() + ", ");
1202                }
1203            }
1204
1205            tresc.Remove(tresc.Length - 2, 1);
1206
1207            tresc.AppendLine(" w/w tytu³u i udokumentowane faktur¹ VAT przes³an¹ na wskazany adres.");
1208
1209            MailForm mf = new MailForm(Utils.customerId(this.reklamaId), "Potwierdzenie z³o¿enia zamówienia " + iD_REKLAMYTextBox.Text, "", tresc.ToString());
1210
1211            mf.ShowDialog();
1212        }
1213
1214        private void button3_Click(object sender, EventArgs e)
1215        {
1216            ogl_rozdzialComboBox.SelectedIndex = -1;
1217            ogl_rozdzialComboBox.SelectedIndex = -1;
1218        }
1219
1220        private void kalendarzButton_Click(object sender, EventArgs e)
1221        {
1222            if (tYTULComboBox.SelectedValue != null)
1223            {
1224                // nie moze byc Copy() bo moze zawierac wiersza do skasowania
1225                REKLAMADataSet.UKAZE_SIE_W_NRDataTable table =
1226                    (REKLAMADataSet.UKAZE_SIE_W_NRDataTable)
1227                    this.rEKLAMADataSet.UKAZE_SIE_W_NR.Copy();
1228
1229                REKLAMADataSet.REKLAMARow reklama = (REKLAMADataSet.REKLAMARow)
1230                    ((DataRowView)(this.rEKLAMABindingSource.Current)).Row;
1231
1232                KalendarzEmisji kalendarz =
1233                    new KalendarzEmisji(tYTULComboBox.SelectedValue.ToString(), reklama, table);
1234
1235                if (kalendarz.ShowDialog() == DialogResult.OK)
1236                {
1237                    DataRow[] rows = kalendarz.Emisje.Select("1=1", "[nr wydania] asc");
1238                    // this.rEKLAMADataSet.UKAZE_SIE_W_NR.Clear();
1239                    for (int i = 0; i < rows.Length; i++)
1240                    {
1241                        if (rEKLAMADataSet.UKAZE_SIE_W_NR.FindByID(
1242                            Convert.ToDecimal(rows[i]["id"])) == null)
1243                        {
1244                            this.rEKLAMADataSet.UKAZE_SIE_W_NR.ImportRow(
1245                                (REKLAMADataSet.UKAZE_SIE_W_NRRow)rows[i]);
1246                        }
1247                    }
1248
1249                    kalendarz.Close();
1250
1251                    ukazeSieDataGridView.EndEdit();
1252                    rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
1253
1254                    // int il = this.rEKLAMADataSet.UKAZE_SIE_W_NR.Count;
1255                    int il = ukazeSieDataGridView.Rows.Count;
1256                    this.kROTNOSCTextBox.Text = il.ToString();
1257                    this.liczbaEmisjiLabel.Text = il.ToString();
1258                    rEKLAMABindingSource.EndEdit();
1259
1260                    PrzeliczCaleZamowienie();
1261                }
1262            }
1263        }
1264
1265        private void nowaEmisja(object sender, DataTableNewRowEventArgs e)
1266        {
1267            ((REKLAMADataSet.UKAZE_SIE_W_NRRow)e.Row).dataDodania = DateTime.Today;
1268        }
1269
1270        private void ukazeSieDataGridView_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
1271        {
1272            DataGridViewRow clickedRow = ukazeSieDataGridView.Rows[e.RowIndex];
1273
1274            if (!ukazeSieDataGridView.SelectedRows.Contains(clickedRow))
1275            {
1276                e.ContextMenuStrip = null;
1277                return;
1278            }
1279
1280            zmienToolStripMenuItem.Visible = true;
1281            usunToolStripMenuItem.Visible = true;
1282            anulujToolStripMenuItem.Visible = false;
1283
1284            if (ukazeSieDataGridView.SelectedRows.Count == 1)
1285            {
1286                if (clickedRow.Selected)
1287                {
1288                    if (clickedRow.DefaultCellStyle.BackColor == Color.MintCream)
1289                    {
1290                        zmienToolStripMenuItem.Visible = false;
1291                        usunToolStripMenuItem.Visible = false;
1292                        anulujToolStripMenuItem.Visible = false;
1293
1294                        e.ContextMenuStrip = emisjeContextMenuStrip;
1295                        return;
1296                    }
1297
1298                    DataRowView emisja = (DataRowView)ukazeSieDataGridView.Rows[e.RowIndex].DataBoundItem;
1299                    REKLAMADataSet.UKAZE_SIE_W_NRRow emisjaRow = (REKLAMADataSet.UKAZE_SIE_W_NRRow)emisja.Row;
1300
1301                    if (emisjaRow.zafakturowana)
1302                    {
1303                        zmienToolStripMenuItem.Visible = true;
1304                        usunToolStripMenuItem.Visible = false;
1305                        anulujToolStripMenuItem.Visible = true;
1306
1307                        e.ContextMenuStrip = emisjeContextMenuStrip;
1308                        return;
1309                    }
1310                }
1311            }
1312               
1313            else if (ukazeSieDataGridView.SelectedRows.Count > 1)
1314            {
1315                zmienToolStripMenuItem.Visible = false;
1316                usunToolStripMenuItem.Visible = true;
1317                anulujToolStripMenuItem.Visible = false;
1318
1319                foreach (DataGridViewRow r in ukazeSieDataGridView.SelectedRows)
1320                {
1321                    if (r.DefaultCellStyle.BackColor == Color.MintCream)
1322                    {
1323                        usunToolStripMenuItem.Visible = false;
1324                        break;
1325                    }
1326
1327                    if (Convert.ToBoolean(((DataRowView)r.DataBoundItem)["zafakturowana"]) == true)
1328                    {
1329                        usunToolStripMenuItem.Visible = false;
1330                        break;
1331                    }
1332                }
1333
1334                e.ContextMenuStrip = emisjeContextMenuStrip;
1335                return;
1336            }
1337
1338            e.ContextMenuStrip = emisjeContextMenuStrip;
1339                 
1340        }
1341
1342        public void usunEmisje(object sender, EventArgs e)
1343        {
1344            foreach (DataGridViewRow r in ukazeSieDataGridView.SelectedRows)
1345            {
1346                ukazeSieDataGridView.Rows.Remove(r);
1347            }
1348
1349            ukazeSieDataGridView.EndEdit();
1350            rEKLAMAUKAZESIEWNRBindingSource.EndEdit();
1351
1352           // int il = this.rEKLAMADataSet.UKAZE_SIE_W_NR.Count;
1353            int il = ukazeSieDataGridView.Rows.Count;
1354            this.kROTNOSCTextBox.Text = il.ToString();
1355            this.liczbaEmisjiLabel.Text = il.ToString();
1356            rEKLAMABindingSource.EndEdit();
1357
1358            PrzeliczCaleZamowienie();
1359
1360        }
1361
1362        private void anulujEmisje(object sender, EventArgs e)
1363        {
1364            if (ukazeSieDataGridView.CurrentCell != null)
1365            {
1366                DataGridViewRow r1 = ukazeSieDataGridView.CurrentCell.OwningRow;
1367
1368                REKLAMADataSet.UKAZE_SIE_W_NRRow row = (REKLAMADataSet.UKAZE_SIE_W_NRRow)(((DataRowView)r1.DataBoundItem).Row);
1369
1370                row.status = 2;
1371                row.dataAnulowania = DateTime.Now;
1372            }
1373        }
1374
1375        private void zmienEmisje(object sender, EventArgs e)
1376        {
1377            if (tYTULComboBox.SelectedValue != null)
1378            {
1379                REKLAMADataSet.UKAZE_SIE_W_NRDataTable table =
1380                (REKLAMADataSet.UKAZE_SIE_W_NRDataTable)
1381                this.rEKLAMADataSet.UKAZE_SIE_W_NR.Copy();
1382
1383                REKLAMADataSet.REKLAMARow reklama = (REKLAMADataSet.REKLAMARow)
1384                 ((DataRowView)(this.rEKLAMABindingSource.Current)).Row;
1385
1386                KalendarzEmisji kalendarz = new KalendarzEmisji(
1387                    tYTULComboBox.SelectedValue.ToString(),
1388                    reklama,
1389                    table,
1390                    (short)ukazeSieDataGridView.CurrentCell.Value);
1391
1392                if (kalendarz.ShowDialog() == DialogResult.OK)
1393                {
1394                    if (ukazeSieDataGridView.CurrentCell != null)
1395                    {
1396                        DataGridViewRow r1 = ukazeSieDataGridView.CurrentCell.OwningRow;
1397                        REKLAMADataSet.UKAZE_SIE_W_NRRow row = (REKLAMADataSet.UKAZE_SIE_W_NRRow)(((DataRowView)r1.DataBoundItem).Row);
1398
1399                        if (row.zafakturowana && (row.Nr_Wydania != kalendarz.NowaEmisja))
1400                        {
1401                            rEKLAMADataSet.zmianyEmisji.AddzmianyEmisjiRow(row, row.Nr_Wydania, kalendarz.NowaEmisja, DateTime.Now);
1402                        }
1403
1404                        row.Nr_Wydania = kalendarz.NowaEmisja;
1405                        kalendarz.Close();       
1406                       
1407                        row.EndEdit();
1408                        this.rEKLAMADataSet.UKAZE_SIE_W_NR.EndInit();
1409                        this.rEKLAMAUKAZESIEWNRBindingSource.ResetBindings(false);
1410                    }
1411                }
1412               
1413            }
1414        }
1415             
1416
1417        private void ukazeSieDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
1418        {
1419            if (ukazaloSieDataGridView.Rows.Count > 0)
1420            {
1421                tYTULComboBox.Enabled = false;
1422            }
1423            else
1424            {
1425                tYTULComboBox.Enabled = true;
1426            }
1427        }
1428
1429        private void ukazeSieDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
1430        {
1431            blokowanieZrealizowanychEmisji();
1432        }
1433
1434        private void walutyComboBox_SelectedIndexChanged(object sender, EventArgs e)
1435        {
1436            if (obslugaZdarzen)
1437            {
1438                if (walutyComboBox.SelectedIndex != -1)
1439                {
1440                    kurs = PobierzKurs(walutyComboBox.SelectedItem.ToString());
1441                    PrzeliczCaleZamowienie();
1442                }
1443            }
1444        }
1445
1446        private void PrzeliczCaleZamowienie()
1447        {
1448            DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1449            REKLAMADataSet.REKLAMARow reklama = (REKLAMADataSet.REKLAMARow)row.Row;
1450
1451            if (walutyComboBox.SelectedIndex != 0)
1452            {
1453                 if (reklama.IsBrutto_EuroNull()){
1454                     reklama.Brutto_Euro = 0;
1455                     reklama.EndEdit();
1456                 }
1457           
1458                if (kurs != null)
1459                {
1460                    przeliczWaluteNaZlotowki(kurs);
1461                }
1462                else
1463                {
1464                    kurs = PobierzKurs(walutyComboBox.SelectedItem.ToString());
1465
1466                    if (kurs == null)
1467                    {
1468                        MessageBox.Show("Wyst¹pi³ b³¹d!");
1469                    }
1470                    else
1471                    {
1472                        przeliczWaluteNaZlotowki(kurs);
1473                    }
1474                }
1475            }
1476            else
1477            {
1478              if (row["id_faktury"] == DBNull.Value)
1479                {
1480                    row["waluta_tabela_nr"] = DBNull.Value;
1481                    row["waluta_Przelicznik"] = DBNull.Value;
1482                    row["waluta_kurs"] = DBNull.Value;
1483                    row["waluta_kurs_z_dnia"] = DBNull.Value;
1484                    row["Brutto_Euro"] = DBNull.Value;
1485                    row["Brutto_Euro_Miano"] = DBNull.Value;
1486                    row["FAKTURA ID KONTA"] = 1;
1487
1488                    rEKLAMABindingSource.EndEdit();
1489                    przelicz();
1490                }
1491                else
1492                {
1493                    MessageBox.Show("Faktura zosta³a ju¿ wystawiona!");
1494                }
1495            }
1496        }
1497
1498        private SLOWNIKDataSet.Kursy_WalutRow PobierzKurs(string kodWaluty)
1499        {         
1500            SLOWNIKDataSet.Kursy_WalutDataTable kursy = kursy_WalutTableAdapter.GetDataByDataPublikacji(
1501                kodWaluty, DateTime.Today);
1502
1503            if (kursy.Count == 0)
1504            {
1505                return null;
1506            }
1507            else
1508            {
1509                return kursy[0];
1510            }
1511        }
1512         
1513        private void brutto_EuroTextBox_Leave(object sender, EventArgs e)
1514        {
1515            rEKLAMABindingSource.EndEdit();
1516            PrzeliczCaleZamowienie();
1517        }
1518
1519        private void rABATTextBox_Leave(object sender, EventArgs e)
1520        {
1521            rEKLAMABindingSource.EndEdit();
1522            PrzeliczCaleZamowienie();
1523        }
1524
1525        private void pVATComboBox_SelectedIndexChanged(object sender, EventArgs e)
1526        {
1527            rEKLAMABindingSource.EndEdit();
1528            PrzeliczCaleZamowienie();
1529        }
1530
1531        private void cENA_JEDNTextBox_Leave(object sender, EventArgs e)
1532        {
1533            rEKLAMABindingSource.EndEdit();
1534            PrzeliczCaleZamowienie();
1535        }
1536
1537        private void wyroznienieCheckBox_Click(object sender, EventArgs e)
1538        {
1539            this.rEKLAMABindingSource.EndEdit();
1540            PrzeliczCaleZamowienie();
1541     
1542        }
1543
1544        /// <summary>
1545        /// formatowanie do decimal -> %
1546        /// </summary>
1547        private void DecimalToProcent(object sender, ConvertEventArgs cevent)
1548        {
1549            if (Convert.ToDecimal(cevent.Value) > 1)
1550            {
1551                //  cevent.Value = Convert.ToDecimal(cevent.Value) / 100;
1552            }
1553            cevent.Value = String.Format("{0:P}", cevent.Value);
1554        }
1555
1556        /// <summary>
1557        /// formatowanie do % -> decimal
1558        /// </summary>
1559        private void ProcentToDecimal(object sender, ConvertEventArgs cevent)
1560        {
1561            string pom = cevent.Value.ToString();
1562            if (cevent.Value.ToString().EndsWith("%"))
1563            {
1564                pom = cevent.Value.ToString().Substring(0, cevent.Value.ToString().Length - 1);
1565            }
1566            decimal p = Decimal.Parse(pom) / 100;
1567            cevent.Value = p.ToString();
1568        }
1569   
1570       private void init()
1571        {
1572            if (rEKLAMABindingSource.Current != null)
1573            {
1574                DataRowView row = (DataRowView)rEKLAMABindingSource.Current;
1575                REKLAMADataSet.REKLAMARow reklama = (REKLAMADataSet.REKLAMARow)row.Row;
1576                //wyliczona cena modulu
1577                double szer = reklama.SZER;
1578                double wys = reklama.WYS;
1579                int krotnosc = reklama.KROTNOŒÆ;
1580                double cenaJednostkowa = reklama.CENA_JEDN;
1581                double vatProcent = reklama.VAT;
1582                double prowizjaProcent = reklama.PROCENT_PROWIZJI;
1583                double rabatProcent = reklama.RABAT;
1584                double rabatWartosc = rabatProcent * cenaJednostkowa;
1585                double netto = (cenaJednostkowa - rabatWartosc) * krotnosc;
1586                double cenaModulu;
1587                if ((szer * wys * krotnosc) != 0)
1588                {
1589                    cenaModulu = netto / (szer * wys * krotnosc);
1590                    cenaModuluLabel.Text = String.Format("{0:C}", cenaModulu);
1591                }
1592                else
1593                {
1594                    cenaModuluLabel.Text = "---";
1595                }
1596
1597                liczbaEmisjiLabel.Text = this.rEKLAMADataSet.UKAZE_SIE_W_NR.Rows.Count.ToString();
1598
1599                this.obslugaZdarzen = false;
1600                if (!reklama.IsBrutto_Euro_MianoNull())
1601                {
1602                    walutyComboBox.SelectedItem = reklama.Brutto_Euro_Miano;
1603                }
1604                else
1605                {
1606                    walutyComboBox.SelectedIndex = 0; //PLN
1607                }
1608                this.obslugaZdarzen = true;
1609            }
1610        }
1611    }
1612}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.