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

Wersja 31, 61.5 KB (wprowadzona przez dorota, 17 years temu)

binding w OrderDetails?

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