예제 #1
0
        // Contract methods
        /// <summary>
        /// add a new contract, under the conditions:
        /// 1)verify if the employer exists
        /// 2)verify if the employee exists
        /// 3)verify if the employee work in the same field of the employer
        /// 4)verify if the employer already works to anothe company
        /// 5)verify if the company already has one year of existence
        /// 6)verify id the minimmun salary is compatible with the employee
        /// 7)verify id the maximmun salary is compatible with the employee
        /// </summary>
        /// <param name="newContract"></param>
        public int AddContract(Contract newContract)
        {
            var employee = this.GetEmployee(newContract.employeeId);
            var employer = this.GetEmployer(newContract.employerId);

            if (newContract.employeeId < 0)
            {
                throw new Exception("Employee ID cannot be a negative number.");
            }

            if (newContract.employerId < 0)
            {
                throw new Exception("Employer ID cannot be a negative number.");
            }

            if (employee == null)
            {
                throw new Exception("Employee in the contract doesn't exist.");
            }

            if (employer == null)
            {
                throw new Exception("Employer in the contract doesn't exist.");
            }

            if (employer.field != this.GetSpecialization(employee.specNumber).specField)
            {
                throw new Exception("This Employee cannot work in this employer's field.");
            }

            if (newContract.endWork < newContract.startWork)
            {
                throw new Exception("Date of start of employment cannot be later than date of end of employment.");
            }

            if (dal.ReturnContracts(c => c.employeeId == employee.id).Any(c => DateTime.Now < c.endWork)) //if the employee has other contracts that don`t ended yet, he can`t assign this contract
            {
                throw new Exception("Employee is already employed by another company.");
            }

            if (dal.ReturnEmployers(e => e.id == employer.id && e.seniority >= 1) == null)
            {
                throw new Exception("Employer doesn't have a seniority of at least one year.");
            }

            if (newContract.workHours <= 0)
            {
                throw new Exception("Daily hours cannot be equal to or less than zero");
            }

            if (newContract.workHours > 12)
            {
                throw new Exception("Cannot work more than 12 hours a day.");
            }

            if (newContract.brutoSalary <= 0)
            {
                throw new Exception("Bruto wage cannot be less or equal to zero.");
            }

            if (newContract.workHours <= 0)
            {
                throw new Exception("Number of daily hours cannot be less or equal to zero");
            }

            var specialization = this.GetSpecialization(employee.specNumber);

            if (newContract.netoSalary < specialization.minSalaryPerHour)
            {
                throw new Exception("Neto salary per hour is too low for this employee's specialization.");
            }

            if (newContract.netoSalary > specialization.maxSalaryPerHour)
            {
                throw new Exception("Neto salary per hour is too high for this employee's specialization.");
            }



            return(dal.AddContract(newContract));
        }