Merge branch 'aureplop/kfet_initial-statement' into 'master'
kfet -- Create initial statement on checkout save See merge request cof-geek/gestioCOF!283
This commit is contained in:
commit
c94a9ecb44
4 changed files with 65 additions and 17 deletions
|
@ -371,6 +371,22 @@ class Checkout(models.Model):
|
|||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
created = self.pk is None
|
||||
|
||||
ret = super().save(*args, **kwargs)
|
||||
|
||||
if created:
|
||||
self.statements.create(
|
||||
amount_taken=0,
|
||||
balance_old=self.balance,
|
||||
balance_new=self.balance,
|
||||
by=self.created_by,
|
||||
)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
class CheckoutTransfer(models.Model):
|
||||
from_checkout = models.ForeignKey(
|
||||
Checkout, on_delete = models.PROTECT,
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
import datetime
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from kfet.models import Account
|
||||
from kfet.models import Account, Checkout
|
||||
|
||||
from .utils import create_user
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
@ -23,3 +28,33 @@ class AccountTests(TestCase):
|
|||
|
||||
with self.assertRaises(Account.DoesNotExist):
|
||||
Account.objects.get_by_password('bernard')
|
||||
|
||||
|
||||
class CheckoutTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.now = timezone.now()
|
||||
|
||||
self.u = create_user()
|
||||
self.u_acc = self.u.profile.account_kfet
|
||||
|
||||
self.c = Checkout(
|
||||
created_by=self.u_acc,
|
||||
valid_from=self.now,
|
||||
valid_to=self.now + datetime.timedelta(days=1),
|
||||
)
|
||||
|
||||
def test_initial_statement(self):
|
||||
"""A statement is added with initial balance on creation."""
|
||||
self.c.balance = 10
|
||||
self.c.save()
|
||||
|
||||
st = self.c.statements.get()
|
||||
self.assertEqual(st.balance_new, 10)
|
||||
self.assertEqual(st.amount_taken, 0)
|
||||
self.assertEqual(st.amount_error, 0)
|
||||
|
||||
# Saving again doesn't create a new statement.
|
||||
self.c.save()
|
||||
|
||||
self.assertEqual(self.c.statements.count(), 1)
|
||||
|
|
|
@ -746,12 +746,16 @@ class CheckoutReadViewTests(ViewTestCaseMixin, TestCase):
|
|||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.checkout = Checkout.objects.create(
|
||||
name='Checkout',
|
||||
created_by=self.accounts['team'],
|
||||
valid_from=self.now,
|
||||
valid_to=self.now + timedelta(days=5),
|
||||
)
|
||||
|
||||
with mock.patch('django.utils.timezone.now') as mock_now:
|
||||
mock_now.return_value = self.now
|
||||
|
||||
self.checkout = Checkout.objects.create(
|
||||
name='Checkout', balance=Decimal('10'),
|
||||
created_by=self.accounts['team'],
|
||||
valid_from=self.now,
|
||||
valid_to=self.now + timedelta(days=1),
|
||||
)
|
||||
|
||||
def test_ok(self):
|
||||
r = self.client.get(self.url)
|
||||
|
@ -794,7 +798,7 @@ class CheckoutUpdateViewTests(ViewTestCaseMixin, TestCase):
|
|||
name='Checkout',
|
||||
valid_from=self.now,
|
||||
valid_to=self.now + timedelta(days=5),
|
||||
balance='3.14',
|
||||
balance=Decimal('3.14'),
|
||||
is_protected=False,
|
||||
created_by=self.accounts['team'],
|
||||
)
|
||||
|
@ -864,6 +868,7 @@ class CheckoutStatementListViewTests(ViewTestCaseMixin, TestCase):
|
|||
self.assertQuerysetEqual(
|
||||
r.context['checkoutstatements'],
|
||||
map(repr, expected_statements),
|
||||
ordered=False,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -528,15 +528,7 @@ class CheckoutCreate(SuccessMessageMixin, CreateView):
|
|||
|
||||
# Creating
|
||||
form.instance.created_by = self.request.user.profile.account_kfet
|
||||
checkout = form.save()
|
||||
|
||||
# Création d'un relevé avec balance initiale
|
||||
CheckoutStatement.objects.create(
|
||||
checkout = checkout,
|
||||
by = self.request.user.profile.account_kfet,
|
||||
balance_old = checkout.balance,
|
||||
balance_new = checkout.balance,
|
||||
amount_taken = 0)
|
||||
form.save()
|
||||
|
||||
return super(CheckoutCreate, self).form_valid(form)
|
||||
|
||||
|
|
Loading…
Reference in a new issue