accounting.py 3.34 KB
Newer Older
Sylvain's avatar
Sylvain committed
1
2
3
"""accounting views for fresh template

:organization: Logilab
Nicolas Chauvat's avatar
Nicolas Chauvat committed
4
:copyright: 2008-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Sylvain's avatar
Sylvain committed
5
6
7
8
9
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"


Sylvain Thénault's avatar
Sylvain Thénault committed
10
from logilab.mtconverter import xml_escape
Sylvain's avatar
Sylvain committed
11

Nsukami Patrick's avatar
Nsukami Patrick committed
12
13
from cubicweb import _
from cubicweb.predicates import is_instance
Sylvain Thénault's avatar
Sylvain Thénault committed
14
from cubicweb.view import EntityView
Sylvain's avatar
Sylvain committed
15
16
17


class ExpenseAccountingXmlView(EntityView):
Sylvain Thénault's avatar
Sylvain Thénault committed
18
    __regid__ = 'accexpense'
Sylvain Thénault's avatar
Sylvain Thénault committed
19
    __select__ = is_instance('Expense')
Sylvain Thénault's avatar
Sylvain Thénault committed
20

Sylvain's avatar
Sylvain committed
21
22
23
24
    title = _('accounting entry view')
    templatable = False
    content_type = 'text/xml'

25
26
27
28
    def call(self):
        """display a list of entities by calling their <item_vid> view
        """
        self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self._cw.encoding)
Sylvain Thénault's avatar
Sylvain Thénault committed
29
30
        self.w(u'<?xml-stylesheet href="%saccounting-entries.xsl" '
               u'rel="stylesheet" type="text/xsl"?>\n' % self._cw.datadir_url)
31
        self.w(u'<ecritures>\n')
32
        for i in range(self.cw_rset.rowcount):
33
34
35
            self.cell_call(i, 0)
        self.w(u'</ecritures>\n')

Sylvain's avatar
Sylvain committed
36
    def cell_call(self, row, col):
Sylvain Thénault's avatar
Sylvain Thénault committed
37
        entity = self.cw_rset.get_entity(row, col)
38
        rset = entity.related('has_lines')
39
        for i in range(len(rset)):
40
            self.wview('accentry', rset, row=i, col=0)
Sylvain's avatar
Sylvain committed
41
42
43


class ExpenseLineAccountingEntryXmlView(EntityView):
Sylvain Thénault's avatar
Sylvain Thénault committed
44
    __regid__ = 'accentry'
Sylvain Thénault's avatar
Sylvain Thénault committed
45
    __select__ = is_instance('ExpenseLine',)
Sylvain Thénault's avatar
Sylvain Thénault committed
46

Sylvain's avatar
Sylvain committed
47
48
49
    title = _('accounting entry view')
    templatable = False
    content_type = 'text/xml'
Sylvain Thénault's avatar
Sylvain Thénault committed
50

Sylvain's avatar
Sylvain committed
51
52
53
    def call(self):
        """display a list of entities by calling their <item_vid> view
        """
Sylvain Thénault's avatar
Sylvain Thénault committed
54
        self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self._cw.encoding)
55
        self.w(u'<?xml-stylesheet href="%saccounting-entries.xsl" '
Sylvain Thénault's avatar
Sylvain Thénault committed
56
               u'rel="stylesheet" type="text/xsl"?>\n' % self._cw.datadir_url)
Sylvain's avatar
Sylvain committed
57
        self.w(u'<ecritures>\n')
58
        for i in range(self.cw_rset.rowcount):
Sylvain's avatar
Sylvain committed
59
60
            self.cell_call(i, 0)
        self.w(u'</ecritures>\n')
Sylvain Thénault's avatar
Sylvain Thénault committed
61

Sylvain's avatar
Sylvain committed
62
    def cell_call(self, row, col):
Sylvain Thénault's avatar
Sylvain Thénault committed
63
        entity = self.cw_rset.complete_entity(row, col)
Sylvain's avatar
Sylvain committed
64
        self.w(u'  <ecriture date="%s">\n' % entity.diem.strftime('%Y-%m-%d'))
Sylvain Thénault's avatar
Sylvain Thénault committed
65
        self.w(u'    <libelle>%s</libelle>\n' % xml_escape(entity.dc_long_title()))
66
67
        amount = round(entity.euro_amount(), 2)
        taxes = round(entity.taxes, 2)
68
69
70
        account = entity.paid_by[0].account and xml_escape(entity.paid_by[0].account) or u''
        self.w(u'    <credit compte="%s" montant="%.2f" />\n'
               % ( account, amount))
Sylvain's avatar
Sylvain committed
71
72
73
        if entity.taxes:
            # XXX hardcoded account for VAT
            self.w(u'    <debit compte="44566" montant="%.2f" />\n' % entity.taxes)
74
        taxfree = int(round((amount - taxes) * 100))
Sylvain's avatar
Sylvain committed
75
        accounts = list(entity.paid_for)
76
77
        debit_quotient = taxfree / len(accounts)
        debit_remainder = taxfree % len(accounts)
Sylvain's avatar
Sylvain committed
78
79
80
81
82
83
        for account in accounts:
            if debit_remainder > 0:
                debit = (debit_quotient + 1) / 100.0
                debit_remainder -= 1
            else:
                debit = debit_quotient / 100.0
84
85
86
            account = account.account and xml_escape(account.account) or u''
            self.w(u'    <debit compte="%s" montant="%.2f" />\n'
                   % (account, debit))
Sylvain's avatar
Sylvain committed
87
        if entity.workcase:
88
            self.w(u'    <groupe>%s</groupe>\n' % xml_escape(entity.workcase))
Sylvain's avatar
Sylvain committed
89
90
        self.w(u'  </ecriture>\n')