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

[16.0] l10n_nl_xaf_auditfile_export: correct opening balance, rounding #413

Merged
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
26 changes: 9 additions & 17 deletions l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,23 +304,15 @@ def get_taxes(self):

def get_ob_totals(self):
"""return totals of opening balance"""
self.env.cr.execute(
"select sum(l.credit), sum(l.debit), count(distinct a.id) "
"from account_move_line l, account_account a "
"where l.account_id = a.id "
"and l.parent_state = 'posted' "
"and l.display_type NOT IN ('line_section', 'line_note') "
"and l.date < %s "
"and l.company_id=%s "
"and a.include_initial_balance = true ",
(self.date_start, self.company_id.id),
)
row = self.env.cr.fetchall()[0]
return dict(
credit=round(row[0] or 0.0, 2),
debit=round(row[1] or 0.0, 2),
count=row[2] or 0,
)
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

def get_ob_lines(self):
"""return opening balance entries"""
Expand Down
132 changes: 119 additions & 13 deletions l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

Expand All @@ -13,25 +14,36 @@
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(
ns_clean=True, recover=True, encoding="utf-8", 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 41 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#L41

Added line #L41 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 @@ -215,3 +227,97 @@
self.assertTrue(record)
self.assertTrue(record.name)
self.assertFalse(record.auditfile_success)

def test_09_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(
[
(
"account_type",
"=",
"asset_receivable",
),
("company_id", "=", self.env.company.id),
],
limit=1,
)
acc_payable = self.env["account.account"].search(
[
("account_type", "=", "liability_payable"),
("company_id", "=", self.env.company.id),
],
limit=1,
)
acc_revenue = self.env["account.account"].search(
[
("account_type", "=", "income"),
("company_id", "=", self.env.company.id),
],
limit=1,
)
journal = self.env["account.journal"].search(
[
("company_id", "=", self.env.company.id),
],
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.action_post()
self.env.flush_all()
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.action_post()
record = self.env["xaf.auditfile.export"].create({})
self.env.flush_all()
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)
14 changes: 9 additions & 5 deletions l10n_nl_xaf_auditfile_export/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,23 @@
<opBalDate><t t-esc="self.date_start" /></opBalDate>
<opBalDesc>Opening balance</opBalDesc>
<linesCount><t t-esc="ob_totals['count']" /></linesCount>
<totalDebit><t t-esc="ob_totals['debit']" /></totalDebit>
<totalCredit><t t-esc="ob_totals['credit']" /></totalCredit>
<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="abs(l['balance'])" /></amnt>
<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 Down
Loading