From b57981eccb1cffa0d86363153e2c1e9030d8b924 Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 8 Feb 2019 13:34:54 +0100 Subject: [PATCH] [FIX] Include past invoices in report View past invoices moves when country is not NL [FIX] Sort past invoices by date [FIX] Domain condition Update roadmap Fix _compute_is_invoice_basis() --- l10n_nl_tax_statement/__manifest__.py | 2 +- l10n_nl_tax_statement/models/account_tax.py | 5 +- .../models/l10n_nl_vat_statement.py | 38 +++++++++------ .../models/l10n_nl_vat_statement_line.py | 47 ++++++++++--------- l10n_nl_tax_statement/readme/ROADMAP.rst | 3 ++ 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/l10n_nl_tax_statement/__manifest__.py b/l10n_nl_tax_statement/__manifest__.py index 905436bc1..c1898145f 100644 --- a/l10n_nl_tax_statement/__manifest__.py +++ b/l10n_nl_tax_statement/__manifest__.py @@ -3,7 +3,7 @@ { 'name': 'Netherlands BTW Statement', - 'version': '11.0.2.1.0', + 'version': '11.0.2.1.1', 'category': 'Localization', 'license': 'AGPL-3', 'author': 'Onestein, Odoo Community Association (OCA)', diff --git a/l10n_nl_tax_statement/models/account_tax.py b/l10n_nl_tax_statement/models/account_tax.py index a3ba9b445..b84b01184 100644 --- a/l10n_nl_tax_statement/models/account_tax.py +++ b/l10n_nl_tax_statement/models/account_tax.py @@ -18,10 +18,11 @@ def get_move_line_partial_domain(self, from_date, to_date, company_id): if not self.env.context.get('skip_invoice_basis_domain'): return res - company = self.env['res.company'].browse(company_id) - if company.country_id != self.env.ref('base.nl'): + if not self.env.context.get('unreported_move'): return res + # Both 'skip_invoice_basis_domain' and 'unreported_move' must be set + # in context, in order to get the domain for the unreported invoices return expression.AND([ [('company_id', '=', company_id)], [('l10n_nl_vat_statement_id', '=', False)], diff --git a/l10n_nl_tax_statement/models/l10n_nl_vat_statement.py b/l10n_nl_tax_statement/models/l10n_nl_vat_statement.py index 0deb4fdef..7872ac4e7 100644 --- a/l10n_nl_tax_statement/models/l10n_nl_vat_statement.py +++ b/l10n_nl_tax_statement/models/l10n_nl_vat_statement.py @@ -1,4 +1,4 @@ -# Copyright 2017 Onestein () +# Copyright 2017-2019 Onestein () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from datetime import datetime @@ -79,8 +79,9 @@ class VatStatement(models.Model): def _compute_unreported_move_ids(self): for statement in self: domain = statement._get_unreported_move_domain() - move_line_ids = self.env['account.move.line'].search(domain) - statement.unreported_move_ids = move_line_ids.mapped('move_id') + move_lines = self.env['account.move.line'].search(domain) + moves = move_lines.mapped('move_id').sorted('date') + statement.unreported_move_ids = moves @api.multi def _get_unreported_move_domain(self): @@ -129,15 +130,17 @@ def _get_unreported_move_domain(self): ) unreported_move_from_date = fields.Date() - @api.multi def _compute_is_invoice_basis(self): - self.is_invoice_basis = False has_invoice_basis = self.env['ir.model.fields'].sudo().search_count([ ('model', '=', 'res.company'), ('name', '=', 'l10n_nl_tax_invoice_basis') ]) - if has_invoice_basis: - self.is_invoice_basis = self.company_id.l10n_nl_tax_invoice_basis + for statement in self: + if has_invoice_basis: + invoice_basis = statement.company_id.l10n_nl_tax_invoice_basis + statement.is_invoice_basis = invoice_basis + else: + statement.is_invoice_basis = False is_invoice_basis = fields.Boolean( string='NL Tax Invoice Basis', @@ -349,7 +352,8 @@ def statement_update(self): # calculate lines lines = self._prepare_lines() taxes = self._compute_taxes() - taxes |= self._compute_past_invoices_taxes() + self._set_statement_lines(lines, taxes) + taxes = self._compute_past_invoices_taxes() self._set_statement_lines(lines, taxes) self._finalize_lines(lines) @@ -369,15 +373,19 @@ def _compute_past_invoices_taxes(self): 'target_move': self.target_move, 'company_id': self.company_id.id, 'skip_invoice_basis_domain': True, + 'unreported_move': True, 'is_invoice_basis': self.is_invoice_basis, 'unreported_move_from_date': self.unreported_move_from_date } taxes = self.env['account.tax'].with_context(ctx) - for move in self.unreported_move_ids: - for move_line in move.line_ids: - if move_line.tax_exigible: - if move_line.tax_line_id: - taxes |= move_line.tax_line_id + moves_to_include = self.unreported_move_ids.filtered( + lambda m: m.l10n_nl_vat_statement_include) + for move_line in moves_to_include.mapped('line_ids'): + if move_line.tax_exigible: + if move_line.tax_line_id: + taxes |= move_line.tax_line_id + if move_line.tax_ids: + taxes |= move_line.tax_ids return taxes def _compute_taxes(self): @@ -432,7 +440,9 @@ def post(self): 'state': 'posted', 'date_posted': fields.Datetime.now() }) - self.unreported_move_ids.write({ + self.unreported_move_ids.filtered( + lambda m: m.l10n_nl_vat_statement_include + ).write({ 'l10n_nl_vat_statement_id': self.id, }) domain = [ diff --git a/l10n_nl_tax_statement/models/l10n_nl_vat_statement_line.py b/l10n_nl_tax_statement/models/l10n_nl_vat_statement_line.py index 86225df2e..1f19ddb14 100644 --- a/l10n_nl_tax_statement/models/l10n_nl_vat_statement_line.py +++ b/l10n_nl_tax_statement/models/l10n_nl_vat_statement_line.py @@ -118,46 +118,47 @@ def get_lines_action(self, tax_or_base='tax'): return vals def _get_move_lines_domain(self, tax_or_base): - if self.statement_id.state == 'draft': - domain = self._get_move_lines_domain_draft(tax_or_base) + statement = self.statement_id + taxes = self._filter_taxes_by_code(statement._compute_taxes()) + past_taxes = statement._compute_past_invoices_taxes() + past_taxes = self._filter_taxes_by_code(past_taxes) + if statement.state == 'draft': + domain = self._get_domain_draft(taxes, tax_or_base) + past_domain = self._get_domain_draft(past_taxes, tax_or_base) else: - domain = self._get_move_lines_domain_posted(tax_or_base) - return domain - - def _get_taxes_by_code(self): + domain = self._get_domain_posted(taxes, tax_or_base) + past_domain = self._get_domain_posted(past_taxes, tax_or_base) + curr_amls = self.env['account.move.line'].search(domain) + past_amls = self.env['account.move.line'].search(past_domain) + res = [('id', 'in', past_amls.ids + curr_amls.ids)] + return res + + def _filter_taxes_by_code(self, taxes): self.ensure_one() tags_map = self.statement_id._get_tags_map() filtered_taxes = self.env['account.tax'] - taxes = self.statement_id._compute_taxes() - taxes |= self.statement_id._compute_past_invoices_taxes() for tax in taxes: for tag in tax.tag_ids: tag_map = tags_map.get(tag.id) if tag_map and tag_map[0] == self.code: filtered_taxes |= tax - return filtered_taxes + return filtered_taxes.with_context(taxes.env.context) - def _get_move_lines_domain_draft(self, tax_or_base): + def _get_domain_draft(self, taxes, tax_or_base): self.ensure_one() - taxes = self._get_taxes_by_code() - statement = self.statement_id - ctx = { - 'from_date': statement.from_date, - 'to_date': statement.to_date, - 'target_move': statement.target_move, - 'company_id': statement.company_id.id, - 'l10n_nl_statement_tax_ids': taxes.ids, - } + ctx = taxes.env.context.copy() + ctx.update({ + 'l10n_nl_statement_tax_ids': taxes.ids + }) AccountTax = self.env['account.tax'].with_context(ctx) return AccountTax.get_move_lines_domain(tax_or_base=tax_or_base) - def _get_move_lines_domain_posted(self, tax_or_base): + def _get_domain_posted(self, taxes, tax_or_base): self.ensure_one() - taxes = self._get_taxes_by_code() statement = self.statement_id domain = [('move_id.l10n_nl_vat_statement_id', '=', statement.id)] if tax_or_base == 'tax': - tax_domain = [('tax_line_id', '=', taxes.ids)] + tax_domain = [('tax_line_id', 'in', taxes.ids)] else: - tax_domain = [('tax_ids', '=', taxes.ids)] + tax_domain = [('tax_ids', 'in', taxes.ids)] return expression.AND([domain, tax_domain]) diff --git a/l10n_nl_tax_statement/readme/ROADMAP.rst b/l10n_nl_tax_statement/readme/ROADMAP.rst index 5676e29de..39151cd0f 100644 --- a/l10n_nl_tax_statement/readme/ROADMAP.rst +++ b/l10n_nl_tax_statement/readme/ROADMAP.rst @@ -1 +1,4 @@ * Exporting in SBR/XBLR format not yet available +* Limit invoices to last 5 year based on fiscal year end date (legal requirement) +* When adding invoices automatically recalculate the tax statement (now a manual recalculate is necessary) +* The unreported from date is calculate as 1 quarter, it should take 1 fiscal year based on fiscal year end date