Replaced the function-based txn_form and txn_store view with the class-based TransactionFormView in the accounting application.

This commit is contained in:
2020-08-16 13:27:31 +08:00
parent 60b3dfa33c
commit 0d38cdd403
9 changed files with 70 additions and 97 deletions

View File

@ -152,6 +152,8 @@ class Transaction(DirtyFieldsMixin, models.Model):
self._records = None
self._is_balanced = None
self._has_order_hole = None
self.old_date = None
self.current_user = None
def __str__(self):
"""Returns the string representation of this accounting
@ -190,17 +192,16 @@ class Transaction(DirtyFieldsMixin, models.Model):
return True
return False
def save(self, current_user=None, old_date: datetime.date = None,
force_insert=False, force_update=False, using=None,
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
# When the date is changed, the orders of the transactions in the same
# day need to be reordered
txn_to_sort = []
if self.date != old_date:
if old_date is not None:
if self.date != self.old_date:
if self.old_date is not None:
txn_same_day = list(
Transaction.objects
.filter(Q(date=old_date), ~Q(pk=self.pk))
.filter(Q(date=self.old_date), ~Q(pk=self.pk))
.order_by("ord"))
for i in range(len(txn_same_day)):
if txn_same_day[i].ord != i + 1:
@ -215,18 +216,18 @@ class Transaction(DirtyFieldsMixin, models.Model):
# Applies the created by and updated by
if self.pk is None:
self.pk = new_pk(Transaction)
if current_user is not None:
self.created_by = current_user
if current_user is not None:
self.updated_by = current_user
if self.current_user is not None:
self.created_by = self.current_user
if self.current_user is not None:
self.updated_by = self.current_user
to_save = [x for x in self.records if x.is_dirty()]
for record in to_save:
if record.pk is None:
record.pk = new_pk(Record)
if current_user is not None:
record.created_by = current_user
if current_user is not None:
record.updated_by = current_user
if self.current_user is not None:
record.created_by = self.current_user
if self.current_user is not None:
record.updated_by = self.current_user
# Runs the update
with transaction.atomic():
super().save(force_insert=force_insert, force_update=force_update,