/
models.py
247 lines (225 loc) · 13.8 KB
/
models.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
# -*- coding: utf-8 -*-
from os import system
from const.accountTypeChoices import *
from crm.models import Contract
from django.db import models
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext as _
from django.db.models import signals
from xml.dom.minidom import Document
from datetime import *
import settings
import djangoUserExtention
class AccountingCalculationUnit(models.Model):
title = models.CharField(max_length=200, verbose_name=_("Title")) # For example "Year 2009", "1st Quarter 2009"
begin = models.DateField(verbose_name=_("Begin"))
end = models.DateField(verbose_name=_("End"))
def createBalanceSheetPDF(self, raisedbyuser):
userExtention = djangoUserExtention.models.UserExtention.objects.filter(user=raisedbyuser.id)
out = open("/tmp/balancesheet_"+str(self.id)+".xml","w")
doc = Document()
main = doc.createElement("koalixaccountingbalacesheet")
calculationUnitName = doc.createElement("calculationUnitName")
calculationUnitName.appendChild(doc.createTextNode(self.__unicode__()))
main.appendChild(calculationUnitName)
organisiationname = doc.createElement("organisiationname")
organisiationname.appendChild(doc.createTextNode(settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.organisationname))
main.appendChild(organisiationname)
calculationUnitTo = doc.createElement("calculationUnitTo")
calculationUnitTo.appendChild(doc.createTextNode(self.end.year.__str__()))
main.appendChild(calculationUnitTo)
calculationUnitFrom = doc.createElement("calculationUnitFrom")
calculationUnitFrom.appendChild(doc.createTextNode(self.begin.year.__str__()))
main.appendChild(calculationUnitFrom)
calculationUnitName = doc.createElement("headerpicture")
calculationUnitName.appendChild(doc.createTextNode(settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.logo.path))
main.appendChild(calculationUnitName)
accountNumber = doc.createElement("AccountNumber")
accounts = Account.objects.all()
overallvalue = 0
for account in list(accounts) :
currentValue = account.valueNow(self)
if (currentValue != 0):
currentAccountElement = doc.createElement("Account")
accountNumber = doc.createElement("AccountNumber")
accountNumber.appendChild(doc.createTextNode(account.accountNumber.__str__()))
currentValueElement = doc.createElement("currentValue")
currentValueElement.appendChild(doc.createTextNode(currentValue.__str__()))
accountNameElement = doc.createElement("accountName")
accountNameElement.appendChild(doc.createTextNode(account.title))
currentAccountElement.setAttribute("accountType", account.accountType.__str__())
currentAccountElement.appendChild(accountNumber)
currentAccountElement.appendChild(accountNameElement)
currentAccountElement.appendChild(currentValueElement)
main.appendChild(currentAccountElement)
if account.accountType == "A":
overallvalue = overallvalue + currentValue;
if account.accountType == "L":
overallvalue = overallvalue - currentValue;
profitloss = doc.createElement("ProfitLoss")
profitloss.appendChild(doc.createTextNode(overallvalue.__str__()))
main.appendChild(profitloss)
doc.appendChild(main)
out.write(doc.toxml("utf-8"))
out.close()
log = open("/tmp/log.txt", "w")
log.write('bash -c "fop -c '+settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.fopConfigurationFile.path+' -xml /tmp/balancesheet_'+str(self.id)+'.xml -xsl ' + settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.balancesheetXSLFile.xslfile.path+' -pdf /tmp/balancesheet_'+str(self.id)+'.pdf"')
log.close()
system ('bash -c "fop -c '+settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.fopConfigurationFile.path+' -xml /tmp/balancesheet_'+str(self.id)+'.xml -xsl ' + settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.balancesheetXSLFile.xslfile.path+' -pdf /tmp/balancesheet_'+str(self.id)+'.pdf"')
return "/tmp/balancesheet_"+str(self.id)+".pdf"
def createProfitLossStatementPDF(self, raisedbyuser):
userExtention = djangoUserExtention.models.UserExtention.objects.filter(user=raisedbyuser.id)
out = open("/tmp/profitlossstatement_"+str(self.id)+".xml","w")
doc = Document()
main = doc.createElement("koalixaccountingprofitlossstatement")
calculationUnitName = doc.createElement("calculationUnitName")
calculationUnitName.appendChild(doc.createTextNode(self.__unicode__()))
main.appendChild(calculationUnitName)
organisiationname = doc.createElement("organisiationname")
organisiationname.appendChild(doc.createTextNode(settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.organisiationname))
main.appendChild(organisiationname)
calculationUnitTo = doc.createElement("calculationUnitTo")
calculationUnitTo.appendChild(doc.createTextNode(self.end.year.__str__()))
main.appendChild(calculationUnitTo)
calculationUnitFrom = doc.createElement("calculationUnitFrom")
calculationUnitFrom.appendChild(doc.createTextNode(self.begin.year.__str__()))
main.appendChild(calculationUnitFrom)
calculationUnitName = doc.createElement("headerpicture")
calculationUnitName.appendChild(doc.createTextNode(settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.logo.path))
main.appendChild(calculationUnitName)
accountNumber = doc.createElement("AccountNumber")
accounts = Account.objects.all()
overallvalue = 0
for account in list(accounts) :
currentValue = account.valueNow(self)
if (currentValue != 0):
currentAccountElement = doc.createElement("Account")
accountNumber = doc.createElement("AccountNumber")
accountNumber.appendChild(doc.createTextNode(account.accountNumber.__str__()))
currentValueElement = doc.createElement("currentValue")
currentValueElement.appendChild(doc.createTextNode(currentValue.__str__()))
accountNameElement = doc.createElement("accountName")
accountNameElement.appendChild(doc.createTextNode(account.title))
currentAccountElement.setAttribute("accountType", account.accountType.__str__())
currentAccountElement.appendChild(accountNumber)
currentAccountElement.appendChild(accountNameElement)
currentAccountElement.appendChild(currentValueElement)
main.appendChild(currentAccountElement)
if account.accountType == "E":
overallvalue = overallvalue + currentValue;
if account.accountType == "S":
overallvalue = overallvalue - currentValue;
profitloss = doc.createElement("ProfitLoss")
profitloss.appendChild(doc.createTextNode(overallvalue.__str__()))
main.appendChild(profitloss)
doc.appendChild(main)
out.write(doc.toxml("utf-8"))
out.close()
log = open("/tmp/log.txt", "w")
log.write('bash -c "fop -c '+settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.fopConfigurationFile.path+' -xml /tmp/profitlossstatement_'+str(self.id)+'.xml -xsl ' + settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.profitLossStatementXSLFile.xslfile.path+' -pdf /tmp/profitlossstatement_'+str(self.id)+'.pdf"')
log.close()
system('bash -c "fop -c '+settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.fopConfigurationFile.path+' -xml /tmp/profitlossstatement_'+str(self.id)+'.xml -xsl ' + settings.MEDIA_ROOT+userExtention[0].defaultTemplateSet.profitLossStatementXSLFile.xslfile.path+' -pdf /tmp/profitlossstatement_'+str(self.id)+'.pdf"')
return "/tmp/profitlossstatement_"+str(self.id)+".pdf"
def __unicode__(self):
return self.title
# TODO: def createNewCalculationUnit() Neues Geschäftsjahr erstellen
class Meta:
app_label = "accounting"
#app_label_koalix = _("Accounting")
verbose_name = _('Accounting Calculation Unit')
verbose_name_plural = _('Accounting Calculation Units')
class Account(models.Model):
accountNumber = models.IntegerField(verbose_name=_("Account Number"))
title = models.CharField(verbose_name=_("Account Title"), max_length=50)
accountType = models.CharField(verbose_name=_("Account Type"), max_length=1, choices=ACCOUNTTYPECHOICES)
isopenreliabilitiesaccount = models.BooleanField(verbose_name=_("Is The Open Liabilities Account"))
# TODO: There may only be one isopenreliabilitiesaccount and it must be an activa
isopeninterestaccount = models.BooleanField(verbose_name=_("Is The Open Interests Account"))
# TODO: There may only be one openinterestaccount and it must be an activa
isProductInventoryActiva = models.BooleanField(verbose_name=_("Is a Product Inventory Account"))
# TODO: This can only be set when accountType is Activa and can not be customerpaymentaccount
isACustomerPaymentAccount = models.BooleanField(verbose_name=_("Is a Customer Payment Account"))
# TODO: This can only be set when accountType is Activa and can not be product inventry as well
def value(self):
date.today()
AccountingCalculationUnit.objects.all()
sum = self.allBookings(fromAccount = False, accountingCalculationUnit = accountingCalculationUnit) - self.allBookings(fromAccount = True, accountingCalculationUnit = accountingCalculationUnit)
if (self.accountType == 'P' or self.accountType == 'E'):
sum = 0 - sum
return sum
def valueNow(self, accountingCalculationUnit):
sum = self.allBookings(fromAccount = False, accountingCalculationUnit = accountingCalculationUnit) - self.allBookings(fromAccount = True, accountingCalculationUnit = accountingCalculationUnit)
return sum
def allBookings(self, fromAccount, accountingCalculationUnit):
sum = 0
if (fromAccount == True):
bookings = Booking.objects.filter(fromAccount=self.id, accountingCalculationUnit=accountingCalculationUnit.id)
else:
bookings = Booking.objects.filter(toAccount=self.id, accountingCalculationUnit=accountingCalculationUnit.id)
for booking in list(bookings):
sum = sum + booking.amount
return sum
def __unicode__(self):
return self.accountNumber.__str__() + " " + self.title
class Meta:
app_label = "accounting"
#app_label_koalix = _("Accounting")
verbose_name = _('Account')
verbose_name_plural = _('Account')
ordering = ['accountNumber']
class ProductCategorie(models.Model):
title = models.CharField(verbose_name=_("Product Categorie Title"), max_length=50)
profitAccount = models.ForeignKey(Account, verbose_name=_("Profit Account"), limit_choices_to={"accountType" : "E"}, related_name="db_profit_account")
lossAccount = models.ForeignKey(Account, verbose_name=_("Loss Account"), limit_choices_to={"accountType" : "S"}, related_name="db_loss_account")
class Meta:
app_label = "accounting"
#app_label_koalix = _("Accounting")
verbose_name = _('Product Categorie')
verbose_name_plural = _('Product Categorie')
def __unicode__(self):
return self.title
class Booking(models.Model):
fromAccount = models.ForeignKey(Account, verbose_name=_("From Account"), related_name="db_booking_fromaccount")
toAccount = models.ForeignKey(Account, verbose_name=_("To Account"), related_name="db_booking_toaccount")
amount = models.DecimalField(max_digits=20, decimal_places=2, verbose_name=_("Amount"))
description = models.CharField(verbose_name=_("Description"), max_length=120, null=True, blank=True)
bookingReference = models.ForeignKey('crm.Invoice', verbose_name=_("Booking Reference"), null=True, blank=True)
bookingDate = models.DateTimeField(verbose_name = _("Booking at"))
accountingCalculationUnit = models.ForeignKey(AccountingCalculationUnit, verbose_name=_("AccountingCalculationUnit"))
staff = models.ForeignKey('auth.User', limit_choices_to={'is_staff': True}, blank=True, verbose_name = _("Reference Staff"), related_name="db_booking_refstaff")
dateofcreation = models.DateTimeField(verbose_name = _("Created at"), auto_now=True)
lastmodification = models.DateTimeField(verbose_name = _("Last modified"), auto_now_add=True)
lastmodifiedby = models.ForeignKey('auth.User', limit_choices_to={'is_staff': True}, blank=True, verbose_name = _("Last modified by"), related_name="db_booking_lstmodified")
def __unicode__(self):
return self.fromAccount.__str__() + " " + self.toAccount.__str__() + " " + self.amount.__str__()
class Meta:
app_label = "accounting"
#app_label_koalix = _("Accounting")
verbose_name = _('Booking')
verbose_name_plural = _('Bookings')
def preSaveCheckFlags(sender, instance, **kwarg):
if (instance.isopenreliabilitiesaccount):
openinterestaccounts = Account.objects.filter(isopenreliabilitiesaccount=True)
if (instance.accountType != "P" ):
instance.isopenreliabilitiesaccount = False
#TODO: Correct Action when not Passiva
elif openinterestaccounts:
instance.isopenreliabilitiesaccount = False
#TODO: Correct Action when there is already a isopenreliabilitiesaccount account
if (instance.isopeninterestaccount):
openinterestaccounts = Account.objects.filter(isopeninterestaccount=True)
if (instance.accountType != "A" ):
instance.isopeninterestaccount = False
#TODO: Correct Action when not Activa
elif openinterestaccounts:
instance.isopeninterestaccount = False
#TODO: Correct Action when there is already a isopenreliabilitiesaccount account
if (instance.isACustomerPaymentAccount):
if (instance.accountType != "A" ):
instance.isACustomerPaymentAccount = False
#TODO: Correct Action when not Activa
if (instance.isProductInventoryActiva):
if (instance.accountType != "A" ):
instance.isProductInventoryActiva = False
#TODO: Correct Action when not Activa
signals.pre_save.connect(preSaveCheckFlags, Account)