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

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