Always compute state

This commit is contained in:
Marc Aymerich 2015-07-14 10:19:45 +00:00
parent 259ed388b4
commit 74f72ed8a1
3 changed files with 15 additions and 22 deletions

View File

@ -108,8 +108,7 @@ class Bill(models.Model):
is_sent = models.BooleanField(_("sent"), default=False) is_sent = models.BooleanField(_("sent"), default=False)
due_on = models.DateField(_("due on"), null=True, blank=True) due_on = models.DateField(_("due on"), null=True, blank=True)
updated_on = models.DateField(_("updated on"), auto_now=True) updated_on = models.DateField(_("updated on"), auto_now=True)
# TODO allways compute total or what? # total = models.DecimalField(max_digits=12, decimal_places=2, null=True)
total = models.DecimalField(max_digits=12, decimal_places=2, null=True)
comments = models.TextField(_("comments"), blank=True) comments = models.TextField(_("comments"), blank=True)
html = models.TextField(_("HTML"), blank=True) html = models.TextField(_("HTML"), blank=True)
@ -125,6 +124,10 @@ class Bill(models.Model):
def get_class_type(cls): def get_class_type(cls):
return cls.__name__.upper() return cls.__name__.upper()
@cached_property
def total(self):
return self.compute_total()
@cached_property @cached_property
def seller(self): def seller(self):
return Account.get_main().billcontact return Account.get_main().billcontact
@ -253,10 +256,10 @@ class Bill(models.Model):
payment = self.account.paymentsources.get_default() payment = self.account.paymentsources.get_default()
if not self.due_on: if not self.due_on:
self.due_on = self.get_due_date(payment=payment) self.due_on = self.get_due_date(payment=payment)
self.total = self.compute_total() total = self.compute_total()
transaction = None transaction = None
if self.get_type() != self.PROFORMA: if self.get_type() != self.PROFORMA:
transaction = self.transactions.create(bill=self, source=payment, amount=self.total) transaction = self.transactions.create(bill=self, source=payment, amount=total)
self.closed_on = timezone.now() self.closed_on = timezone.now()
self.is_open = False self.is_open = False
self.is_sent = False self.is_sent = False
@ -442,12 +445,6 @@ class BillLine(models.Model):
total += self.sublines.aggregate(sub_total=Sum('total'))['sub_total'] or 0 total += self.sublines.aggregate(sub_total=Sum('total'))['sub_total'] or 0
return round(total, 2) return round(total, 2)
# def save(self, *args, **kwargs):
# super(BillLine, self).save(*args, **kwargs)
# if self.bill.is_open:
# self.bill.total = self.bill.get_total()
# self.bill.save(update_fields=['total'])
class BillSubline(models.Model): class BillSubline(models.Model):
""" Subline used for describing an item discount """ """ Subline used for describing an item discount """
@ -465,10 +462,3 @@ class BillSubline(models.Model):
description = models.CharField(_("description"), max_length=256) description = models.CharField(_("description"), max_length=256)
total = models.DecimalField(max_digits=12, decimal_places=2) total = models.DecimalField(max_digits=12, decimal_places=2)
type = models.CharField(_("type"), max_length=16, choices=TYPES, default=OTHER) type = models.CharField(_("type"), max_length=16, choices=TYPES, default=OTHER)
# def save(self, *args, **kwargs):
# # TODO cost of this shit
# super(BillSubline, self).save(*args, **kwargs)
# if self.line.bill.is_open:
# self.line.bill.total = self.line.bill.get_total()
# self.line.bill.save(update_fields=['total'])

View File

@ -176,10 +176,10 @@ commit.verbose_name = _("Commit")
def delete_selected(modeladmin, request, queryset): def delete_selected(modeladmin, request, queryset):
""" Has to have same name as admin.actions.delete_selected """ """ Has to have same name as admin.actions.delete_selected """
related_transactions = helpers.pre_delete_processes(modelamdin, request, queryset) related_transactions = helpers.pre_delete_processes(modeladmin, request, queryset)
response = actions.delete_selected(modeladmin, request, queryset) response = actions.delete_selected(modeladmin, request, queryset)
if response is None: if response is None:
helpers.post_delete_processes(modelamdin, request, related_transactions) helpers.post_delete_processes(modeladmin, request, related_transactions)
return response return response
delete_selected.short_description = actions.delete_selected.short_description delete_selected.short_description = actions.delete_selected.short_description

View File

@ -94,8 +94,10 @@ class TransactionAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
actions = change_view_actions + (actions.report,) actions = change_view_actions + (actions.report,)
filter_by_account_fields = ('bill', 'source') filter_by_account_fields = ('bill', 'source')
change_readonly_fields = ('amount', 'currency') change_readonly_fields = ('amount', 'currency')
readonly_fields = ('bill_link', 'display_state', 'process_link', 'account_link', 'source_link') readonly_fields = (
list_select_related = ('source', 'bill__account') 'bill_link', 'display_state', 'process_link', 'account_link', 'source_link'
)
list_select_related = ('source', 'bill__account', 'process')
date_hierarchy = 'created_at' date_hierarchy = 'created_at'
bill_link = admin_link('bill') bill_link = admin_link('bill')
@ -174,7 +176,8 @@ class TransactionProcessAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
def delete_view(self, request, object_id, extra_context=None): def delete_view(self, request, object_id, extra_context=None):
queryset = self.model.objects.filter(id=object_id) queryset = self.model.objects.filter(id=object_id)
related_transactions = helpers.pre_delete_processes(self, request, queryset) related_transactions = helpers.pre_delete_processes(self, request, queryset)
response = super(TransactionProcessAdmin, self).delete_view(request, object_id, extra_context) response = super(TransactionProcessAdmin, self).delete_view(
request, object_id, extra_context)
if isinstance(response, HttpResponseRedirect): if isinstance(response, HttpResponseRedirect):
helpers.post_delete_processes(self, request, related_transactions) helpers.post_delete_processes(self, request, related_transactions)
return response return response