Beispiel #1
0
def delete_employee(id):
    # calling function to delete the employee
    emp = Employee.fetching_all_emps_by_id_test(id=id)
    # print(emp.employee_name)
    try:
        deleted_employee_of_id = Employee.delete_employee(id)
        flash('Employee DELETED')
    except Exception:
        flash(
            f' cannot delete employee named  {emp.employee_name} because he/she has payrolls. Consider clearing the payroll first',
            'warning')

    return redirect(url_for('employees'))
Beispiel #2
0
def update_employee(id):
    if request.method == 'POST':
        full_names = request.form['full_names']
        email = request.form['email']
        national_id_no = request.form['national_id_no']
        phone_number = request.form['phone_number']
        gender = request.form['gender']
        age = request.form['age']
        kra_pin = request.form['kra_pin']
        update_salary = request.form['update_salary']
        update_benefits = request.form['update_benefits']
        dep_id = request.form['dep_id']

        try:

            updates = Employee.update_details(id=id,
                                              employee_name=full_names,
                                              email=email,
                                              kra=kra_pin,
                                              salary=update_salary,
                                              benefits=update_benefits,
                                              age=age,
                                              gender=gender,
                                              national_id=national_id_no,
                                              phone_number=phone_number,
                                              update_department_id=dep_id)
            flash(f'updates on employee named {full_names} done succesfully',
                  'info')
            return redirect(url_for('employees'))
        except Exception:
            flash('un error occured .plz retry', 'danger')
            return redirect(url_for('employees'))
Beispiel #3
0
    def create_employee(data: CreateEmployee, db: Session):
        """create a new employee"""
        email = db.query(Employee).filter(
            Employee.email_address == data.email_address).first()
        if email:
            raise HTTPException(status_code=400, detail='email taken!')

        # save to database
        record = Employee(**data.dict())
        db.add(record)
        db.commit()
        db.refresh(record)
        return record
Beispiel #4
0
def add(name: str, birthday: date, department: int, working_since: date,
        salary: float):
    """ Insert new employee. """
    logger.debug('Adding employee with name %s.', name)
    new_employee = Employee(name, birthday, salary, department, working_since)
    try:
        db.session.add(new_employee)
    except Exception as exception:
        logger.error(
            'An error occurred while adding employee with name %s. '
            'Exception: %s', name, str(exception))
        db.session.rollback()
        raise
    db.session.commit()
    logger.info('Successfully added new employee with name %s.', name)
    return new_employee.id
Beispiel #5
0
def employees():

    # fetching all departments available inthe department tables
    all_deps = Department.query.all()

    all_employees = Employee.query.all()
    if request.method == 'POST':
        name = request.form['full_names']
        email = request.form['email']
        gender = request.form['gender']
        phone_number = request.form['phone_number']
        national_id_number = request.form['national_id_no']
        age = request.form['age']
        kra_pin = request.form['kra_pin']
        dep_id = request.form['dep_id']
        salary = request.form['entered_salary']
        benefits = request.form['entered_benefits']

        # sending info to the db
        # create an object of the Employee class
        print(name)
        if Employee.checking_employee(national_id_number):

            flash('Employee exists.Similar nationall id number exists',
                  'warning')
            # returns you to ur previous stage.it takes the function name of the route
            return redirect(url_for('employees'))

            # calling function to check if kra exists.
        elif Employee.check_kra_exists(kra_pin):
            flash(' Cannot create employee! KRA PIN exists in this system',
                  'warning')
            return redirect(url_for('employees'))
        else:
            # this equates the  the columns where data will be inputed and values to be inputed
            emp = Employee(employee_name=name,
                           employee_email=email,
                           employee_gender=gender,
                           employee_national_id=national_id_number,
                           employee_age=age,
                           employee_KRA_PIN=kra_pin,
                           department_id=dep_id,
                           employee_salary=salary,
                           employee_benefits=benefits,
                           employee_phone_number=phone_number)
            # this calls the function fro the employee_models to commit te data to the db

            emp.create()
            flash('Successfully created', 'info')
            return redirect(url_for('employees'))
    return render_template('employees.html',
                           depart=all_deps,
                           all_emps=all_employees)
def fill_test_database():
    basedir = os.path.abspath(os.path.dirname(__file__))
    project_dir = os.path.dirname(os.path.dirname(basedir))  # up two levels
    with open(os.path.join(project_dir, 'tests',
                           'starting_companies.json')) as file:
        companies_json = json.load(file)
    with open(os.path.join(project_dir, 'tests',
                           'starting_employees.json')) as file:
        employees_json = json.load(file)
    companies = [Company(c['name']) for c in companies_json]
    employees = [
        Employee(e['name'], e['email'],
                 e['companyID'] if 'companyID' in e else None,
                 e['managerID'] if 'managerID' in e else None)
        for e in employees_json
    ]
    db.session.add_all(companies)
    db.session.add_all(employees)
    db.session.commit()
def add_employee():
    required = ['name', 'email']

    missing = check_missing_parameters(request, required)
    if len(missing) > 0:
        return error_handler(400, aeh.HTTP_MISSING_PARAMS, missing)

    name = request.json['name']
    email = request.json['email']
    
    try:
        companyID = None if 'companyID' not in request.json else int(request.json['companyID'])
        managerID = None if 'managerID' not in request.json else int(request.json['managerID'])
    except:
        return error_handler(400, aeh.HTTP_PARAM_TYPE, ['companyID', 'managerID'], 'numeric')

    new_employee = Employee(name, email, companyID, managerID)

    if companyID is not None:
        company = Company.query.get(request.json['companyID'])
        if company is None:
            return error_handler(400, aeh.SQL_NOT_FOUND, request.json['companyID'], message=aeh.NO_COMPANY_TO_ASSOCIATE)
    
    if managerID is not None:
        manager = Employee.query.get(request.json['managerID'])
        if manager is None:
            return error_handler(400, aeh.SQL_NOT_FOUND, request.json['managerID'], message=aeh.NO_EMPLOYEE_TO_ASSIGN)
        if manager.companyID != new_employee.companyID:
            return error_handler(400, aeh.API_NOT_SAME_COMPANY, manager.companyID, new_employee.companyID)
        # There's no need to verify for loops since the employee is being created now, therefore has no subordinates.

    try:
        db.session.add(new_employee)
        db.session.flush()
        sql_result = employee_schema.dump(new_employee)
        db.session.commit()
        return jsonify(sql_result), 200

    except Exception as e:
        db.session.rollback()
        db.session.flush()
        return error_handler(400, aeh.SQL_CONSTRAINT_FAILED, message=str(e))