//private UnionReportT5Repository _repoUnionReportT5 = new UnionReportT5Repository(SetupProgram.Connection);

        public fmUnionReport(Form owner, UnionReportCt unionReportCt)
        {
            InitializeComponent();
            this.SingleFormMode(owner);
            this.BaseFormStyle("Об'єднана звітність. Таблиці");
            _unionReportCt = unionReportCt;
        }
        //Получить данные для экспорта таблицы 2
        public static List <ExportUnionReportT2> GetExportUnionReportT2(UnionReportCt unionReportCt, List <UnionReportT2> unionReportT2s)
        {
            var result = new List <ExportUnionReportT2>();
            var np     = 0;

            foreach (var t2 in unionReportT2s)
            {
                result.Add(new ExportUnionReportT2
                {
                    NP       = ++np,
                    PERIOD   = unionReportCt.UnionReportCt_Qrt,
                    RIK      = unionReportCt.UnionReportCt_Year,
                    KOD      = t2.UnionReportT2_USREOU,
                    TYP      = t2.UnionReportT2_Type,
                    TIN      = t2.UnionReportT2_TIN,
                    S_NAR    = t2.UnionReportT2_AccrIncSm,
                    S_DOX    = t2.UnionReportT2_PaidIncSm,
                    S_TAXN   = t2.UnionReportT2_AccrTaxSm,
                    S_TAXP   = t2.UnionReportT2_TransfTaxSm,
                    OZN_DOX  = t2.UnionReportT2_IncFlg,
                    D_PRIYN  = t2.UnionReportT2_DOR,
                    D_ZVILN  = t2.UnionReportT2_DOD,
                    OZN_PILG = t2.UnionReportT2_SocBenefitFlg,
                    OZNAKA   = 0,
                    A051     = t2.UnionReportT2_AccrWarTaxSm,
                    A05      = t2.UnionReportT2_TransWarTaxSm
                });
            }

            return(result);
        }
        private void FillDataRec(SqlDataReader reader, UnionReportCt ustCt)
        {
            int      resInt  = 0;
            DateTime resDate = DateTime.MinValue;

            if (int.TryParse(reader["UnionReportCt_Id"].ToString(), out resInt))
            {
                ustCt.UnionReportCt_Id = resInt;
            }
            if (int.TryParse(reader["UnionReportCt_Qrt"].ToString(), out resInt))
            {
                ustCt.UnionReportCt_Qrt = resInt;
            }
            if (int.TryParse(reader["UnionReportCt_Year"].ToString(), out resInt))
            {
                ustCt.UnionReportCt_Year = resInt;
            }
            if (int.TryParse(reader["UnionReportCt_Nmr"].ToString(), out resInt))
            {
                ustCt.UnionReportCt_Nmr = resInt;
            }
            ustCt.UnionReportCt_Nm = reader["UnionReportCt_Nm"].ToString();
            if (DateTime.TryParse(reader["UnionReportCt_DateClc"].ToString(), out resDate))
            {
                ustCt.UnionReportCt_DateClc = resDate;
            }
            if (int.TryParse(reader["UnionReportCt_Flg"].ToString(), out resInt))
            {
                ustCt.UnionReportCt_Flg = resInt;
            }
        }
        public UnionReportCt GetData()
        {
            var unionReportCt = new UnionReportCt();

            unionReportCt.UnionReportCt_Id   = _id;
            unionReportCt.UnionReportCt_Qrt  = GetQrtFromComboboxIndex(cmbQrt.SelectedIndex);
            unionReportCt.UnionReportCt_Year = GetYearFromComboboxIndex(cmbYr.SelectedIndex);

            int resInt = 0;

            if (int.TryParse(tbNmr.Text, out resInt))
            {
                unionReportCt.UnionReportCt_Nmr = resInt;
            }
            unionReportCt.UnionReportCt_Nm  = tbNm.Text;
            unionReportCt.UnionReportCt_Flg = 0;

            if (rbAskClc.Checked)
            {
                unionReportCt.UnionReportCt_Flg |= (int)EnumActionEnterUnionReport.AskCalc;
            }
            else if (rbClc.Checked)
            {
                unionReportCt.UnionReportCt_Flg |= (int)EnumActionEnterUnionReport.AlwaysCalc;
            }
            unionReportCt.UnionReportCt_DateClc = DateTime.MinValue;

            return(unionReportCt);
        }
        //Расчет отчетности
        private bool CalcUnionReport(UnionReportCt unionReportCt)
        {
            if (unionReportCt == null)
            {
                return(false);
            }

            string error;

            if (!_repoUnionReportT1.DeleteUnionReportT1ByParams(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка видалення таблиці 1.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT2.DeleteUnionReportT2ByParams(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка видалення таблиці 2.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT4.DeleteUnionReportT4ByParams(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка видалення таблиці 4.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT5.DeleteUnionReportT5ByParams(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка видалення таблиці 5.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT1.CalcUnionReportT1(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка розрахунку таблиці 1.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT2.CalcUnionReportT2(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка розрахунку таблиці 2.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT4.CalcUnionReportT4(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка розрахунку таблиці 4.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            if (!_repoUnionReportT5.CalcUnionReportT5(unionReportCt.UnionReportCt_Id, out error))
            {
                MessageBox.Show("Помилка розрахунку таблиці 5.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }
            unionReportCt.UnionReportCt_DateClc = DateTime.Now;
            if (!_repoUnionReportCt.ModifyUnionReportCt(unionReportCt, out error))
            {
                MessageBox.Show("Помилка оновлення рядка каталога.\nТехнічна інформація: " + error, "Помилка");
                return(false);
            }

            return(true);
        }
        //Добавить каталог объединенной ведомости
        public int AddUnionReportCt(UnionReportCt unionReportCt, out string error)
        {
            error = string.Empty;
            if (unionReportCt == null)
            {
                error = "unionReportCt == null";
                return(0);
            }
            if (conn == null)
            {
                error = "conn == null";
                return(0);
            }
            SqlCommand command = new SqlCommand(spUnionReportCtInsert, conn);

            command.CommandType = CommandType.StoredProcedure;
            command.Connection  = conn;
            command.Parameters.AddWithValue("@inUnionReportCt_Qrt", unionReportCt.UnionReportCt_Qrt.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Year", unionReportCt.UnionReportCt_Year.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Nmr", unionReportCt.UnionReportCt_Nmr.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Nm", unionReportCt.UnionReportCt_Nm);
            command.Parameters.AddWithValue("@inUnionReportCt_DateClc", unionReportCt.UnionReportCt_DateClc == DateTime.MinValue
                ? Convert.DBNull : unionReportCt.UnionReportCt_DateClc);
            command.Parameters.AddWithValue("@inUnionReportCt_Flg", unionReportCt.UnionReportCt_Flg.ToString());
            // определяем выходной параметр
            SqlParameter outId = new SqlParameter
            {
                ParameterName = "outId",
                Direction     = ParameterDirection.Output,
                SqlDbType     = SqlDbType.Int
            };

            command.Parameters.Add(outId);
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(0);
            }
            int id = 0;

            int.TryParse(command.Parameters["outId"].Value.ToString(), out id);
            return(id);;
        }
 public void SetData(UnionReportCt unionReportCt)
 {
     _id = unionReportCt.UnionReportCt_Id;
     cmbQrt.SelectedIndex = GetComboboxQrtIndex(unionReportCt.UnionReportCt_Qrt);
     cmbYr.SelectedIndex  = GetComboboxYearIndex(unionReportCt.UnionReportCt_Year);
     tbNmr.Text           = unionReportCt.UnionReportCt_Nmr.ToString();
     tbNm.Text            = unionReportCt.UnionReportCt_Nm;
     if ((unionReportCt.UnionReportCt_Flg & (int)EnumActionEnterUnionReport.AskCalc) > 0)
     {
         rbAskClc.Checked = true;
     }
     else if ((unionReportCt.UnionReportCt_Flg & (int)EnumActionEnterUnionReport.AlwaysCalc) > 0)
     {
         rbClc.Checked = true;
     }
     else
     {
         rbNoClc.Checked = true;
     }
 }
        //Получить список каталогов объединенной ведомости
        public List <UnionReportCt> GetAllUnionReportCts(out string error)
        {
            error = string.Empty;

            List <UnionReportCt> unionReportCts = new List <UnionReportCt>();

            if (conn == null)
            {
                error = "conn == null";
                return(unionReportCts);
            }

            SqlCommand command = new SqlCommand(spUnionReportCtSelect, conn);

            command.CommandType = CommandType.StoredProcedure;
            SqlDataReader reader = null;

            try
            {
                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    UnionReportCt unionReportCt = new UnionReportCt();
                    FillDataRec(reader, unionReportCt);
                    unionReportCts.Add(unionReportCt);
                }
            }
            catch (Exception exc)
            {
                error = exc.Message;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return(unionReportCts);
        }
        //Изменить каталог объединенной ведомости
        public bool ModifyUnionReportCt(UnionReportCt unionReportCt, out string error)
        {
            error = string.Empty;
            if (conn == null)
            {
                error = "conn == null";
                return(false);
            }
            if (unionReportCt == null)
            {
                error = "unionReportCt == null";
                return(false);
            }
            SqlCommand command = new SqlCommand(spUnionReportCtUpdate, conn);

            command.CommandType = CommandType.StoredProcedure;
            command.Connection  = conn;
            command.Parameters.AddWithValue("@inUnionReportCt_Id", unionReportCt.UnionReportCt_Id);
            command.Parameters.AddWithValue("@inUnionReportCt_Qrt", unionReportCt.UnionReportCt_Qrt.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Year", unionReportCt.UnionReportCt_Year.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Nmr", unionReportCt.UnionReportCt_Nmr.ToString());
            command.Parameters.AddWithValue("@inUnionReportCt_Nm", unionReportCt.UnionReportCt_Nm);
            command.Parameters.AddWithValue("@inUnionReportCt_DateClc", unionReportCt.UnionReportCt_DateClc == DateTime.MinValue
                ? Convert.DBNull : unionReportCt.UnionReportCt_DateClc);
            command.Parameters.AddWithValue("@inUnionReportCt_Flg", unionReportCt.UnionReportCt_Flg.ToString());
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
            return(true);
        }