forked from CICIC/gestioCI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.py
213 lines (170 loc) · 7.47 KB
/
cron.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
#encoding=utf-8
from django_cron import CronJobBase, Schedule
from django.core import mail
from decimal import Decimal
from datetime import datetime
from Invoices.models import Email, EmailNotification
from Invoices.models import Email, EmailNotification, period, PeriodClose, paymentEntities, Soci, SalesInvoice, PurchaseInvoice, periodTaxes
class testBot(CronJobBase):
RUN_EVERY_MINS = 0 # every 1 min
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'TestEmailsCron' # a unique code
def do(self):
from Invoices.bots import bot_period_manager
bot = bot_period_manager()
bot.render()
"""
Not tested code, runcrons to exect it.
"""
class testEmail(CronJobBase):
RUN_EVERY_MINS = 0 # every 1 min
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'TestEmailsCron' # a unique code
def do(self):
print "Going to execute cron: TestEmailsCron..."
print "https://github.com/CICIC/gestioCI/blob/master/cron.py#L11"
yNotifications = Email()
yNotifications.subject = "Test GestioCI"
yNotifications.body = "Testing Django GestioCI Email server"
yNotifications.efrom = "gestioci@cooperativa.cat"
yNotifications.to_list = ["ox@enredaos.net", "aleph@riseup.net"]
print "Method 1"
# from django.core.mail import send_mail
# print send_mail(yNotifications.subject, yNotifications.body, yNotifications.efrom, yNotifications.to_list)
print "Method 2"
connection = mail.get_connection()
print "conection getted"
print "HOST: " + connection.host
print "PORT: " + str( connection.port )
print "USER: " + connection.username
print "PASS: " + connection.password
print "TLS: " + str ( connection.use_tls )
print connection.open()
print "connaction opened"
print "Sending"
oneemail = mail.EmailMessage(yNotifications.subject,
yNotifications.body,
yNotifications.efrom,
yNotifications.to_list(),
connection=connection
)
print oneemail.send()
print "Closing connection"
connection.close()
class EmailsNotifierCron(CronJobBase):
RUN_EVERY_MINS = 0 # every 1 min
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'EmailsNotifierCron' # a unique code
def do(self):
print "EmailsNotifierCron Cron is disabled by code"
return
output = "Send mail start"
#Get all active notificators
EmailsToSend = EmailNotification.objects.filter(is_active=True)
#For each notificator...
#output = ""
connection = mail.get_connection()
connection.open()
for yNotifications in EmailsToSend.all():
#If execution date matches present date
print yNotifications.execution_date()
if yNotifications.execution_date() is datetime.now() and "ww" == "ee":
#Open email contection
#Create mail
# oneemail = mail.EmailMessage(yNotifications.subject, yNotifications.body, yNotifications.efrom, ["gestioci@cooperativa.cat",], [yNotifications.get_notification_emails_list(),], connection=connection)
try:
print "a"
#oneemail = mail.EmailMessage("XXXXXXprueba sin u", yNotifications.body, yNotifications.efrom, ["gestioci@cooperativa.cat",], ["gestioci@cooperativa.cat", "jaimesanchezm@gmail.com","gallardonegro@correo.nu"], connection=connection)
oneemail = mail.EmailMessage("En pruebas: Consulta administracion " + yNotifications.subject, yNotifications.body, yNotifications.efrom, ["gestioci@cooperativa.cat",], ["ox@enredaos.net", "gestioci@cooperativa.cat", "aleph@riseup.net", "jaimesanchezm@gmail.com","gallardonegro@correo.nu"], connection=connection)
print "b"
oneemail.send()
except:
print "caso"
#oneemail = mail.EmailMessage("XXXXXXprueba con u", yNotifications.body, yNotifications.efrom, [u"jaimesanchezm@gmail.com",], [u"aleph@riseup.net",u"jaimesanchezm@gmail.com",u"gallardonegro@correo.nu"], connection=connection)
#Send
#oneemail.send()
print "paso"
#Close conection
output += "Email notificator. Subject: " + yNotifications.subject + " Sent to: " + str(yNotifications.get_notification_emails_list())
connection.close()
print output
return output
class PeriodCloseAutomaticClose( CronJobBase ):
RUN_EVERY_MINS = 0 # every 1 min
schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
code = 'PeriodCloseAutomaticClose' # a unique code
def do(self):
print "PeriodCloseAutomaticClose Cron is disabled by code"
return
#All periods already closed
qs_periods = period.objects.filter( date_close__lt=datetime.now() )
print "Periods already closed"
print qs_periods
for ob_period in qs_periods:
print "process "
print ob_period
#For each cooper
a = 0
for ob_cooper in Soci.objects.filter(user__is_superuser=False):
if not PeriodClose.objects.filter(period=ob_period, user=ob_cooper.user):
a = a + 1
print "cooper: "
print ob_cooper
print " needs automatic close! Procceding..."
ob_PeriodClose = PeriodClose(period=ob_period, user=ob_cooper.user)
qs_Sales = SalesInvoice.objects.filter(period=ob_period, user=ob_cooper.user)
sales_total = sales_invoicedVAT = sales_assignedVAT = sales_totalVAT = Decimal('0.00')
for item in qs_Sales.all():
sales_total += item.value
sales_invoicedVAT += item.invoicedVAT()
sales_assignedVAT += item.assignedVAT()
sales_totalVAT += item.total()
ob_PeriodClose.Sales_total = Decimal ( "%.2f" % sales_total )
ob_PeriodClose.Sales_invoicedVAT = Decimal ( "%.2f" % sales_invoicedVAT )
ob_PeriodClose.Sales_assignedVAT = Decimal ( "%.2f" % sales_assignedVAT )
ob_PeriodClose.Sales_totalVAT = Decimal ( "%.2f" % sales_totalVAT )
qs_Purchase = PurchaseInvoice.objects.filter(period=ob_period, user=ob_cooper.user)
purchases_total = purchases_expencedVAT = purchases_IRPFRetention = purchases_totalVAT = Decimal('0.00')
for item in qs_Purchase.all():
purchases_total += item.value
purchases_expencedVAT += item.expencedVAT()
purchases_IRPFRetention += item.IRPFRetention()
purchases_totalVAT += item.total()
ob_PeriodClose.Purchases_total = Decimal ( "%.2f" % purchases_total )
ob_PeriodClose.Purchases_expencedVAT = Decimal ( "%.2f" % purchases_expencedVAT )
ob_PeriodClose.Purchases_IRPFRetention = Decimal ( "%.2f" % purchases_IRPFRetention )
ob_PeriodClose.Purchases_totalVAT = Decimal ( "%.2f" % purchases_totalVAT )
#VATS
totalVAT1 = Decimal ( "%.2f" % (sales_invoicedVAT - purchases_expencedVAT) )
if totalVAT1 < 0:
totalVAT1 = 0
totalVAT2 = Decimal ( "%.2f" % (sales_assignedVAT - purchases_expencedVAT) )
if totalVAT2 < 0:
totalVAT2 = 0
ob_PeriodClose.VAT_1 = totalVAT1
ob_PeriodClose.VAT_2 = totalVAT2
#QUOTA
qs_Tax = periodTaxes.objects.filter(min_base__lte=sales_total, max_base__gte=sales_total)
value = Decimal('0.00')
if qs_Tax.count() == 1:
value = Decimal ( "%.2f" % qs_Tax[0].taxId )
else:
value = -1
ob_PeriodClose.periodTAX = value
ob_PeriodClose.preTAX = ob_cooper.preTAX
if value > -1:
print value
print ob_cooper.preTAX
value = value - ob_cooper.preTAX
if value > -1:
ob_PeriodClose.periodTAXeuro = value
else:
ob_PeriodClose.periodTAXeuro = 0
else:
ob_PeriodClose.periodTAXeuro = 0
#ob_PeriodClose.payment_entity.add(paymentEntities.objects.all())
print " Going to save..."
ob_PeriodClose.save()
print " saved!"
#execute and see log --> python manage.py runcrons
#see register created in http://localhost:8080/admin/django_cron/cronjoblog/ Code = EmailsNotifierCron