Replaced the function-based txn_delete() view with the class-based TransactionDeleteView view in the accounting application.

This commit is contained in:
2020-08-09 13:19:20 +08:00
parent b84c9306cb
commit 836151d9d5
3 changed files with 39 additions and 33 deletions

View File

@ -20,7 +20,8 @@
"""
from dirtyfields import DirtyFieldsMixin
from django.conf import settings
from django.db import models
from django.db import models, transaction
from django.db.models import Q
from django.urls import reverse
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
@ -180,6 +181,24 @@ class Transaction(DirtyFieldsMixin, models.Model):
return True
return False
def delete(self, using=None, keep_parents=False):
txn_same_day = list(
Transaction.objects
.filter(Q(date=self.date), ~Q(pk=self.pk))
.order_by("ord"))
txn_to_sort = []
for i in range(len(txn_same_day)):
txn_same_day[i].ord = i + 1
if txn_same_day[i].is_dirty():
txn_to_sort.append(txn_same_day[i])
with transaction.atomic():
for record in self.record_set.all():
record.delete()
super(Transaction, self).delete(
using=using, keep_parents=keep_parents)
for txn in txn_to_sort:
txn.save()
class Meta:
db_table = "accounting_transactions"