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

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