Skip to content
Snippets Groups Projects
accounting.py 2.44 KiB
Newer Older
Sylvain's avatar
Sylvain committed
"""accounting views for fresh template

:organization: Logilab
:copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"


from logilab.mtconverter import html_escape

Sylvain Thénault's avatar
Sylvain Thénault committed
from cubicweb.selectors import implements
from cubicweb.view import EntityView
Sylvain's avatar
Sylvain committed


class ExpenseAccountingXmlView(EntityView):
    id = 'accexpense'
Sylvain Thénault's avatar
Sylvain Thénault committed
    __select__ = implements('Expense')

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

    def cell_call(self, row, col):
        entity = self.entity(row, col)
        self.wview('accentry', entity.related('has_lines'))


class ExpenseLineAccountingEntryXmlView(EntityView):
    id = 'accentry'
Sylvain Thénault's avatar
Sylvain Thénault committed
    __select__ = implements('ExpenseLine',)

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

Sylvain's avatar
Sylvain committed
    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.req.encoding)
        self.w(u'<ecritures>\n')
        for i in xrange(self.rset.rowcount):
            self.cell_call(i, 0)
        self.w(u'</ecritures>\n')
Sylvain Thénault's avatar
Sylvain Thénault committed

Sylvain's avatar
Sylvain committed
    def cell_call(self, row, col):
        entity = self.complete_entity(row, col)
        self.w(u'  <ecriture date="%s">\n' % entity.diem.strftime('%Y-%m-%d'))
        self.w(u'    <libelle>%s</libelle>\n' % html_escape(entity.dc_long_title()))
        amount = round(entity.euro_amount(), 2)
        taxes = round(entity.taxes, 2)
Sylvain's avatar
Sylvain committed
        self.w(u'    <credit compte="%s" montant="%.2f" />\n' % (
            entity.paid_by[0].account, amount))
        if entity.taxes:
            # XXX hardcoded account for VAT
            self.w(u'    <debit compte="44566" montant="%.2f" />\n' % entity.taxes)
        taxfree = int(round((amount - taxes) * 100))
Sylvain's avatar
Sylvain committed
        accounts = list(entity.paid_for)
        debit_quotient = taxfree / len(accounts)
        debit_remainder = taxfree % len(accounts)
Sylvain's avatar
Sylvain committed
        for account in accounts:
            if debit_remainder > 0:
                debit = (debit_quotient + 1) / 100.0
                debit_remainder -= 1
            else:
                debit = debit_quotient / 100.0
            self.w(u'    <debit compte="%s" montant="%.2f" />\n' %
                   (account.account, debit))
        if entity.workcase:
            self.w(u'    <groupe>%s</groupe>\n' % entity.workcase)
        self.w(u'  </ecriture>\n')