-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
299 lines (242 loc) · 9.63 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# This Python file uses the following encoding: utf-8
from django.http import HttpResponse, HttpResponseServerError, HttpResponseRedirect, Http404
from django.core.cache import cache
from django.template.loader import get_template
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from django.template import Template, RequestContext
from django.conf import settings
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from math import fabs
from datetime import date, datetime
from biereapp.models import BiereUser, FactureForm, Facture, CommandeProduitForm, Transaction, Prix, ProduitForm, Produit, TransactionForm, ClientForm, PrixForm, Option, Client
from biereapp import models
from biereapp.models import logit
@login_required
def Dashboard(request):
return render_to_response('dashboard.html', { 'user': request.user })
@login_required
def CreerFacture(request):
created = False
facture = False
# Special case for restricted users, see README for more
user = BiereUser.as_current_user()
client = user.is_restricted_user()
if client:
facture = Facture()
facture.Client = client
d = datetime.today()
facture.Note = "Facture du " + str(d.year) +'/'+ str(d.month) +'/'+ str(d.day)
facture.save()
return HttpResponseRedirect('/factures/'+ str(facture.id)+'/')
# There is a new commande to create
if request.method == 'POST':
factureform = FactureForm(request.POST)
if factureform.is_valid():
bill = factureform.save( commit=False )
bill.User = request.user
bill.save()
created = True
facture = bill
else:
try:
if request.GET['client']:
id = int(request.GET['client'])
factureform = FactureForm(initial={'Client': id})
else:
factureform = FactureForm()
except Exception as e:
print e
factureform = FactureForm()
facture = Facture()
commande = False
produits = False
return render_to_response('facture/new.html', {'created':created, 'factureform': factureform, 'user': request.user, 'facture': facture })
@login_required
def AddUserTransation(request):
"""Ajoute un produit à une commande"""
facture = False
commande = TransactionForm( request.POST )
# transaction to add
# ToDo: Save the form in a instance of the Transaction model! Or make the ModelForm and override the __init__
if commande.is_valid():
transaction = commande.save()
facture = transaction.Facture
# Ajoute comme une commande
#transaction.add_to_commande( )
transaction.save()
if not facture:
facture = commande
template_var = { 'user': request.user, 'facture': facture }
# Detects AJAX requests made from jQuery (which I use in this program)
if request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest':
return render_to_response('facture/add_produit.ajax', template_var)
return render_to_response('facture/facture.html', template_var)
@login_required
def NewProduit(request):
created = False
if request.method == 'POST':
produit = ProduitForm(request.POST)
if produit.is_valid():
produit = produit.save();
produit.save()
created = True
return HttpResponseRedirect('/produits/'+str(produit.id) +'/')
else:
produit = models.ProduitForm()
return render_to_response('produits/new.html', {'created':created, 'produit': produit, 'user': request.user })
@login_required
def NewClient(request):
client = False
form = ClientForm()
if request.POST:
client = ClientForm(request.POST)
if client.is_valid():
# Save the form
client = client.save()
#save the entry
client.save()
return render_to_response('clients/new.html', {'client': client, 'form': form})
@login_required
def FactureDetails(request, facture_id):
facture = Facture.objects.get(id = facture_id)
client_interne = False
try:
client = Option.get('Client interne')
if facture.Client.Nom == client:
client_interne = True
except:
client_interne = False
return render_to_response('facture/details.html', {'facture': facture})
@login_required
def AddPrixProduit(request, object_id):
created = False
prix = False
if request.POST:
form = PrixForm(request.POST)
if form.is_valid():
prix = form.save()
prix.save()
created = True
# Because a Prix was created, we can tell the template to use the empty form
prix = False
produit = Produit.objects.get(id=object_id)
return render_to_response('produits/produit.html', { 'produit': produit, 'created': created, 'prix': prix })
@login_required
def FactureFermer(request, facture_id):
facture = False
if facture_id > 0:
facture = Facture.objects.get(id=facture_id)
else:
raise Http404
if facture:
facture.fermer()
return HttpResponseRedirect('/factures/'+str(facture.id)+'/')
@login_required
def FactureInFermer(request, facture_id):
facture = False
if facture_id > 0:
facture = Facture.objects.get(id=facture_id)
else:
raise Http404
if facture:
facture.EstFermee = False
facture.save()
return HttpResponseRedirect('/factures/'+str(facture.id)+'/')
@login_required
def ProduitInventaire(request):
liste_produits = Produit.objects.all().order_by('Brasseur', 'Nom')
return render_to_response('produits/inventaire.html', {'produits': liste_produits})
@login_required
def CommandeFournisseur(request):
facture = create_commande_fournisseur(request)
return render_to_response('facture/facture.html', {'facture': facture })
# Make a new Commande Fournisseur and returns it
def create_commande_fournisseur(request):
# Try to get the Client interne Option
try:
client = Option.get('Client interne')
client = Client.objects.filter(Nom = client)[0:1].get()
except:
raise Exception("Impossible de faire une commande fournisseur sans l'option Client interne ")
facture = Facture()
d = datetime.today()
facture.Note = "Commande fournisseur du " + str(d.year) +'/'+ str(d.month) +'/'+ str(d.day)
facture.Client = client
facture.save()
return facture
@login_required
def Commande(request):
# Try to get the Client interne Option
try:
client = Option.get('Client interne')
client = Client.objects.filter(Nom = client)[0:1].get()
except:
raise Exception("Impossible de faire une commande fournisseur sans l'option Client interne ")
return render_to_response('clients/client.html', {'client': client})
@login_required
def AJAX_AddInventaire(request):
if(request.POST):
erreur = []
try:
facture = Option.get('Facture inventaire')
facture = Facture.objects.get( id=int(facture) )
except:
erreur.append (u"L'option Facture inventaire n'existe pas, vous ne pouvez faire de mise à jour de l'inventaire")
try:
print request.POST['Produit']
prix = int(request.POST['Produit'])
prix = Produit.objects.get(id = prix)
prix = Prix.objects.filter(Produit=prix)[0:1].get()
except Exception as e:
print e
erreur.append(u"Impossible d'identifier le produit, il n'a pas de prix. <a href=\\\"/produits/"+request.POST['Produit']+"/\\\">Ajoutez en un</a>")
try:
qte = int(request.POST['Qte'])
except:
erreur.append(u"la quantité n'est pas une chiffre valide")
if len(erreur) > 0:
return render_to_response('ajax/ajust_qte.js', {'erreur': erreur})
t = Transaction()
t.Facture = facture
t.Prix = prix
if qte < 0:
t.Type = "INV_OUT"
else:
t.Type = "INV_IN"
t.Qte = abs(qte)
t.save()
return render_to_response('ajax/ajust_qte.js', {'trans': t})
else:
raise Http404()
def AJAX_DeleteTransaction(request):
if(request.POST):
erreur = []
try:
trans = Transaction.objects.get( id = int(request.POST['transaction_id']) )
if request.user.has_perm('biereapp.delete_transaction'):
trans.delete()
else:
erreur.append(u'Méchant, méchant! Tu ne peux pas supprimer de transaction! --Peace')
except Exception as e:
print e
erreur.append(u'Impossible d\'identifier la transaction.')
if len(erreur) > 0:
return render_to_response('ajax/transaction_delete.js', {'erreur': erreur})
return render_to_response('ajax/transaction_delete.js', {'trans': trans})
else:
Http404()
@login_required
def super_facture(request):
a = request.GET['a']
if 'commande_comite' == a:
pass
if 'vente_comptant' == a:
client = Option.get('Client comptant')
pass
if 'vente_comite' == a:
pass
if 'reception_fournisseur' == a:
pass
return render_to_response('facture/super.html', {})