def account_edit(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 user.is_administrator == False or account is None: return HTTPFound(request.application_url) if request.method == "POST": name = request.params["name"] benefits_and_bonus = long(request.params["benefits_and_bonus"]) acc = DBSession.query(Account).filter_by(name=name).first() if acc is None or acc.id == account.id: DBSession.query(Account).filter_by(id=account_id).update({ 'name': name, 'benefits_and_bonus': benefits_and_bonus }) DBSession.flush() return HTTPFound(request.application_url + "/administration/company") return dict(logged_in=authenticated_userid(request), header=Header("administration"), account=account, user=user) except: return HTTPFound(request.application_url)
def person_enable_login(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 = request.matchdict.get('person_id') if user_id == person_id: return HTTPFound(request.application_url + "/administration/employees") person = DBSession.query(User).filter_by(id=person_id).filter_by(account_id=account_id).first() password = None if person is None: return HTTPFound(request.application_url + "/administration/employees") password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) encrypted_password = bcrypt.hashpw(password, bcrypt.gensalt()) person.password = encrypted_password DBSession.flush() return dict(logged_in=authenticated_userid(request), header=Header("administration"), person=person, user=user, password=password) except: return HTTPFound(request.application_url)
def department_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 is False: return HTTPFound(request.application_url) department_id = request.matchdict['department_id'] 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 or dept.id == department_id: DBSession.query(Department).filter_by( id=department_id).filter_by(account_id=account_id).update( {'name': name}) DBSession.flush() return HTTPFound(location=request.application_url + "/administration/company") department = DBSession.query(Department).filter_by( id=department_id).first() return dict(logged_in=authenticated_userid(request), header=Header("administration"), department=department, user=user) except: return HTTPFound(request.application_url)
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)
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)
def client_projects_update(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) client_id = long(request.matchdict['client_id']) client = DBSession.query(Client).filter_by(id=client_id).filter_by( account_id=account_id).first() if client is None or user.can_access_client(client, "financials") == False: return HTTPFound(request.application_url) if request.method == "POST": for project in account.projects: if project.is_active and project.client.office_id == client.office_id: revenue_local = long( request.params.get(str(project.id) + "-revenue")) if user.currency is None: revenue = revenue_local else: revenue = revenue_local * user.currency.currency_to_usd start_date_text = request.params.get( str(project.id) + "-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.get( str(project.id) + "-end_date") end_dateparts = end_date_text.split("/") end_date = datetime.date(long(end_dateparts[2]), long(end_dateparts[0]), long(end_dateparts[1])) 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)) return dict(logged_in=authenticated_userid(request), header=Header("financials"), user=user, account=account, client=client) except: traceback.print_exc() return HTTPFound(request.application_url)
def getHeader(department): header = Header("finanicials") header.division = "department" header.divisionname = department.name header.divisionid = department.id return header
def getHeader(office): header = Header("finanicials") header.division = "office" if office is not None: header.divisionname = office.name header.divisionid = office.id return header
def global_expenses(request): try: user_id = long(request.session['uid']) account_id = long(request.session['aid']) year = request.matchdict['year'] 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 not ( user.is_administrator or user.permissions_global_financials): return HTTPFound(request.application_url) offices = DBSession.query(Office).filter_by( account_id=account_id).all() if offices is None: return HTTPFound(request.application_url) if request.method == "POST": for office in offices: change = False ase = None sga = None ase_text = request.POST.get("ase_" + str(office.id)) if ase_text is not None and ase_text != '': ase_local = long(ase_text) if user.currency is None: ase = ase_local else: ase = ase_local * user.currency.currency_to_usd office.allocated_salary_expense = ase sga_text = request.POST.get("sga_" + str(office.id)) if sga_text is not None and sga_text != '': sga_local = long(sga_text) if user.currency is None: sga = sga_local else: sga = sga_local * user.currency.currency_to_usd office.sga_expense = sga DBSession.flush() # return HTTPFound(request.application_url + "/administration/expenses") return dict(logged_in=authenticated_userid(request), header=Header("financials"), offices=offices, year=year, user=user) except: traceback.print_exc() return HTTPFound(request.application_url)
def role(request): role_id = request.matchdict.get('role_id') user = DBSession.query(User).filter_by( id=long(request.session['uid'])).first() role = DBSession.query(Role).filter_by(id=long(role_id)).first() return dict(logged_in=authenticated_userid(request), header=Header("reviews"), role=role, user=user)
def role_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: return HTTPFound(request.application_url) role_id = long(request.matchdict['role_id']) role = DBSession.query(Role).filter_by(id=role_id).filter_by( account_id=account_id).first() if request.method == "POST": name = request.params["name"].lower() department_id = long(request.params["department_id"]) salary_low_local = long(request.params["salary_low"]) salary_high_local = long(request.params["salary_high"]) if user.currency is None: salary_low = salary_low_local salary_high = salary_high_local else: salary_low = salary_low_local * user.currency.currency_to_usd salary_high = salary_high_local * user.currency.currency_to_usd department = DBSession.query(Department).filter_by( id=department_id).filter_by(account_id=account.id).first() role_ok = True for r in department.roles: if r.name == name and r.id != role_id: role_ok = False if role_ok: role.name = name role.department = department role.salary_low = salary_low role.salary_high = salary_high DBSession.flush() 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, role=role) except: return HTTPFound(request.application_url)
def getHeader(client): header = Header("finanicials") header.division = "office" if client is not None: header.divisionname = client.office.name header.divisionid = client.office.id header.subdivision = "clients" header.subdivisionid = client.id header.subdivisionname = client.name return header
def administration_revenue(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 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 project in account.projects: revenue_local = long( request.params.get(str(project.id) + "-revenue")) if user.currency is None: revenue = revenue_local else: revenue = revenue_local * user.currency.currency_to_usd actual_revenue = DBSession.query(ActualRevenue).filter_by( project_id=project.id).filter_by( quarter_end_date=quarter_end_date).first() if actual_revenue is not None: actual_revenue.revenue = revenue else: actual_revenue = ActualRevenue(project, revenue, quarter_end_date) DBSession.flush() return HTTPFound(request.application_url + "/administration/company") return dict(logged_in=authenticated_userid(request), header=Header("administration"), account=account, user=user) except: traceback.print_exc() return HTTPFound(request.application_url)
def administration_company(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) return dict(logged_in=authenticated_userid(request), header=Header("administration"), account=account, user=user) except: return HTTPFound(request.application_url)
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)
def currency_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: return HTTPFound(request.application_url) currency_id = long(request.matchdict['currency_id']) currency = DBSession.query(Currency).filter_by(id=currency_id).first() if currency == None: return HTTPFound(request.application_url + "/administration/company") if request.method == "POST": error = False name = request.params["name"].lower() if name == "usd": error = True currency_to_usd = float(request.params["currency_to_usd"]) cur = DBSession.query(Currency).filter_by( account_id=account_id).filter_by(name=name).first() if cur is not None and cur.id != currency_id: error = True if error == False: currency.name = name currency.currency_to_usd = currency_to_usd DBSession.flush() return HTTPFound(request.application_url + "/administration/company") return dict(logged_in=authenticated_userid(request), header=Header("administration"), currency=currency, user=user) except: return HTTPFound(request.application_url)
def administration_password(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: return HTTPFound(request.application_url) if request.method == "POST": password = request.params["password"] user.set_password(password) transaction.commit() return HTTPFound(request.application_url) return dict(logged_in=authenticated_userid(request), header=Header("administration"), account=account, user=user) except: return HTTPFound(request.application_url)
def office_edit(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) office_id = request.matchdict['office_id'] if request.method == "POST": name = request.params["name"].lower() off = DBSession.query(Office).filter_by( account_id=long(request.session['aid'])).filter_by( name=name).first() if off is None or str(off.id) == office_id: benefits_and_bonus = long(request.params["benefits_and_bonus"]) DBSession.query(Office).filter_by(id=office_id).filter_by( account_id=account_id).update({ 'name': name, 'benefits_and_bonus': benefits_and_bonus }) DBSession.flush() return HTTPFound(request.application_url + "/administration/company") office = DBSession.query(Office).filter_by(id=office_id).first() return dict(logged_in=authenticated_userid(request), header=Header("administration"), office=office, user=user) except: return HTTPFound(request.application_url)
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)
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)
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)
def freelancer_edit(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": 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 role is None or 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) parsed_name = HumanName(name) freelancer.first_name = parsed_name.first freelancer.middle_name = parsed_name.middle freelancer.last_name = parsed_name.last freelancer.office = office freelancer.client = client freelancer.start_date = start_date freelancer.end_date = end_date freelancer.utilization = utilization freelancer.hourly_rate = hourly_rate DBSession.flush() 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)) 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: print("************* no c") 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: print("************* no o") return HTTPFound(request.application_url) roles = DBSession.query(Role).filter_by(account_id=long(request.session['aid'])).all() return dict(logged_in=authenticated_userid(request), header=Header('financials'), clients=clients, offices=offices, roles=roles, freelancer=freelancer, user=user, account=account) except: print("*************") traceback.print_exc() return HTTPFound(request.application_url)
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)
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)
def person_assign_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: print("*** no user") 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: print("*** no person") return HTTPFound(request.application_url) source_type_text = request.params.get("source_type") if source_type_text is None: source_type = "office" elif source_type_text == "ghost_client": source_type = "ghost/client" elif source_type_text == "client": source_type = source_type_text elif source_type_text == "administration": source_type = source_type_text else: source_type = "office" source_id_text = request.params.get("source_id") if source_id_text is None: source_id = person.office_id else: source_id = long(source_id_text) if request.method == "POST": for assignment in person.allocations: client_id = None ghost_client_id = None client_id = request.params.get(str(assignment.id) + "-client_id") ghost_client_id = request.params.get(str(assignment.id) + "-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: 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, "utilization") == False: return HTTPFound(request.application_url) utilization = long(request.params[str(assignment.id) + "-utilization"]) start_date_text = request.params[str(assignment.id) + "-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[str(assignment.id) + "-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) assignment.client = client assignment.ghost_client = ghost_client assignment.utilization = utilization assignment.start_date = start_date assignment.end_date = end_date DBSession.flush() return HTTPFound(request.application_url + "/" + source_type + "/" + str(source_id) + "/utilization/" + str( datetime.datetime.now().year)) assignments_all = DBSession.query(UserAllocation).filter_by(user_id=person_id).all() assignments = [] if user.is_administrator or user.permissions_global_utilization: assignments = assignments_all else: for assignment in assignments_all: if assignment.client_id is not None: if user.can_access_client(assignment.client, "utilization"): assignments.append(assignment) elif assignment.ghost_client_id is not None: if user.can_access_office(assignment.ghost_client.office, "utilization"): assignments.append(assignment) # Commented for now. # if len(assignments) == 0: # return HTTPFound(request.application_url + "/" + source_type + "/" + str(source_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): 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, assignments=assignments, source_id=source_id, source_type=source_type, year=str(datetime.datetime.now().year)) except: print("*****") traceback.print_exc() return HTTPFound(request.application_url)
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)
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)
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)
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)