using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Baza_Reklam
{
    public partial class ProvisionForm : Form
    {

        private short rok;
        private short miesiac;
        private int agencja;


        /// <summary>
        /// Klasa pomocnicza do wyświetlania w 0 - 2 w gridzie
        /// </summary>
        class Ocena
        {
            private int label;
            private Single value;

            public int Label
            {
                get { return label; }
                set { label = value; }
            }

            public Single Value
            {
                get { return value; }
                set { this.value = value; }
            }

            public Ocena(int l, Single v)
            {
                label = l;
                value = v;
            }
        }




        private static ProvisionForm provisionForm;

        public static ProvisionForm getProvisionForm(MDIBazaReklam parent)
        {
            if (provisionForm == null)
            {
                provisionForm = new ProvisionForm(parent);
            }
            return provisionForm;
        }

        public ProvisionForm(Form parent)
        {
            InitializeComponent();

            MdiParent = parent;

            pLANTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            aGENCITableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
            agencjeTableAdapter1.Connection.ConnectionString = ConnString.getConnString().Value;

            //bindowanie ocen z comboBoxColumn
            List<Ocena> oceny = new List<Ocena>();
            oceny.Add(new Ocena(0, 0));
            oceny.Add(new Ocena(1, 50));
            oceny.Add(new Ocena(2, 100));

            OCENAComboCoxColumn.DataSource = oceny;
            OCENAComboCoxColumn.DisplayMember = "label";
            OCENAComboCoxColumn.ValueMember = "value";

            dodajPlanToolStripButton.Enabled = User.getUser().St_kierownik;
            usunToolStripButton.Enabled = User.getUser().St_kierownik;
        }

        private void ProvisionForm_Load(object sender, EventArgs e)
        {
            rokToolStripTextBox.Text = DateTime.Today.Year.ToString();
            msToolStripTextBox.Text = DateTime.Today.Month.ToString();

            ((ComboBox)agencjaToolStripComboBox.Control).DataSource = agencjeTableAdapter1.GetData();
            ((ComboBox)agencjaToolStripComboBox.Control).DisplayMember = "Symbol";
            ((ComboBox)agencjaToolStripComboBox.Control).ValueMember = "ID_Agencji";

            ((ComboBox)agencjaToolStripComboBox.Control).SelectedValue = User.getUser().IdAgencji;

            if (!User.getUser().St_kierownik)
            {
                agencjaToolStripComboBox.Enabled = false;
            }

        }

/*
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().Value);

            if (e.Node != null)
            {
                switch (e.Node.Level)
                {
                    case 0:
                        e.Node.Nodes.Clear();

                        TreeNode node;

                        command.CommandText = "select distinct datepart(year,DATA_W) as rok from dbo.NR where datepart(year,DATA_W) is not null and datepart(year,DATA_W)>= 2003 order by rok desc";
                        command.Connection.Open();
                        SqlDataReader reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            node = new TreeNode(reader.GetValue(0).ToString());
                            node.Name = reader.GetValue(0).ToString();

                            for (int i = 12; i >= 1; i--)
                            {
                                TreeNode node2 = new TreeNode(i.ToString());
                                node2.Name = i.ToString();
                                //  node2.Nodes.Add(new TreeNode());
                                node.Nodes.Add(node2);
                            }
                            e.Node.Nodes.Add(node);
                        }
                        command.Connection.Close();

                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
            }
        }
*/

        /// <summary>
        ///  Pobiera dane o kontaktach i budżecie.
        /// </summary>
        private void pobierzDaneToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            SqlCommand command = new SqlCommand();
            command.Connection = new SqlConnection(ConnString.getConnString().Value);
            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "PROC_WYLICZ_I_ZAPISZ_DANE_O_KONTAKTACH_I_BUDZECIE";
            command.Parameters.AddWithValue("@rok", rok);
            command.Parameters.AddWithValue("@ms", miesiac);

            command.Connection.Open();

            SqlTransaction transaction = command.Connection.BeginTransaction();

            command.Transaction = transaction;

            try
            {
                command.ExecuteNonQuery();
                transaction.Commit();
                MessageBox.Show("Dane pobrano!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType() + ex.Message);

                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("ROLLBACK:" + ex2.GetType() + ex2.Message);
                }
            }

            command.Connection.Close();

            rEKLAMADataSet.PLAN.Clear();
            pLANTableAdapter.FillByAgencja(rEKLAMADataSet.PLAN, rok, miesiac, agencja);
            Cursor = Cursors.Default;
        }

        /// <summary>
        /// Wylicza i zapisuje prowizję agenta do planu
        /// </summary>
        private void zapiszToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            SqlCommand command = new SqlCommand();

            command.Connection = new SqlConnection(ConnString.getConnString().Value);

            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "PROC_WYLICZ_I_ZAPISZ_PROWIZJE_AGENTA_DO_PLANU";
            command.Parameters.AddWithValue("@rok", rok);
            command.Parameters.AddWithValue("@ms", miesiac);
            command.Parameters.AddWithValue("@id_Agencji", agencja);
            command.Connection.Open();

            SqlTransaction transaction = command.Connection.BeginTransaction();

            command.Transaction = transaction;

            try
            {
                command.ExecuteNonQuery();
                transaction.Commit();
                MessageBox.Show("Prowizja została zapisana!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType() + ex.Message);

                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("ROLLBACK:" + ex2.GetType() + ex2.Message);
                }
            }

            command.Connection.Close();

            rEKLAMADataSet.PLAN.Clear();

            pLANTableAdapter.FillByAgencja(rEKLAMADataSet.PLAN, rok, miesiac, agencja);

            Cursor = Cursors.Default;
        }

        private void naniesNaReklamyToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            SqlCommand command = new SqlCommand();

            command.Connection = new SqlConnection(ConnString.getConnString().Value);

            command.CommandType = CommandType.StoredProcedure;

            // command.CommandText = "PROC_WYLICZ_I_ZAPISZ_PROWIZJE_DO_REKLAM";
            command.CommandText = "PROC_WYLICZ_I_ZAPISZ_PROWIZJE";
            command.Parameters.AddWithValue("@rok", rok);
            command.Parameters.AddWithValue("@ms", miesiac);
            command.Parameters.AddWithValue("@idAgencji", agencja);

            command.Connection.Open();

            SqlTransaction transaction = command.Connection.BeginTransaction();

            command.Transaction = transaction;

            try
            {
                command.ExecuteNonQuery();
                transaction.Commit();
                MessageBox.Show("Prowizja została naniesiona na zlecenia");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType() + ex.Message);

                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("ROLLBACK:" + ex2.GetType() + ex.Message);
                }
            }

            command.Connection.Close();

            Cursor = Cursors.Default;

            MessageBox.Show("Prowizja naniesiona na zlecenia");
        }

        /*
        private void przeliczStripButton_Click(object sender, EventArgs e)
        {
            if (pLANBindingSource.List.Count != 0)
            {
                foreach (DataRowView r in pLANBindingSource.List)
                {
                    REKLAMADataSet.PLANRow row = (REKLAMADataSet.PLANRow)r.Row;
                    przelicz(row);
                    pLANBindingSource.EndEdit();
                }
            }

        }*/

        /*
        private void przelicz(REKLAMADataSet.PLANRow row)
        {

            row.ECP = 0;
            row.ESP = 0;
            row.ELK = 0;
            row.EP = 0;
            row.EPK = 0;
            row.EPR = 0;
            row.WH = 0;
            row.QO = 0;
            row.OCENA = 0;

            if (row.IsNull("BUDZET")) { row.BUDZET = 0; }
            if (row.IsNull("REKLAMY")) { row.REKLAMY = 0; }
            if (row.IsNull("W_REKLAMY")) { row.W_REKLAMY = 0; }
            if (row.IsNull("W_BUDZET")) { row.W_BUDZET = 0; }
            if (row.IsNull("N_KLIENCI")) { row.N_KLIENCI = 0; }
            if (row.IsNull("W_N_KLIENCI")) { row.W_N_KLIENCI = 0; }
            if (row.IsNull("W_SPOTKANIA")) { row.W_SPOTKANIA = 0; }
            if (row.IsNull("W_TEL")) { row.W_TEL = 0; }
            if (row.IsNull("W_EMAIL")) { row.W_EMAIL = 0; }
            if (row.IsNull("W_FAX")) { row.W_FAX = 0; }
            if (row.IsNull("W_LIST")) { row.W_LIST = 0; }
            if (row.IsNull("KONTAKTY")) { row.KONTAKTY = 0; }


            float W_BUDZET = Convert.ToSingle(row.W_BUDZET);
            float BUDZET = Convert.ToSingle(row.BUDZET);
            float W_REKLAMY = Convert.ToSingle(row.W_REKLAMY);
            float REKLAMY = Convert.ToSingle(row.REKLAMY);
            float W_N_KLIENCI = Convert.ToSingle(row.W_N_KLIENCI);
            float N_KLIENCI = Convert.ToSingle(row.N_KLIENCI);
            float W_SPOTKANIA = Convert.ToSingle(row.W_SPOTKANIA);
            float W_TEL = Convert.ToSingle(row.W_TEL);
            float W_EMAIL = Convert.ToSingle(row.W_EMAIL);
            float W_FAX = Convert.ToSingle(row.W_FAX);
            float W_LIST = Convert.ToSingle(row.W_LIST);
            float KONTAKTY = Convert.ToSingle(row.KONTAKTY);
            float JAKOSC_KORESPONDENCJI = Convert.ToSingle(row.JAKOSC_KORESPONDENCJI);
            float JAKOSC_OFERT = Convert.ToSingle(row.JAKOSC_OFERT);
            float JAKOSC_ROZMOWY_TEL = Convert.ToSingle(row.JAKOSC_ROZMOWY_TEL);
            float JAKOSC_SPOTKAN = Convert.ToSingle(row.JAKOSC_SPOTKAN);
            float WIEDZA_O_FIRMIE = Convert.ToSingle(row.WIEDZA_O_FIRMIE);
            float WIEDZA_O_PRODUKCIE = Convert.ToSingle(row.WIEDZA_O_PRODUKCIE);
            float WIEDZA_O_RYNKU = Convert.ToSingle(row.WIEDZA_O_RYNKU);
            float WIEDZA_OGOLNO_HANDLOWA = Convert.ToSingle(row.WIEDZA_OGOLNO_HANDLOWA);

            if (BUDZET != 0)
            {
                row.EP = 100 * W_BUDZET / BUDZET;
            }

            if (REKLAMY != 0)
            {
                row.ESP = 100 * W_REKLAMY / REKLAMY;
            }

            if ((REKLAMY != 0) && (W_REKLAMY != 0))
            {
                row.EPR = 100 * (W_BUDZET/W_REKLAMY)/(BUDZET/REKLAMY);
            }

            if (N_KLIENCI != 0) 
            {
                row.EPK = 100 * (W_N_KLIENCI/N_KLIENCI);
            }

            row.ECP = 100 * ((120 * W_SPOTKANIA + 5 * W_TEL + 30 * W_EMAIL + 30 * W_FAX + 30 * W_LIST) / (20 * 7 * 60));

            if (KONTAKTY != 0) 
            { 
                row.ELK = 100 * ((W_SPOTKANIA + W_TEL + W_EMAIL + W_FAX + W_LIST) / KONTAKTY);
            }

            row.QO = 100 * (JAKOSC_KORESPONDENCJI / 10 + JAKOSC_OFERT / 10 + JAKOSC_ROZMOWY_TEL / 10 + JAKOSC_SPOTKAN / 10) / 4;
            row.WH = 100 * (WIEDZA_O_FIRMIE / 10 + WIEDZA_O_PRODUKCIE / 10 + WIEDZA_O_RYNKU / 10 + WIEDZA_OGOLNO_HANDLOWA / 10) / 4;
            row.OCENA = (float)Math.Round(((row.WH + row.QO + row.ELK + row.ECP + row.EPK + 1 * row.EPR + 1 * row.ESP + 1 * row.EP) / 8), 2);
        }
        */

        private void dodajPlanToolStripButton_Click(object sender, EventArgs e)
        {
            OcenaAddForm oaf = new OcenaAddForm();
            oaf.Show();
        }

        private void usunToolStripButton_Click(object sender, EventArgs e)
        {
            if (pLANBindingSource.Current != null)
            {
                if (MessageBox.Show("Czy na pewno usunąć rekord?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    pLANBindingSource.RemoveCurrent();
                    pLANBindingSource.EndEdit();
                    pLANTableAdapter.Update(rEKLAMADataSet.PLAN);
                }
            }
        }

        private void zapiszDaneToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            if (Validate())
            {
                pLANBindingSource.EndEdit();
                pLANTableAdapter.Update(rEKLAMADataSet.PLAN);
            }
            Cursor = Cursors.Default;

            MessageBox.Show("Zmiany zapisane");
        }

/*
        private void ukryjKolumnyToolStripButton_Click(object sender, EventArgs e)
        {
            rOKDataGridViewTextBoxColumn.Visible = rOKDataGridViewTextBoxColumn.Visible ? false : true;

            mSDataGridViewTextBoxColumn.Visible = mSDataGridViewTextBoxColumn.Visible ? false : true;
        }
*/

        private void prowizjeDataGridView_Leave(object sender, EventArgs e)
        {
            //ukrytyLabel.Select();
            prowizjeDataGridView.EndEdit();
            //pLANBindingSource.EndEdit();

            REKLAMADataSet.PLANDataTable changes = rEKLAMADataSet.PLAN.GetChanges(DataRowState.Modified) as
                REKLAMADataSet.PLANDataTable;

            if (changes != null)
            {
                if (Validate())
                {
                    pLANBindingSource.EndEdit();
                    pLANTableAdapter.Update(rEKLAMADataSet.PLAN);
                }
            }
        }

        /// <summary>
        /// Podsumowuje budżet do wykonania
        /// </summary>
        private void uzupelnijBDsuma()
        {
            int kasa = 0;
            foreach (DataRowView r in pLANBindingSource.List)
            {
                kasa += Convert.ToInt32(r["BUDZET"]);
            }
            BDsumaTextBox.Text = String.Format("{0:C}", kasa);
        }

        /// <summary>
        /// Podsumowuje budżet wykonany
        /// </summary>
        private void uzupelnijBDWykonanySuma()
        {
            int kasa = 0;
            foreach (DataRowView r in pLANBindingSource.List)
            {
                kasa += Convert.ToInt32(r["W_BUDZET"]);
            }
            BDWykonanySumaTextBox.Text = String.Format("{0:C}", kasa);
        }

        /// <summary>
        /// Zamienia wpisaną liczbę do decimal
        /// </summary>
        private void prowizjeDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            ukrytyLabel.Select();
            prowizjeDataGridView.EndEdit();
            pLANBindingSource.EndEdit();

            if (prowizjeDataGridView.Columns[e.ColumnIndex].Name == "PR_PROWIZJI")
            {

                if (Convert.ToDecimal(prowizjeDataGridView[e.ColumnIndex, e.RowIndex].Value) >= 1)
                {
                    prowizjeDataGridView[e.ColumnIndex, e.RowIndex].Value = Convert.ToDecimal(prowizjeDataGridView[e.ColumnIndex, e.RowIndex].Value) / 100;
                }
            }

            REKLAMADataSet.PLANDataTable changes = rEKLAMADataSet.PLAN.GetChanges(DataRowState.Modified) as
                 REKLAMADataSet.PLANDataTable;

            if (changes != null)
            {
                pLANBindingSource.EndEdit();
                pLANTableAdapter.Update(rEKLAMADataSet.PLAN);
            }
        }

        private void pLANBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            uzupelnijBDsuma();
            uzupelnijBDWykonanySuma();
        }


        private void szukajToolStripButton_Click(object sender, EventArgs e)
        {
            if (rokToolStripTextBox.Text != "")
            {
                short i;
                if (!Int16.TryParse(rokToolStripTextBox.Text, out i))
                {
                    MessageBox.Show("Podaj prawidłowy rok.");
                    Cursor = Cursors.Default;
                    return;
                }
            }
            else
            {
                MessageBox.Show("Podaj prawidłowy rok.");
                Cursor = Cursors.Default;
                return;
            }

            if (msToolStripTextBox.Text != "")
            {
                short i;
                if (!Int16.TryParse(msToolStripTextBox.Text, out i))
                {
                    MessageBox.Show("Podaj prawidłowy miesiąc.");
                    Cursor = Cursors.Default;
                    return;
                }
            }
            else
            {
                MessageBox.Show("Podaj prawidłowy miesiąc.");
                Cursor = Cursors.Default;
                return;
            }

            if (((ComboBox)agencjaToolStripComboBox.Control).SelectedValue == null)
            {
                MessageBox.Show("Wybierz agencję");
                return;
            }

            rok = Convert.ToInt16(rokToolStripTextBox.Text);
            miesiac = Convert.ToInt16(msToolStripTextBox.Text);
            agencja = Convert.ToInt32(((ComboBox)agencjaToolStripComboBox.Control).SelectedValue);

            uprawnienia();

            sLOWNIKDataSet.AGENCI.Clear();
            rEKLAMADataSet.PLAN.Clear();

            aGENCITableAdapter.FillByIdAgencji(sLOWNIKDataSet.AGENCI, agencja);
            pLANTableAdapter.FillByAgencja(rEKLAMADataSet.PLAN, rok, miesiac, agencja);
        }

        /*
        private void szukajToolStripButton_Click(object sender, EventArgs e)
        {            
            this.Cursor = Cursors.WaitCursor;

            string query = "SELECT   top 1000 [PLAN].* FROM AGENCJE INNER JOIN AGENCI ON AGENCJE.Id_agencji = AGENCI.ID_AGENCJI " +
                " INNER JOIN [PLAN] ON AGENCI.Symbol = [PLAN].SYMBOL WHERE  1 = 1 ";

            SqlCommand command = new SqlCommand();
            command.CommandText = query;

            if (rokToolStripTextBox.Text != "")
            {
                short i;
                if (!Int16.TryParse(rokToolStripTextBox.Text, out i))
                {
                    MessageBox.Show("Podaj prawidłowy rok.");
                    this.Cursor = Cursors.Default;
                    return;
                }
                command.CommandText += " AND [PLAN].rok = @rok ";
                command.Parameters.AddWithValue("@rok", rokToolStripTextBox.Text);
                this.rok = Convert.ToInt16(rokToolStripTextBox.Text);
            }            

            if (msToolStripTextBox.Text != "")
            {
                short i;
                if (!Int16.TryParse(msToolStripTextBox.Text, out i))
                {
                    MessageBox.Show("Podaj prawidłowy miesiąc.");
                    this.Cursor = Cursors.Default;
                    return;
                }
                command.CommandText += " AND [PLAN].ms = @ms ";
                command.Parameters.AddWithValue("ms", msToolStripTextBox.Text);
                this.miesiac = Convert.ToInt16(msToolStripTextBox.Text);
            }

            if (((ComboBox)agencjaToolStripComboBox.Control).SelectedValue == null)
            {
                MessageBox.Show("Wybierz agencję");
                return;
            }
            else
            {
                command.CommandText += " AND AGENCJE.Id_Agencji=@agencja";
                command.Parameters.AddWithValue("@agencja", ((ComboBox)agencjaToolStripComboBox.Control).SelectedValue);
                this.agencja = Convert.ToInt32(((ComboBox)agencjaToolStripComboBox.Control).SelectedValue);
            }

          //  uprawnienia();    
            poblokujWiersze();
                     
            
            this.sLOWNIKDataSet.AGENCI.Clear();
            this.aGENCITableAdapter.FillByIdAgencji(this.sLOWNIKDataSet.AGENCI, this.agencja);

            this.rEKLAMADataSet.PLAN.Clear();
           // this.pLANTableAdapter.FillByAgencja(this.rEKLAMADataSet.PLAN, this.rok, this.miesiac, this.agencja);

            command.Connection = new SqlConnection(ConnString.getConnString().Value);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = command;

            adapter.Fill(rEKLAMADataSet.PLAN);

            this.Cursor = Cursors.Default;   
        }

        private void poblokujWiersze()
        {
            if (User.getUser().St_kierownik)
            {
                prowizjeDataGridView.ReadOnly = false;
                foreach (DataGridViewRow r in prowizjeDataGridView.Rows)
                {
                    if (!((Convert.ToInt32(r.Cells["rOKDataGridViewTextBoxColumn"].Value) > DateTime.Today.Year) |
                        ((Convert.ToInt32(r.Cells["rOKDataGridViewTextBoxColumn"].Value) == DateTime.Today.Year) & (Convert.ToInt32(r.Cells["mSDataGridViewTextBoxColumn"].Value) >= DateTime.Today.Month - 1)) |
                        ((Convert.ToInt32(r.Cells["rOKDataGridViewTextBoxColumn"].Value) == DateTime.Today.Year - 1) & (Convert.ToInt32(r.Cells["mSDataGridViewTextBoxColumn"].Value) == 12) & (DateTime.Today.Month == 1))))
                    {
                        r.ReadOnly = true;
                    }
                }
            }
        }
        */

        private void uprawnienia()
        {
            if (User.getUser().St_kierownik)
            {

                if (!((rok > DateTime.Today.Year) |
                    ((rok == DateTime.Today.Year) & (miesiac >= DateTime.Today.Month - 1)) |
                    ((rok == DateTime.Today.Year - 1) & (miesiac == 12) & (DateTime.Today.Month == 1))))
                {
                    OCENAComboCoxColumn.Visible = false;
                    oCENADataGridViewTextBoxColumn.Visible = true;

                    prowizjeDataGridView.ReadOnly = true;
                    usunToolStripButton.Enabled = false;
                    pobierzDaneToolStripButton.Enabled = false;
                    naniesNaReklamyToolStripButton.Enabled = false;
                    zapiszDaneToolStripButton.Enabled = false;
                    zapiszToolStripButton.Enabled = false;

                }
                else
                {
                    OCENAComboCoxColumn.Visible = true;
                    oCENADataGridViewTextBoxColumn.Visible = false;
                    prowizjeDataGridView.ReadOnly = false;

                    usunToolStripButton.Enabled = true;
                    pobierzDaneToolStripButton.Enabled = true;
                    naniesNaReklamyToolStripButton.Enabled = true;
                    zapiszDaneToolStripButton.Enabled = true;
                    zapiszToolStripButton.Enabled = true;

                    stareProwizjeToolStripButton.Enabled = true;
                }
            }
        }

        private void prowizjeDataGridView_DataError_1(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Wprowadź poprawne dane");
        }

        private void ProvisionForm_Leave(object sender, EventArgs e)
        {
            prowizjeDataGridView.EndEdit();
            //pLANBindingSource.EndEdit();

            REKLAMADataSet.PLANDataTable changes = rEKLAMADataSet.PLAN.GetChanges(DataRowState.Modified) as
                REKLAMADataSet.PLANDataTable;

            if (changes != null)
            {
                if (Validate())
                {
                    pLANBindingSource.EndEdit();
                    pLANTableAdapter.Update(rEKLAMADataSet.PLAN);
                }
            }
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            PrintDGV.Print_DataGridView(prowizjeDataGridView, 50);
        }

        private void wyczyscToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void przeliczStripButton_Click(object sender, EventArgs e)
        {

        }

        private void stareProwizjeToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            SqlCommand command = new SqlCommand();

            command.Connection = new SqlConnection(ConnString.getConnString().Value);

            command.CommandType = CommandType.StoredProcedure;

            command.CommandText = "PROC_WYLICZ_I_ZAPISZ_PROWIZJE_DO_REKLAM";
            command.Parameters.AddWithValue("@rok", rok);
            command.Parameters.AddWithValue("@ms", miesiac);

            command.Connection.Open();

            SqlTransaction transaction = command.Connection.BeginTransaction();

            command.Transaction = transaction;

            try
            {
                command.ExecuteNonQuery();
                transaction.Commit();
                MessageBox.Show("Prowizja została naniesiona na zlecenia");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.GetType() + ex.Message);

                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex2)
                {
                    MessageBox.Show("ROLLBACK:" + ex2.GetType() + ex.Message);
                }
            }

            command.Connection.Close();

            Cursor = Cursors.Default;

            MessageBox.Show("Prowizja naniesiona na zlecenia");
        }
    }
}