Ejemplo n.º 1
0
def list_totals(context):
    cl = context.get("cl")
    if hasattr(cl.model_admin, "list_totals"):
        query_string = cl.get_query_string()
        multi_page = cl.multi_page
        if cl.show_all:
            multi_page = False
        query_set = cl.query_set
        totals = []
        kwargs = {}
        # set up arguments for aggregate()
        for total in cl.model_admin.list_totals:
            if isinstance(total, tuple):
                field = total[0]
                title = total[1]
            else:
                field = total
                title = cl.model_admin.opts.get_field(field).verbose_name
                title = capfirst(title)
            totals.append({"field": field, "title": title})
            kwargs.update({field: Sum(field)})
        # run the query
        results = query_set.aggregate(**kwargs)
        # get results ready for template
        for total in totals:
            # the "or 0" is because the result is be None when the cl is empty
            result = add_currency_symbol(results[total["field"]] or 0)
            total.update({"total": result})
        return {"totals": totals, "multi_page": multi_page}
Ejemplo n.º 2
0
 def tax_report(self, request):
     start_field = "start_date"
     end_field = "end_date"
     form = DateRangeForm(request.GET, start_field=start_field,
                          end_field=end_field)
     # imitate the behavior of the admin site for invalid lookup params
     if not form.is_valid():
         return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')
     start_date = form.cleaned_data.get(start_field)
     end_date = form.cleaned_data.get(end_field)
     invoice_filter = {}
     invoiceentry_filter = {}
     show_clear_link = False
     if start_date or end_date:
         show_clear_link = True
     if start_date:
         invoice_filter.update({"date_created__gte": start_date})
         invoiceentry_filter.update(
             {"invoice__date_created__gte": start_date})
     if end_date:
         invoice_filter.update({"date_created__lte": end_date})
         invoiceentry_filter.update(
             {"invoice__date_created__lte": end_date})
     # the 'or 0's below are because aggregate() can return None
     tax = Invoice.objects.filter(**invoice_filter).aggregate(
         tax=Sum("total_tax"))["tax"] or 0
     taxed_sales = InvoiceEntry.objects.filter(
         is_taxable=True, **invoiceentry_filter).aggregate(
         sales=Sum("total"))["sales"] or 0
     total = tax + taxed_sales
     context = {"title": "Tax", "tax": add_currency_symbol(tax),
                "taxed_sales": add_currency_symbol(taxed_sales),
                "total": add_currency_symbol(total), "form": form,
                "show_clear_link": show_clear_link}
     context.update(context_helper(start_field, end_field))
     return TemplateResponse(request, "admin/reports/tax.html", context,
                             current_app=self.name)
Ejemplo n.º 3
0
 def __unicode__(self):
     return u"%s %s payment for %s" % (add_currency_symbol(self.amount),
                                       self.payment_type, self.invoice)
Ejemplo n.º 4
0
 def __unicode__(self):
     return u"Deposit of %s on %s" % (add_currency_symbol(self.total),
                                      self.date)
Ejemplo n.º 5
0
 def value_to_string(self, obj):
     value = self.value_from_object(obj)
     # fixme: don't format decimal places here (issue #165)
     sub = "%" + "0.%sf" % get_currency_decimal_places()
     value = sub % value
     return add_currency_symbol(value)
Ejemplo n.º 6
0
 def render(self, name, value, attrs=None):
     final_attrs = self._get_final_attrs(name, value, attrs)
     return mark_safe(add_currency_symbol(u" <input%s /> " % flatatt(final_attrs)))