Applied the base model from the Mia core application to the models in the accounting application.
This commit is contained in:
parent
216979dcf4
commit
c72d550cd4
@ -21,17 +21,15 @@
|
|||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from dirtyfields import DirtyFieldsMixin
|
from dirtyfields import DirtyFieldsMixin
|
||||||
from django.conf import settings
|
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.db.models import Q, Max
|
from django.db.models import Q, Max
|
||||||
|
|
||||||
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr, \
|
from mia_core.models import BaseModel
|
||||||
new_pk
|
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
|
||||||
|
|
||||||
|
|
||||||
class Account(DirtyFieldsMixin, models.Model):
|
class Account(DirtyFieldsMixin, BaseModel):
|
||||||
"""An account."""
|
"""An account."""
|
||||||
id = models.PositiveIntegerField(primary_key=True)
|
|
||||||
parent = models.ForeignKey(
|
parent = models.ForeignKey(
|
||||||
"self", on_delete=models.PROTECT, null=True,
|
"self", on_delete=models.PROTECT, null=True,
|
||||||
related_name="child_set")
|
related_name="child_set")
|
||||||
@ -39,14 +37,6 @@ class Account(DirtyFieldsMixin, models.Model):
|
|||||||
title_zh_hant = models.CharField(max_length=32)
|
title_zh_hant = models.CharField(max_length=32)
|
||||||
title_en = models.CharField(max_length=128, null=True)
|
title_en = models.CharField(max_length=128, null=True)
|
||||||
title_zh_hans = models.CharField(max_length=32, null=True)
|
title_zh_hans = models.CharField(max_length=32, null=True)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
|
||||||
created_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="created_accounting_accounts")
|
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
|
||||||
updated_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="updated_accounting_accounts")
|
|
||||||
CASH = "1111"
|
CASH = "1111"
|
||||||
ACCUMULATED_BALANCE = "3351"
|
ACCUMULATED_BALANCE = "3351"
|
||||||
NET_CHANGE = "3353"
|
NET_CHANGE = "3353"
|
||||||
@ -61,7 +51,6 @@ class Account(DirtyFieldsMixin, models.Model):
|
|||||||
self.is_for_credit = None
|
self.is_for_credit = None
|
||||||
self._is_in_use = None
|
self._is_in_use = None
|
||||||
self._is_parent_and_in_use = None
|
self._is_parent_and_in_use = None
|
||||||
self.current_user = None
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Returns the string representation of this account."""
|
"""Returns the string representation of this account."""
|
||||||
@ -71,15 +60,8 @@ class Account(DirtyFieldsMixin, models.Model):
|
|||||||
update_fields=None):
|
update_fields=None):
|
||||||
self.parent = None if len(self.code) == 1\
|
self.parent = None if len(self.code) == 1\
|
||||||
else Account.objects.get(code=self.code[:-1])
|
else Account.objects.get(code=self.code[:-1])
|
||||||
if self.pk is None:
|
super().save(force_insert=force_insert, force_update=force_update,
|
||||||
self.pk = new_pk(Account)
|
using=using, update_fields=update_fields)
|
||||||
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
|
|
||||||
with transaction.atomic():
|
|
||||||
super().save(force_insert=force_insert, force_update=force_update,
|
|
||||||
using=using, update_fields=update_fields)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "accounting_accounts"
|
db_table = "accounting_accounts"
|
||||||
@ -126,20 +108,11 @@ class Account(DirtyFieldsMixin, models.Model):
|
|||||||
self._is_in_use = value
|
self._is_in_use = value
|
||||||
|
|
||||||
|
|
||||||
class Transaction(DirtyFieldsMixin, models.Model):
|
class Transaction(DirtyFieldsMixin, BaseModel):
|
||||||
"""An accounting transaction."""
|
"""An accounting transaction."""
|
||||||
id = models.PositiveIntegerField(primary_key=True)
|
|
||||||
date = models.DateField()
|
date = models.DateField()
|
||||||
ord = models.PositiveSmallIntegerField(default=1)
|
ord = models.PositiveSmallIntegerField(default=1)
|
||||||
notes = models.CharField(max_length=128, null=True, blank=True)
|
notes = models.CharField(max_length=128, null=True, blank=True)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
|
||||||
created_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="created_accounting_transactions")
|
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
|
||||||
updated_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="updated_accounting_transactions")
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@ -195,22 +168,10 @@ class Transaction(DirtyFieldsMixin, models.Model):
|
|||||||
# Collects the records to be deleted
|
# Collects the records to be deleted
|
||||||
to_keep = [x.pk for x in self.records if x.pk is not None]
|
to_keep = [x.pk for x in self.records if x.pk is not None]
|
||||||
to_delete = [x for x in self.record_set.all() if x.pk not in to_keep]
|
to_delete = [x for x in self.record_set.all() if x.pk not in to_keep]
|
||||||
# Applies the created by and updated by
|
|
||||||
if self.pk is None:
|
|
||||||
self.pk = new_pk(Transaction)
|
|
||||||
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
|
to_save = [x for x in self.records
|
||||||
if x.is_dirty(check_relationship=True)]
|
if x.is_dirty(check_relationship=True)]
|
||||||
for record in to_save:
|
for record in to_save:
|
||||||
if record.pk is None:
|
record.current_user = self.current_user
|
||||||
record.pk = new_pk(Record)
|
|
||||||
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
|
# Runs the update
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
super().save(force_insert=force_insert, force_update=force_update,
|
super().save(force_insert=force_insert, force_update=force_update,
|
||||||
@ -373,9 +334,8 @@ class Transaction(DirtyFieldsMixin, models.Model):
|
|||||||
return "transfer"
|
return "transfer"
|
||||||
|
|
||||||
|
|
||||||
class Record(DirtyFieldsMixin, models.Model):
|
class Record(DirtyFieldsMixin, BaseModel):
|
||||||
"""An accounting record."""
|
"""An accounting record."""
|
||||||
id = models.PositiveIntegerField(primary_key=True)
|
|
||||||
transaction = models.ForeignKey(
|
transaction = models.ForeignKey(
|
||||||
Transaction, on_delete=models.CASCADE)
|
Transaction, on_delete=models.CASCADE)
|
||||||
is_credit = models.BooleanField()
|
is_credit = models.BooleanField()
|
||||||
@ -384,14 +344,6 @@ class Record(DirtyFieldsMixin, models.Model):
|
|||||||
Account, on_delete=models.PROTECT)
|
Account, on_delete=models.PROTECT)
|
||||||
summary = models.CharField(max_length=128, blank=True, null=True)
|
summary = models.CharField(max_length=128, blank=True, null=True)
|
||||||
amount = models.PositiveIntegerField()
|
amount = models.PositiveIntegerField()
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
|
||||||
created_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="created_accounting_records")
|
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
|
||||||
updated_by = models.ForeignKey(
|
|
||||||
settings.AUTH_USER_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name="updated_accounting_records")
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user