def queryset(self, request, queryset): paid_status = get_or_create_paid_invoice_status() if self.value() == "unpaid": return queryset.exclude(status=paid_status) if self.value() == "unpaid_overdue": return queryset.exclude(status=paid_status).filter( date_due__lt=datetime.date.today())
def queryset(self, request): query_set = super(CustomerInvoiceInline, self).queryset(request) display_paid = get_display_paid() paid_status = get_or_create_paid_invoice_status() if not display_paid: query_set = query_set.exclude(status__status=paid_status) return query_set
def queryset(self, request, queryset): paid_status = get_or_create_paid_invoice_status() # the reason this doesn't just exclude paid invoices is because then # the query result includes customers *without* invoices if self.value() == "unpaid": return queryset.filter( pk__in=Invoice.objects.values("customer").filter( status__in=InvoiceStatus.objects.exclude( status=paid_status))) if self.value() == "unpaid_overdue": return queryset.filter( pk__in=Invoice.objects.values("customer").filter( date_due__lt=datetime.date.today(), status__in=InvoiceStatus.objects.exclude( status=paid_status)))
def update_status(self): if get_auto_invoice_status(): # this method is usually called right after self.update_totals(), # so grand_total has been calculated but not yet stored in the DB. # if the calculated total has extra decimal places, it will appear # that the payment entered by the user isn't enough to mark the # invoice as paid. e.g. grand_total could be calculated to be # 10.073, which != the payment of 10.07. the solution here is we # first round the just-calculated grand_total in the same way as it # will be rounded in the DB. # rounding copied from django.db.backends.utils.format_number context = decimal.getcontext().copy() field = Invoice._meta.get_field_by_name("grand_total")[0] context.prec = field.max_digits self.grand_total = self.grand_total.quantize( decimal.Decimal(".1") ** field.decimal_places, context=context) total_payments = self.payment_set.all().aggregate( total=models.Sum("amount"))["total"] paid_status = get_or_create_paid_invoice_status() if total_payments >= self.grand_total: self.status = paid_status elif (self.status == paid_status) and \ (total_payments < self.grand_total): self.status = get_or_create_default_invoice_status()
def _get_queryset(self, q, request): paid_status = get_or_create_paid_invoice_status() qs = super(UnpaidInvoiceAjaxChannel, self)._get_queryset(q, request) return qs.exclude(status=paid_status)