Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][ADD] l10n_nl_xaf_auditfile_export: opening balance support #411

Merged
4 commits merged into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,46 @@ def get_taxes(self):
('company_id', '=', self.company_id.id),
])

@api.multi
def get_ob_totals(self):
"""return totals of opening balance"""
result = dict(
credit=0.0,
debit=0.0,
count=0
)
for line in self.get_ob_lines():
balance = line['balance']
if balance > 0:
result['debit'] += balance
else:
result['credit'] -= balance
result['count'] += 1
return result

@api.multi
def get_ob_lines(self):
"""return opening balance entries"""
self.env.cr.execute(
# pylint: disable=sql-injection
"select a.id, a.code, sum(l.balance) " +
"from account_move_line l, account_account a, "
" account_move m, account_account_type t "
"where a.user_type_id = t.id "
"and a.id = l.account_id and l.date < %s "
"and l.move_id = m.id and m.state = 'posted' "
"and l.company_id=%s "
"and t.include_initial_balance = true "
"group by a.id, a.code",
(self.date_start, self.company_id.id),
)
for result in self.env.cr.fetchall():
yield dict(
hbrunn marked this conversation as resolved.
Show resolved Hide resolved
account_id=result[0],
account_code=result[1],
balance=round(result[2], 2),
)

@api.multi
def get_move_line_count(self):
'''return amount of move lines'''
Expand Down
1 change: 1 addition & 0 deletions l10n_nl_xaf_auditfile_export/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Holger Brunn <[email protected]>
* Andrea Stirpe <[email protected]>
* Stefan Rijnhart <[email protected]>
* Tom Blauwendraat <[email protected]>
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import base64
from datetime import timedelta
from lxml import etree
from io import BytesIO
import os
from zipfile import ZipFile


from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger


def get_transaction_line_count_from_xml(auditfile):
''' Helper XML method to parse and return the transaction line count '''
line_count = 0
def xaf_xpath(auditfile, query):
with ZipFile(BytesIO(base64.b64decode(auditfile)), 'r') as z:
contents = z.read(z.filelist[-1]).decode()
parser = etree.XMLParser(
Expand All @@ -23,16 +23,29 @@
remove_blank_text=True
)
root = etree.XML(bytes(contents, encoding='utf8'), parser=parser)
# xpath query to select all element nodes in namespace
# Source: https://stackoverflow.com/a/30233635
query = "descendant-or-self::*[namespace-uri()!='']"
for element in root.xpath(query):
element.tag = etree.QName(element).localname
journals = root.xpath("/auditfile/company/transactions/journal")
for journal in journals:
transactions = journal.xpath("transaction/trLine")
for _ in transactions:
line_count += 1
for element in root.xpath(
query, namespaces={'a': 'http://www.auditfiles.nl/XAF/3.2'}
):
yield element


def get_transaction_line_count_from_xml(auditfile):
''' Helper XML method to parse and return the transaction line count '''
line_count = 0
# xpath query to select all element nodes in namespace
# Source: https://stackoverflow.com/a/30233635
query = "descendant-or-self::*[namespace-uri()!='']"
root = None
for element in xaf_xpath(auditfile, query):
element.tag = etree.QName(element).localname
root = root or element.getroottree()
if not root:
return 0

Check warning on line 43 in l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py

View check run for this annotation

Codecov / codecov/patch

l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py#L43

Added line #L43 was not covered by tests
journals = root.xpath("/auditfile/company/transactions/journal")
for journal in journals:
transactions = journal.xpath("transaction/trLine")
for _ in transactions:
line_count += 1
return line_count


Expand Down Expand Up @@ -146,3 +159,67 @@
line_count_after = record_after.get_move_line_count()
parsed_count_after = get_transaction_line_count_from_xml(record_after.auditfile)
self.assertTrue(parsed_line_count == parsed_count_after == line_count_after)

def test_07_opening_balance(self):
"""Test that we calculate the opening balance correctly"""
record = self.env['xaf.auditfile.export'].create({})

acc_receivable = self.env['account.account'].search([
(
'user_type_id', '=',
self.env.ref('account.data_account_type_receivable').id
),
], limit=1)
acc_payable = self.env['account.account'].search([
('user_type_id', '=', self.env.ref('account.data_account_type_payable').id),
], limit=1)
acc_revenue = self.env['account.account'].search([
('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id),
], limit=1)
journal = self.env['account.journal'].search([], limit=1)

move_receivable = self.env['account.move'].create({
'journal_id': journal.id,
'date': record.date_start - timedelta(days=1),
'line_ids': [
(0, 0, {'account_id': acc_receivable.id, 'credit': 42, 'debit': 0}),
(0, 0, {'account_id': acc_revenue.id, 'credit': 0, 'debit': 42}),
]
})
move_payable = self.env['account.move'].create({
'journal_id': journal.id,
'date': record.date_start - timedelta(days=1),
'line_ids': [
(0, 0, {'account_id': acc_payable.id, 'credit': 0, 'debit': 4242}),
(0, 0, {'account_id': acc_revenue.id, 'credit': 4242, 'debit': 0}),
]
})

move_receivable.post()
record.button_generate()

def xaf_val(auditfile, xpath):
return float(''.join(xaf_xpath(auditfile, xpath)))

total_credit = xaf_val(
record.auditfile, '//a:openingBalance/a:totalCredit/text()')
self.assertEqual(total_credit, 42)
total_debit = xaf_val(
record.auditfile, '//a:openingBalance/a:totalDebit/text()')
self.assertEqual(total_debit, 0)
lines_count = xaf_val(
record.auditfile, '//a:openingBalance/a:linesCount/text()')
self.assertEqual(lines_count, 1)

move_payable.post()
record = self.env['xaf.auditfile.export'].create({})
record.button_generate()
total_credit = xaf_val(
record.auditfile, '//a:openingBalance/a:totalCredit/text()')
self.assertEqual(total_credit, 42)
total_debit = xaf_val(
record.auditfile, '//a:openingBalance/a:totalDebit/text()')
self.assertEqual(total_debit, 4242)
lines_count = xaf_val(
record.auditfile, '//a:openingBalance/a:linesCount/text()')
self.assertEqual(lines_count, 2)
27 changes: 19 additions & 8 deletions l10n_nl_xaf_auditfile_export/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@
<glAccountHistory />
</ledgerAccount>
</generalLedger>
<!-- we might fill in those later
<openingBalance />
/-->
<vatCodes>
<vatCode t-foreach="self.get_taxes()" t-as="t">
<vatID><t t-esc="t.id" /></vatID>
Expand All @@ -119,10 +116,24 @@
<endDatePeriod><t t-esc="p.date_end" /></endDatePeriod>
</period>
</periods>
<openingBalance>
<t t-set="ob_totals" t-value="self.get_ob_totals()" />
<opBalDate><t t-esc="self.date_start" /></opBalDate>
<opBalDesc>Opening balance</opBalDesc>
<linesCount><t t-esc="ob_totals['count']" /></linesCount>
<totalDebit><t t-esc="round(ob_totals['debit'], 2)" /></totalDebit>
<totalCredit><t t-esc="round(ob_totals['credit'], 2)" /></totalCredit>
<obLine t-foreach="self.get_ob_lines()" t-as="l">
<nr><t t-esc="l['account_id']" /></nr>
<accID><t t-esc="l['account_code']" /></accID>
<amnt><t t-esc="round(abs(l['balance']), 2)" /></amnt>
<amntTp><t t-esc="'C' if l['balance'] &lt; 0 else 'D'" /></amntTp>
</obLine>
</openingBalance>
<transactions>
<linesCount><t t-esc="self.get_move_line_count()" /></linesCount>
<totalDebit><t t-esc="self.get_move_line_total_debit()" /></totalDebit>
<totalCredit><t t-esc="self.get_move_line_total_credit()" /></totalCredit>
<totalDebit><t t-esc="round(self.get_move_line_total_debit(), 2)" /></totalDebit>
<totalCredit><t t-esc="round(self.get_move_line_total_credit(), 2)" /></totalCredit>
<journal t-foreach="self.get_journals()" t-as="j">
<jrnID><t t-esc="j.code" /></jrnID>
<desc><t t-esc="j.name" /></desc>
Expand All @@ -136,7 +147,7 @@
<desc><t t-esc="m.name" /></desc>
<periodNumber><t t-esc="self.get_move_period_number(m)" /></periodNumber>
<trDt><t t-esc="m.date" /></trDt>
<amnt><t t-esc="m.amount" /></amnt>
<amnt><t t-esc="round(m.amount,2)" /></amnt>
<!-- left to inheriting modules
<sourceID />
<userID />
Expand All @@ -147,7 +158,7 @@
<docRef><t t-esc="l.ref and l.ref[:35] if self.unit4 == True else l.ref" t-options='{"widget": "auditfile.string999"}' /></docRef>
<effDate><t t-esc="l.date" /></effDate>
<desc><t t-esc="l.name" /></desc>
<amnt><t t-esc="abs(l.balance)" /></amnt>
<amnt><t t-esc="abs(round(l.balance,2))" /></amnt>
<amntTp><t t-esc="'C' if l.credit else 'D'" /></amntTp>
<recRef t-if="l.full_reconcile_id"><t t-esc="l.full_reconcile_id.name" /></recRef>
<custSupID t-if="l.partner_id"><t t-esc="l.partner_id.id" /></custSupID>
Expand All @@ -170,7 +181,7 @@
/-->
<currency t-if="l.currency_id and l.amount_currency">
<curCode><t t-esc="l.currency_id.name" /></curCode>
<curAmnt><t t-esc="l.amount_currency" /></curAmnt>
<curAmnt><t t-esc="round(l.amount_currency,2)" /></curAmnt>
</currency>
</trLine>
</transaction>
Expand Down
Loading