8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-09 19:36:27 +00:00

[Printer] Create payment & invoice

This commit is contained in:
Maxime Bombar 2018-09-09 00:51:18 +02:00 committed by root
parent 4552fcceea
commit 00db1392db
2 changed files with 61 additions and 5 deletions

View file

@ -14,4 +14,5 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^new_job/$', views.new_job, name="new-job"), url(r'^new_job/$', views.new_job, name="new-job"),
url(r'^success/$', views.success, name="success"), url(r'^success/$', views.success, name="success"),
url(r'^payment/$', views.payment, name="payment"),
] ]

View file

@ -28,6 +28,15 @@ from .forms import (
PrintForm, PrintForm,
) )
from cotisations.models import(
Paiement,
Facture,
Vente,
)
from cotisations.utils import find_payment_method
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@login_required @login_required
@ -52,11 +61,14 @@ def new_job(request):
job = job.save(commit=False) job = job.save(commit=False)
job.filename = filename job.filename = filename
job.user=request.user job.user=request.user
# raise ValidationError("'%(path)s'", code='path', params = {'path': job.printAs})
if job.printAs is None: if job.printAs is None:
job.printAs = request.user job.printAs = request.user
job.status='Printable' job.status='Printable'
# raise # raise
# raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES['form-%s-file' % i].temporary_file_path()}) # raise ValidationError("'%(path)s'", code='path', params = {'path': request.FILES['form-%s-file' % i].temporary_file_path()})
# job_data = model_to_dict(job)
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': bool(job.printAs is None) })
metadata = pdfinfo(request.FILES['form-%s-file' % i].temporary_file_path()) metadata = pdfinfo(request.FILES['form-%s-file' % i].temporary_file_path())
job.pages = metadata["Pages"] job.pages = metadata["Pages"]
# raise ValidationError("'%(path)s'", code='path', params = {'path': type(job)}) # raise ValidationError("'%(path)s'", code='path', params = {'path': type(job)})
@ -91,12 +103,12 @@ def new_job(request):
job_formset = formset_factory(PrintForm)( job_formset = formset_factory(PrintForm)(
request.POST, request.POST,
) )
id_list = [request.session['id-form-%s-file' % i] for i in range(n)] jids = [request.session['id-form-%s-file' % i] for i in range(n)]
# raise ValidationError("'%(path)s'", code='path', params = {'path': id_list }) # raise ValidationError("'%(path)s'", code='path', params = {'path': id_list })
if job_formset.is_valid(): if job_formset.is_valid():
for job_obj in job_formset: for job_obj in job_formset:
i=0 i=0
old_job = JobWithOptions.objects.get(id=id_list[i]) old_job = JobWithOptions.objects.get(id=jids[i])
job = job_obj.save(commit=False) job = job_obj.save(commit=False)
job.user = request.user job.user = request.user
job.status = 'Running' job.status = 'Running'
@ -106,9 +118,9 @@ def new_job(request):
i+=1 i+=1
# raise ValidationError("'%(plop)s'", code='plop', params = {'plop': request.method}) # raise ValidationError("'%(plop)s'", code='plop', params = {'plop': request.method})
# raise ValidationError("'%(path)s'", code='path', params = {'path': str(n) }) # raise ValidationError("'%(path)s'", code='path', params = {'path': str(n) })
return redirect(reverse( request.session['jids']=jids
'printer:success', return redirect('printer:payment')
))
raise Exception("Invalid Job_formset") raise Exception("Invalid Job_formset")
else: else:
@ -124,6 +136,49 @@ def new_job(request):
request request
) )
def payment(request):
"""
View used to create a new invoice and make the payment
"""
# user = request.user
jids = request.session['jids']
# raise ValidationError("'%(path)s'", code='path', params = {'path': jids})
jobs = JobWithOptions.objects.filter(id__in=jids)
users = {}
for job in jobs:
try:
users[job.printAs]+=job.price
except KeyError:
users[job.printAs]=job.price
for user in users:
# If payment_method balance doesn't exist, then you're not allowed to print.
try:
balance = find_payment_method(Paiement.objects.get(is_balance=True))
except Paiement.DoesNotExist:
messages.error(
request,
_("You are not allowed to print")
)
return redirect(reverse(
'users:profil',
kwargs={'userid': request.user.id}
))
invoice = Facture(user=user)
invoice.paiement = balance.payment
# invoice.valid = True
invoice.save()
Vente.objects.create(
facture=invoice,
name='Printing',
prix=users[user],
number=1,
)
invoice.paiement.end_payment(invoice, request)
return redirect(reverse(
'printer:success',
))
def success(request): def success(request):
return form( return form(
{}, {},