|
1
|
|
|
# Copyright 2013 Mathias WOLFF |
|
2
|
|
|
# This file is part of pyfreebilling. |
|
3
|
|
|
# |
|
4
|
|
|
# pyfreebilling is free software: you can redistribute it and/or modify |
|
5
|
|
|
# it under the terms of the GNU General Public License as published by |
|
6
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
# (at your option) any later version. |
|
8
|
|
|
# |
|
9
|
|
|
# pyfreebilling is distributed in the hope that it will be useful, |
|
10
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
# GNU General Public License for more details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License |
|
15
|
|
|
# along with pyfreebilling. If not, see <http://www.gnu.org/licenses/> |
|
16
|
|
|
|
|
17
|
|
|
from django.core.management.base import BaseCommand, CommandError |
|
18
|
|
|
from pyfreebill.models import Company |
|
19
|
|
|
from templated_email import send_templated_mail |
|
20
|
|
|
from django.conf import settings |
|
21
|
|
|
|
|
22
|
|
|
class Command(BaseCommand): |
|
23
|
|
|
args = '<date>' |
|
24
|
|
|
help = 'Alert messages - balance, balance status - lowbalance, alert low balance - custom ' |
|
25
|
|
|
|
|
26
|
|
|
def handle(self, *args, **options): |
|
27
|
|
|
for var in args: |
|
28
|
|
|
# try: |
|
29
|
|
|
|
|
30
|
|
|
if var == "balance": |
|
31
|
|
|
#print "balance" |
|
32
|
|
|
qs = Company.objects.filter(customer_enabled=True).exclude(email_alert__isnull=True).exclude(email_alert__exact='') |
|
33
|
|
|
for c in qs: |
|
34
|
|
|
#print "name %s - balance %s - email : %s "% (c.name, c.customer_balance, c.email_alert) |
|
35
|
|
|
send_templated_mail( |
|
36
|
|
|
template_name='balance', |
|
37
|
|
|
from_email=settings.EMAIL_HOST_USER, |
|
38
|
|
|
recipient_list=[c.email_alert], |
|
39
|
|
|
context={ |
|
40
|
|
|
'company':c.name, |
|
41
|
|
|
'balance':c.customer_balance, |
|
42
|
|
|
'signature':settings.EMAIL_SIGNATURE |
|
43
|
|
|
}, |
|
44
|
|
|
) |
|
45
|
|
|
elif var == "lowbalance": |
|
46
|
|
|
print "lowbalance" |
|
47
|
|
|
qs = Company.objects.filter(customer_enabled=True).exclude(email_alert__isnull=True).exclude(email_alert__exact='') |
|
48
|
|
|
for c in qs: |
|
49
|
|
|
""" CREDIT LIMIT ALERT """ |
|
50
|
|
|
print "name %s - balance %s - low_credit_alert : %s - low_credit_alert_sent : %s"% (c.name, c.customer_balance, c.low_credit_alert, c.low_credit_alert_sent) |
|
51
|
|
|
# Check alert status and update if necessary |
|
52
|
|
|
if c.low_credit_alert_sent == True and c.customer_balance > c.low_credit_alert: |
|
53
|
|
|
print "balance > low_credit_alert and low_credit_alert_sent is true" |
|
54
|
|
|
# set low_credit_alert to False |
|
55
|
|
|
c.low_credit_alert_sent = False |
|
56
|
|
|
c.save() |
|
57
|
|
|
elif c.low_credit_alert_sent == False and c.customer_balance > c.low_credit_alert: |
|
58
|
|
|
print "balance > low_credit_alert and low_credit_alert_sent is false" |
|
59
|
|
|
# Nothing to do - good |
|
60
|
|
|
pass |
|
61
|
|
|
elif c.low_credit_alert_sent == False and c.customer_balance <= c.low_credit_alert: |
|
62
|
|
|
print "balance < low_credit_alert and low_credit_alert_sent is false" |
|
63
|
|
|
# set low_credit_alert to True |
|
64
|
|
|
c.low_credit_alert_sent = True |
|
65
|
|
|
c.save() |
|
66
|
|
|
# send alert email |
|
67
|
|
|
send_templated_mail( |
|
68
|
|
|
template_name='lowbalance', |
|
69
|
|
|
from_email=settings.EMAIL_HOST_USER, |
|
70
|
|
|
recipient_list=[c.email_alert], |
|
71
|
|
|
context={ |
|
72
|
|
|
'company':c.name, |
|
73
|
|
|
'balance':c.customer_balance, |
|
74
|
|
|
'creditalert':c.low_credit_alert, |
|
75
|
|
|
'signature':settings.EMAIL_SIGNATURE |
|
76
|
|
|
}, |
|
77
|
|
|
) |
|
78
|
|
|
""" CREDIT OVER """ |
|
79
|
|
|
print "name %s - prepaid : %s - balance %s - credit_limit : %s - account_blocked_alert_sent : %s"% (c.name, c.prepaid, c.customer_balance, c.credit_limit, c.account_blocked_alert_sent) |
|
80
|
|
|
# Check alert status and update if necessary |
|
81
|
|
|
if c.account_blocked_alert_sent == True: |
|
82
|
|
|
if ((c.prepaid == False and c.customer_balance > c.credit_limit) or (c.prepaid == True and c.customer_balance > 0)): |
|
83
|
|
|
print "balance > credit_limit or 0 and account_blocked_alert_sent is true" |
|
84
|
|
|
# set account_blocked_alert to False |
|
85
|
|
|
c.account_blocked_alert_sent = False |
|
86
|
|
|
c.save() |
|
87
|
|
|
elif c.account_blocked_alert_sent == False: |
|
88
|
|
|
if ((c.prepaid == False and c.customer_balance <= c.credit_limit) or (c.prepaid == True and c.customer_balance <= 0)): |
|
89
|
|
|
print "balance < credit_limit or 0 and account_blocked_alert_sent is false" |
|
90
|
|
|
# set account_blocked_alert to True |
|
91
|
|
|
c.account_blocked_alert_sent = True |
|
92
|
|
|
c.save() |
|
93
|
|
|
# send alert email |
|
94
|
|
|
send_templated_mail( |
|
95
|
|
|
template_name='nobalance', |
|
96
|
|
|
from_email=settings.EMAIL_HOST_USER, |
|
97
|
|
|
recipient_list=[c.email_alert], |
|
98
|
|
|
context={ |
|
99
|
|
|
'company':c.name, |
|
100
|
|
|
'balance':c.customer_balance, |
|
101
|
|
|
'signature':settings.EMAIL_SIGNATURE |
|
102
|
|
|
}, |
|
103
|
|
|
) |
|
104
|
|
|
elif var == "custom": |
|
105
|
|
|
pass |
|
106
|
|
|
else: |
|
107
|
|
|
return |
|
108
|
|
|
|
|
109
|
|
|
# except: |
|
110
|
|
|
# return CommandError |
|
111
|
|
|
|
|
112
|
|
|
self.stdout.write('Successfully alerts ') |
|
113
|
|
|
|