예제 #1
0
        public List <ContingencyFund> GetTop()
        {
            List <ContingencyFund> contingencyFunds = new List <ContingencyFund>();

            NpgsqlDataReader reader = null;

            try
            {
                string query = "SELECT * FROM contingency_funds ORDER BY id";
                dal.OpenConnection();
                reader = dal.ExecuteDataReader(query);

                while (reader.Read())
                {
                    ContingencyFund contingencyFund = new ContingencyFund();
                    contingencyFund.Id   = Convert.ToInt64(reader["id"]);
                    contingencyFund.Name = reader["name"].ToString();
                    contingencyFunds.Add(contingencyFund);
                }
                reader.Close();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                this.dal.CloseConection();
            }
            return(contingencyFunds);
        }
        /*
         * Parte relacionada a adição dos códigos de Depto ao contrato
         */
        //private void btnAddDept_Click(object sender, EventArgs e)
        //{
        //    string dptCode = this.txtDptCode.Text;
        //    Department dept = _facade.GetDepartmentByCode(dptCode);
        //    if (dept == null)
        //    {
        //        if (DialogResult.Yes == MessageBox.Show("Departamento não encontrado na base de dados, deseja adicioná-lo?", "Adicionar Departamento Novo",
        //            MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
        //        {
        //            FrmAddDepartment frmAddDepartment = new FrmAddDepartment(dptCode);
        //            frmAddDepartment.FormClosed += new FormClosedEventHandler(_FormAddDeptClosed);
        //            frmAddDepartment.ShowDialog();
        //        }
        //    }
        //    else
        //    {
        //        _TryAddingDept(dept);
        //    }
        //}

        //private void _TryAddingDept(Department dept)
        //{
        //    if (this.allDeptCodes.Add(dept))
        //    {
        //        ListViewItem item;
        //        item = new ListViewItem();
        //        item.Text = dept.Code;
        //        item.SubItems.Add(dept.Name);
        //        this.listDepts.Items.Add(item);
        //        this.txtDptCode.Text = "";
        //    }
        //}

        //private void _FormAddDeptClosed (object sender, FormClosedEventArgs e)
        //{
        //    FrmAddDepartment frmAddDepartment = (FrmAddDepartment)sender;
        //    _TryAddingDept(frmAddDepartment.Department);
        //}

        /*
         * Parte relacionada a adição das alíquotas ao contrato
         */
        private void btnAddAliquot_Click(object sender, EventArgs e)
        {
            try
            {
                if (Regex.IsMatch(this.txtAliquotValue.Text, aliqPattern))
                {
                    double             aliqValue = Convert.ToDouble(this.txtAliquotValue.Text);
                    ContingencyFund    cf        = this.cbContigencyFunds.SelectedItem as ContingencyFund;
                    ContingencyAliquot contAliq  = new ContingencyAliquot(aliqValue, cf);

                    _TryAddingContingencyAliquot(contAliq);
                }
                else
                {
                    MessageBox.Show("A taxa de alíquota inserida deve conter dois dígitos decimais e duas casas decimais separadas por vírgula, exemplo: 10,30%",
                                    "Erro de Conversão de Taxa", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("A taxa de alíquota inserida deve ser válida, exemplo: 10,30% ou 8,00%", "Erro de Conversão de Taxa", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (OverflowException)
            {
                MessageBox.Show("A taxa inserida deve ser entre 00,00 e 99,99%", "Erro de Conversão de Taxa", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 public void Update(ContingencyFund contingencyFund)
 {
     try
     {
         string rowCount = DataBase.DBService.ExecuteCommandScalar(string.Format(SELECT_COUNT, contingencyFund.OptionId, contingencyFund.PlannerId));
         DataBase.DBService.BeginTransaction();
         if (rowCount == "0")
         {
             DataBase.DBService.ExecuteCommandString(string.Format(INSERT_CONTINGENCYFUND,
                                                                   contingencyFund.PlannerId,
                                                                   contingencyFund.OptionId,
                                                                   contingencyFund.Amount), true);
         }
         else
         {
             DataBase.DBService.ExecuteCommandString(string.Format(UPDATE_CONTINGENCYFUND,
                                                                   contingencyFund.Amount,
                                                                   contingencyFund.OptionId,
                                                                   contingencyFund.PlannerId), true);
         }
         Activity.ActivitiesService.Add(ActivityType.UpdateCurrentStatusToGoal, EntryStatus.Success,
                                        Source.Server, contingencyFund.UpdatedByUserName, "CurrentStatusToGoal", contingencyFund.MachineName);
         DataBase.DBService.CommitTransaction();
     }
     catch (Exception ex)
     {
         DataBase.DBService.RollbackTransaction();
         StackTrace st = new StackTrace();
         StackFrame sf = st.GetFrame(0);
         MethodBase currentMethodName = sf.GetMethod();
         LogDebug(currentMethodName.Name, ex);
         throw ex;
     }
 }
예제 #4
0
        public int Insert(ContingencyFund contingencyFund)
        {
            //int rowsAffected = -1;
            object obj        = null;
            int    idReturned = -1;

            try
            {
                string cmdInsert = "INSERT INTO contingency_funds(name) VALUES (:name) RETURNING id";

                NpgsqlCommand cmd = new NpgsqlCommand(cmdInsert);

                cmd.Parameters.Add(new NpgsqlParameter("name", NpgsqlTypes.NpgsqlDbType.Text));

                cmd.Parameters[0].Value = contingencyFund.Name;

                dal.OpenConnection();
                obj = dal.ExecuteScalar(cmd);
                if (obj != null)
                {
                    idReturned = (int)obj;
                }
            }
            finally
            {
                this.dal.CloseConection();
            }
            return(idReturned);
        }
        private void btnSaveContingencyFunds_Click(object sender, EventArgs e)
        {
            ContingencyFund cf = new ContingencyFund();

            if (!String.IsNullOrEmpty(txtContingencyFunds.Text))
            {
                cf.Name = txtContingencyFunds.Text;
            }

            try
            {
                int retId = _facade.InsertContigencyFund(cf);
                if (retId > 0)
                {
                    cf.Id = retId;
                    ListViewItem item;
                    this.contingencyFunds.Add(cf);
                    item      = new ListViewItem();
                    item.Text = cf.Id.ToString();
                    item.SubItems.Add(cf.Name);
                    this.listContingencyFunds.Items.Add(item);
                    MessageBox.Show("Verba de Contingenciamento " + cf.Name + " cadastrada com sucesso.",
                                    "Cadastro de Verbas de Contingenciamento", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Verba de Contingenciamento " + cf.Name + " não retornou um ID de cadastro válido!",
                                    "Cadastro de Verbas de Contingenciamento", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ocorreu o seguinte erro: " + ex.Message, "Erro no Cadastro da Verba de Contingenciamento", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #6
0
        public ContingencyFund Get <K>(K id)
        {
            ContingencyFund  contingencyFund = new ContingencyFund();
            NpgsqlDataReader reader          = null;

            try
            {
                string cmdSelect = "SELECT * FROM contingency_funds WHERE id = " + id + " ORDER BY id";
                dal.OpenConnection();
                reader = dal.ExecuteDataReader(cmdSelect);

                if (reader.Read())
                {
                    contingencyFund.Id   = Convert.ToInt64(reader["id"]);
                    contingencyFund.Name = reader["name"].ToString();
                }
                reader.Close();
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                this.dal.CloseConection();
            }
            return(contingencyFund);
        }
        public List <ContingencyAliquot> GetByContract(Contract ct)
        {
            List <ContingencyAliquot> contingencyAliquots = new List <ContingencyAliquot>();
            NpgsqlDataReader          reader = null;

            try
            {
                string selectCMD = "SELECT ca.id as ca_id, ca.value as ca_value, " +
                                   "cf.id as cf_id, cf.name as cf_name, ctt.id as ctt_id, ctt.name as ctt_name " +
                                   "FROM (contingency_aliquot ca INNER JOIN contingency_funds cf ON (ca.contingency_fund_id = cf.id)) " +
                                   "INNER JOIN contracts ctt ON (ca.contract_id = ctt.id) " +
                                   "WHERE ca.contract_id = :ctID ORDER BY ca.id";

                NpgsqlCommand cmd = new NpgsqlCommand(selectCMD);

                cmd.Parameters.Add(new NpgsqlParameter("ctID", NpgsqlTypes.NpgsqlDbType.Bigint));
                cmd.Parameters[0].Value = ct.Id;

                Contract           contract;
                ContingencyFund    contingencyFund;
                ContingencyAliquot contingencyAliquot;
                dal.OpenConnection();
                reader = dal.ExecuteDataReader(cmd);

                while (reader.Read())
                {
                    contingencyAliquot       = new ContingencyAliquot();
                    contingencyAliquot.Id    = Convert.ToInt64(reader["ca_id"]);
                    contingencyAliquot.Value = Convert.ToDouble(reader["ca_value"]);
                    contract = new Contract(Convert.ToInt64(reader["ctt_id"]), reader["ctt_name"].ToString());
                    contingencyAliquot.Contract        = contract;
                    contingencyFund                    = new ContingencyFund(Convert.ToInt64(reader["cf_id"]), reader["cf_name"].ToString());
                    contingencyAliquot.ContingencyFund = contingencyFund;

                    contingencyAliquots.Add(contingencyAliquot);
                }
                reader.Close();
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                this.dal.CloseConection();
            }
            return(contingencyAliquots);
        }
        public Result Update(ContingencyFund contingencyFund)
        {
            var result = new Result();

            try
            {
                ContingencyFundSesrvice contingencyFundSesrvice = new ContingencyFundSesrvice();
                contingencyFundSesrvice.Update(contingencyFund);
                result.IsSuccess = true;
            }
            catch (Exception exception)
            {
                result.IsSuccess     = false;
                result.ExceptionInfo = exception;
            }
            return(result);
        }
        public ContingencyFund GetContingencyFund(int operationId, int plannerId)
        {
            ContingencyFund contingency = new ContingencyFund();

            Logger.LogInfo("Get: Contingency fund process start");

            DataTable dtFund = DataBase.DBService.ExecuteCommand(string.Format(SELECT_ID, operationId, plannerId));

            foreach (DataRow dr in dtFund.Rows)
            {
                contingency.Amount    = double.Parse(dr["Amount"].ToString());
                contingency.OptionId  = dr.Field <int>("OID");
                contingency.PlannerId = dr.Field <int>("PID");
                contingency.Amount    = double.Parse(dr["Amount"].ToString());
            }
            Logger.LogInfo("Get: Contingency fund allocation process completed");
            return(contingency);
        }
예제 #10
0
        public void Update <K>(K id, ContingencyFund contingencyFund)
        {
            int rowsAffected = -1;

            try
            {
                NpgsqlCommand cmd = new NpgsqlCommand("UPDATE contingency_funds SET \"name\" = :name"
                                                      + " WHERE \"id\" = '" + id + "' ;");

                cmd.Parameters.Add(new NpgsqlParameter("name", NpgsqlTypes.NpgsqlDbType.Text));

                cmd.Parameters[0].Value = contingencyFund.Name;

                dal.OpenConnection();
                rowsAffected = dal.ExecuteNonQuery(cmd);
            }
            finally
            {
                this.dal.CloseConection();
            }
        }
예제 #11
0
        public HashSet <ContingencyPast> GetContingencyPastsByEmployeeHistoryList(List <EmployeeHistory> employeeHistories, ContingencyFund cf)
        {
            HashSet <ContingencyPast> ehContPasts = new HashSet <ContingencyPast>(new ContingencyPastComparer());
            NpgsqlDataReader          reader      = null;

            try
            {
                string selectCMD = "SELECT cp.id as cp_id, cp.monetary_fund_name as cp_monetary_fund_name, cp.calculated_value as cp_calculated_value, " +
                                   "eh.id as eh_id, eh.epoch as eh_epoch, eh.base_salary as eh_base_salary, eh.net_salary as eh_net_salary, " +
                                   "eh.total_earnings as eh_total_earnings, eh.in_vacation as eh_in_vacation, eh.start_vacation_taken as eh_start_vacation_taken, " +
                                   "eh.end_vacation_taken as eh_end_vacation_taken, eh.hazard_additional as eh_hazard_additional, " +
                                   "eh.dangerousness_additional as eh_dangerousness_additional, eh.thirteenth_salary as eh_thirteenth_salary, " +
                                   "eh.thirteenth_proportional_salary as eh_thirteenth_proportional_salary, eh.vacation_pay as eh_vacation_pay, " +
                                   "eh.vacation_proportional_pay as eh_vacation_proportional_pay, eh.penalty_rescission as eh_penalty_rescission, " +
                                   "eh.contract_id as eh_contract_id, eh.processed as eh_processed, emp.id as emp_id, emp.name as emp_name, " +
                                   "emp.matriculation as emp_matriculation, emp.admission_date as emp_admission_date, emp.birthday as emp_birthday, " +
                                   "emp.pis as emp_pis, emp.cpf as emp_cpf, emp.demission_date as emp_demission_date, " +
                                   "rol.id as rol_id, rol.name as rol_name, dep.id as dep_id, dep.name as dep_name, dep.code as dep_code, " +
                                   "ca.id as ca_id, ca.start_date as ca_start_date, ca.end_date as ca_end_date, ca.value as ca_value, " +
                                   "cf.id as cf_id, cf.name as cf_name " +
                                   "FROM (contingency_past cp INNER JOIN employee_history eh ON(cp.employee_history_id = eh.id)) " +
                                   "INNER JOIN contingency_aliquot ca ON (cp.contingency_aliquot_id = ca.id) " +
                                   "INNER JOIN contingency_funds cf ON (ca.contingency_fund_id = cf.id) " +
                                   "INNER JOIN employees emp ON (eh.employee_id = emp.id) " +
                                   "LEFT JOIN roles rol ON (emp.role_id = rol.id) " +
                                   "LEFT JOIN departments dep ON (eh.department_id = dep.id) " +
                                   "WHERE eh.employee_id IN ";

                string empIds = "";
                int    count  = 0;
                foreach (EmployeeHistory eh in employeeHistories)
                {
                    empIds += (count == 0) ? eh.Employee.Id.ToString() : "," + eh.Employee.Id.ToString();
                    count++;
                }

                if (String.IsNullOrEmpty(cf.Name))
                {
                    selectCMD += "(" + empIds + ") ORDER BY emp.name ASC, eh.epoch;";
                }
                else
                {
                    selectCMD += "(" + empIds + ") AND cf.name LIKE '" + cf.Name + "' ORDER BY emp.name ASC, eh.epoch;";
                }

                NpgsqlCommand cmd = new NpgsqlCommand(selectCMD);

                ContingencyPast contingencyPast;
                dal.OpenConnection();
                reader = dal.ExecuteDataReader(cmd);
                while (reader.Read())
                {
                    contingencyPast    = new ContingencyPast();
                    contingencyPast.Id = Convert.ToInt64(reader["cp_id"]);
                    contingencyPast.MonetaryFundName = reader["cp_monetary_fund_name"].ToString();
                    contingencyPast.EmployeeHistory  = new EmployeeHistory(Convert.ToInt64(reader["eh_id"]));

                    if (ehContPasts.Add(contingencyPast))
                    {
                        contingencyPast.ContingencyAliquots.Add(new ContingencyAliquot(Convert.ToInt64(reader["ca_id"]), Convert.ToDouble(reader["ca_value"]),
                                                                                       new ContingencyFund(Convert.ToInt64(reader["cf_id"]), reader["cf_name"].ToString()), Convert.ToDouble(reader["cp_calculated_value"])));

                        contingencyPast.EmployeeHistory.Epoch = Convert.ToDateTime(reader["eh_epoch"]);
                        contingencyPast.EmployeeHistory.StartVacationTaken           = Convert.ToDateTime(reader["eh_start_vacation_taken"]);
                        contingencyPast.EmployeeHistory.EndVacationTaken             = Convert.ToDateTime(reader["eh_end_vacation_taken"]);
                        contingencyPast.EmployeeHistory.InVacation                   = Convert.ToBoolean(reader["eh_in_vacation"]);
                        contingencyPast.EmployeeHistory.BaseSalary                   = Convert.ToDouble(reader["eh_base_salary"]);
                        contingencyPast.EmployeeHistory.NetSalary                    = Convert.ToDouble(reader["eh_net_salary"]);
                        contingencyPast.EmployeeHistory.TotalEarnings                = Convert.ToDouble(reader["eh_total_earnings"]);
                        contingencyPast.EmployeeHistory.HazardAdditional             = Convert.ToDouble(reader["eh_hazard_additional"]);
                        contingencyPast.EmployeeHistory.DangerousnessAdditional      = Convert.ToDouble(reader["eh_dangerousness_additional"]);
                        contingencyPast.EmployeeHistory.ThirteenthSalary             = Convert.ToDouble(reader["eh_thirteenth_salary"]);
                        contingencyPast.EmployeeHistory.ThirteenthProportionalSalary = Convert.ToDouble(reader["eh_thirteenth_proportional_salary"]);
                        contingencyPast.EmployeeHistory.VacationPay                  = Convert.ToDouble(reader["eh_vacation_pay"]);
                        contingencyPast.EmployeeHistory.VacationProportionalPay      = Convert.ToDouble(reader["eh_vacation_proportional_pay"]);
                        contingencyPast.EmployeeHistory.PenaltyRescission            = Convert.ToDouble(reader["eh_penalty_rescission"]);
                        contingencyPast.EmployeeHistory.Processed                    = Convert.ToBoolean(reader["eh_processed"]);

                        if (!(reader["emp_id"] is DBNull))
                        {
                            Employee employee = new Employee();
                            employee.Id                   = Convert.ToInt64(reader["emp_id"]);
                            employee.Name                 = reader["emp_name"].ToString();
                            employee.Matriculation        = reader["emp_matriculation"].ToString();
                            employee.Birthday             = Convert.ToDateTime(reader["emp_birthday"]);
                            employee.CurrentAdmissionDate = Convert.ToDateTime(reader["emp_admission_date"]);
                            employee.CurrentDemissionDate = Convert.ToDateTime(reader["emp_demission_date"]);
                            employee.PIS                  = reader["emp_pis"].ToString();
                            employee.CPF                  = reader["emp_cpf"].ToString();
                            if (!(reader["rol_id"] is DBNull))
                            {
                                Role role = new Role();
                                role.Id       = Convert.ToInt64(reader["rol_id"]);
                                role.Name     = reader["rol_name"].ToString();
                                employee.Role = role;
                            }
                            contingencyPast.EmployeeHistory.Employee = employee;
                        }

                        if (!(reader["dep_id"] is DBNull))
                        {
                            Department department = new Department();
                            department.Id   = Convert.ToInt64(reader["dep_id"]);
                            department.Name = reader["dep_name"].ToString();
                            department.Code = reader["dep_code"].ToString();
                            contingencyPast.EmployeeHistory.Department = department;
                        }
                    }
                    else
                    {
                        foreach (ContingencyPast cp in ehContPasts)
                        {
                            if (cp.EmployeeHistory.Id == contingencyPast.EmployeeHistory.Id)
                            {
                                cp.ContingencyAliquots.Add(new ContingencyAliquot(Convert.ToInt64(reader["ca_id"]), Convert.ToDouble(reader["ca_value"]),
                                                                                  new ContingencyFund(Convert.ToInt64(reader["cf_id"]), reader["cf_name"].ToString()), Convert.ToDouble(reader["cp_calculated_value"])));
                            }
                        }
                    }
                }
                reader.Close();
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                this.dal.CloseConection();
            }
            return(ehContPasts);
        }
예제 #12
0
        public HashSet <ContingencyPast> GetContingencyPastsByEmployeeHistoryList(List <EmployeeHistory> employeeHistories, ContingencyFund cf)
        {
            HashSet <ContingencyPast> allContingencyPasts = this._contingencyPastDAO.GetContingencyPastsByEmployeeHistoryList(employeeHistories, cf);
            HashSet <ContingencyPast> hsContingencyPasts  = new HashSet <ContingencyPast>();
            DateTime dtEpoch;
            int      value1;
            int      value2;

            foreach (ContingencyPast cp in allContingencyPasts)
            {
                dtEpoch = cp.EmployeeHistory.Epoch;
                foreach (EmployeeHistory eh in employeeHistories)
                {
                    if (cp.EmployeeHistory.Employee.Id == eh.Employee.Id)
                    {
                        value1 = DateTime.Compare(dtEpoch, eh.StartVacationTaken);
                        value2 = DateTime.Compare(dtEpoch, eh.EndVacationTaken);
                        if (value1 >= 0 && value2 <= 0)
                        {
                            hsContingencyPasts.Add(cp);
                        }
                    }
                }
            }

            // List<ContingencyPast> hsContingencyPastsCopy = new List<ContingencyPast>(hsContingencyPasts);
            HashSet <ContingencyPast> toRemove = new HashSet <ContingencyPast>();

            foreach (ContingencyPast contPast in hsContingencyPasts)
            {
                foreach (ContingencyPast contPast2 in hsContingencyPasts)
                {
                    if (contPast.EmployeeHistory.Employee.Id == contPast2.EmployeeHistory.Employee.Id)
                    {
                        if (contPast.EmployeeHistory.Epoch.CompareTo(contPast2.EmployeeHistory.Epoch) == 0)
                        {
                            if (contPast.EmployeeHistory.InVacation != contPast.EmployeeHistory.InVacation)
                            {
                                bool had = false;
                                foreach (ContingencyPast cpR in toRemove)
                                {
                                    if (contPast.EmployeeHistory.Employee.Id == cpR.EmployeeHistory.Id && contPast.EmployeeHistory.Epoch.CompareTo(cpR.EmployeeHistory.Epoch) == 0)
                                    {
                                        had = true;
                                        break;
                                    }
                                }
                                if (!had)
                                {
                                    toRemove.Add(contPast);
                                }
                            }
                        }
                    }
                }
            }

            foreach (ContingencyPast cp in toRemove)
            {
                hsContingencyPasts.Remove(cp);
            }
            return(hsContingencyPasts);
        }
        //public Contract GetByName<K>(K name)
        //{

        //    Contract contract = new Contract();
        //    NpgsqlDataReader reader = null;
        //    try
        //    {
        //        string SELECTCMD = "SELECT * FROM contracts WHERE matricula = '" + name + "'";
        //        dal.OpenConnection();
        //        reader = dal.ExecuteDataReader(SELECTCMD);

        //        if (reader.Read())
        //        {
        //            contract.Id = Convert.ToInt64(reader["id"]);
        //            contract.Name = reader["name"].ToString();
        //            contract.Matriculation = reader["matriculation"].ToString();
        //            contract.Birthday = Convert.ToDateTime(reader["birthday"]);
        //            contract.CurrentStartDate = Convert.ToDateTime(reader["start_date"]);
        //            contract.CurrentEndDate = Convert.ToDateTime(reader["end_date"]);
        //            contract.PIS = reader["pis"].ToString();
        //            contract.CPF = reader["cpf"].ToString();

        //        }
        //        reader.Close();
        //    }

        //    finally
        //    {
        //        if (reader != null)
        //        {
        //            reader.Close();
        //        }
        //        this.dal.CloseConection();
        //    }
        //    return contract;
        //}

        public List <Contract> GetTop()
        {
            List <Contract> contracts = new List <Contract>();
            Contract        contract  = null;
            //List<Department> departments = new List<Department>();
            HashSet <MonetaryFund>    monetaryFunds       = new HashSet <MonetaryFund>(new MonetaryFundComparer());
            List <ContingencyAliquot> contingencyAliquots = new List <ContingencyAliquot>();

            NpgsqlDataReader reader = null;

            try
            {
                NpgsqlCommand cmd;

                string selectCMD = "SELECT * FROM contracts";

                cmd = new NpgsqlCommand(selectCMD);

                dal.OpenConnection();
                reader = dal.ExecuteDataReader(cmd);
                while (reader.Read())
                {
                    contract             = new Contract();
                    contract.Id          = Convert.ToInt64(reader["id"]);
                    contract.Name        = reader["name"].ToString();
                    contract.Description = reader["description"].ToString();
                    contract.Balance     = Convert.ToDouble(reader["balance"]);

                    if (reader["start_date"] is DBNull)
                    {
                        contract.StartDate = DateTime.MinValue;
                    }
                    else
                    {
                        contract.StartDate = Convert.ToDateTime(reader["start_date"]);
                    }

                    if (reader["end_date"] is DBNull)
                    {
                        contract.EndDate = DateTime.MinValue;
                    }
                    else
                    {
                        contract.EndDate = Convert.ToDateTime(reader["end_date"]);
                    }

                    contracts.Add(contract);
                }
                reader.Close();

                //Department department;
                MonetaryFund       monetaryFund;
                ExtraFund          extraFund;
                ContingencyAliquot contingencyAliquot;
                ContingencyFund    contingencyFund;

                string selectMonetaryFundsCMD = "SELECT c.id as c_id, c.name as c_name, c.start_date as c_start_date, c.end_date as c_end_date, " +
                                                "cmf.id as cmf_id, cmf.contract_id as cmf_contract_id, cmf.monetary_fund_id as cmf_monetary_fund_id, " +
                                                "mf.id as mf_id, mf.name as mf_name, mf.primal as mf_primal, " +
                                                "ef.id as ef_id, ef.name as ef_name " +
                                                "FROM (contracts c INNER JOIN contract_monetary_funds cmf ON " +
                                                "(c.id = cmf.contract_id)) " +
                                                "INNER JOIN monetary_funds mf ON (cmf.monetary_fund_id = mf.id) " +
                                                "LEFT JOIN extra_funds ef ON (cmf.extra_fund_id = ef.id) " +
                                                "WHERE c.id = :passedId ORDER BY c.id";

                string selectContAliquotCMD = "SELECT c.id as c_id, c.name as c_name, c.start_date as c_start_date, c.end_date as c_end_date, " +
                                              "ca.id as ca_id, ca.start_date as ca_start_date, ca.end_date as ca_end_date, ca.value as ca_value, " +
                                              "cf.id as cf_id, cf.name as cf_name FROM (contracts c INNER JOIN contingency_aliquot ca ON " +
                                              "(c.id = ca.contract_id)) " +
                                              "LEFT JOIN contingency_funds cf ON (ca.contingency_fund_id = cf.id) " +
                                              "WHERE c.id = :passedId ORDER BY c.id";

                cmd.Parameters.Add(new NpgsqlParameter("passedId", NpgsqlTypes.NpgsqlDbType.Bigint));

                foreach (Contract ct in contracts)
                {
                    //Consultar as verbas monetárias do contrato
                    cmd.CommandText         = selectMonetaryFundsCMD;
                    cmd.Parameters[0].Value = ct.Id;
                    monetaryFunds           = new HashSet <MonetaryFund>(new MonetaryFundComparer());
                    contingencyAliquots     = new List <ContingencyAliquot>();

                    reader = dal.ExecuteDataReader(cmd);

                    long currentMFId = 0;
                    while (reader.Read())
                    {
                        currentMFId = Convert.ToInt64(reader["mf_id"]);

                        if (reader["ef_id"] is DBNull && reader["ef_name"] is DBNull)
                        {
                            monetaryFund = new MonetaryFund(Convert.ToInt64(reader["mf_id"]), reader["mf_name"].ToString());
                            monetaryFunds.Add(monetaryFund);
                        }
                        else
                        {
                            extraFund    = new ExtraFund(Convert.ToInt64(reader["ef_id"]), reader["ef_name"].ToString());
                            monetaryFund = new MonetaryFund(Convert.ToInt64(reader["mf_id"]), reader["mf_name"].ToString());
                            if (monetaryFunds.Add(monetaryFund))
                            {
                                monetaryFund.ExtraFunds.Add(extraFund);
                            }
                            else
                            {
                                foreach (MonetaryFund mf in monetaryFunds)
                                {
                                    if (mf.Id == currentMFId)
                                    {
                                        mf.ExtraFunds.Add(extraFund);
                                    }
                                }
                            }
                        }
                    }
                    reader.Close();
                    ct.MonetaryFunds = new List <MonetaryFund>(monetaryFunds);

                    //Consultar as alíquotas do contrato
                    cmd.CommandText = selectContAliquotCMD;

                    reader = dal.ExecuteDataReader(cmd);
                    while (reader.Read())
                    {
                        contingencyFund    = new ContingencyFund(Convert.ToInt64(reader["cf_id"]), reader["cf_name"].ToString());
                        contingencyAliquot = new ContingencyAliquot(Convert.ToInt64(reader["ca_id"]), Convert.ToDouble(reader["ca_value"]),
                                                                    Convert.ToDateTime(reader["ca_start_date"]), Convert.ToDateTime(reader["ca_end_date"]), contingencyFund);

                        contingencyAliquots.Add(contingencyAliquot);
                    }
                    reader.Close();
                    ct.ContingencyAliquot = contingencyAliquots;
                }
            }

            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                this.dal.CloseConection();
            }
            return(contracts);
        }
 public int InsertContigencyFund(ContingencyFund contingencyFund)
 {
     return(this._contingencyFundDAO.Insert(contingencyFund));
 }
 public void UpdateContigencyFund(int id, ContingencyFund contingencyFund)
 {
     this._contingencyFundDAO.Update <int>(id, contingencyFund);
 }