Exemplo n.º 1
0
def client_relations(username, client_type, date=None):
    '''
    Gets the relations that a client has with counterparts
    :param username: username of the user
    :param client_type: the type of the client (buyer or supplier)
    :param date: date to check for
    :return: list --> list[id, client_name]
    '''
    if date is None:
        date = datetime.datetime.now().date()
        date = date.strftime('%Y%m%d')
    try:
        if client_type == params.buyer:
            attributes = [params.supplier_id, params.supplier_name]
            results = info_queries.get_relations(buyer_user=username,
                                                 on_date=date)
        else:
            attributes = [params.buyer_id, params.buyer_name]
            results = info_queries.get_relations(supplier_user=username,
                                                 on_date=date)
        relations = []
        for item in results:
            relations.append([item[attributes[0]], item[attributes[1]]])
        return relations
    except psycopg2.DatabaseError as e:
        err = 'Could not retrieve relations from database'
        raise psycopg2.DatabaseError(err)
Exemplo n.º 2
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)
Exemplo n.º 3
0
def retrieve_invoices(buyer_id=None,
                      supplier_id=None,
                      status=None,
                      limit_rows=0,
                      offset=0,
                      as_dict=False):
    '''
    Gets details of invoices from the database
    :param buyer_id: buyer's client id
    :param supplier_id: supplier's client id
    :param status: status of the invoices
    :param limit_rows: the number of invoices that should be retrieved
    :param offset: the number of invoices that should be skipped
    :param as_dict: specifies if invoice details are wanted in dictionary format
    :return: list of Invoice objects if as_dict=False;
            else list of dictionaries with details of each invoice
    '''
    invoices = info_queries.get_invoice(buyer_id=buyer_id,
                                        supplier_id=supplier_id,
                                        status=status,
                                        limit_rows=limit_rows,
                                        offset=offset)
    if len(list(invoices.keys())) > 0:
        financing = info_queries.get_forwarded_payments(list(invoices.keys()))
        repayments = info_queries.get_received_payments(list(invoices.keys()))
    else:
        financing = []
        repayments = []
    current_date = times.current_date()
    rates = info_queries.get_rates(current_date)
    client_ids = handyman.combine_items([buyer_id, supplier_id])
    overrides = info_queries.get_overridden_rates(None if len(client_ids) ==
                                                  0 else client_ids)
    fee_fractions = info_queries.get_relations(buyer_id=buyer_id,
                                               supplier_id=supplier_id)

    holder = []
    for id_ in invoices:
        fin_info = None
        rep_info = None
        if id_ in financing.keys():
            fin_info = financing[id_]
        if id_ in repayments.keys():
            rep_info = repayments[id_]
        invoice = Invoice(invoices[id_], fin_info, rep_info, rates)
        invoice.with_overridden_rates(overrides)
        invoice.with_fee_split(fee_fractions)
        invoice.add_dues(current_date)
        holder.append(invoice)
    if as_dict:
        store = []
        for invoice in holder:
            store.append(invoice.get_details())
        return store
    return holder
Exemplo n.º 4
0
 def with_fee_split(self, info=None):
     '''
     Sets the share of the fee borne by the buyer and the supplier
     :param info: fee split details; default None
     '''
     if info is None:
         info = info_queries.get_relations(buyer_id=self.buyer_id(),
                                           supplier_id=self.supplier_id(),
                                           on_date=self.submitted_on())
     found = False
     if len(info) > 0:
         for relation in info:
             if relation[params.buyer_id] == self.buyer_id():
                 if relation[params.supplier_id] == self.supplier_id():
                     self.buyer_fraction = relation[params.buyer_fraction]
                     self.supplier_fraction = relation[
                         params.supplier_fraction]
                     found = True
     if not found:
         err = 'No relation found on {0} between buyer (id: {1}) and supplier (id: {2})'\
             .format(str(self.submitted_on()),
                     str(self.buyer_id()),
                     str(self.supplier_id()))
         raise LookupError(err)
Exemplo n.º 5
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)