From 996baa944c6fc55f75eafeee0e44841600692a6e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?=
Date: Thu, 11 Aug 2016 15:14:23 +0200
Subject: [PATCH] =?UTF-8?q?Ajout=20des=20relev=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
kfet/forms.py | 8 +++-
kfet/models.py | 17 +++++++
kfet/templates/kfet/account_read.html | 2 +-
kfet/templates/kfet/checkout.html | 2 +-
kfet/templates/kfet/checkout_read.html | 20 ++++++++
kfet/templates/kfet/checkoutstatement.html | 13 +++++
.../kfet/checkoutstatement_create.html | 15 ++++++
kfet/urls.py | 43 ++++++++++-------
kfet/views.py | 47 ++++++++++++++++++-
9 files changed, 147 insertions(+), 20 deletions(-)
create mode 100644 kfet/templates/kfet/checkoutstatement.html
create mode 100644 kfet/templates/kfet/checkoutstatement_create.html
diff --git a/kfet/forms.py b/kfet/forms.py
index 50873987..07b08aae 100644
--- a/kfet/forms.py
+++ b/kfet/forms.py
@@ -2,7 +2,8 @@ from django import forms
from django.core.exceptions import ValidationError
from django.contrib.auth.models import User
from django.forms import modelformset_factory
-from kfet.models import Account, Checkout, Article, OperationGroup, Operation
+from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
+ CheckoutStatement)
from gestioncof.models import CofProfile
# -----
@@ -97,6 +98,11 @@ class CheckoutRestrictForm(CheckoutForm):
class Meta(CheckoutForm.Meta):
fields = ['name', 'valid_from', 'valid_to']
+class CheckoutStatementForm(forms.ModelForm):
+ class Meta:
+ model = CheckoutStatement
+ fields = ['balance_new', 'amount_taken']
+
# -----
# Article forms
# -----
diff --git a/kfet/models.py b/kfet/models.py
index a4c7d1d4..3b0fc06a 100644
--- a/kfet/models.py
+++ b/kfet/models.py
@@ -6,6 +6,7 @@ from django.contrib.auth.models import User
from gestioncof.models import CofProfile
from django.utils.six.moves import reduce
from django.utils import timezone
+from django.db import transaction
from datetime import date, timedelta
import re
@@ -260,6 +261,22 @@ class CheckoutStatement(models.Model):
amount_error = models.DecimalField(max_digits = 6, decimal_places = 2)
at = models.DateTimeField(auto_now_add = True)
+ def __str__(self):
+ return '%s %s' % (self.checkout, self.at)
+
+ def save(self, *args, **kwargs):
+ if not self.pk:
+ checkout_id = self.checkout_id
+ self.balance_old = (Checkout.objects
+ .values_list('balance', flat=True).get(pk=checkout_id))
+ self.amount_error = (
+ self.balance_new + self.amount_taken - self.balance_old)
+ with transaction.atomic():
+ Checkout.objects.filter(pk=checkout_id).update(balance=self.balance_new)
+ super(CheckoutStatement, self).save(*args, **kwargs)
+ else:
+ super(CheckoutStatement, self).save(*args, **kwargs)
+
class ArticleCategory(models.Model):
name = models.CharField(max_length = 45)
diff --git a/kfet/templates/kfet/account_read.html b/kfet/templates/kfet/account_read.html
index 3a81a5ec..e30eb04f 100644
--- a/kfet/templates/kfet/account_read.html
+++ b/kfet/templates/kfet/account_read.html
@@ -27,7 +27,7 @@
Statut COF: {{ account.is_cof }}
Compte gelé: {{ account.is_frozen }}
Solde: {{ account.balance }} €
-{% if account.balance != account.real_balance %}
+{% if account.negative.balance_offset %}
Solde réel: {{ account.real_balance }} €
{% endif %}
{% if account.negative.authorized_overdraft %}
diff --git a/kfet/templates/kfet/checkout.html b/kfet/templates/kfet/checkout.html
index f68bc393..f4fb6dae 100644
--- a/kfet/templates/kfet/checkout.html
+++ b/kfet/templates/kfet/checkout.html
@@ -8,7 +8,7 @@
diff --git a/kfet/templates/kfet/checkout_read.html b/kfet/templates/kfet/checkout_read.html
index e7c8bf5d..9d8aafad 100644
--- a/kfet/templates/kfet/checkout_read.html
+++ b/kfet/templates/kfet/checkout_read.html
@@ -12,10 +12,30 @@
{% endif %}
+
+
+ Effectuer un relevé
+
+
+
Nom: {{ checkout.name }}
Valide du {{ checkout.valid_from|date:'l j F Y, G:i' }} au {{ checkout.valid_to|date:'l j F Y, G:i' }}
Créée par: {{ checkout.created_by }}
Balance: {{ checkout.balance }} €
Protected: {{ checkout.is_protected }}
+Relevés:
+{% with statements=checkout.statements.all %}
+{% if not statements %}
+ Pas de relevé
+{% else %}
+
+ {% for statement in statements %}
+ - {{ statement }}
+ {% endfor %}
+
+{% endif %}
+{% endwith %}
+
+
{% endblock %}
diff --git a/kfet/templates/kfet/checkoutstatement.html b/kfet/templates/kfet/checkoutstatement.html
new file mode 100644
index 00000000..ac7cf9f8
--- /dev/null
+++ b/kfet/templates/kfet/checkoutstatement.html
@@ -0,0 +1,13 @@
+{% extends "kfet/base.html" %}
+
+{% block title %}Relevés{% endblock %}
+
+{% block content %}
+
+
+{% for statement in checkoutstatements %}
+ - {{ statement }}
+{% endfor %}
+
+
+{% endblock %}
diff --git a/kfet/templates/kfet/checkoutstatement_create.html b/kfet/templates/kfet/checkoutstatement_create.html
new file mode 100644
index 00000000..91fe1b23
--- /dev/null
+++ b/kfet/templates/kfet/checkoutstatement_create.html
@@ -0,0 +1,15 @@
+{% extends "kfet/base.html" %}
+
+{% block title %}Nouveau relevé{% endblock %}
+
+{% block content %}
+
+En caisse : {{ checkout.balance }}
+
+
+
+{% endblock %}
diff --git a/kfet/urls.py b/kfet/urls.py
index c0368f8f..591b5c02 100644
--- a/kfet/urls.py
+++ b/kfet/urls.py
@@ -12,29 +12,29 @@ urlpatterns = [
# -----
# Account - General
- url(r'^account/$', views.account,
+ url(r'^accounts/$', views.account,
name = 'kfet.account'),
- url(r'^account/is_validandfree$', views.account_is_validandfree_ajax,
+ url(r'^accounts/is_validandfree$', views.account_is_validandfree_ajax,
name = 'kfet.account.is_validandfree.ajax'),
# Account - Create
- url(r'^account/new$', views.account_create,
+ url(r'^accounts/new$', views.account_create,
name = 'kfet.account.create'),
- url(r'^account/new/user/(?P.+)$', views.account_create_ajax,
+ url(r'^accounts/new/user/(?P.+)$', views.account_create_ajax,
name = 'kfet.account.create.fromuser'),
- url(r'^account/new/clipper/(?P.+)$', views.account_create_ajax,
+ url(r'^accounts/new/clipper/(?P.+)$', views.account_create_ajax,
name = 'kfet.account.create.fromclipper'),
- url(r'^account/new/empty$', views.account_create_ajax,
+ url(r'^accounts/new/empty$', views.account_create_ajax,
name = 'kfet.account.create.empty'),
url(r'^autocomplete/account_new$', autocomplete.account_create,
name = 'kfet.account.create.autocomplete'),
# Account - Read
- url(r'^account/(?P.{3})$', views.account_read,
+ url(r'^accounts/(?P.{3})$', views.account_read,
name = 'kfet.account.read'),
# Account - Update
- url(r'^account/(?P.{3})/edit$', views.account_update,
+ url(r'^accounts/(?P.{3})/edit$', views.account_update,
name = 'kfet.account.update'),
# -----
@@ -42,40 +42,51 @@ urlpatterns = [
# -----
# Checkout - General
- url('^checkout/$',
+ url('^checkouts/$',
permission_required('kfet.is_team')(views.CheckoutList.as_view()),
name = 'kfet.checkout'),
# Checkout - Create
- url('^checkout/new$',
+ url('^checkouts/new$',
permission_required('kfet.is_team')(views.CheckoutCreate.as_view()),
name = 'kfet.checkout.create'),
# Checkout - Read
- url('^checkout/(?P\d+)$',
+ url('^checkouts/(?P\d+)$',
permission_required('kfet.is_team')(views.CheckoutRead.as_view()),
name = 'kfet.checkout.read'),
# Checkout - Update
- url('^checkout/(?P\d+)/edit$',
+ url('^checkouts/(?P\d+)/edit$',
permission_required('kfet.is_team')(views.CheckoutUpdate.as_view()),
name = 'kfet.checkout.update'),
+ ### Checkout Statements urls
+
+ # Checkout Statement - General
+ url('^checkouts/statements/$',
+ permission_required('kfet.is_team')(views.CheckoutStatementList.as_view()),
+ name = 'kfet.checkoutstatement'),
+ # Checkout Statement - Create
+ url('^checkouts/(?P\d+)/statements/add',
+ permission_required('kfet.is_team')(views.CheckoutStatementCreate.as_view()),
+ name = 'kfet.checkoutstatement.create'),
+
# -----
# Article urls
# -----
# Article - General
- url('^article/$',
+ url('^articles/$',
permission_required('kfet.is_team')(views.ArticleList.as_view()),
name = 'kfet.article'),
# Article - Create
- url('^article/new$',
+ url('^articles/new$',
permission_required('kfet.is_team')(views.ArticleCreate.as_view()),
name = 'kfet.article.create'),
# Article - Read
- url('^article/(?P\d+)$',
+ url('^articles/(?P\d+)$',
permission_required('kfet.is_team')(views.ArticleRead.as_view()),
name = 'kfet.article.read'),
# Article - Update
- url('^article/(?P\d+)/edit$',
+ url('^articles/(?P\d+)/edit$',
permission_required('kfet.is_team')(views.ArticleUpdate.as_view()),
name = 'kfet.article.update'),
diff --git a/kfet/views.py b/kfet/views.py
index ac980ecb..5ed53a3a 100644
--- a/kfet/views.py
+++ b/kfet/views.py
@@ -13,7 +13,8 @@ from django.db import IntegrityError, transaction
from django.db.models import F
from django.utils import timezone
from gestioncof.models import CofProfile, Clipper
-from kfet.models import Account, Checkout, Article, Settings, AccountNegative
+from kfet.models import (Account, Checkout, Article, Settings, AccountNegative,
+ CheckoutStatement)
from kfet.forms import *
from collections import defaultdict
@@ -303,6 +304,50 @@ class CheckoutUpdate(SuccessMessageMixin, UpdateView):
# Updating
return super(CheckoutUpdate, self).form_valid(form)
+# -----
+# Checkout Statement views
+# -----
+
+# Checkout Statement - General
+
+class CheckoutStatementList(ListView):
+ model = CheckoutStatement
+ queryset = CheckoutStatement.objects.order_by('-at')
+ template_name = 'kfet/checkoutstatement.html'
+ context_object_name= 'checkoutstatements'
+
+# Checkout Statement - Create
+
+class CheckoutStatementCreate(SuccessMessageMixin, CreateView):
+ model = CheckoutStatement
+ template_name = 'kfet/checkoutstatement_create.html'
+ form_class = CheckoutStatementForm
+ success_message = 'Nouveau relevé : %(checkout)s - %(at)s'
+
+ def get_success_url(self):
+ return reverse_lazy('kfet.checkout.read', kwargs={'pk':self.kwargs['pk_checkout']})
+
+ def get_success_message(self, cleaned_data):
+ return self.success_message % dict(
+ cleaned_data,
+ checkout = self.object.checkout.name,
+ at = self.object.at)
+
+ def get_context_data(self, **kwargs):
+ context = super(CheckoutStatementCreate, self).get_context_data(**kwargs)
+ checkout = Checkout.objects.get(pk=self.kwargs['pk_checkout'])
+ context['checkout'] = checkout
+ return context
+
+ def form_valid(self, form):
+ # Checking permission
+ if not self.request.user.has_perm('kfet.add_checkoutstatement'):
+ raise PermissionDenied
+ # Creating
+ form.instance.checkout_id = self.kwargs['pk_checkout']
+ form.instance.by = self.request.user.profile.account_kfet
+ return super(CheckoutStatementCreate, self).form_valid(form)
+
# -----
# Article views
# -----