forked from aleph1888/calaCOOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.py
168 lines (134 loc) · 6.08 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
#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 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 = "[email protected]"
yNotifications.to_list = ["[email protected]", "[email protected]"]
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):
#Get all active notificators
EmailsToSend = EmailNotification.objects.filter(is_active=True)
connection = mail.get_connection()
connection.open()
for yNotifications in EmailsToSend.all():
#If execution date matches present date
if yNotifications.execution_date() is datetime.now():
oneemail = mail.EmailMessage(yNotifications.subject, yNotifications.body, yNotifications.efrom, yNotifications.get_notification_emails_list(), connection=connection)
oneemail.send()
connection.close()
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):
#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
print " Going to save..."
ob_PeriodClose.save()
print " saved!"
print a
#execute and see log --> python manage.py runcrons
#see register created in http://localhost:8080/admin/django_cron/cronjoblog/