Beispiel #1
0
def open_invoices(request):
    '''
    Renders a web page containing details of all open invoices
    :param request: Http request
    :return: Http response
    '''
    if params.username in request.session and request.session[
            params.user_type] == params.client:
        if request.method == 'GET':
            context = dict()
            context[params.username] = request.session[params.username]
            context[params.title] = pages.title_open_invoices
            context[params.page_type] = request.session[params.page_type]
            client_id = grabber.clientid_from_username(
                request.session[params.username])
            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,
                                                     as_dict=True)
                else:
                    data = grabber.retrieve_invoices(supplier_id=client_id,
                                                     status=params.open_status,
                                                     as_dict=True)
                data = sorted(data, key=lambda k: k[params.submitted_on])
                context[params.data] = data
                return render(request, pages.open_invoices, context)
            else:
                return render(request, pages.invalid_client_type, context)
    else:
        return redirect(login)
Beispiel #2
0
def save_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 == 'POST':
            if params.client_id in request.session:
                client_id = request.session[params.client_id]

                info = request.POST
                paid_on = info[params.repayment_date]
                amount = float(info[params.repaid_amount])
                fees = float(info[params.repayment_transaction_cost])
                payment_type = info[params.repayment_payment_type]
                notes = info[params.repayment_notes]

                if request.session[params.page_type] in request.session[params.client_type]:
                    if request.session[params.page_type] == params.buyer:
                        invoices = grabber.retrieve_invoices(buyer_id=client_id,
                                                             status=params.open_status)
                    else:
                        invoices = grabber.retrieve_invoices(supplier_id=client_id,
                                                             status=params.open_status)
                    portfolio = Portfolio(invoices)
                    payment = InvoicePayment(portfolio=portfolio,
                                             amount=amount,
                                             fees=fees,
                                             payment_date=paid_on,
                                             payment_method=payment_type,
                                             updated_by=request.session[params.username],
                                             paid_by=client_id,
                                             notes=notes)
                    try:
                        payment.receive()
                        context[params.messages] = [messages.msg_payment_booked]
                        return render(request, pages.client_repayment, context)
                    except TypeError as e:
                        raise TypeError(e) from e
                    except ValueError as e:
                        context[params.has_errors] = [e]
                        return render(request, pages.client_open_invoices, context)
                    except psycopg2.DatabaseError as e:
                        raise Exception(e) from e
                        context[params.has_errors] = [messages.error_book_repayment]
                        return render(request, pages.client_open_invoices, context)
                    except Exception as e:
                        raise Exception(e)
    else:
        return redirect(login)
Beispiel #3
0
def history(request):
    '''
    Gets a web page with all closed invoices
    :param request: Http request
    :return: Http response
    '''
    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_history
        if request.method == 'GET':
            # set the number of rows to fetch
            if params.previous_page in request.session and request.session[params.previous_page] == history:
                if params.row_multiplier in request.session:
                    current_multiplier = request.session[params.row_multiplier]
            else:
                request.session[params.previous_page] = history
                current_multiplier = 1
            std_offset = (std_limit * current_multiplier) - std_limit

            data = grabber.retrieve_invoices(status=params.closed_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.admin_history, context)
    else:
        return redirect(login)
Beispiel #4
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)
Beispiel #5
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