Esempio n. 1
0
def add_relation(request):
    '''
    Web page to add a new relation
    :param request: Http request
    :return: Http response
    '''
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_relations
        if request.method == 'GET':
            try:
                context[params.start_date] = times.current_date()
                all_clients = info_queries.name_from_clientid(date=times.current_date())
                context[params.client] = all_clients
                all_rms = info_queries.rm_details(date=times.current_date())
                all_rms = list(all_rms.keys())
                context[params.rm_name] = all_rms
                return render(request, pages.add_relation, context)
            except psycopg2.DatabaseError as e:
                raise psycopg2.DatabaseError(e)
            except Exception as e:
                raise Exception(e)
        elif request.method == 'POST':
            try:
                info = request.POST
                start_date = info[params.start_date]
                buyer_id = info[params.buyer]
                buyer_name = info_queries.name_from_clientid(clientid=buyer_id)[int(buyer_id)]
                supplier_id = info[params.supplier]
                supplier_name = info_queries.name_from_clientid(clientid=supplier_id)[int(supplier_id)]
                buyer_fraction = info[params.buyer_fraction]
                supplier_fraction = info[params.supplier_fraction]
                buyer_approval = int(info[params.buyer_approval])
                supplier_approval = int(info[params.supplier_approval])
                rm_name = info[params.rm_name]

                data = dict()
                data[params.start_date] = start_date
                data[params.buyer_id] = buyer_id
                data[params.buyer_name] = buyer_name
                data[params.supplier_id] = supplier_id
                data[params.supplier_name] = supplier_name
                data[params.buyer_fraction] = buyer_fraction
                data[params.supplier_fraction] = supplier_fraction
                data[params.buyer_approval] = buyer_approval
                data[params.supplier_approval] = supplier_approval
                data[params.rm_name] = rm_name

                inject_queries.add_relation(data)
                context[params.messages] = ['Successfully added relation between ' +\
                                           buyer_name + ' and ' + supplier_name]
            except psycopg2.DatabaseError as e:
                context[params.has_errors] = [messages.error_relation_add]
            return render(request, pages.add_relation, context)
    else:
        return redirect(login)
Esempio n. 2
0
def client_repayment(request):
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_repayment
        if request.method == 'GET':
            if params.client_id in request.session:
                client_id = request.session[params.client_id]
                client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
                context[params.client_name] = client_name
                return render(request, pages.client_repayment, context)
    else:
        return redirect(login)
Esempio n. 3
0
def client_open_invoices(request):
    std_limit = 100
    std_offset = 0
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_open_invoices
        if params.page_type in request.session:
            context[params.page_type] = request.session[params.page_type]
        if request.method == 'GET':
            if params.client_id in request.session:
                client_id = request.session[params.client_id]
                client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
                context[params.client_name] = client_name

                # set the number of rows to fetch
                if params.previous_page in request.session and\
                                request.session[params.previous_page] == client_open_invoices:
                    if params.row_multiplier in request.session:
                        current_multiplier = request.session[params.row_multiplier]
                else:
                    request.session[params.previous_page] = client_open_invoices
                    current_multiplier = 1
                std_offset = (std_limit * current_multiplier) - std_limit

                if request.session[params.page_type] in request.session[params.client_type]:
                    if request.session[params.page_type] == params.buyer:
                        data = grabber.retrieve_invoices(buyer_id=client_id,
                                                         status=params.open_status,
                                                         limit_rows=std_limit,
                                                         offset=std_offset,
                                                         as_dict=True)
                    else:
                        data = grabber.retrieve_invoices(supplier_id=client_id,
                                                         status=params.open_status,
                                                         limit_rows=std_limit,
                                                         offset=std_offset,
                                                         as_dict=True)
                    data = sorted(data, key=lambda k: k[params.submitted_on], reverse=True)
                    context[params.data] = data
                    context[params.row_multiplier] = current_multiplier
                    context[params.row_count] = len(context[params.data])
                    request.session[params.row_multiplier] = current_multiplier
                    return render(request, pages.client_open_invoices, context)
                else:
                    return render(request, pages.admin_invalid_client_type, context)
    else:
        return redirect(login)
Esempio n. 4
0
def client_profile(request):
    '''
    Gets a web page with a client's detail
    :param request: Http request
    :return: Http response
    '''
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_clients
        client_id = None

        if request.method == 'GET':
            if params.client_id in request.session:
                client_id = request.session[params.client_id]
                client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
                context[params.client_name] = client_name

        elif request.method == 'POST':
            info = request.POST
            client_id = info[params.client_id]

            # set the client type for this client
            request.session[params.client_type] = grabber.get_client_type(clientid=client_id)
            request.session[params.client_id] = int(client_id)

        data = grabber.present_client_profile(client_id, times.current_date())
        context.update(data)

        # set the page type
        if params.buyer not in request.session[params.client_type]:
            request.session[params.page_type] = params.supplier
        else:
            request.session[params.page_type] = params.buyer

        context[params.page_type] = request.session[params.page_type]
        context[params.edit] = 2
        if params.edit in request.session:
            context[params.edit] = request.session[params.edit]
            del request.session[params.edit]

        if request.session[params.page_type] in request.session[params.client_type]:
            request.method = 'GET'
            return render(request, pages.admin_client_profile, context)
        else:
            return render(request, pages.admin_invalid_client_type, context)
    else:
        return redirect(login)
Esempio n. 5
0
def client_relations(request):
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_relations
        if params.page_type in request.session:
            context[params.page_type] = request.session[params.page_type]

        if request.method == 'GET':
            if params.client_id in request.session:
                client_id = request.session[params.client_id]
                client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
                context[params.client_name] = client_name

                if params.client_type not in request.session:
                    request.session[params.client_type] = grabber.get_client_type(clientid=client_id)

                if request.session[params.page_type] in request.session[params.client_type]:
                    if request.session[params.page_type] == params.buyer:
                        relations = info_queries.get_relations(buyer_id=client_id)
                    else:
                        relations = info_queries.get_relations(supplier_id=client_id)
                    for rel in relations:
                        rel[params.start_date] = times.date_to_string(rel[params.start_date])
                        rel[params.end_date] = times.date_to_string(rel[params.end_date])
                    context[params.client_relations] = relations
                    return render(request, pages.client_relations, context)
                else:
                    context[params.page_type] = request.session[params.page_type]
                    return render(request, pages.admin_invalid_client_type, context)
        elif request.method == 'POST':
            try:
                info = request.POST
                buyer_id = info[params.buyer_id]
                supplier_id = info[params.supplier_id]
                start_date = info[params.start_date]

                request.session[params.buyer_id] = buyer_id
                request.session[params.supplier_id] = supplier_id
                request.session[params.start_date] = start_date

                request.method = 'GET'
                return redirect(edit_relations)
            except psycopg2.DatabaseError as e:
                pass
    else:
        return redirect(login)
Esempio n. 6
0
def edit_profile(request):
    '''
    Turns on the edit mode of the client profile page and redirects to it
    :param request: Http request
    :return: Http response
    '''
    if params.username in request.session and request.session[params.user_type] == params.admin:
        if request.method == 'GET':
            request.session[params.edit] = 1
            return redirect(client_profile)
        elif request.method == 'POST':
            context = dict()
            context[params.title] = pages.title_clients
            if params.client_id in request.session:
                client_id = request.session[params.client_id]
                client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
                context[params.client_name] = client_name

                info = request.POST
                data = dict()
                data[params.client_id] = client_id
                data[params.address] = info[params.address]
                data[params.city] = info[params.city]
                data[params.client_zip] = info[params.client_zip]
                data[params.country] = info[params.country]
                data[params.registration] = info[params.registration]
                data[params.tax_pin] = info[params.tax_pin]
                data[params.office_phone] = info[params.office_phone]
                data[params.description] = info[params.description]
                data[params.client_type] = info[params.client_type]
                data[params.industry] = info[params.industry]
                data[params.client_size] = info[params.client_size]
                try:
                    inject_queries.update_client_profile(data)
                    context[params.messages] = ['Successfully updated client information']
                except psycopg2.DatabaseError as e:
                    context[params.has_errors] = [messages.error_client_edit]
                finally:
                    return render(request, pages.admin_client_profile, context)
            else:
                return redirect(client_profile)
    else:
        return redirect(login)
Esempio n. 7
0
def upload_invoice(date,
                   username,
                   buyer_id,
                   supplier_id,
                   invoice_date,
                   financing_product_id,
                   description,
                   invoice_value,
                   currency='KES',
                   timestamp=None):
    new_invoice_value = float(invoice_value)
    buyer_id = int(buyer_id)
    supplier_id = int(supplier_id)
    errors = []

    # check buyer limits first
    buyer_open_invoices = grabber.retrieve_invoices(buyer_id=buyer_id,
                                                    status=params.open_status)
    buyer_portfolio = Portfolio(buyer_open_invoices)
    buyer_limits = ClientLimits(buyer_id, date)
    if not buyer_limits.within_discounting_giv(
            currency,
            buyer_portfolio.current_giv() + new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.discounting_max_giv,
                buyer_limits.get_discounting_giv(currency),
                buyer_portfolio.current_giv() + new_invoice_value))
        return errors

    if not buyer_limits.within_discounting_single_iv(currency,
                                                     new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.discounting_max_single_iv,
                buyer_limits.get_discounting_single_iv(currency),
                new_invoice_value))
        return errors

    if not buyer_limits.within_discounting_invoice_count(
            currency,
            buyer_portfolio.invoice_count() + 1):
        errors.append(
            messages.limit_breach(
                params.discounting_max_invoice_count,
                buyer_limits.get_discounting_invoice_count(currency),
                buyer_limits.get_discounting_invoice_count(currency) + 1))
        return errors

    # check supplier limits second
    supplier_open_invoices = grabber.retrieve_invoices(
        supplier_id=supplier_id, status=params.open_status)
    supplier_portfolio = Portfolio(supplier_open_invoices)
    supplier_limits = ClientLimits(supplier_id, date)
    if not supplier_limits.within_invoicing_giv(
            currency,
            supplier_portfolio.current_giv() + new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.invoicing_max_giv,
                supplier_limits.get_invoicing_giv(currency),
                supplier_portfolio.current_giv() + new_invoice_value))
        return errors

    if not supplier_limits.within_invoicing_single_iv(currency,
                                                      new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.invoicing_max_giv,
                supplier_limits.get_invoicing_single_iv(currency),
                new_invoice_value))
        return errors

    if not supplier_limits.within_invoicing_invoice_count(
            currency,
            supplier_portfolio.invoice_count() + 1):
        errors.append(
            messages.limit_breach(
                params.invoicing_max_invoice_count,
                supplier_limits.get_invoicing_invoice_count(currency),
                supplier_limits.get_invoicing_invoice_count(currency) + 1))
        return errors

    # check relation limits last
    relation_open_invoices = grabber.retrieve_invoices(
        buyer_id=buyer_id, supplier_id=supplier_id, status=params.open_status)
    relation_portfolio = Portfolio(relation_open_invoices)
    relation_limits = RelationLimits(buyer_id, supplier_id, date)
    if not relation_limits.within_relation_giv(
            currency,
            relation_portfolio.current_giv() + new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.relation_max_giv,
                relation_limits.get_relation_giv(currency),
                relation_portfolio.current_giv() + new_invoice_value))
        return errors

    if not relation_limits.within_relation_single_iv(currency,
                                                     new_invoice_value):
        errors.append(
            messages.limit_breach(
                params.relation_max_single_iv,
                relation_limits.get_relation_single_iv(currency),
                new_invoice_value))
        return errors

    if not relation_limits.within_relation_invoice_count(
            currency,
            relation_portfolio.invoice_count() + 1):
        errors.append(
            messages.limit_breach(
                params.relation_max_invoice_count,
                relation_limits.get_relation_invoice_count(currency),
                relation_limits.get_relation_invoice_count(currency) + 1))
        return errors

    client_names = info_queries.name_from_clientid([buyer_id, supplier_id])
    financing_products = info_queries.financing_product_from_id(
        financing_product_id)
    data = dict()
    data[params.invoice_ref_id] = handyman.mint_reference_id(
        date, 1, buyer_id, supplier_id)
    data[params.buyer_id] = buyer_id
    data[params.buyer_name] = client_names[buyer_id]
    data[params.supplier_id] = supplier_id
    data[params.supplier_name] = client_names[supplier_id]
    data[params.financing_product_id] = financing_product_id
    data[params.financing_product] = financing_products[financing_product_id]
    data[params.submission_timestamp] = times.current_timestamp(
    ) if timestamp is None else timestamp
    data[params.submitted_on] = date
    data[params.invoice_date] = invoice_date
    data[params.description] = description
    data[params.currency] = currency
    data[params.invoice_total] = new_invoice_value
    data[params.submitted_by] = username
    data[params.submission_overridden] = 'f'
    data[params.approved_on] = date
    data[params.approved_by] = username
    data[params.invoice_status] = params.open_status

    output = inject_queries.add_new_invoice(data)
    return output
Esempio n. 8
0
def edit_relations(request):
    if params.username in request.session and request.session[params.user_type] == params.admin:
        context = dict()
        context[params.title] = pages.title_relations
        if params.client_id in request.session:
            client_id = request.session[params.client_id]
            client_name = info_queries.name_from_clientid(clientid=client_id)[int(client_id)]
            context[params.client_name] = client_name
        if request.method == 'GET':
            if params.buyer_id in request.session and\
                            params.supplier_id in request.session and\
                            params.start_date in request.session:

                buyer_id = request.session[params.buyer_id]
                supplier_id = request.session[params.supplier_id]
                start_date = request.session[params.start_date]

                del request.session[params.buyer_id]
                del request.session[params.supplier_id]
                del request.session[params.start_date]

                relations = info_queries.get_relations(buyer_id=buyer_id,
                                                       supplier_id=supplier_id,
                                                       on_date=start_date)
                context.update(relations[0])

                all_rms = info_queries.rm_details(date=times.current_date())
                all_rms = list(all_rms.keys())
                context[params.rm_name] = all_rms

                errors = []
                if len(relations) > 1:
                    errors.append(messages.error_multiple_entries)
                if len(errors) > 0:
                    context[params.has_errors] = errors
                return render(request, pages.client_edit_relations, context)
        elif request.method == 'POST':
            info = request.POST
            buyer_id = info[params.buyer_id]
            supplier_id = info[params.supplier_id]
            buyer_name = info[params.buyer_name]
            supplier_name = info[params.supplier_name]
            buyer_fraction = float(info[params.buyer_fraction])
            supplier_fraction = float(info[params.supplier_fraction])
            buyer_aproval = info[params.buyer_approval]
            supplier_approval = info[params.supplier_approval]
            rm_name = info[params.rm_name]

            errors = []
            if buyer_fraction + supplier_fraction != 100:
                errors.append('Buyer and supplier fractions must sum up to 100')
            if len(errors) > 0:
                context[params.has_errors] = errors
            else:
                data = {params.buyer_fraction: buyer_fraction,
                        params.supplier_fraction: supplier_fraction,
                        params.buyer_approval: buyer_aproval,
                        params.supplier_approval: supplier_approval,
                        params.rm_name: rm_name}
                try:
                    inject_queries.edit_relation(buyer_id, supplier_id, data, date=times.current_date())
                except psycopg2.DatabaseError as e:
                    raise psycopg2.DatabaseError(e)
                context[params.messages] = ['Successfully updated relation details for ' +
                                            buyer_name + ' and ' + supplier_name]
            return render(request, pages.client_edit_relations, context)
    else:
        return redirect(login)