Permalink
Browse files

imporved interface to the fop pdf creator. now having error messages …

…that were displayed on the admin page when pdf creation fails. currently available for accounting only
  • Loading branch information...
1 parent a944ada commit 71fc1da962efd8f223e1ff1455add94ee15306b1 @scaphilo committed Jan 1, 2013
Showing with 55 additions and 75 deletions.
  1. +5 −4 accounting/admin.py
  2. +3 −56 accounting/models.py
  3. +31 −6 accounting/views.py
  4. +16 −9 templatefiles/en/profitlossstatement.xsl
View
@@ -3,6 +3,7 @@
from django.core.urlresolvers import reverse
from crm.models import *
from accounting.models import *
+from accounting.views import *
from django.utils.translation import ugettext as _
from django.contrib import admin
from django.http import HttpResponse
@@ -112,19 +113,19 @@ def save_formset(self, request, form, formset, change):
instance.staff = request.user
instance.save()
- def createBalanceSheetPDF(self, request, queryset):
+ def createBalanceSheet(self, request, queryset):
for obj in queryset:
- response = HttpResponseRedirect('/export/balancesheet/'+str(obj.id))
+ response = createBalanceSheetPDF(self, request, obj.id)
return response
createBalanceSheetPDF.short_description = _("Create PDF of Balance Sheet")
def createProfitLossStatement(self, request, queryset):
for obj in queryset:
- response = HttpResponseRedirect('/export/profitlossstatement/'+str(obj.id))
+ response = createProfitLossStatementPDF(self, request, obj.id)
return response
createProfitLossStatement.short_description = _("Create PDF of Profit Loss Statement Sheet")
- actions = ['createBalanceSheetPDF', 'createProfitLossStatement']
+ actions = ['createBalanceSheet', 'createProfitLossStatement']
class OptionProductCategorie(admin.ModelAdmin):
list_display = ('title', 'profitAccount', 'lossAccount')
View
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-from os import system
+from subprocess import *
from const.accountTypeChoices import *
from crm.models import Contract
from crm.exceptions import TemplateSetMissing
@@ -29,54 +29,6 @@ def getCurrentValidAccountingPeriod():
if currentValidAccountingPeriod == None:
raise NoFeasableAccountingPeriodFound()
- def createPDF(self, deliveryorder):
- XMLSerializer = serializers.get_serializer("xml")
- xml_serializer = XMLSerializer()
- out = open(settings.PDF_OUTPUT_ROOT+"invoice_"+str(self.id)+".xml", "w")
- objectsToSerialize = list(Invoice.objects.filter(id=self.id))
- objectsToSerialize += list(SalesContract.objects.filter(id=self.id))
- objectsToSerialize += list(Contact.objects.filter(id=self.customer.id))
- objectsToSerialize += list(Currency.objects.filter(id=self.currency.id))
- objectsToSerialize += list(SalesContractPosition.objects.filter(contract=self.id))
- for position in list(SalesContractPosition.objects.filter(contract=self.id)):
- objectsToSerialize += list(Position.objects.filter(id=position.id))
- objectsToSerialize += list(Product.objects.filter(id=position.product.id))
- objectsToSerialize += list(Unit.objects.filter(id=position.unit.id))
- objectsToSerialize += list(auth.models.User.objects.filter(id=self.staff.id))
- userExtension = djangoUserExtension.models.UserExtension.objects.filter(user=self.staff.id)
- if (len(userExtension) == 0):
- raise UserExtensionMissing(_("During Invoice PDF Export"))
- phoneAddress = djangoUserExtension.models.UserExtensionPhoneAddress.objects.filter(userExtension=userExtension[0].id)
- objectsToSerialize += list(userExtension)
- objectsToSerialize += list(PhoneAddress.objects.filter(id=phoneAddress[0].id))
- templateset = djangoUserExtension.models.TemplateSet.objects.filter(id=userExtension[0].defaultTemplateSet.id)
- if (len(templateset) == 0):
- raise TemplateSetMissing(_("During Invoice PDF Export"))
- objectsToSerialize += list(templateset)
- objectsToSerialize += list(auth.models.User.objects.filter(id=self.lastmodifiedby.id))
- objectsToSerialize += list(PostalAddressForContact.objects.filter(person=self.customer.id))
- for address in list(PostalAddressForContact.objects.filter(person=self.customer.id)):
- objectsToSerialize += list(PostalAddress.objects.filter(id=address.id))
- xml_serializer.serialize(objectsToSerialize, stream=out, indent=3)
- out.close()
- xml = etree.parse(settings.PDF_OUTPUT_ROOT+"invoice_"+str(self.id)+".xml")
- rootelement = xml.getroot()
- projectroot = etree.SubElement(rootelement, "projectroot")
- projectroot.text = settings.PROJECT_ROOT
- xml.write(settings.PDF_OUTPUT_ROOT+"invoice_"+str(self.id)+".xml")
- if (deliveryorder == False):
- log = open(settings.PDF_OUTPUT_ROOT+"log.txt", "w")
- log.write('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.invoiceXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.pdf"')
- log.close()
- system('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.invoiceXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.pdf"')
- return settings.PDF_OUTPUT_ROOT+"invoice_"+str(self.id)+".pdf"
- else:
- log = open(settings.PDF_OUTPUT_ROOT+"log.txt", "w")
- log.write('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.deilveryorderXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'deliveryorder_'+str(self.id)+'.pdf"')
- log.close()
- system('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'invoice_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.deilveryorderXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'deliveryorder_'+str(self.id)+'.pdf"')
- return settings.PDF_OUTPUT_ROOT+"deliveryorder_"+str(self.id)+".pdf"
-
def createBalanceSheetPDF(self, raisedbyuser):
userExtension = djangoUserExtension.models.UserExtension.objects.filter(user=raisedbyuser.id)
if (len(userExtension) == 0):
@@ -128,9 +80,7 @@ def createBalanceSheetPDF(self, raisedbyuser):
out.write(doc.toxml("utf-8"))
out.close()
log = open(settings.PDF_OUTPUT_ROOT+"log.txt", "w")
- log.write('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.balancesheetXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.pdf"')
- log.close()
- system ('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.balancesheetXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.pdf"')
+ check_output(['/usr/bin/fop', '-c', userExtension[0].defaultTemplateSet.fopConfigurationFile.path, '-xml', settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.xml', '-xsl', userExtension[0].defaultTemplateSet.balancesheetXSLFile.xslfile.path, '-pdf', settings.PDF_OUTPUT_ROOT+'balancesheet_'+str(self.id)+'.pdf'], stderr=STDOUT)
return settings.PDF_OUTPUT_ROOT+"balancesheet_"+str(self.id)+".pdf"
def createProfitLossStatementPDF(self, raisedbyuser):
@@ -183,10 +133,7 @@ def createProfitLossStatementPDF(self, raisedbyuser):
doc.appendChild(main)
out.write(doc.toxml("utf-8"))
out.close()
- log = open(settings.PDF_OUTPUT_ROOT+"log.txt", "w")
- log.write('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.profitLossStatementXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.pdf"')
- log.close()
- system ('bash -c "fop -c '+userExtension[0].defaultTemplateSet.fopConfigurationFile.path+' -xml '+settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.xml -xsl ' + userExtension[0].defaultTemplateSet.profitLossStatementXSLFile.xslfile.path+' -pdf '+settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.pdf"')
+ check_output(['/usr/bin/fop', '-c', userExtension[0].defaultTemplateSet.fopConfigurationFile.path, '-xml', settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.xml', '-xsl', userExtension[0].defaultTemplateSet.profitLossStatementXSLFile.xslfile.path, '-pdf', settings.PDF_OUTPUT_ROOT+'profitlossstatement_'+str(self.id)+'.pdf'], stderr=STDOUT)
return settings.PDF_OUTPUT_ROOT+"profitlossstatement_"+str(self.id)+".pdf"
def __unicode__(self):
View
@@ -4,23 +4,48 @@
from django.http import HttpResponse
from django.core.servers.basehttp import FileWrapper
from django.http import Http404
+from django.http import HttpResponse
+from django.http import HttpResponseRedirect
+from subprocess import *
-def createBalanceSheetPDF(request, calculationunitid):
+def createBalanceSheetPDF(modelAdmin, request, calculationunitid):
try:
accountingPeriod = AccountingPeriod.objects.get(id=calculationunitid)
pdf = accountingPeriod.createBalanceSheetPDF(request.user)
response = HttpResponse(FileWrapper(file(pdf)), mimetype='application/pdf')
response['Content-Length'] = path.getsize(pdf)
return response
- except (TemplateSetMissing, UserExtensionMissing), e:
- raise Http404
+ except (TemplateSetMissing, UserExtensionMissing, CalledProcessError), e:
+ if type(e) == UserExtensionMissing:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, _("User Extension Missing"))
+ elif type(e) == TemplateSetMissing:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, _("Templateset Missing"))
+ elif type(e) ==CalledProcessError:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, e.output)
+ else:
+ raise Http404
+ return response
-def createProfitLossStatementPDF(request, calculationunitid):
+def createProfitLossStatementPDF(modelAdmin, request, calculationunitid):
try:
accountingPeriod = AccountingPeriod.objects.get(id=calculationunitid)
pdf = accountingPeriod.createProfitLossStatementPDF(request.user)
response = HttpResponse(FileWrapper(file(pdf)), mimetype='application/pdf')
response['Content-Length'] = path.getsize(pdf)
return response
- except (TemplateSetMissing, UserExtensionMissing), e:
- raise Http404
+ except (TemplateSetMissing, UserExtensionMissing, CalledProcessError), e:
+ if type(e) == UserExtensionMissing:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, _("User Extension Missing"))
+ elif type(e) == TemplateSetMissing:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, _("Templateset Missing"))
+ elif type(e) ==CalledProcessError:
+ response = HttpResponseRedirect('/admin/accounting/accountingperiod/')
+ modelAdmin.message_user(request, e.output)
+ else:
+ raise Http404
+ return response
@@ -42,7 +42,7 @@
color="black"
text-align="left"
font-weight="bold">
- Profit / Loss Statement of the <xsl:value-of select="organisationname"/>
+ Erfolgsrechnung der koalix Riedener
</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after" >
@@ -67,7 +67,7 @@
<fo:block font-size="8pt"
font-family="BitstreamVeraSans"
font-weight="bold"
- text-align="end">Page <fo:page-number/>/<fo:page-number-citation ref-id="last-page"/></fo:block>
+ text-align="end">Seite <fo:page-number/>/<fo:page-number-citation ref-id="last-page"/></fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
@@ -78,25 +78,25 @@
<fo:block font-size="9pt"
font-family="BitstreamVeraSans"
text-align="left"
- line-height="13pt" >Earnings</fo:block>
+ line-height="13pt" >Ertrag</fo:block>
<fo:table table-layout="fixed" width="100%">
<fo:table-column column-width="3cm"/>
<fo:table-column column-width="11cm"/>
<fo:table-column column-width="3cm"/>
<fo:table-header font-size="9pt" line-height="9pt" font-weight="bold" font-family="BitstreamVeraSans">
<fo:table-cell border-color="black" border-style="solid" border-width="0.5pt" padding="2.5pt">
<fo:block text-align="start" >
- Account Number
+ Kontonummer
</fo:block>
</fo:table-cell>
<fo:table-cell border-color="black" border-style="solid" border-width="0.5pt" padding="2.5pt">
<fo:block text-align="start" >
- Account
+ Konto
</fo:block>
</fo:table-cell>
<fo:table-cell border-color="black" border-style="solid" border-width="0.5pt" padding="2.5pt">
<fo:block text-align="end" >
- Value
+ Wert
</fo:block>
</fo:table-cell>
</fo:table-header>
@@ -133,13 +133,12 @@
font-family="BitstreamVeraSans"
text-align="left"
line-height="13pt"
- padding-top="0.7cm">Spendings</fo:block>
+ padding-top="0.7cm">Aufwand</fo:block>
<fo:table table-layout="fixed" width="100%">
<fo:table-column column-width="3cm"/>
<fo:table-column column-width="11cm"/>
<fo:table-column column-width="3cm"/>
- <fo:table-body font-size="9pt"
- font-family="BitstreamVeraSans">
+ <fo:table-header font-size="9pt" line-height="9pt" font-weight="bold" font-family="BitstreamVeraSans">
<fo:table-cell border-color="black" border-style="solid" border-width="0.5pt" padding="2.5pt">
<fo:block text-align="start" font-size="9pt" line-height="9pt" font-weight="bold" font-family="BitstreamVeraSans" >
Kontonummer
@@ -155,6 +154,12 @@
Wert
</fo:block>
</fo:table-cell>
+ </fo:table-header>
+ <xsl:choose>
+ <xsl:when test="Account[accountType='S']/None">-</xsl:when>
+ <xsl:otherwise>
+ <fo:table-body font-size="9pt"
+ font-family="BitstreamVeraSans">
<xsl:for-each select="Account[@accountType='S']">
<xsl:sort select="AccountNumber" data-type="number"/>
<fo:table-row keep-together="always">
@@ -176,6 +181,8 @@
</fo:table-row>
</xsl:for-each>
</fo:table-body>
+ </xsl:otherwise>
+ </xsl:choose>
</fo:table>
<fo:block font-size="9pt"
font-family="BitstreamVeraSans"

0 comments on commit 71fc1da

Please sign in to comment.