Index: trunk/SQL/StoredProcedures/sp_AddUpdateIssueChange.txt
===================================================================
--- trunk/SQL/StoredProcedures/sp_AddUpdateIssueChange.txt (revision 764)
+++ trunk/SQL/StoredProcedures/sp_AddUpdateIssueChange.txt (revision 764)
@@ -0,0 +1,53 @@
+
+SET ANSI_NULLS ON
+GO
+SET QUOTED_IDENTIFIER ON
+GO
+
+-- =============================================
+-- Author:		marek
+-- Create date: 22/07/2009
+-- Description:	Dodaje lub uaktualnia rekord zmiany emisji w tabeli ZmianyEmisji
+-- =============================================
+ALTER PROCEDURE [dbo].[sp_AddUpdateIssueChange]
+	-- Add the parameters for the stored procedure here
+	@issueId DECIMAL(18,0),
+	@currentIssueNumer INT,
+	@newIssueNumber INT
+AS
+BEGIN
+	-- SET NOCOUNT ON added to prevent extra result sets from
+	-- interfering with SELECT statements.
+	SET NOCOUNT ON;
+
+    -- Insert statements for procedure here
+	DECLARE @rows INT
+
+	SELECT @rows = COUNT(Id) FROM zmianyEmisji WHERE idEmisji=@issueId AND YEAR(dataZmiany)=YEAR(GETDATE()) AND MONTH(dataZmiany)=MONTH(GETDATE())
+
+	IF @rows>0
+		BEGIN
+			UPDATE zmianyEmisji 
+			SET nastNrWyd=@newIssueNumber, dataZmiany=GETDATE()
+			WHERE idEmisji=@issueId AND YEAR(dataZmiany)=YEAR(GETDATE()) AND MONTH(dataZmiany)=MONTH(GETDATE())
+
+			--sprawdz czy nowy numer emisji nie jest taki sam jak stary numer, jesli tak to usun zmiane emisji
+			DECLARE @issueNumber INT
+			SELECT @issueNumber=popNrWyd FROM zmianyEmisji WHERE idEmisji=@issueId AND YEAR(dataZmiany)=YEAR(GETDATE()) AND MONTH(dataZmiany)=MONTH(GETDATE())
+			IF @issueNumber=@newIssueNumber
+				DELETE FROM zmianyEmisji WHERE idEmisji=@issueId AND YEAR(dataZmiany)=YEAR(GETDATE()) AND MONTH(dataZmiany)=MONTH(GETDATE())
+		END
+	ELSE 
+		BEGIN
+			INSERT INTO zmianyEmisji(idEmisji, popNrWyd, nastNrWyd, dataZmiany)
+			VALUES(@issueId, @currentIssueNumer, @newIssueNumber, GETDATE())
+		END
+
+END
+GO
+
+SET ANSI_NULLS OFF
+GO
+SET QUOTED_IDENTIFIER OFF
+GO
+
Index: trunk/BazaReklam/OrderDetails.cs
===================================================================
--- trunk/BazaReklam/OrderDetails.cs (revision 750)
+++ trunk/BazaReklam/OrderDetails.cs (revision 764)
@@ -1276,7 +1276,7 @@
                     if (emisjaRow.zafakturowana)
                     {
-                        zmienToolStripMenuItem.Visible = true;
+                        zmienToolStripMenuItem.Visible = emisjaRow.status == 0;
                         usunToolStripMenuItem.Visible = false;
-                        anulujToolStripMenuItem.Visible = true;
+                        anulujToolStripMenuItem.Visible = emisjaRow.status == 0;
 
                         e.ContextMenuStrip = emisjeContextMenuStrip;
@@ -1360,9 +1360,9 @@
                                                             emisja.Nr_Wydania);
 
-            if (kalendarz.ShowDialog() == DialogResult.OK)
+            if (kalendarz.ShowDialog() == DialogResult.OK && kalendarz.NowaEmisja.HasValue)
             {
                 short staraEmisja = emisja.Nr_Wydania;
 
-                emisja.Nr_Wydania = kalendarz.NowaEmisja;
+                emisja.Nr_Wydania = kalendarz.NowaEmisja.Value;
                 emisja.EndEdit();
                 rEKLAMADataSet.UKAZE_SIE_W_NR.EndInit();
@@ -1370,8 +1370,5 @@
 
                 if (emisja.zafakturowana && (staraEmisja != kalendarz.NowaEmisja))
-                    rEKLAMADataSet.zmianyEmisji.AddzmianyEmisjiRow(emisja,
-                                                                   staraEmisja,
-                                                                   kalendarz.NowaEmisja,
-                                                                   DateTime.Now);
+                    ReklamaHelper.AddUpdateIssueChange((double)emisja.ID, staraEmisja, kalendarz.NowaEmisja.Value);
             }
         }
Index: trunk/BazaReklam/ZamowieniaForm.cs
===================================================================
--- trunk/BazaReklam/ZamowieniaForm.cs (revision 732)
+++ trunk/BazaReklam/ZamowieniaForm.cs (revision 764)
@@ -1225,5 +1225,5 @@
                 usunKorekte(faktura);
             else
-                usunFakture(faktura);
+                UsunFakture(faktura);
 
             Cursor = Cursors.Default;
@@ -1288,5 +1288,5 @@
         }
 
-        private void usunFakture(REKLAMADataSet.FAKTURYRow faktura)
+        private void UsunFakture(REKLAMADataSet.FAKTURYRow faktura)
         {
             int idFakt = faktura.ID_FAKTURY;
@@ -1408,5 +1408,5 @@
                 DataRow[] rows = rEKLAMADataSet.FAKTURY.Select("id_faktury=" + id);
                 if (rows.Length > 0)
-                    usunFakture((REKLAMADataSet.FAKTURYRow) rows[0]);
+                    UsunFakture((REKLAMADataSet.FAKTURYRow) rows[0]);
                 
                 RestoreConnections();
Index: trunk/BazaReklam/Classes/Helpers/ReklamaHelper.cs
===================================================================
--- trunk/BazaReklam/Classes/Helpers/ReklamaHelper.cs (revision 750)
+++ trunk/BazaReklam/Classes/Helpers/ReklamaHelper.cs (revision 764)
@@ -68,4 +68,34 @@
 
         }
+
+        public static void AddUpdateIssueChange(double issueId, int currentIssueNumer, int newIssueNumber)
+        {
+            SqlConnection conn = null;
+            SqlCommand cmd = null;
+
+            try
+            {
+                conn = new SqlConnection(ConnString.getConnString().Value);
+                conn.Open();
+                cmd = new SqlCommand("sp_AddUpdateIssueChange", conn);
+                cmd.CommandType = CommandType.StoredProcedure;
+                cmd.Parameters.AddWithValue("@issueId", issueId);
+                cmd.Parameters.AddWithValue("@currentIssueNumer", currentIssueNumer);
+                cmd.Parameters.AddWithValue("@newIssueNumber", newIssueNumber);
+                cmd.ExecuteNonQuery();
+            }
+            finally
+            {
+                if (cmd != null)
+                    cmd.Dispose();
+
+                if (conn != null && conn.State == ConnectionState.Open)
+                {
+                    conn.Close();
+                    conn.Dispose();
+                }
+            }
+
+        }
     }
 }
Index: trunk/BazaReklam/KalendarzEmisji.cs
===================================================================
--- trunk/BazaReklam/KalendarzEmisji.cs (revision 732)
+++ trunk/BazaReklam/KalendarzEmisji.cs (revision 764)
@@ -9,19 +9,4 @@
     public partial class KalendarzEmisji : Form
     {
-        //emisja, która ukazala sie w gazecie
-        public readonly string WYDANA = "wydana";
-        //emisja, ktora sie dopiero ukaze
-        public readonly string NIEWYDANA = "niewydana";
-        //emisja zafakturowana
-        public readonly string ZAFAKTUROWANA = "zafakturowana";
-        //emisja zafakturowana i wydana
-        public readonly string ZAFAKTUROWANA_WYDANA = "zafakturowana_wydana";
-        //emisja siê nie ukazala, ale jest w miesiacu, ktory jest juz zafakturowany
-        public readonly string MS_ZAFAKTUROWANY_NIEWYDANA = "zafakturowana_wydana";
-        //emisja do zmiany
-        public readonly string DO_ZMIANY = "do_zmiany";
-        //emisja do zmiany
-        public readonly string NOWA = "nowa";
-
         string tytul;
 
@@ -37,5 +22,5 @@
 
         readonly short emisjaDoZmiany;
-        short nowaEmisja;
+        short? nowaEmisja;
 
         int iloscEmisji;
@@ -47,5 +32,5 @@
         #region properties
 
-        public short NowaEmisja
+        public short? NowaEmisja
         {
             get { return nowaEmisja; }
@@ -117,6 +102,4 @@
             nRDataGridView.CellValueChanged += nRDataGridView_CellValueChanged;
             nRDataGridView.CellContentClick += nRDataGridView_CellContentClicked;
-
-           // nRBindingSource.ResetBindings(false);
         }
 
@@ -155,49 +138,66 @@
         private void zmienButton_Click(object sender, EventArgs e)
         {
-            foreach (REKLAMADataSet.UKAZE_SIE_W_NRRow r in emisje)
-            {
-                if (r.RowState != DataRowState.Deleted)
-                {
-                    if (r.Nr_Wydania == emisjaDoZmiany)
-                    {
-                        r.Nr_Wydania = nowaEmisja;
-                        r.EndEdit();
-                        break;
-                    }
-                }
-            }
+            //foreach (REKLAMADataSet.UKAZE_SIE_W_NRRow r in emisje)
+            //{
+            //    if (r.RowState != DataRowState.Deleted)
+            //    {
+            //        if (r.Nr_Wydania == emisjaDoZmiany && nowaEmisja.HasValue)
+            //        {
+            //            r.Nr_Wydania = nowaEmisja.Value;
+            //            r.EndEdit();
+            //            break;
+            //        }
+            //    }
+            //}
+            nowaEmisja = GetNewIssueNumber();
+
+        }
+
+        private short? GetNewIssueNumber()
+        {
+            short? newIssueNumer = null;
+            bool? isIssueChecked;
+
+            foreach (DataGridViewRow row in nRDataGridView.Rows)
+            {
+                if (row.ReadOnly || (row.DefaultCellStyle.BackColor != Color.White && row.DefaultCellStyle.BackColor != Color.LightYellow)) continue;
+
+                isIssueChecked = (bool?)row.Cells[3].Value;
+                if (!isIssueChecked.HasValue || !isIssueChecked.Value) continue;
+
+                newIssueNumer = (short)row.Cells[0].Value;
+                break;
+            }
+
+            return newIssueNumer;
         }
 
         private void nRDataGridView_CellContentClicked(object sender, DataGridViewCellEventArgs e)
         {
-            if (e.RowIndex >= 0)
-            {
-                if (!nRDataGridView.Rows[e.RowIndex].ReadOnly)
-                {
-                    if (e.ColumnIndex == 3)
-                    {
-                        if (nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor == Color.White)
-                        {
-                            if (emisjaDoZmiany != 0 && index != -1)
-                            {
-                                nRDataGridView.Rows[index].Cells[3].Value = false;
-                                nRDataGridView.Rows[index].DefaultCellStyle.BackColor = Color.White;
-                            }
-
-                            nowaEmisja = (short)nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
-                            index = e.RowIndex;
-
-                            nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightYellow;
-                        }
-                        else
-                        {
-                            nowaEmisja = (short)nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
-                            index = e.RowIndex;
-
-                            nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
-                        }
-
-                    }
-                }
+            if (e.RowIndex < 0) return;
+
+            if (nRDataGridView.Rows[e.RowIndex].ReadOnly) return;
+
+            if (e.ColumnIndex != 3) return;
+
+            if (nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor == Color.White)
+            {
+                if (emisjaDoZmiany != 0 && index != -1)
+                {
+                    nRDataGridView.Rows[index].Cells[3].Value = false;
+                    nRDataGridView.Rows[index].DefaultCellStyle.BackColor = Color.White;
+                }
+
+                nowaEmisja = (short) nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
+                index = e.RowIndex;
+
+                nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightYellow;
+            }
+            else
+            {
+                nowaEmisja = (short) nRDataGridView.Rows[e.RowIndex].Cells[0].Value;
+                index = e.RowIndex;
+
+                nRDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
             }
             nRDataGridView.EndEdit();
@@ -206,81 +206,74 @@
         private void nRDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
         {
-            if (e.ColumnIndex == 3)
-            {
-                if (((bool)nRDataGridView[e.ColumnIndex, e.RowIndex].Value))
-                {
-                    iloscEmisji++;
-                }
-                else
-                {
-                    iloscEmisji--;
-                }
-
-                label2.Text = iloscEmisji.ToString();
-            }
+            if (e.ColumnIndex != 3) return;
+
+            if (((bool) nRDataGridView[e.ColumnIndex, e.RowIndex].Value))
+                iloscEmisji++;
+            else
+                iloscEmisji--;
+
+            label2.Text = iloscEmisji.ToString();
         }
 
         private void nRDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
         {
-            if (e.ListChangedType == ListChangedType.Reset)
-            {
-                foreach (DataGridViewRow row in nRDataGridView.Rows)
-                {
-                    row.DefaultCellStyle.BackColor = Color.White;
-
-                    row.Cells[2].Value = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames
-                        [(int)Convert.ToDateTime(row.Cells[1].Value).DayOfWeek];
-
-                    DataRow[] table = emisje.Select("[Nr Wydania]=" + row.Cells[0].Value);
-
-                    if (table != null && table.Length > 0)
+            if (e.ListChangedType != ListChangedType.Reset) return;
+
+            foreach (DataGridViewRow row in nRDataGridView.Rows)
+            {
+                row.DefaultCellStyle.BackColor = Color.White;
+
+                row.Cells[2].Value = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames
+                    [(int) Convert.ToDateTime(row.Cells[1].Value).DayOfWeek];
+
+                DataRow[] table = emisje.Select("[Nr Wydania]=" + row.Cells[0].Value);
+
+                if (table != null && table.Length > 0)
+                {
+                    row.ReadOnly = true;
+                    row.Cells[3].Value = true;
+                    row.DefaultCellStyle.BackColor = Color.WhiteSmoke;
+
+                    REKLAMADataSet.UKAZE_SIE_W_NRRow em =
+                        (REKLAMADataSet.UKAZE_SIE_W_NRRow) table[0];
+
+                    if (Convert.ToDateTime(row.Cells[1].Value) < DateTime.Today)
+                    {
+                        row.DefaultCellStyle.BackColor = Color.MintCream;
+                    }
+                    else
+                    {
+                        if (em.zafakturowana)
+                        {
+                            row.DefaultCellStyle.BackColor = Color.LightGray;
+                        }
+
+                        //anulowana
+                        if (em.status == 2)
+                        {
+                            row.DefaultCellStyle.ForeColor = Color.Red;
+                        }
+
+                        if (emisjaDoZmiany == (short) row.Cells[0].Value)
+                        {
+                            row.DefaultCellStyle.BackColor = Color.MistyRose;
+                        }
+                    }
+                }
+                else if (emisjaDoZmiany == 0)
+                {
+                    //emisja nie zosta³a wczeniej wybrana, ale jej misi¹c ju¿
+                    // jest zafakturowany
+                    DateTime dataWydania = (DateTime) row.Cells[1].Value;
+                    if (dataWydania.Year <= dataOstatatniejZafakturowanejEmisji.Year
+                        &&
+                        dataWydania.Month <= dataOstatatniejZafakturowanejEmisji.Month)
                     {
                         row.ReadOnly = true;
-                        row.Cells[3].Value = true;
-                        row.DefaultCellStyle.BackColor = Color.WhiteSmoke;
-
-                        REKLAMADataSet.UKAZE_SIE_W_NRRow em =
-                            (REKLAMADataSet.UKAZE_SIE_W_NRRow)table[0];
-
-                        if (Convert.ToDateTime(row.Cells[1].Value) < DateTime.Today)
-                        {
-                            row.DefaultCellStyle.BackColor = Color.MintCream;
-                        }
-                        else
-                        {
-                            if (em.zafakturowana)
-                            {
-                                row.DefaultCellStyle.BackColor = Color.LightGray;
-                            }
-
-                            //anulowana
-                            if (em.status == 2)
-                            {
-                                row.DefaultCellStyle.ForeColor = Color.Red;
-                            }
-
-                            if (emisjaDoZmiany == (short)row.Cells[0].Value)
-                            {
-                                row.DefaultCellStyle.BackColor = Color.MistyRose;
-                            }
-                        }
-                    }
-                    else if (emisjaDoZmiany == 0)
-                    {
-                        //emisja nie zosta³a wczeniej wybrana, ale jej misi¹c ju¿
-                        // jest zafakturowany
-                        DateTime dataWydania = (DateTime)row.Cells[1].Value;
-                        if (dataWydania.Year <= dataOstatatniejZafakturowanejEmisji.Year
-                            &&
-                            dataWydania.Month <= dataOstatatniejZafakturowanejEmisji.Month)
-                        {
-                            row.ReadOnly = true;
-                            row.DefaultCellStyle.BackColor = Color.LightGray;
-                        }
-                    }
-                }
-            }
-        }
-
+                        row.DefaultCellStyle.BackColor = Color.LightGray;
+                    }
+                }
+            }
+        }
     }
 }
