mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Refound Account
This commit is contained in:
14
accounting/templates/accounting/refound_account.jinja
Normal file
14
accounting/templates/accounting/refound_account.jinja
Normal file
@ -0,0 +1,14 @@
|
||||
{% extends "core/base.jinja" %}
|
||||
|
||||
{% block title %}
|
||||
{% trans %}Refound account{% endtrans %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h3>{% trans %}Refound account{% endtrans %}</h3>
|
||||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p() }}
|
||||
<p><input type="submit" value="{% trans %}Refound{% endtrans %}" /></p>
|
||||
</form>
|
||||
{% endblock %}
|
@ -1,3 +1,47 @@
|
||||
from django.test import TestCase
|
||||
from django.test import Client, TestCase
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth.models import Group
|
||||
from django.core.management import call_command
|
||||
from django.conf import settings
|
||||
|
||||
# Create your tests here.
|
||||
from core.models import User
|
||||
from counter.models import Counter
|
||||
|
||||
|
||||
class RefoundAccountTest(TestCase):
|
||||
def setUp(self):
|
||||
call_command("populate")
|
||||
self.skia = User.objects.filter(username="skia").first()
|
||||
# reffil skia's account
|
||||
self.skia.customer.amount = 800
|
||||
self.skia.customer.save()
|
||||
|
||||
def test_permission_denied(self):
|
||||
self.client.login(useername='guy', password='plop')
|
||||
response_post = self.client.post(reverse("accounting:refound_account"),
|
||||
{"user": self.skia.id})
|
||||
response_get = self.client.get(reverse("accounting:refound_account"))
|
||||
self.assertTrue(response_get.status_code == 403)
|
||||
self.assertTrue(response_post.status_code == 403)
|
||||
|
||||
def test_root_granteed(self):
|
||||
self.client.login(username='root', password='plop')
|
||||
response_post = self.client.post(reverse("accounting:refound_account"),
|
||||
{"user": self.skia.id})
|
||||
self.skia = User.objects.filter(username='skia').first()
|
||||
response_get = self.client.get(reverse("accounting:refound_account"))
|
||||
self.assertFalse(response_get.status_code == 403)
|
||||
self.assertTrue('<form action="" method="post">' in str(response_get.content))
|
||||
self.assertFalse(response_post.status_code == 403)
|
||||
self.assertTrue(self.skia.customer.amount == 0)
|
||||
|
||||
def test_comptable_granteed(self):
|
||||
self.client.login(username='comptable', password='plop')
|
||||
response_post = self.client.post(reverse("accounting:refound_account"),
|
||||
{"user": self.skia.id})
|
||||
self.skia = User.objects.filter(username='skia').first()
|
||||
response_get = self.client.get(reverse("accounting:refound_account"))
|
||||
self.assertFalse(response_get.status_code == 403)
|
||||
self.assertTrue('<form action="" method="post">' in str(response_get.content))
|
||||
self.assertFalse(response_post.status_code == 403)
|
||||
self.assertTrue(self.skia.customer.amount == 0)
|
||||
|
@ -38,6 +38,8 @@ urlpatterns = [
|
||||
url(r'^label/(?P<clubaccount_id>[0-9]+)$', LabelListView.as_view(), name='label_list'),
|
||||
url(r'^label/(?P<label_id>[0-9]+)/edit$', LabelEditView.as_view(), name='label_edit'),
|
||||
url(r'^label/(?P<label_id>[0-9]+)/delete$', LabelDeleteView.as_view(), name='label_delete'),
|
||||
# User account
|
||||
url(r'^refound/account$', RefoundAccountView.as_view(), name='refound_account'),
|
||||
]
|
||||
|
||||
|
||||
|
@ -1,9 +1,13 @@
|
||||
from django.views.generic import ListView, DetailView, RedirectView
|
||||
from django.views.generic.edit import UpdateView, CreateView, DeleteView
|
||||
from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView
|
||||
from django.shortcuts import render
|
||||
from django.core.urlresolvers import reverse_lazy
|
||||
from django.core.urlresolvers import reverse_lazy, reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.forms.models import modelform_factory
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.forms import HiddenInput
|
||||
from django.db import transaction
|
||||
from django.conf import settings
|
||||
from django import forms
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.utils.translation import ugettext as _
|
||||
@ -15,6 +19,7 @@ from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultip
|
||||
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
|
||||
from core.views.forms import SelectFile, SelectDate
|
||||
from accounting.models import BankAccount, ClubAccount, GeneralJournal, Operation, AccountingType, Company, SimplifiedAccountingType, Label
|
||||
from counter.models import Counter, Selling
|
||||
|
||||
# Main accounting view
|
||||
|
||||
@ -481,3 +486,49 @@ class LabelDeleteView(CanEditMixin, DeleteView):
|
||||
|
||||
def get_success_url(self):
|
||||
return self.object.get_absolute_url()
|
||||
|
||||
class CloseCustomerAccountForm(forms.Form):
|
||||
user = AutoCompleteSelectField('users', label=_('Refound this account'), help_text=None, required=True)
|
||||
|
||||
class RefoundAccountView(FormView):
|
||||
"""
|
||||
Create a selling with the same amount than the current user money
|
||||
"""
|
||||
template_name = "accounting/refound_account.jinja"
|
||||
form_class = CloseCustomerAccountForm
|
||||
|
||||
def permission(self, user):
|
||||
if user.is_root or user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
||||
return True
|
||||
else:
|
||||
raise PermissionDenied
|
||||
|
||||
def dispatch(self, request, *arg, **kwargs):
|
||||
res = super(RefoundAccountView, self).dispatch(request, *arg, **kwargs)
|
||||
if self.permission(request.user):
|
||||
return res
|
||||
|
||||
def post(self, request, *arg, **kwargs):
|
||||
self.operator = request.user
|
||||
if self.permission(request.user):
|
||||
return super(RefoundAccountView, self).post(self, request, *arg, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
self.customer = form.cleaned_data['user']
|
||||
self.create_selling()
|
||||
return super(RefoundAccountView, self).form_valid(form)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse('accounting:refound_account')
|
||||
|
||||
def create_selling(self):
|
||||
with transaction.atomic():
|
||||
uprice = self.customer.customer.amount
|
||||
main_club_counter = Counter.objects.filter(club__unix_name=settings.SITH_MAIN_CLUB['unix_name'],
|
||||
type='OFFICE').first()
|
||||
main_club = main_club_counter.club
|
||||
s = Selling(label=_('Refound account'), unit_price=uprice,
|
||||
quantity=1, seller=self.operator,
|
||||
customer=self.customer.customer,
|
||||
club=main_club, counter=main_club_counter)
|
||||
s.save()
|
||||
|
Reference in New Issue
Block a user