Example #1
0
File: bill.py Project: rokj/sellout
def list_bills(request, company):
    c = get_object_or_404(Company, url_name=company)

    # check permissions: needs to be guest
    if not has_permission(request.user, c, 'bill', 'view'):
        return no_permission_view(request, c, _("You have no permission to view bills."))

    N = 10  # if no search was made, display last N bills (below)
    searched = False

    # use GET for everything: there's little parameters and when using POST, paginator gets
    # in the way with GET requests
    form = BillSearchForm(data=request.GET, user=request.user, company=c)

    if form.is_valid():
        # decide which way to order the results
        # this is the fake switch statement that is missing in python for no obvious reason
        ordering = {
            'serial': 'serial',
            'date': 'timestamp',
            'amount': 'total'
        }
        order = ordering.get(form.cleaned_data.get('sort_by'))
        if form.cleaned_data.get('sort_order') == 'desc':
            order = '-' + order

        bills = Bill.objects.filter(company=c).order_by(order)

        # filter by whatever is in the form:
        # issue date: from
        t = form.cleaned_data.get('issued_from')
        if t:
            bills = bills.filter(timestamp__gte=t)

        # issue date: to
        t = form.cleaned_data.get('issued_to')
        if t:
            bills = bills.filter(timestamp__lte=t)

        # item:
        t = form.cleaned_data.get('item_code')
        if t:
            ids = [i.id for i in BillItem.objects.only('bill_id').filter(code__icontains=t)]

            # find all bills that include item that contains this code
            bills = bills.filter(id__in=ids)

        # contact
        t = form.cleaned_data.get('contact')
        if t:
            bills = bills.filter(contact__first_name__icontains=t) | \
                    bills.filter(contact__last_name__icontains=t) | \
                    bills.filter(contact__company_name__icontains=t)

        # bill number
        t = form.cleaned_data.get('serial')
        if t:
            bills = bills.filter(serial__icontains=t)

        # status
        t = form.cleaned_data.get('status')
        if t:
            bills = bills.filter(payment__status=t)

        # amount: from
        t = form.cleaned_data.get('amount_from')
        if t:
            bills = bills.filter(total__gte=t)

        # amount: to
        t = form.cleaned_data.get('amount_to')
        if t:
            bills = bills.filter(total__lte=t)

        # user
        t = form.cleaned_data.get('user_name')
        if t:
            bills = bills.filter(user_name__icontains=t)

        page = form.cleaned_data.get('page')
        searched = True
    else:
        form = BillSearchForm(data=None, user=request.user, company=c)
        page = 1

        bills = Bill.objects.filter(company=c).order_by('-timestamp')

    # format all bills manually
    bills = [bill_to_dict(request.user, c, b) for b in bills]

    paginator = Paginator(bills, g.MISC['bills_per_page'])
    if page:
        bills = paginator.page(page)
    else:
        bills = paginator.page(1)

    context = {
        'company': c,

        'bills': bills,
        'searched': searched,

        'filter_form': form,

        'title': _("Bills"),
        'site_title': g.MISC['site_title'],
        'date_format_django': get_date_format(request.user, c, 'django'),
        'date_format_js': get_date_format(request.user, c, 'js'),
        'currency': get_company_value(request.user, c, 'pos_currency'),
    }

    return render(request, 'pos/manage/bills.html', context)
Example #2
0
File: bill.py Project: rokj/sellout
def list_bills(request, company_id):
    try:
        c = Company.objects.get(id=company_id)
    except Company.DoesNotExist:
        return JsonError(_("Company does not exist"))

    # check permissions: needs to be guest
    if not has_permission(request.user, c, 'bill', 'view'):
        return JsonError(_("You have no permission to view bills."))

    N = 10  # if no search was made, display last N bills (below)
    searched = False

    data = JsonParse(request.POST['data'])

    if data.get('search'):
        # decide which way to order the results
        # this is the fake switch statement that is missing in python for no obvious reason
        ordering = {
            'id': 'serial',
            'date': 'timestamp',
            'amount': 'total'
        }
        order = ordering.get(data.get('sort_by'))
        if data.get('sort_order') == 'desc':
            order = '-' + order

        bills = Bill.objects.filter(company=c, payment__status=PAID).order_by(order)

        # filter by whatever is in the form:
        # issue date: from
        issued_from = data.get('issued_from')
        if issued_from:
            t = dtm.date(year=issued_from[0], month=issued_from[1],
                                 day=issued_from[2])
            bills = bills.filter(timestamp__gte=t)

        # issue date: to
        issued_to = data.get('issued_to')
        if issued_to:
            t = dtm.date(year=issued_to[0], month=issued_to[1],
                                 day=issued_to[2])
            bills = bills.filter(timestamp__lte=t)

        # item:
        t = data.get('item_code')
        if t:
            ids = [i.id for i in BillItem.objects.only('bill_id').filter(code__icontains=t)]

            # find all bills that include item that contains this code
            bills = bills.filter(id__in=ids)

        # contact
        t = data.get('contact')
        if t:
            bills = bills.filter(contact__first_name__icontains=t) | \
                    bills.filter(contact__last_name__icontains=t) | \
                    bills.filter(contact__company_name__icontains=t)


        # bill number
        t = data.get('id')
        if t:
            bills = bills.filter(serial=t)

        # amount: from
        t = data.get('amount_from')
        if t:
            bills = bills.filter(total__gte=t)

        # amount: to
        t = data.get('amount_to')
        if t:
            bills = bills.filter(total__lte=t)

        # user
        t = data.get('user_name')
        if t:
            bills = bills.filter(user_name__icontains=t)

        page = data.get('page')
        searched = True
    else:
        bills = Bill.objects.filter(company=c, payment__status=PAID).order_by('-timestamp')[:N]

    # format all bills manually
    bills = [bill_to_dict(request.user, c, b) for b in bills]

    return JsonOk(extra=bills)