1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright 2013 Mathias WOLFF |
3
|
|
|
# This file is part of pyfreebilling. |
4
|
|
|
# |
5
|
|
|
# pyfreebilling is free software: you can redistribute it and/or modify |
6
|
|
|
# it under the terms of the GNU General Public License as published by |
7
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
# (at your option) any later version. |
9
|
|
|
# |
10
|
|
|
# pyfreebilling is distributed in the hope that it will be useful, |
11
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
# GNU General Public License for more details. |
14
|
|
|
# |
15
|
|
|
# You should have received a copy of the GNU General Public License |
16
|
|
|
# along with pyfreebilling. If not, see <http://www.gnu.org/licenses/> |
17
|
|
|
|
18
|
|
|
# -*- coding: utf-8 -*- |
19
|
|
|
|
20
|
|
|
from __future__ import unicode_literals |
21
|
|
|
from django.core.files.storage import default_storage |
22
|
|
|
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage |
23
|
|
|
from django.db.models.fields.files import FieldFile |
24
|
|
|
from django.views.generic import FormView, ListView, View, CreateView |
25
|
|
|
from django.views.generic.base import TemplateView |
26
|
|
|
from django.views.generic.edit import FormMixin |
27
|
|
|
from django.contrib import messages |
28
|
|
|
from django.contrib.auth.views import login |
29
|
|
|
from django.contrib.auth.decorators import login_required |
30
|
|
|
from django.shortcuts import get_object_or_404 |
31
|
|
|
from django.http import HttpResponse |
32
|
|
|
from django.utils.translation import ugettext_lazy as _ |
33
|
|
|
|
34
|
|
|
from braces.views import LoginRequiredMixin |
35
|
|
|
|
36
|
|
|
from django_tables2 import RequestConfig |
37
|
|
|
|
38
|
|
|
from djqscsv import render_to_csv_response |
39
|
|
|
|
40
|
|
|
import datetime |
41
|
|
|
import calendar |
42
|
|
|
|
43
|
|
|
from dateutil.relativedelta import relativedelta |
44
|
|
|
|
45
|
|
|
from pyfreebilling.customerdirectory.models import CustomerDirectory |
46
|
|
|
|
47
|
|
|
from pyfreebilling.pyfreebill.models import Company,\ |
48
|
|
|
CompanyBalanceHistory,\ |
49
|
|
|
CustomerRates,\ |
50
|
|
|
CustomerRateCards,\ |
51
|
|
|
Person |
52
|
|
|
|
53
|
|
|
from pyfreebilling.cdr.models import CDR |
54
|
|
|
|
55
|
|
|
from .forms import CDRSearchForm, RatesForm, RatesListFormHelper |
56
|
|
|
from .filters import RatesListFilter |
57
|
|
|
from .tables import RatesTable |
58
|
|
|
from .utils import PagedFilteredTableView |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
class CreateUserView(CreateView): |
62
|
|
|
template_name = "customer/register.html" |
63
|
|
|
model = Person |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
@login_required |
67
|
|
|
def rates_csv_view(request, *args, **kwargs): |
68
|
|
|
ratecard = kwargs['ratecard'] |
69
|
|
|
|
70
|
|
|
qs = CustomerRates.objects.values( |
71
|
|
|
'destination', |
72
|
|
|
'prefix', |
73
|
|
|
'rate', |
74
|
|
|
'block_min_duration', |
75
|
|
|
'minimal_time', |
76
|
|
|
'init_block') |
77
|
|
|
|
78
|
|
|
try: |
79
|
|
|
usercompany = Person.objects.get(user=request.user) |
80
|
|
|
company = get_object_or_404(Company, name=usercompany.company) |
81
|
|
|
rc = CustomerRateCards.objects.filter( |
82
|
|
|
company=company.pk)\ |
83
|
|
|
.filter(ratecard__enabled=True)\ |
84
|
|
|
.order_by('priority') |
85
|
|
|
qs = qs.filter(ratecard__pk=ratecard) |
86
|
|
|
except Person.DoesNotExist: |
87
|
|
|
messages.error(request, |
88
|
|
|
_(u"""This user is not linked to a customer !""")) |
89
|
|
|
|
90
|
|
|
if ratecard and int(ratecard) and ratecard in rc: |
91
|
|
|
ratecard = int(ratecard) |
92
|
|
|
qs = qs.filter(ratecard__pk=ratecard) |
93
|
|
|
else: |
94
|
|
|
qs.none() |
95
|
|
|
return render_to_csv_response(qs, |
96
|
|
|
append_datestamp=True) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
@login_required |
100
|
|
|
def csv_view(request, *args, **kwargs): |
101
|
|
|
day = kwargs['day'] |
102
|
|
|
month = kwargs['month'] |
103
|
|
|
daymonth = None |
104
|
|
|
|
105
|
|
|
qs = CDR.objects.values('customer__name', |
106
|
|
|
'caller_id_number', |
107
|
|
|
'destination_number', |
108
|
|
|
'start_stamp', |
109
|
|
|
'billsec', |
110
|
|
|
'prefix', |
111
|
|
|
'sell_destination', |
112
|
|
|
'rate', |
113
|
|
|
'init_block', |
114
|
|
|
'block_min_duration', |
115
|
|
|
'total_sell', |
116
|
|
|
'customer_ip', |
117
|
|
|
'sip_user_agent' |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
try: |
121
|
|
|
usercompany = Person.objects.get(user=request.user) |
122
|
|
|
company = get_object_or_404(Company, name=usercompany.company) |
123
|
|
|
qs = qs.filter(customer=company.pk)\ |
124
|
|
|
.exclude(effective_duration="0")\ |
125
|
|
|
.order_by('-start_stamp') |
126
|
|
|
except Person.DoesNotExist: |
127
|
|
|
messages.error(request, |
128
|
|
|
_(u"""This user is not linked to a customer !""")) |
129
|
|
|
|
130
|
|
|
if day and int(day) < 8 and int(day) > 0: |
131
|
|
|
day = int(day) |
132
|
|
|
start_date = datetime.date.today() - datetime.timedelta(days=int(day)) |
133
|
|
|
end_date = start_date + datetime.timedelta(days=1) |
134
|
|
|
daymonth = 'OK' |
135
|
|
|
|
136
|
|
|
if month and int(month) < 4 and int(month) > 0: |
137
|
|
|
month = int(month) |
138
|
|
|
dm = datetime.date.today() |
139
|
|
|
start_date = datetime.date(dm.year, dm.month, 1) - relativedelta(months=int(month)) |
140
|
|
|
end_date = start_date + relativedelta(months=1) |
141
|
|
|
end_date = end_date - datetime.timedelta(days=1) |
142
|
|
|
daymonth = 'OK' |
143
|
|
|
|
144
|
|
|
if daymonth: |
145
|
|
|
qs = qs.filter(start_stamp__range=(start_date, end_date)) |
146
|
|
|
else: |
147
|
|
|
qs.none() |
148
|
|
|
# import pdb; pdb.set_trace() |
149
|
|
|
return render_to_csv_response( |
150
|
|
|
qs, |
151
|
|
|
append_datestamp=True, |
152
|
|
|
field_header_map={'customer__name': 'Customer'}) |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
class Template404View(LoginRequiredMixin, TemplateView): |
156
|
|
|
template_name = 'customer/404.html' |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
class Template500View(LoginRequiredMixin, TemplateView): |
160
|
|
|
template_name = 'customer/500.html' |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
class ListExportCustView(LoginRequiredMixin, TemplateView): |
164
|
|
|
template_name = 'customer/report.html' |
165
|
|
|
|
166
|
|
|
def get_context_data(self, **kwargs): |
167
|
|
|
context = super(ListExportCustView, self).get_context_data(**kwargs) |
168
|
|
|
context['day_1'] = datetime.date.today() - datetime.timedelta(days=1) |
169
|
|
|
context['day_2'] = datetime.date.today() - datetime.timedelta(days=2) |
170
|
|
|
context['day_3'] = datetime.date.today() - datetime.timedelta(days=3) |
171
|
|
|
context['day_4'] = datetime.date.today() - datetime.timedelta(days=4) |
172
|
|
|
context['day_5'] = datetime.date.today() - datetime.timedelta(days=5) |
173
|
|
|
context['day_6'] = datetime.date.today() - datetime.timedelta(days=6) |
174
|
|
|
context['day_7'] = datetime.date.today() - datetime.timedelta(days=7) |
175
|
|
|
dm = datetime.date.today() |
176
|
|
|
context['month_1'] = datetime.date(dm.year, dm.month, 1) - relativedelta(months=1) |
177
|
|
|
context['month_2'] = datetime.date(dm.year, dm.month, 1) - relativedelta(months=2) |
178
|
|
|
context['month_3'] = datetime.date(dm.year, dm.month, 1) - relativedelta(months=3) |
179
|
|
|
return context |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
class HomePageCustView(LoginRequiredMixin, TemplateView): |
183
|
|
|
template_name = 'customer/home.html' |
184
|
|
|
|
185
|
|
|
def get_context_data(self, **kwargs): |
186
|
|
|
context = super(HomePageCustView, self).get_context_data(**kwargs) |
187
|
|
|
messages.info(self.request, _(u'Welcome')) |
188
|
|
|
try: |
189
|
|
|
usercompany = Person.objects.get(user=self.request.user) |
190
|
|
|
try: |
191
|
|
|
context['company'] = Company.objects.get(name=usercompany.company) |
192
|
|
|
if context['company'].low_credit_alert > context['company'].customer_balance: |
193
|
|
|
messages.warning(self.request, |
194
|
|
|
_(u'ALERT : Low balance (credit alert level : %s)') % context['company'].low_credit_alert) |
195
|
|
|
if context['company'].account_blocked_alert_sent: |
196
|
|
|
messages.error(self.request, |
197
|
|
|
_(u'ALERT : Account blocked - no remaining credit - Please make an urgent payment')) |
198
|
|
|
context['ratecards'] = CustomerRateCards.objects.filter( |
199
|
|
|
company=context['company'].pk)\ |
200
|
|
|
.filter(ratecard__enabled=True)\ |
201
|
|
|
.order_by('priority') |
202
|
|
|
except Company.DoesNotExist: |
203
|
|
|
pass |
204
|
|
|
except Person.DoesNotExist: |
205
|
|
|
messages.error(self.request, |
206
|
|
|
_(u"""This user is not linked to a customer !""")) |
207
|
|
|
|
208
|
|
|
# integrer panneau contact et stats |
209
|
|
|
# integrer facture |
210
|
|
|
# integrer prestation |
211
|
|
|
return context |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
class RatesFilteredTableView(PagedFilteredTableView): |
215
|
|
|
template_name = 'customer/rates_table.html' |
216
|
|
|
model = CustomerRates |
217
|
|
|
# queryset = CustomerRates.objects.all() |
218
|
|
|
table_class = RatesTable |
219
|
|
|
filter_class = RatesListFilter |
220
|
|
|
formhelper_class = RatesListFormHelper |
221
|
|
|
|
222
|
|
|
def get_queryset(self, **kwargs): |
223
|
|
|
qs = super(RatesFilteredTableView, self).get_queryset() |
224
|
|
|
try: |
225
|
|
|
self.usercompany = Person.objects.get(user=self.request.user) |
226
|
|
|
self.company = get_object_or_404(Company, |
227
|
|
|
name=self.usercompany.company) |
228
|
|
|
self.rc = CustomerRateCards.objects.filter( |
229
|
|
|
company=self.company.pk)\ |
230
|
|
|
.filter(ratecard__enabled=True)\ |
231
|
|
|
.order_by('priority') |
232
|
|
|
qs = qs.filter(enabled=True).order_by('destination') |
233
|
|
|
except Person.DoesNotExist: |
234
|
|
|
messages.error(self.request, |
235
|
|
|
_(u"""This user is not linked to a customer !""")) |
236
|
|
|
# ratecard |
237
|
|
|
if self.kwargs['ratecard'] and self.rc.filter(ratecard_id=self.kwargs['ratecard']): |
238
|
|
|
self.ratecard = self.kwargs['ratecard'] |
239
|
|
|
else: |
240
|
|
|
self.rc = CustomerRateCards.objects.filter( |
241
|
|
|
company=self.company.pk)\ |
242
|
|
|
.filter(ratecard__enabled=True)\ |
243
|
|
|
.order_by('priority') |
244
|
|
|
self.ratecard = self.rc[0].ratecard_id |
245
|
|
|
|
246
|
|
|
if self.ratecard: # and ratecard.isnumeric(): |
247
|
|
|
qs = qs.filter(ratecard__pk=self.ratecard) |
248
|
|
|
return qs |
249
|
|
|
|
250
|
|
|
return qs.none() |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
class StatsCustView(LoginRequiredMixin, TemplateView): |
254
|
|
|
template_name = 'customer/stats.html' |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
class SipAccountCustView(LoginRequiredMixin, ListView): |
258
|
|
|
template_name = 'customer/sip_account.html' |
259
|
|
|
context_object_name = 'sipaccount' |
260
|
|
|
paginate_by = 10 |
261
|
|
|
model = CustomerDirectory |
262
|
|
|
|
263
|
|
|
def get_queryset(self): |
264
|
|
|
qs = super(SipAccountCustView, self).get_queryset() |
265
|
|
|
try: |
266
|
|
|
self.usercompany = Person.objects.get(user=self.request.user) |
267
|
|
|
self.company = get_object_or_404(Company, |
268
|
|
|
name=self.usercompany.company) |
269
|
|
|
return CustomerDirectory.objects.filter(company=self.company.pk)\ |
270
|
|
|
.order_by('id') |
271
|
|
|
except Person.DoesNotExist: |
272
|
|
|
messages.error(self.request, |
273
|
|
|
_(u"""This user is not linked to a customer !""")) |
274
|
|
|
return qs.none() |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
class CdrReportCustView(LoginRequiredMixin, FormMixin, ListView): |
278
|
|
|
template_name = 'customer/cdr_view.html' |
279
|
|
|
context_object_name = 'Cdr' |
280
|
|
|
form_class = CDRSearchForm |
281
|
|
|
paginate_by = 30 |
282
|
|
|
model = CDR |
283
|
|
|
|
284
|
|
|
def get_queryset(self): |
285
|
|
|
qs = super(CdrReportCustView, self).get_queryset() |
286
|
|
|
try: |
287
|
|
|
self.usercompany = Person.objects.get(user=self.request.user) |
288
|
|
|
self.company = get_object_or_404(Company, |
289
|
|
|
name=self.usercompany.company) |
290
|
|
|
qs = qs.filter(customer=self.company.pk)\ |
291
|
|
|
.exclude(effective_duration="0")\ |
292
|
|
|
.order_by('-start_stamp') |
293
|
|
|
except Person.DoesNotExist: |
294
|
|
|
messages.error(self.request, |
295
|
|
|
_(u"""This user is not linked to a customer !""")) |
296
|
|
|
|
297
|
|
|
# set start_date and end_date |
298
|
|
|
end_date = datetime.date.today() + datetime.timedelta(days=1) |
299
|
|
|
start_date = end_date - datetime.timedelta(days=30) |
300
|
|
|
|
301
|
|
|
start_d = {'y': [], 'm': [], 'd': [], 'h': [], 'min': [], 'status': True} |
302
|
|
|
end_d = {'y': [], 'm': [], 'd': [], 'h': [], 'min': [],'status': True} |
303
|
|
|
li = ['y', 'm', 'd', 'h', 'min'] |
304
|
|
|
for i in li: |
305
|
|
|
start_d[str(i)] = self.request.GET.get("from_" + str(i)) |
306
|
|
|
if start_d[str(i)] and start_d[str(i)].isnumeric(): |
307
|
|
|
start_d[str(i)] = int(start_d[str(i)]) |
308
|
|
|
else: |
309
|
|
|
start_d['status'] = False |
310
|
|
|
end_d[str(i)] = self.request.GET.get("to_" + str(i)) |
311
|
|
|
if end_d[str(i)] and end_d[str(i)].isnumeric(): |
312
|
|
|
end_d[str(i)] = int(end_d[str(i)]) |
313
|
|
|
else: |
314
|
|
|
end_d['status'] = False |
315
|
|
|
# dest num |
316
|
|
|
dest_num = self.request.GET.get("dest_num") |
317
|
|
|
if start_d['status']: |
318
|
|
|
start_date = datetime.datetime(start_d['y'], start_d['m'], start_d['d'], start_d['h'], start_d['min']) |
319
|
|
|
if end_d['status']: |
320
|
|
|
end_date = datetime.datetime(end_d['y'], end_d['m'], end_d['d'], end_d['h'], end_d['min']) |
321
|
|
|
if start_date and end_date: |
322
|
|
|
qs = qs.filter(start_stamp__range=(start_date, end_date)) |
323
|
|
|
|
324
|
|
|
if dest_num and dest_num.isnumeric(): |
325
|
|
|
qs = qs.filter(destination_number__startswith=dest_num) |
326
|
|
|
|
327
|
|
|
# test if get succes or not |
328
|
|
|
if start_d['status'] or end_d['status'] or dest_num: |
329
|
|
|
return qs |
330
|
|
|
return qs.none() |
331
|
|
|
|
332
|
|
|
|
333
|
|
|
class BalanceHistoryCustView(LoginRequiredMixin, ListView): |
334
|
|
|
template_name = 'customer/balance.html' |
335
|
|
|
model = CompanyBalanceHistory |
336
|
|
|
context_object_name = 'CustomerBalance' |
337
|
|
|
paginate_by = 10 |
338
|
|
|
|
339
|
|
|
def get_queryset(self): |
340
|
|
|
qs = super(BalanceHistoryCustView, self).get_queryset() |
341
|
|
|
try: |
342
|
|
|
self.usercompany = Person.objects.get(user=self.request.user) |
343
|
|
|
self.company = get_object_or_404(Company, name=self.usercompany.company) |
344
|
|
|
return CompanyBalanceHistory.objects.filter(company=self.company.pk)\ |
345
|
|
|
.filter(operation_type='customer')\ |
346
|
|
|
.order_by('-date_modified') |
347
|
|
|
except Person.DoesNotExist: |
348
|
|
|
messages.error(self.request, _(u"""This user is not linked to a customer !""")) |
349
|
|
|
return qs.none() |
350
|
|
|
|
351
|
|
|
def get_context_data(self, **kwargs): |
352
|
|
|
context = super(BalanceHistoryCustView, self).get_context_data(**kwargs) |
353
|
|
|
try: |
354
|
|
|
self.usercompany = Person.objects.get(user=self.request.user) |
355
|
|
|
context['company'] = get_object_or_404(Company, name=self.usercompany.company) |
356
|
|
|
return context |
357
|
|
|
except Person.DoesNotExist: |
358
|
|
|
messages.error(self.request, _(u"""This user is not linked to a customer !""")) |
359
|
|
|
return context |
360
|
|
|
|
361
|
|
|
|
362
|
|
|
class ProfileCustView(LoginRequiredMixin, TemplateView): |
363
|
|
|
template_name = 'customer/home.html' |
364
|
|
|
|
365
|
|
|
def get_context_data(self, **kwargs): |
366
|
|
|
context = super(HomePageCustView, self).get_context_data(**kwargs) |
367
|
|
|
messages.info(self.request, 'Your profile') |
368
|
|
|
return context |
369
|
|
|
|