Skip to content

Commit

Permalink
added check and receipts excel generation
Browse files Browse the repository at this point in the history
  • Loading branch information
egourlao committed Aug 20, 2016
1 parent 3144ffd commit 80d1b65
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
43 changes: 43 additions & 0 deletions core/services/excel_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# coding: utf8

from facture import models as facture_models

def generate_checks_xls(worksheet):
qs = list(facture_models.Cheque.objects.all().order_by('num').prefetch_related('facturerecue'))
for n, val in enumerate(["Référence", "Destinataire", "Valeur", "Etat", "Numéro de facture",
"Destinataire de la facture"]):
worksheet.write(0, n, val)
for num, cheque in enumerate(qs):
for n, val in enumerate([cheque.num, cheque.destinataire, cheque.valeur, cheque.get_state_display()]):
worksheet.write(num+1, n, val)
if cheque.facturerecue:
worksheet.write(num+1, 4, cheque.facturerecue_id)
worksheet.write(num+1, 5, cheque.facturerecue.nom_entreprise)
else:
worksheet.write(num+1, 4, "--")
worksheet.write(num+1, 5, "--")
return worksheet

def generate_receipts_xls(worksheet):
qs = list(facture_models.FactureRecue.objects.all().order_by('date').prefetch_related('perm')
.prefetch_related('categorie'))
for n, val in enumerate(["Référence", "Entreprise", "Date", "Date de paiement", "Prix TTC", "TVA", "Etat",
"Personne à rembourser", "Date de remboursement", "Nom de la perm", "Date de la perm",
"Période de la perm", "Responsable de la perm"]):
worksheet.write(0, n, val)
for num, facture in enumerate(qs):
for n, val in enumerate([facture.categorie.code + str(facture.id) if facture.categorie else facture.id,
facture.nom_entreprise, facture.date, facture.date_paiement, facture.prix,
facture.get_total_taxes(), facture.get_etat_display(),
facture.personne_a_rembourser or "", facture.date_remboursement or ""]):
worksheet.write(num+1, n, val)
if facture.perm:
worksheet.write(num+1, 9, facture.perm.nom)
worksheet.write(num + 1, 10, facture.perm.date)
worksheet.write(num + 1, 11, facture.perm.get_periode_display())
worksheet.write(num + 1, 12, facture.perm.nom_resp + " - " + facture.perm.mail_resp)
else:
for i in range(9, 13):
worksheet.write(num+1, i, "--")
return worksheet

28 changes: 27 additions & 1 deletion facture/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

from sets import Set

from django.http import HttpResponse
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import api_view, permission_classes, renderer_classes
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
import xlwt

from picsous.permissions import IsAdmin, IsAuthorizedUser
from picsous.settings import NEMOPAY_FUNDATION_ID

from core import models as core_models
from core import viewsets as core_viewsets
from core.services import payutc
from core.services import payutc, excel_generation
from facture import models as facture_models
from facture import serializers as facture_serializers

Expand Down Expand Up @@ -107,3 +109,27 @@ def tva_info(request, id):
return Response({'tva_deductible': tva_deductible,
'tva_a_declarer': tva_a_declarer,
'tva_a_declarer_total': sum(tva['montant'] for tva in tva_a_declarer)})

def excel_check_generation(request):
# Vue permettant de générer un fichier excel avec la liste des chèques, et des factures associées
response = HttpResponse(content_type='application/vnd.ms-excel; charset=utf-8')
response['Content-Disposition'] = 'attachment; filename="Picasso_cheques.xls"'

writer = xlwt.Workbook(encoding="utf-8")
ws = writer.add_sheet('Chèques')
excel_dump = excel_generation.generate_checks_xls(ws)
writer.save(response)
return response

def excel_facture_generation(request):
# Vue permettant de générer un fichier excel avec la liste des factures, et des perms associées
response = HttpResponse(content_type='application/vnd.ms-excel; charset=utf-8')
response['Content-Disposition'] = 'attachment; filename="Picasso_factures_recues.xls"'

writer = xlwt.Workbook(encoding="utf-8")
ws = writer.add_sheet('Factures reçues')
excel_dump = excel_generation.generate_receipts_xls(ws)
writer.save(response)
return response


2 changes: 2 additions & 0 deletions picsous/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

url(r'^facture/(?P<id>\d+)$', facture_views.facture),
url(r'^tvainfo/(?P<id>\d+)$', facture_views.tva_info),
url(r'^generate/cheques$', facture_views.excel_check_generation),
url(r'^generate/factures$', facture_views.excel_facture_generation),

url(r'^convention/(?P<id>\d+)$', perm_views.convention_partenariat),
url(r'^createpayutcarticle/(?P<id>\d+)/$', perm_views.create_payutc_article),
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ django-cors-headers
django-extensions
djangorestframework
requests
xlwt==1.1.2

0 comments on commit 80d1b65

Please sign in to comment.