Exemplo n.º 1
0
def department_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            dept = DBSession.query(Department).filter_by(
                account_id=account_id).filter_by(name=name).first()
            if dept is None:
                new_department = Department(name, account)
                DBSession.add(new_department)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(location=request.application_url +
                                     "/administration/company")

        return dict(logged_in=authenticated_userid(request),
                    header=Header("administration"),
                    user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 2
0
def person(request):
    #FIX PERMISSIONS!
    person_id = request.matchdict.get('person_id')
    review_year = request.params.get('year')

    user = DBSession.query(User).filter_by(id=long(request.session['uid'])).first()

    person = DBSession.query(User).filter_by(id=long(person_id)).first()

    account = DBSession.query(Account).filter_by(id=long(request.session['aid'])).first()

    if review_year is None or review_year == "":
        review_year = account.latest_review_year

    review = DBSession.query(Review).filter_by(user_id=person.id).filter_by(review_cycle_year=review_year).first()

    if review is None:
        if review_year == account.latest_review_year:
            review = Review(user, review_year)
            DBSession.add(review)
            DBSession.flush()
        else:
            HTTPFound(location=request.application_url)

    return dict(logged_in=authenticated_userid(request), person=person, user=user, review=review, account=account)
Exemplo n.º 3
0
def office_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        account = DBSession.query(Account).filter_by(id=account_id).first()
        user = DBSession.query(User).filter_by(id=user_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            office = DBSession.query(Office).filter_by(
                account_id=long(request.session['aid'])).filter_by(
                    name=name).first()
            if office is None:
                benefits_and_bonus = long(request.params["benefits_and_bonus"])
                new_office = Office(name, benefits_and_bonus, account)
                DBSession.add(new_office)
                DBSession.flush()

            if request.params.get("add_another") is None:
                return HTTPFound(request.application_url +
                                 "/administration/company")

        return dict(logged_in=authenticated_userid(request),
                    header=Header("administration"),
                    user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 4
0
def currency_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            currency_to_usd = float(request.params["currency_to_usd"])
            error = False
            if name == "usd":
                error = True

            currency = DBSession.query(Currency).filter_by(account_id=account_id).filter_by(name=name).first()
            if currency is not None:
                error = True

            if error == False:
                new_currency = Currency(name, account, currency_to_usd)
                DBSession.add(new_currency)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url + "/administration/company")

        return dict(logged_in=authenticated_userid(request), header=Header("administration"), user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 5
0
def role_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            department_id = long(request.params["department_id"])
            salary_low = long(request.params["salary_low"])
            salary_high = long(request.params["salary_high"])
            department = DBSession.query(Department).filter_by(
                id=department_id).filter_by(account_id=account.id).first()

            role_ok = True
            for role in department.roles:
                if role.name == name:
                    role_ok = False
                    break

            if role_ok:
                new_role = Role(account, name, department, salary_high,
                                salary_low)
                DBSession.add(new_role)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url +
                                     "/administration/company")

        departments = DBSession.query(Department).filter_by(
            account_id=long(request.session['aid'])).all()
        return dict(logged_in=authenticated_userid(request),
                    header=Header("administration"),
                    departments=departments,
                    user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 6
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        huge = Account("Finance", 2013)
        office = Office("New York", 10, huge)
        corporate = Department("Corporate", huge)
        ceo = Role(huge, "Senior Developer", corporate, 150000, 50000)
        aaron = User(huge, "Atish Narlawar", "*****@*****.**", office, ceo, 100000, datetime.date(2012, 1, 1))
        aaron.set_password("atish")
        aaron.is_administrator = True
        salary = Salary(aaron, 100000, 1, datetime.date(2012, 1, 1), 100)
        aaron.salary_history.append(salary)

        DBSession.add(aaron)
Exemplo n.º 7
0
def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        huge = Account("Finance", 2013)
        office = Office("New York", 10, huge)
        corporate = Department("Corporate", huge)
        ceo = Role(huge, "Senior Developer", corporate, 150000, 50000)
        aaron = User(huge, "Atish Narlawar", "*****@*****.**", office,
                     ceo, 100000, datetime.date(2012, 1, 1))
        aaron.set_password("atish")
        aaron.is_administrator = True
        salary = Salary(aaron, 100000, 1, datetime.date(2012, 1, 1), 100)
        aaron.salary_history.append(salary)

        DBSession.add(aaron)
Exemplo n.º 8
0
def role_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            department_id = long(request.params["department_id"])
            salary_low = long(request.params["salary_low"])
            salary_high = long(request.params["salary_high"])
            department = DBSession.query(Department).filter_by(id=department_id).filter_by(
                account_id=account.id).first()

            role_ok = True
            for role in department.roles:
                if role.name == name:
                    role_ok = False
                    break

            if role_ok:
                new_role = Role(account, name, department, salary_high, salary_low)
                DBSession.add(new_role)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url + "/administration/company")

        departments = DBSession.query(Department).filter_by(account_id=long(request.session['aid'])).all()
        return dict(logged_in=authenticated_userid(request), header=Header("administration"), departments=departments,
                    user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 9
0
def skillset_entry_add(request):
    rating = request.matchdict.get('rating')
    skillset_id = request.matchdict.get('skillset_id')
    person_id = request.matchdict.get('person_id')
    review_id = request.matchdict.get('review_id')
    interval = request.matchdict.get('interval')

    is_midyear = False
    if interval == "midyear":
        is_midyear = True

    account = DBSession.query(Account).filter_by(
        id=long(request.session['aid'])).first()
    user = DBSession.query(User).filter_by(
        id=long(request.session['uid'])).first()
    person = DBSession.query(User).filter_by(id=long(person_id)).first()
    review = DBSession.query(Review).filter_by(id=long(review_id)).first()
    skillset = DBSession.query(Skillset).filter_by(
        id=long(skillset_id)).first()

    if user.permissions.is_administrator or person.manager_id == user.id:
        if is_midyear and (
                datetime.datetime.now() > account.midyear_review_end_date
                or datetime.datetime.now() < account.midyear_review_start):
            return {'ok': 'False'}
        if is_midyear == False and (
                datetime.datetime.now() > account.annual_review_end_date
                or datetime.datetime.now() < account.annual_review_start):
            return {'ok': 'False'}

        skillset_entry = SkillsetEntry(review, is_midyear, skillset, rating)
        DBSession.add(skillset_entry)
        transaction.commit()

        return {"ok": "True"}

    return {'ok': 'False'}
Exemplo n.º 10
0
def currency_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            currency_to_usd = float(request.params["currency_to_usd"])
            error = False
            if name == "usd":
                error = True

            currency = DBSession.query(Currency).filter_by(
                account_id=account_id).filter_by(name=name).first()
            if currency is not None:
                error = True

            if error == False:
                new_currency = Currency(name, account, currency_to_usd)
                DBSession.add(new_currency)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url +
                                     "/administration/company")

        return dict(logged_in=authenticated_userid(request),
                    header=Header("administration"),
                    user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 11
0
def department_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or user.is_administrator == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            dept = DBSession.query(Department).filter_by(account_id=account_id).filter_by(name=name).first()
            if dept is None:
                new_department = Department(name, account)
                DBSession.add(new_department)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(location=request.application_url + "/administration/company")

        return dict(logged_in=authenticated_userid(request), header=Header("administration"), user=user)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 12
0
def freelancer_convert(request):
    try:
        freelancer_id = long(request.matchdict['freelancer_id'])

        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        freelancer = DBSession.query(Freelancer).filter_by(account_id=account_id).filter_by(id=freelancer_id).first()
        if freelancer is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            email = request.params["email"]
            person = DBSession.query(User).filter_by(email=email).first()

            if person is not None:
                source = 'financials'
            else:

                account = DBSession.query(Account).filter_by(id=account_id).first()

                name = request.params["name"].lower()
                if request.params.get("employee_number") is None or request.params.get("employee_number") == '':
                    employee_number = 0
                else:
                    employee_number = long(request.params.get("employee_number"))

                if request.params.get("salary") is None or request.params.get("salary") == '':
                    salary = 0
                else:
                    salary_local = long(request.params.get("salary"))
                    if user.currency is None:
                        salary = salary_local
                    else:
                        salary = salary_local * user.currency.currency_to_usd

                if request.params.get("office_id") is None or request.params.get("office_id") == '':
                    office_id = None
                    office = None
                else:
                    office_id = long(request.params.get("office_id"))
                    office = DBSession.query(Office).filter_by(id=office_id).filter_by(account_id=account_id).first()

                if request.params.get("role_id") is None or request.params.get("role_id") == '':
                    role_id = None
                    return HTTPFound(request.application_url + "/person/add")
                else:
                    role_id = long(request.params.get("role_id"))
                    role = DBSession.query(Role).filter_by(id=role_id).first()

                if request.params.get("percent_billable") is None or request.params.get("percent_billable") == '':
                    percent_billable = 100
                elif request.params.get("percent_billable") == '0':
                    percent_billable = 0
                else:
                    percent_billable = long(request.params.get("percent_billable"))

                if request.params.get("currency_id") is None or request.params.get("currency_id") == '':
                    currency_id = None
                    currency = None
                else:
                    currency_id = long(request.params.get("currency_id"))
                    currency = DBSession.query(Currency).filter_by(id=currency_id).filter_by(
                        account_id=account_id).first()

                start_date_text = request.params.get("start_date")
                if start_date_text is None or start_date_text == '':
                    start_date = datetime.datetime.now()
                else:
                    start_dateparts = start_date_text.split("/")
                    start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]),
                                               long(start_dateparts[1]))

                end_date_text = request.params.get("end_date")
                if end_date_text is None or end_date_text == '':
                    end_date = None
                else:
                    end_dateparts = end_date_text.split("/")
                    end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

                is_administrator = False

                is_hr_administrator = False

                u = DBSession.query(User).filter_by(email=email).first()
                if u is not None:
                    return HTTPFound(request.application_url + "/person/add")

                new_user = User(account, name, email, office, role, salary, start_date)
                new_user.employee_number = employee_number
                new_user.percent_billable = percent_billable
                new_user.end_date = end_date
                new_user.is_administrator = is_administrator
                new_user.is_hr_administrator = is_hr_administrator
                new_user.currency = currency

                s = Salary(new_user, salary, role.id, start_date)
                new_user.salary_history.append(s)

                DBSession.add(new_user)

                freelancer.converted_fulltime = True

                expected_freelancer_end_date = start_date - timedelta(days=1)
                if freelancer.end_date.date() > expected_freelancer_end_date:
                    freelancer.end_date = expected_freelancer_end_date

                DBSession.flush()
                return HTTPFound(request.application_url + "/client/" + str(freelancer.client.id) + "/utilization/" + str(datetime.datetime.now().year))

        departments = DBSession.query(Department).filter_by(account_id=account_id).all()
        offices = DBSession.query(Office).filter_by(account_id=account_id).all()
        roles = DBSession.query(Role).filter_by(account_id=account_id).all()
        currencies = DBSession.query(Currency).filter_by(account_id=account_id).all()

        return dict(logged_in=authenticated_userid(request), header=Header('financials'),
                    offices=offices, roles=roles, freelancer=freelancer, currencies=currencies, user=user,
                    account=account, departments=departments)
    except:
        print("*************")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 13
0
def client_assign_ghost(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            ghost_user_id = request.params.get('ghost_user_id')
            if ghost_user_id is None or len(
                    ghost_user_id) == 0 or ghost_user_id == '':
                return HTTPFound(request.application_url)
            ghost_user = DBSession.query(GhostUser).filter_by(
                id=ghost_user_id).filter_by(account_id=account_id).first()

            client_id = request.params.get('client_id')
            client = None
            if client_id is not None and len(
                    client_id) > 0 and client_id != '':
                client = DBSession.query(Client).filter_by(
                    account_id=account_id).filter_by(
                        id=long(client_id)).first()

            ghost_client_id = request.params.get('ghost_client_id')
            ghost_client = None
            if ghost_client_id is not None and len(
                    ghost_client_id) > 0 and ghost_client_id != '':
                ghost_client = DBSession.query(GhostClient).filter_by(
                    account_id=account_id).filter_by(
                        id=long(ghost_client_id)).first()

            if client is None and ghost_client is None:
                return HTTPFound(request.application_url)

            if client is not None and user.can_access_client(
                    client, "utilization") == False:
                return HTTPFound(request.application_url)

            if ghost_client is not None and user.can_access_office(
                    ghost_client.office, "utilization") == False:
                return HTTPFound(request.application_url)

            utilization = long(request.params["utilization"])

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]),
                                       long(start_dateparts[0]),
                                       long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]),
                                     long(end_dateparts[0]),
                                     long(end_dateparts[1]))

            if ghost_user.start_date.date(
            ) > start_date or start_date > end_date:
                print('**** late start')
                return HTTPFound(request.path)

            gua = GhostAllocation(ghost_user, client, ghost_client,
                                  utilization, start_date, end_date)
            DBSession.add(gua)

            if ghost_user.start_date.date() > start_date:
                ghost_user.start_date = datetime.datetime.combine(
                    start_date, datetime.time())

            DBSession.flush()
            if client is not None:
                return HTTPFound(request.application_url + "/client/" +
                                 str(client_id) + "/utilization/" +
                                 str(datetime.datetime.now().year))
            if ghost_client is not None:
                return HTTPFound(request.application_url + "/ghost/client/" +
                                 str(ghost_client_id) + "/utilization/" +
                                 str(datetime.datetime.now().year))

        currentClientId = request.params.get('clientid')
        currentClient = None

        if currentClientId is not None:
            currentClient = DBSession.query(Client).filter_by(
                id=currentClientId).filter_by(is_active=True).first()

        currentGhostClientId = request.params.get('ghostclientid')
        currentGhostClient = None

        if currentGhostClientId is not None:
            currentGhostClient = DBSession.query(GhostClient).filter_by(
                id=currentGhostClientId).filter_by(is_active=True).first()

        clients_all = DBSession.query(Client).filter_by(
            account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)

        #fix to handle disabled
        ghost_users_all = DBSession.query(GhostUser).filter_by(
            account_id=account_id).all()
        ghost_users = []
        if user.is_administrator or user.permissions_global_utilization:
            ghost_users = ghost_users_all
        else:
            for ghost_user in ghost_users:
                if user.can_access_office(ghost_user.office, "utilization"):
                    ghost_users.append(ghost_user)

        ghost_clients = []
        ghost_clients_all = DBSession.query(GhostClient).filter_by(
            account_id=account_id).all()
        if user.is_administrator or user.permissions_global_utilization:
            ghost_clients = ghost_clients_all
        else:
            for ghost_client in ghost_clients_all:
                if user.can_access_office(ghost_client.office, "utilization"):
                    ghost_clients.append(ghost_client)

        return dict(logged_in=authenticated_userid(request),
                    header=getHeader(None),
                    clients=clients,
                    ghost_users=ghost_users,
                    user=user,
                    account=account,
                    ghost_clients=ghost_clients,
                    currentClient=currentClient,
                    currentGhostClient=currentGhostClient)
    except:
        print("*****")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 14
0
def administration_expenses_clients(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        account = DBSession.query(Account).filter_by(id=account_id).first()
        user = DBSession.query(User).filter_by(id=user_id).first()
        year = str(datetime.datetime.now().year)
        quarter_end_year_text = None
        if user is None or account is None or not (user.is_administrator or user.permissions_global_financials):
            return HTTPFound(request.application_url)

        if request.method == "POST":

            quarter_end_date_text = request.params.get("quarter_end_type")
            quarter_end_year_text = request.params.get("quarter_end_year")
            quarter_end_date_parts = quarter_end_date_text.split("/")
            quarter_end_date = datetime.date(long(quarter_end_year_text), long(quarter_end_date_parts[0]),
                                             long(quarter_end_date_parts[1]))

            for client in account.clients:
                if request.params.get(str(client.id) + "-expense") is not None and request.params.get(
                                str(client.id) + "-expense") != "":
                    expense_lc = long(request.params.get(str(client.id) + "-expense"))
                else:
                    expense_lc = 0

                if user.currency is None:
                    expense_local = expense_lc
                else:
                    expense_local = expense_lc * user.currency.currency_to_usd

                actual_expense = DBSession.query(ActualExpense).filter_by(client_id=client.id).filter_by(
                    quarter_end_date=quarter_end_date).first()

                if actual_expense is not None:
                    actual_expense.expense_local += expense_local
                else:
                    actual_expense = ActualExpense(None, client, expense_local, None, quarter_end_date)
                    DBSession.add(actual_expense)

            DBSession.flush()

        usd_to_local = 1
        if user.currency is not None:
            usd_to_local = user.currency.usd_to_currency

        if quarter_end_year_text is not None:
            expense_year = int(quarter_end_year_text)
        else:
            expense_year = int(year)

        all_client_expense_sga = []
        for client in account.clients:
            client_expense_sga = [0, 0, 0, 0]

            for actual_expense in client.actual_expenses:
                if actual_expense.quarter_end_date.year == expense_year:

                    if actual_expense.quarter_end_date.month == 3:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[0] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 6:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[1] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 9:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[2] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 12:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[3] = actual_expense.expense_local * usd_to_local

            all_client_expense_sga.append(client_expense_sga)
        return dict(logged_in=authenticated_userid(request), header=Header("administration"), account=account,
                    user=user, year=year, all_client_expense_sga=all_client_expense_sga, expense_year=expense_year)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 15
0
def administration_expenses_clients(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        account = DBSession.query(Account).filter_by(id=account_id).first()
        user = DBSession.query(User).filter_by(id=user_id).first()
        year = str(datetime.datetime.now().year)
        quarter_end_year_text = None
        if user is None or account is None or not (
                user.is_administrator or user.permissions_global_financials):
            return HTTPFound(request.application_url)

        if request.method == "POST":

            quarter_end_date_text = request.params.get("quarter_end_type")
            quarter_end_year_text = request.params.get("quarter_end_year")
            quarter_end_date_parts = quarter_end_date_text.split("/")
            quarter_end_date = datetime.date(long(quarter_end_year_text),
                                             long(quarter_end_date_parts[0]),
                                             long(quarter_end_date_parts[1]))

            for client in account.clients:
                if request.params.get(str(client.id) + "-expense"
                                      ) is not None and request.params.get(
                                          str(client.id) + "-expense") != "":
                    expense_lc = long(
                        request.params.get(str(client.id) + "-expense"))
                else:
                    expense_lc = 0

                if user.currency is None:
                    expense_local = expense_lc
                else:
                    expense_local = expense_lc * user.currency.currency_to_usd

                actual_expense = DBSession.query(ActualExpense).filter_by(
                    client_id=client.id).filter_by(
                        quarter_end_date=quarter_end_date).first()

                if actual_expense is not None:
                    actual_expense.expense_local += expense_local
                else:
                    actual_expense = ActualExpense(None, client, expense_local,
                                                   None, quarter_end_date)
                    DBSession.add(actual_expense)

            DBSession.flush()

        usd_to_local = 1
        if user.currency is not None:
            usd_to_local = user.currency.usd_to_currency

        if quarter_end_year_text is not None:
            expense_year = int(quarter_end_year_text)
        else:
            expense_year = int(year)

        all_client_expense_sga = []
        for client in account.clients:
            client_expense_sga = [0, 0, 0, 0]

            for actual_expense in client.actual_expenses:
                if actual_expense.quarter_end_date.year == expense_year:

                    if actual_expense.quarter_end_date.month == 3:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[
                                0] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 6:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[
                                1] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 9:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[
                                2] = actual_expense.expense_local * usd_to_local

                    elif actual_expense.quarter_end_date.month == 12:
                        if actual_expense.expense_local is not None:
                            client_expense_sga[
                                3] = actual_expense.expense_local * usd_to_local

            all_client_expense_sga.append(client_expense_sga)
        return dict(logged_in=authenticated_userid(request),
                    header=Header("administration"),
                    account=account,
                    user=user,
                    year=year,
                    all_client_expense_sga=all_client_expense_sga,
                    expense_year=expense_year)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 16
0
def client_assign_resource(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()
        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            client_id = long(request.params['client_id'])
            client = DBSession.query(Client).filter_by(
                account_id=account_id).filter_by(id=client_id).first()

            person_id = long(request.params["user_id"])
            person = DBSession.query(User).filter_by(id=person_id).filter_by(
                account_id=account_id).first()

            utilization = long(request.params["utilization"])

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]),
                                       long(start_dateparts[0]),
                                       long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]),
                                     long(end_dateparts[0]),
                                     long(end_dateparts[1]))

            if person is None or client is None or user.can_access_client(
                    client, "utilization") == False:
                return HTTPFound(request.application_url)

            if person.start_date.date() > start_date or start_date > end_date:
                print("*** late start")
                return HTTPFound(request.path)

            ua = UserAllocation(person, client, None, utilization, start_date,
                                end_date)
            DBSession.add(ua)
            DBSession.flush()
            return HTTPFound(request.application_url + "/client/" +
                             str(client_id) + "/utilization/" +
                             str(datetime.datetime.now().year))

        clients_all = DBSession.query(Client).filter_by(
            account_id=account_id).filter_by(is_active=True).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)

        access_administration = user.is_administrator

        # fix this so the filtering is btter instead of doing a big loop
        users_all = DBSession.query(User).filter_by(
            account_id=account_id).filter_by(is_active=True).all()
        users = []
        for u in users_all:
            if u.is_active and u.percent_billable > 0 and user.can_access_office(
                    u.office, "utilization"):
                users.append(u)
        if len(users) == 0:
            return HTTPFound(request.application_url)

        currentClientId = request.params.get('clientid')
        currentClient = None

        if currentClientId is not None:
            currentClient = DBSession.query(Client).filter_by(
                id=currentClientId).filter_by(is_active=True).first()

        return dict(logged_in=authenticated_userid(request),
                    header=getHeader(None),
                    clients=clients,
                    users=users,
                    user=user,
                    account=account,
                    access_administration=access_administration,
                    currentClient=currentClient)
    except:
        print("*****")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 17
0
def person_edit(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or (user.is_administrator == False and user.is_hr_administrator == False):
            return HTTPFound(request.application_url)

        person_id = long(request.matchdict['person_id'])
        person = DBSession.query(User).filter_by(id=person_id).first()

        if request.method == "GET":
            try:
                source = request.params["source"]
            except:
                source = 'administration'

        if request.method == "POST":
            name = request.params["name"].lower()

            employee_number = request.POST.get('employee_number')
            source = request.params["source"]
            email = request.params["email"].lower()
            salary = long(request.params["salary"])

            is_raise = request.POST.get('raise')

            change_allocation = request.POST.get('change_allocation')

            office_id = request.POST.get('office_id')
            if office_id == '':
                office = None
            else:
                office_id = long(office_id)
                office = DBSession.query(Office).filter_by(id=office_id).filter_by(account_id=account_id).first()
            role_id = long(request.params["role_id"])
            role = DBSession.query(Role).filter_by(id=role_id).filter_by(account_id=account_id).first()
            percent_billable = long(request.params["percent_billable"])

            start_date_text = request.POST.get('start_date')
            if start_date_text == '':
                start_date = datetime.datetime.now()
            else:
                start_dateparts = start_date_text.split("/")
                start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.POST.get('end_date')
            if end_date_text == '':
                end_date = None
            else:
                end_dateparts = end_date_text.split("/")
                end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            currency_id = request.POST.get('currency_id')
            if currency_id != '':
                currency_id = long(currency_id)
                currency = DBSession.query(Currency).filter_by(id=currency_id).first()
            else:
                currency = None

            is_a = long(request.POST.get('is_administrator', '0'))
            if is_a == 1:
                is_administrator = True
            else:
                is_administrator = False

            is_h_a = long(request.POST.get('is_hr_administrator', '0'))
            if is_h_a == 1:
                is_hr_administrator = True
            else:
                is_hr_administrator = False

            is_e_a = long(request.POST.get('employee_assignment_access', '0'))
            if is_e_a == 1:
                employee_assignment_access = True
            else:
                employee_assignment_access = False


            u = DBSession.query(User).filter_by(email=email).first()
            if u is None or u.id == person_id:
                permissions_office_financials = request.params.getall("permissions_office_financials")
                permissions_global_financials = False
                for office_id in permissions_office_financials:
                    if office_id == "all":
                        permissions_global_financials = True
                        break
                permissions_office_utilization = request.params.getall("permissions_office_utilization")
                permissions_global_utilization = False
                for office_id in permissions_office_utilization:
                    if office_id == "all":
                        permissions_global_utilization = True
                        break
                permissions_office_pipeline = request.params.getall("permissions_office_pipeline")
                permissions_global_pipeline = False
                for office_id in permissions_office_pipeline:
                    if office_id == "all":
                        permissions_global_pipeline = True
                        break

                parsed_name = HumanName(name.lower())
                person.first_name = parsed_name.first
                person.middle_name = parsed_name.middle
                person.last_name = parsed_name.last
                person.employee_number = employee_number
                person.email = email.lower()
                person.salary = salary
                person.office = office
                person.role = role
                person.percent_billable = percent_billable
                person.start_date = start_date
                person.end_date = end_date
                person.currency = currency
                person.is_administrator = is_administrator
                person.is_hr_administrator = is_hr_administrator
                person.employee_assignment_access = employee_assignment_access
                person.permissions_global_financials = permissions_global_financials
                person.permissions_global_utilization = permissions_global_utilization
                person.permissions_global_pipeline = permissions_global_pipeline

                person.permissions_office_financials = []
                if person.permissions_global_financials == False:
                    for office_id in permissions_office_financials:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(
                            id=office_id).first()
                        if office is not None:
                            person.permissions_office_financials.append(office)
                person.permissions_office_utilization = []
                if person.permissions_global_utilization == False:
                    for office_id in permissions_office_utilization:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(
                            id=office_id).first()
                        if office is not None:
                            person.permissions_office_utilization.append(office)
                person.permissions_office_pipeline = []
                if person.permissions_global_pipeline == False:
                    for office_id in permissions_office_pipeline:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(
                            id=office_id).first()
                        if office is not None:
                            person.permissions_office_pipeline.append(office)
                person.permissions_client_financials = []
                permissions_client_financials = request.params.getall("permissions_client_financials")
                for client_id in permissions_client_financials:
                    client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        person.permissions_client_financials.append(client)
                person.permissions_client_utilization = []
                permissions_client_utilization = request.params.getall("permissions_client_utilization")
                for client_id in permissions_client_utilization:
                    client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        person.permissions_client_utilization.append(client)
                person.permissions_client_pipeline = []
                permissions_client_pipeline = request.params.getall("permissions_client_pipeline")
                for client_id in permissions_client_pipeline:
                    client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        person.permissions_client_pipeline.append(client)
                person.permissions_department_financials = []
                permissions_department_financials = request.params.getall("permissions_department_financials")
                for department_id in permissions_department_financials:
                    department = DBSession.query(Department).filter_by(account_id=account_id).filter_by(
                        id=client_id).first()
                    if department is not None:
                        person.permissions_department_financials.append(department)
                person.permissions_department_utilization = []
                permissions_department_utilization = request.params.getall("permissions_client_utilization")
                for department_id in permissions_client_utilization:
                    department = DBSession.query(Department).filter_by(account_id=account_id).filter_by(
                        id=department_id).first()
                    if department is not None:
                        person.permissions_department_utilization.append(department)

                s = DBSession.query(Salary).filter_by(user_id=person.id).order_by(Salary.start_date.desc()).first()

                if (is_raise is not None or change_allocation is not None) and (datetime.datetime.now().date() != s.start_date.date()):
                    s = Salary(person, salary, role.id, datetime.datetime.now(), percent_billable)
                    DBSession.add(s)
                else:
                    s.salary = salary
                    s.role_id = int(role.id)
                    s.percent_billable = percent_billable

                DBSession.flush()

                if source == "reviews":
                    return HTTPFound(request.application_url + "/people/all/all/all")
                else:
                    return HTTPFound(request.application_url + "/administration/employees")

        departments = DBSession.query(Department).filter_by(account_id=account_id).all()
        offices = DBSession.query(Office).filter_by(account_id=account_id).all()
        clients = DBSession.query(Client).filter_by(account_id=account_id).all()

        roles = DBSession.query(Role).filter_by(account_id=account_id).all()
        currencies = DBSession.query(Currency).filter_by(account_id=account_id).all()

        return dict(logged_in=authenticated_userid(request), header=Header(source), departments=departments,
                    offices=offices, clients=clients, roles=roles, user=user, person=person, currencies=currencies)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 18
0
def client_assign_resource(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()
        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            client_id = long(request.params['client_id'])
            client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()

            person_id = long(request.params["user_id"])
            person = DBSession.query(User).filter_by(id=person_id).filter_by(account_id=account_id).first()

            utilization = long(request.params["utilization"])

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            if person is None or client is None or user.can_access_client(client, "utilization") == False:
                return HTTPFound(request.application_url)

            if person.start_date.date() > start_date or start_date > end_date:
                print("*** late start")
                return HTTPFound(request.path)

            ua = UserAllocation(person, client, None, utilization, start_date, end_date)
            DBSession.add(ua)
            DBSession.flush()
            return HTTPFound(request.application_url + "/client/" + str(client_id) + "/utilization/" + str(
                datetime.datetime.now().year))

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).filter_by(is_active=True).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)

        access_administration = user.is_administrator

        # fix this so the filtering is btter instead of doing a big loop
        users_all = DBSession.query(User).filter_by(account_id=account_id).filter_by(is_active=True).all()
        users = []
        for u in users_all:
            if u.is_active and u.percent_billable > 0 and user.can_access_office(u.office, "utilization"):
                users.append(u)
        if len(users) == 0:
            return HTTPFound(request.application_url)

        currentClientId = request.params.get('clientid')
        currentClient = None

        if currentClientId is not None:
            currentClient = DBSession.query(Client).filter_by(id=currentClientId).filter_by(is_active=True).first()

        return dict(logged_in=authenticated_userid(request), header=getHeader(None), clients=clients, users=users,
                    user=user, account=account, access_administration=access_administration,
                    currentClient=currentClient)
    except:
        print("*****")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 19
0
def project_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["project_code"]
            client_id = long(request.params["client_id"])
            revenue_local = long(request.params["revenue"])

            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()

            if client is None or user.can_access_client(client, "financials") == False:
                return HTTPFound(request.application_url)

            for project in client.projects:
                if project.name == name:
                    return HTTPFound(request.application_url)

            new_project = Project(name, code, account, client, revenue, start_date, end_date)
            DBSession.add(new_project)
            DBSession.flush()

            department_matches = []
            for department in account.departments:
                percent_allocation = request.params.get(str(department.id) + "-allocation")
                if percent_allocation is not None and percent_allocation != "":
                    department_matches.append(str(department.id) + "-" + str(percent_allocation))

            for d in department_matches:
                ds = d.split('-')
                department = DBSession.query(Department).filter_by(id=int(ds[0])).first()
                budget_allocation = BudgetAllocation(department, new_project, None, int(ds[1]))
                DBSession.add(budget_allocation)

            DBSession.flush()

            if request.params.get("add_another") is None:
                return HTTPFound(request.application_url + "/client/" + str(client_id) + "/projects/" + str(
                    datetime.datetime.now().year))

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)
        return dict(logged_in=authenticated_userid(request), header=Header("financials"), clients=clients, user=user,
                    account=account)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 20
0
def freelancer_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            name = request.params["name"].lower()
            role_id = long(request.params["role_id"])
            client_id = request.params.get('client_id')
            office_id = request.params.get('office_id')

            client = None
            if client_id is not None and client_id != '' and len(client_id) > 0:
                client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=long(client_id)).first()

            office = None
            if office_id is not None and office_id != '' and len(office_id) > 0:
                office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=long(office_id)).first()

            if client is None and office is None:
                return HTTPFound(request.application_url)

            utilization = long(request.params["utilization"])
            hourly_rate_local = long(request.params["hourly_rate"])
            if user.currency is None:
                hourly_rate = hourly_rate_local
            else:
                hourly_rate = hourly_rate_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            role = DBSession.query(Role).filter_by(id=role_id).filter_by(account_id=account_id).first()

            if office is not None and user.can_access_office(office, "utilization") == False:
                return HTTPFound(request.application_url)

            if client is not None and user.can_access_client(client, "utilization") == False:
                return HTTPFound(request.application_url)

            freelancer = Freelancer(account, name, role, start_date, end_date, hourly_rate, utilization, client, office)
            DBSession.add(freelancer)
            DBSession.flush()

            if request.params.get("add_another") is None:
                if client is not None:
                    return HTTPFound(request.application_url + "/client/" + str(client_id) + "/utilization/" + str(
                        datetime.datetime.now().year))

                if office is not None:
                    return HTTPFound(request.application_url + "/office/" + str(office_id) + "/utilization/" + str(
                        datetime.datetime.now().year))

        currentClientId = request.params.get('clientid')
        currentClient = None

        if currentClientId is not None:
            currentClient = DBSession.query(Client).filter_by(id=currentClientId).filter_by(is_active=True).first()

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)

        offices_all = DBSession.query(Office).filter_by(account_id=account_id).all()
        offices = []
        if user.is_administrator or user.permissions_global_utilization:
            offices = offices_all
        else:
            for office in offices_all:
                if user.can_access_office(office, "utilization"):
                    offices.append(office)
        if len(offices) == 0:
            return HTTPFound(request.application_url)

        roles = DBSession.query(Role).filter_by(account_id=account_id).all()

        return dict(logged_in=authenticated_userid(request), header=Header('financials'), clients=clients,
                    offices=offices, roles=roles, user=user, account=account, currentClient=currentClient)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 21
0
def person_assign_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        person_id = long(request.matchdict['person_id'])
        person = DBSession.query(User).filter_by(id=person_id).first()

        if person is None or user.can_access_office(person.office, "utilization") == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            client_id = request.params.get("client_id")
            ghost_client_id = request.params.get("ghost_client_id")
            client = None
            ghost_client = None

            if client_id is not None and client_id != '' and len(client_id) > 0:
                client = DBSession.query(Client).filter_by(id=client_id).filter_by(account_id=account_id).first()

            if ghost_client_id is not None and ghost_client_id != '' and len(ghost_client_id) > 0:
                ghost_client = DBSession.query(GhostClient).filter_by(id=ghost_client_id).filter_by(
                    account_id=account_id).first()

            if client is None and ghost_client is None:
                print("*** no client")
                return HTTPFound(request.application_url)

            if client is not None and user.can_access_client(client, "utilization") == False:
                print("*** no client permission")
                return HTTPFound(request.application_url)

            if ghost_client is not None and user.can_access_office(ghost_client.office, "utilization") == False:
                print("*** no ghost client permission")
                return HTTPFound(request.application_url)

            utilization = long(request.params.get("utilization"))

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            if person.start_date.date() > start_date or start_date > end_date:
                print("*** late start")
                return HTTPFound(request.path)

            ua = UserAllocation(person, client, ghost_client, utilization, start_date, end_date)
            DBSession.add(ua)
            DBSession.flush()

            return HTTPFound(request.application_url + "/office/" + str(person.office_id) + "/utilization/" + str(
                datetime.datetime.now().year))

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        ghost_clients_all = DBSession.query(GhostClient).filter_by(account_id=account_id).all()
        ghost_clients = []
        if user.is_administrator or user.permissions_global_utilization:
            ghost_clients = ghost_clients_all
        else:
            for ghost_client in ghost_clients_all:
                if user.can_access_office(ghost_client.office, "utilization"):
                    ghost_clients.append(ghost_client)
        if len(clients) == 0 and len(ghost_clients) == 0:
            return HTTPFound(request.application_url)

        return dict(logged_in=authenticated_userid(request), header=Header("financials"), clients=clients,
                    ghost_clients=ghost_clients, person=person, user=user, account=account)
    except:
        print("*****")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 22
0
def person_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None or (user.is_administrator == False and user.is_hr_administrator == False):
            return HTTPFound(request.application_url)

        if request.method == "GET":
            source = request.params.get("source")
            if source is None or source == '' or len(source) == 0:
                source = 'financials'

        if request.method == "POST":
            email = request.params["email"]
            person = DBSession.query(User).filter_by(email=email).first()

            if person is not None:
                source = 'financials'
            else:

                account = DBSession.query(Account).filter_by(id=account_id).first()

                name = request.params["name"].lower()
                if request.params.get("employee_number") is None or request.params.get("employee_number") == '':
                    employee_number = 0
                else:
                    employee_number = long(request.params.get("employee_number"))

                source = request.params["source"]

                if request.params.get("salary") is None or request.params.get("salary") == '':
                    salary = 0
                else:
                    salary_local = long(request.params.get("salary"))
                    if user.currency is None:
                        salary = salary_local
                    else:
                        salary = salary_local * user.currency.currency_to_usd

                if request.params.get("office_id") is None or request.params.get("office_id") == '':
                    office_id = None
                    office = None
                else:
                    office_id = long(request.params.get("office_id"))
                    office = DBSession.query(Office).filter_by(id=office_id).filter_by(account_id=account_id).first()

                if request.params.get("role_id") is None or request.params.get("role_id") == '':
                    role_id = None
                    return HTTPFound(request.application_url + "/person/add")
                else:
                    role_id = long(request.params.get("role_id"))
                    role = DBSession.query(Role).filter_by(id=role_id).first()

                if request.params.get("percent_billable") is None or request.params.get("percent_billable") == '':
                    percent_billable = 100
                elif request.params.get("percent_billable") == '0':
                    percent_billable = 0
                else:
                    percent_billable = long(request.params.get("percent_billable"))

                if request.params.get("currency_id") is None or request.params.get("currency_id") == '':
                    currency_id = None
                    currency = None
                else:
                    currency_id = long(request.params.get("currency_id"))
                    currency = DBSession.query(Currency).filter_by(id=currency_id).filter_by(account_id=account_id).first()

                start_date_text = request.params.get("start_date")
                if start_date_text is None or start_date_text == '':
                    start_date = datetime.datetime.now()
                else:
                    start_dateparts = start_date_text.split("/")
                    start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

                end_date_text = request.params.get("end_date")
                if end_date_text is None or end_date_text == '':
                    end_date = None
                else:
                    end_dateparts = end_date_text.split("/")
                    end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

                if request.params.get("is_administrator") is None or request.params.get("is_administrator") == '':
                    is_administrator = False
                else:
                    is_administrator = True

                if request.params.get("is_hr_administrator") is None or request.params.get("is_hr_administrator") == '':
                    is_hr_administrator = False
                else:
                    is_hr_administrator = True

                if request.params.get("employee_assignment_access") is None or request.params.get("employee_assignment_access") == '':
                    employee_assignment_access = False
                else:
                    employee_assignment_access = True

                u = DBSession.query(User).filter_by(email=email).first()
                if u is not None:
                    return HTTPFound(request.application_url + "/person/add")

                new_user = User(account, name, email, office, role, salary, start_date)
                new_user.employee_number = employee_number
                new_user.percent_billable = percent_billable
                new_user.end_date = end_date
                new_user.is_administrator = is_administrator
                new_user.is_hr_administrator = is_hr_administrator
                new_user.currency = currency
                new_user.employee_assignment_access = employee_assignment_access

                permissions_office_financials = request.params.getall("permissions_office_financials")
                for office_id in permissions_office_financials:
                    if office_id == "all":
                        new_user.permissions_global_financials = True
                        break
                if new_user.permissions_global_financials == False:
                    for office_id in permissions_office_financials:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=office_id).first()
                        if office is not None:
                            new_user.permissions_office_financials.append(office)

                permissions_office_pipeline = request.params.getall("permissions_office_pipeline")
                for office_id in permissions_office_pipeline:
                    if office_id == "all":
                        new_user.permissions_global_pipeline = True
                        break
                if new_user.permissions_global_pipeline == False:
                    for office_id in permissions_office_pipeline:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=office_id).first()
                        if office is not None:
                            new_user.permissions_office_pipeline.append(office)

                permissions_office_utilization = request.params.getall("permissions_office_utilization")
                for office_id in permissions_office_utilization:
                    if office_id == "all":
                        new_user.permissions_global_utilization = True
                        break
                if new_user.permissions_global_utilization == False:
                    for office_id in permissions_office_utilization:
                        office = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=office_id).first()
                        if office is not None:
                            new_user.permissions_office_utilization.append(office)

                permissions_client_financials = request.params.getall("permissions_client_financials")
                for client_id in permissions_client_financials:
                    client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        new_user.permissions_client_financials.append(client)
                permissions_client_pipeline = request.params.getall("permissions_client_pipeline")
                for client_id in permissions_client_pipeline:
                    client = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        new_user.permissions_client_pipeline.append(client)
                permissions_client_utilization = request.params.getall("permissions_client_utilization")
                for client_id in permissions_client_utilization:
                    client = DBSession.query(Office).filter_by(account_id=account_id).filter_by(id=client_id).first()
                    if client is not None:
                        new_user.permissions_client_utilization.append(client)
                permissions_department_financials = request.params.getall("permissions_department_financials")
                for department_id in permissions_department_financials:
                    department = DBSession.query(Department).filter_by(account_id=account_id).filter_by(
                        id=department_id).first()
                    if department is not None:
                        new_user.permissions_department_financials.append(department)
                permissions_department_utilization = request.params.getall("permissions_client_utilization")
                for department_id in permissions_client_utilization:
                    department = DBSession.query(Department).filter_by(account_id=account_id).filter_by(
                        id=department_id).first()
                    if department is not None:
                        new_user.permissions_department_utilization.append(department)

                s = Salary(new_user, salary, role.id, start_date)
                new_user.salary_history.append(s)

                DBSession.add(new_user)
                DBSession.flush()

                if request.params.get("add_another") is None:
                    if source == "reviews":
                        return HTTPFound(request.application_url + "/people/all/all/all")
                    else:
                        source = "financials"
                        return HTTPFound(request.application_url + "/administration/employees")

        departments = DBSession.query(Department).filter_by(account_id=account_id).all()
        offices = DBSession.query(Office).filter_by(account_id=account_id).all()
        clients= DBSession.query(Client).filter_by(account_id=account_id).all()
        roles = DBSession.query(Role).filter_by(account_id=account_id).all()
        currencies = DBSession.query(Currency).filter_by(account_id=account_id).all()

        return dict(logged_in=authenticated_userid(request), header=Header(source), offices=offices, roles=roles,
                    clients=clients, currencies=currencies, user=user, departments=departments)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 23
0
def project_edit(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        project_id = long(request.matchdict['project_id'])
        project = DBSession.query(Project).filter_by(id=project_id).filter_by(account_id=account_id).first()

        if project is None or user.can_access_client(project.client, "financials") == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["project_code"]

            client_id = long(request.params["client_id"])
            revenue_local = long(request.params["revenue"])

            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=client_id).first()

            if client is None or user.can_access_client(client, "financials") == False:
                return HTTPFound(request.application_url)

            for p in client.projects:
                if p.name == name and p.id != project.id:
                    return HTTPFound(request.application_url)

            matched_departments = []
            for department in account.departments:
                percent_allocation = request.params.get(str(department.id) + "-allocation")
                for budget_allocation in project.budget_allocations:
                    if budget_allocation.department_id == department.id:
                        matched_departments.append(department.id)
                        if percent_allocation is None or percent_allocation == "":
                            DBSession.delete(budget_allocation)
                        else:
                            budget_allocation.percent_allocation = percent_allocation

            for department in account.departments:
                if department.id not in matched_departments:
                    percent_allocation = request.params.get(str(department.id) + "-allocation")
                    if percent_allocation is not None and percent_allocation != "":
                        ba = BudgetAllocation(department, project, None, int(percent_allocation))
                        DBSession.add(ba)

            project.name = name
            project.code = code
            project.client = client
            project.revenue = revenue
            project.start_date = start_date
            project.end_date = end_date
            DBSession.flush()

            return HTTPFound(request.application_url + "/client/" + str(client_id) + "/projects/" + str(
                datetime.datetime.now().year))

        matched_departments = []
        for budget_allocation in project.budget_allocations:
            matched_departments.append(budget_allocation.department_id)

        budgetAllocations = project.budget_allocations

        for department in account.departments:
            if department.id not in matched_departments:
                budgetAllocations.append(BudgetAllocation(department, project, None, 0))

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)
        return dict(logged_in=authenticated_userid(request), header=Header("financials"), clients=clients, user=user,
                    account=account, project=project, budgetAllocations=budgetAllocations)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 24
0
def project_add(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["project_code"]
            client_id = long(request.params["client_id"])
            revenue_local = long(request.params["revenue"])

            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]),
                                       long(start_dateparts[0]),
                                       long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]),
                                     long(end_dateparts[0]),
                                     long(end_dateparts[1]))

            client = DBSession.query(Client).filter_by(
                account_id=account_id).filter_by(id=client_id).first()

            if client is None or user.can_access_client(client,
                                                        "financials") == False:
                return HTTPFound(request.application_url)

            for project in client.projects:
                if project.name == name:
                    return HTTPFound(request.application_url)

            new_project = Project(name, code, account, client, revenue,
                                  start_date, end_date)
            DBSession.add(new_project)
            DBSession.flush()

            department_matches = []
            for department in account.departments:
                percent_allocation = request.params.get(
                    str(department.id) + "-allocation")
                if percent_allocation is not None and percent_allocation != "":
                    department_matches.append(
                        str(department.id) + "-" + str(percent_allocation))

            for d in department_matches:
                ds = d.split('-')
                department = DBSession.query(Department).filter_by(
                    id=int(ds[0])).first()
                budget_allocation = BudgetAllocation(department, new_project,
                                                     None, int(ds[1]))
                DBSession.add(budget_allocation)

            DBSession.flush()

            if request.params.get("add_another") is None:
                return HTTPFound(request.application_url + "/client/" +
                                 str(client_id) + "/projects/" +
                                 str(datetime.datetime.now().year))

        clients_all = DBSession.query(Client).filter_by(
            account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)
        return dict(logged_in=authenticated_userid(request),
                    header=Header("financials"),
                    clients=clients,
                    user=user,
                    account=account)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 25
0
def client_add(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["client_code"]
            office_id = long(request.params["office_id"])
            project_name = request.params["project_name"]
            project_code = request.params["project_code"]
            target_margin = request.params["target_margin"]
            revenue_local = long(request.params["revenue"])
            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            office = DBSession.query(Office).filter_by(id=office_id).filter_by(account_id=account_id).first()
            client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(name=name).first()

            if client is None and office is not None and user.can_access_office(office, "financials"):
                new_client = Client(name, office, code, target_margin)
                DBSession.add(new_client)

                new_project = Project(project_name, project_code, account, new_client, revenue, start_date, end_date)
                DBSession.add(new_project)

                department_matches = []
                for department in account.departments:
                    percent_allocation = request.params.get(str(department.id) + "-allocation")
                    if percent_allocation is not None and percent_allocation != "":
                        department_matches.append(str(department.id) + "-" + str(percent_allocation))

                DBSession.flush()

                for d in department_matches:
                    ds = d.split('-')
                    department = DBSession.query(Department).filter_by(id=int(ds[0])).first()
                    budget_allocation = BudgetAllocation(department, new_project, None, int(ds[1]))
                    DBSession.add(budget_allocation)

                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url + "/office/" + str(office_id) + "/clients/" + str(
                        datetime.datetime.now().year))

        officeId = request.params.get('officeid')
        currentOffice = None

        if officeId is not None:
            currentOffice = DBSession.query(Office).filter_by(id=officeId).filter_by(is_active=True).first()

        offices_all = DBSession.query(Office).filter_by(account_id=long(request.session['aid'])).all()
        offices = []
        if user.is_administrator or user.permissions_global_financials:
            offices = offices_all
        else:
            for office in offices_all:
                if user.can_access_office(office, "financials"):
                    offices.append(office)

        return dict(logged_in=authenticated_userid(request), header=getHeader(None), offices=offices, user=user,
                    account=account, currentOffice=currentOffice)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 26
0
def project_edit(request):
    try:
        account_id = long(request.session['aid'])
        user_id = long(request.session['uid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        project_id = long(request.matchdict['project_id'])
        project = DBSession.query(Project).filter_by(id=project_id).filter_by(
            account_id=account_id).first()

        if project is None or user.can_access_client(project.client,
                                                     "financials") == False:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["project_code"]

            client_id = long(request.params["client_id"])
            revenue_local = long(request.params["revenue"])

            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]),
                                       long(start_dateparts[0]),
                                       long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]),
                                     long(end_dateparts[0]),
                                     long(end_dateparts[1]))

            client = DBSession.query(Client).filter_by(
                account_id=account_id).filter_by(id=client_id).first()

            if client is None or user.can_access_client(client,
                                                        "financials") == False:
                return HTTPFound(request.application_url)

            for p in client.projects:
                if p.name == name and p.id != project.id:
                    return HTTPFound(request.application_url)

            matched_departments = []
            for department in account.departments:
                percent_allocation = request.params.get(
                    str(department.id) + "-allocation")
                for budget_allocation in project.budget_allocations:
                    if budget_allocation.department_id == department.id:
                        matched_departments.append(department.id)
                        if percent_allocation is None or percent_allocation == "":
                            DBSession.delete(budget_allocation)
                        else:
                            budget_allocation.percent_allocation = percent_allocation

            for department in account.departments:
                if department.id not in matched_departments:
                    percent_allocation = request.params.get(
                        str(department.id) + "-allocation")
                    if percent_allocation is not None and percent_allocation != "":
                        ba = BudgetAllocation(department, project, None,
                                              int(percent_allocation))
                        DBSession.add(ba)

            project.name = name
            project.code = code
            project.client = client
            project.revenue = revenue
            project.start_date = start_date
            project.end_date = end_date
            DBSession.flush()

            return HTTPFound(request.application_url + "/client/" +
                             str(client_id) + "/projects/" +
                             str(datetime.datetime.now().year))

        matched_departments = []
        for budget_allocation in project.budget_allocations:
            matched_departments.append(budget_allocation.department_id)

        budgetAllocations = project.budget_allocations

        for department in account.departments:
            if department.id not in matched_departments:
                budgetAllocations.append(
                    BudgetAllocation(department, project, None, 0))

        clients_all = DBSession.query(Client).filter_by(
            account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)
        return dict(logged_in=authenticated_userid(request),
                    header=Header("financials"),
                    clients=clients,
                    user=user,
                    account=account,
                    project=project,
                    budgetAllocations=budgetAllocations)
    except:
        return HTTPFound(request.application_url)
Exemplo n.º 27
0
def client_assign_ghost(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":
            ghost_user_id = request.params.get('ghost_user_id')
            if ghost_user_id is None or len(ghost_user_id) == 0 or ghost_user_id == '':
                return HTTPFound(request.application_url)
            ghost_user = DBSession.query(GhostUser).filter_by(id=ghost_user_id).filter_by(account_id=account_id).first()

            client_id = request.params.get('client_id')
            client = None
            if client_id is not None and len(client_id) > 0 and client_id != '':
                client = DBSession.query(Client).filter_by(account_id=account_id).filter_by(id=long(client_id)).first()

            ghost_client_id = request.params.get('ghost_client_id')
            ghost_client = None
            if ghost_client_id is not None and len(ghost_client_id) > 0 and ghost_client_id != '':
                ghost_client = DBSession.query(GhostClient).filter_by(account_id=account_id).filter_by(
                    id=long(ghost_client_id)).first()

            if client is None and ghost_client is None:
                return HTTPFound(request.application_url)

            if client is not None and user.can_access_client(client, "utilization") == False:
                return HTTPFound(request.application_url)

            if ghost_client is not None and user.can_access_office(ghost_client.office, "utilization") == False:
                return HTTPFound(request.application_url)

            utilization = long(request.params["utilization"])

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]), long(start_dateparts[0]), long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1]))

            if ghost_user.start_date.date() > start_date or start_date > end_date:
                print('**** late start')
                return HTTPFound(request.path)

            gua = GhostAllocation(ghost_user, client, ghost_client, utilization, start_date, end_date)
            DBSession.add(gua)

            if ghost_user.start_date.date() > start_date:
                ghost_user.start_date = datetime.datetime.combine(start_date, datetime.time())

            DBSession.flush()
            if client is not None:
                return HTTPFound(request.application_url + "/client/" + str(client_id) + "/utilization/" + str(
                    datetime.datetime.now().year))
            if ghost_client is not None:
                return HTTPFound(
                    request.application_url + "/ghost/client/" + str(ghost_client_id) + "/utilization/" + str(
                        datetime.datetime.now().year))

        currentClientId = request.params.get('clientid')
        currentClient = None

        if currentClientId is not None:
            currentClient = DBSession.query(Client).filter_by(id=currentClientId).filter_by(is_active=True).first()

        currentGhostClientId = request.params.get('ghostclientid')
        currentGhostClient = None

        if currentGhostClientId is not None:
            currentGhostClient = DBSession.query(GhostClient).filter_by(id=currentGhostClientId).filter_by(
                is_active=True).first()

        clients_all = DBSession.query(Client).filter_by(account_id=account_id).all()
        clients = []
        if user.is_administrator or user.permissions_global_utilization:
            clients = clients_all
        else:
            for client in clients_all:
                if user.can_access_client(client, "utilization"):
                    clients.append(client)
        if len(clients) == 0:
            return HTTPFound(request.application_url)

        #fix to handle disabled
        ghost_users_all = DBSession.query(GhostUser).filter_by(account_id=account_id).all()
        ghost_users = []
        if user.is_administrator or user.permissions_global_utilization:
            ghost_users = ghost_users_all
        else:
            for ghost_user in ghost_users:
                if user.can_access_office(ghost_user.office, "utilization"):
                    ghost_users.append(ghost_user)

        ghost_clients = []
        ghost_clients_all = DBSession.query(GhostClient).filter_by(account_id=account_id).all()
        if user.is_administrator or user.permissions_global_utilization:
            ghost_clients = ghost_clients_all
        else:
            for ghost_client in ghost_clients_all:
                if user.can_access_office(ghost_client.office, "utilization"):
                    ghost_clients.append(ghost_client)

        return dict(logged_in=authenticated_userid(request), header=getHeader(None), clients=clients,
                    ghost_users=ghost_users, user=user, account=account, ghost_clients=ghost_clients,
                    currentClient=currentClient, currentGhostClient=currentGhostClient)
    except:
        print("*****")
        traceback.print_exc()
        return HTTPFound(request.application_url)
Exemplo n.º 28
0
def client_add(request):
    try:
        user_id = long(request.session['uid'])
        account_id = long(request.session['aid'])
        user = DBSession.query(User).filter_by(id=user_id).first()
        account = DBSession.query(Account).filter_by(id=account_id).first()

        if user is None or account is None:
            return HTTPFound(request.application_url)

        if request.method == "POST":

            name = request.params["name"].lower()
            code = request.params["client_code"]
            office_id = long(request.params["office_id"])
            project_name = request.params["project_name"]
            project_code = request.params["project_code"]
            target_margin = request.params["target_margin"]
            revenue_local = long(request.params["revenue"])
            if user.currency is None:
                revenue = revenue_local
            else:
                revenue = revenue_local * user.currency.currency_to_usd

            start_date_text = request.params["start_date"]
            start_dateparts = start_date_text.split("/")
            start_date = datetime.date(long(start_dateparts[2]),
                                       long(start_dateparts[0]),
                                       long(start_dateparts[1]))

            end_date_text = request.params["end_date"]
            end_dateparts = end_date_text.split("/")
            end_date = datetime.date(long(end_dateparts[2]),
                                     long(end_dateparts[0]),
                                     long(end_dateparts[1]))

            office = DBSession.query(Office).filter_by(id=office_id).filter_by(
                account_id=account_id).first()
            client = DBSession.query(Client).filter_by(
                account_id=account_id).filter_by(name=name).first()

            if client is None and office is not None and user.can_access_office(
                    office, "financials"):
                new_client = Client(name, office, code, target_margin)
                DBSession.add(new_client)

                new_project = Project(project_name, project_code, account,
                                      new_client, revenue, start_date,
                                      end_date)
                DBSession.add(new_project)

                department_matches = []
                for department in account.departments:
                    percent_allocation = request.params.get(
                        str(department.id) + "-allocation")
                    if percent_allocation is not None and percent_allocation != "":
                        department_matches.append(
                            str(department.id) + "-" + str(percent_allocation))

                DBSession.flush()

                for d in department_matches:
                    ds = d.split('-')
                    department = DBSession.query(Department).filter_by(
                        id=int(ds[0])).first()
                    budget_allocation = BudgetAllocation(
                        department, new_project, None, int(ds[1]))
                    DBSession.add(budget_allocation)

                DBSession.flush()

                if request.params.get("add_another") is None:
                    return HTTPFound(request.application_url + "/office/" +
                                     str(office_id) + "/clients/" +
                                     str(datetime.datetime.now().year))

        officeId = request.params.get('officeid')
        currentOffice = None

        if officeId is not None:
            currentOffice = DBSession.query(Office).filter_by(
                id=officeId).filter_by(is_active=True).first()

        offices_all = DBSession.query(Office).filter_by(
            account_id=long(request.session['aid'])).all()
        offices = []
        if user.is_administrator or user.permissions_global_financials:
            offices = offices_all
        else:
            for office in offices_all:
                if user.can_access_office(office, "financials"):
                    offices.append(office)

        return dict(logged_in=authenticated_userid(request),
                    header=getHeader(None),
                    offices=offices,
                    user=user,
                    account=account,
                    currentOffice=currentOffice)
    except:
        traceback.print_exc()
        return HTTPFound(request.application_url)