diff --git a/account_financial_report/report/vat_report.py b/account_financial_report/report/vat_report.py index da215819..0c32585e 100644 --- a/account_financial_report/report/vat_report.py +++ b/account_financial_report/report/vat_report.py @@ -10,8 +10,11 @@ class VATReport(models.AbstractModel): _name = 'report.account_financial_report.vat_report' _description = "VAT Report" - def _get_tax_data(self, tax_ids): - taxes = self.env['account.tax'].browse(tax_ids) + def _get_tax_data(self, tax_ids, hide_tax_at_zero): + domain = [] + if hide_tax_at_zero: + domain += [('id', 'in', tax_ids)] + taxes = self.env['account.tax'].search(domain) tax_data = {} for tax in taxes: tax_data.update({ @@ -50,7 +53,7 @@ class VATReport(models.AbstractModel): return domain def _get_vat_report_data(self, company_id, date_from, - date_to, only_posted_moves): + date_to, only_posted_moves, hide_tax_at_zero): tax_domain = self._get_tax_report_domain( company_id, date_from, date_to, only_posted_moves ) @@ -85,7 +88,7 @@ class VATReport(models.AbstractModel): }) tax_ids = list(map(operator.itemgetter('tax_line_id'), vat_data)) tax_ids = list(set(tax_ids)) - tax_data = self._get_tax_data(tax_ids) + tax_data = self._get_tax_data(tax_ids, hide_tax_at_zero) return vat_data, tax_data def _get_tax_group_data(self, tax_group_ids): @@ -101,48 +104,68 @@ class VATReport(models.AbstractModel): }) return tax_group_data - def _get_vat_report_group_data(self, vat_report_data, tax_data, tax_detail): + @api.model + def _initialize_vat_report( + self, tax_id, tag_or_group_id, vat_report, tax_data + ): + if tag_or_group_id not in vat_report.keys(): + vat_report[tag_or_group_id] = {} + vat_report[tag_or_group_id]['net'] = 0.0 + vat_report[tag_or_group_id]['tax'] = 0.0 + vat_report[tag_or_group_id][tax_id] = dict(tax_data[tax_id]) + vat_report[tag_or_group_id][tax_id].update( + {'net': 0.0, 'tax': 0.0} + ) + else: + if tax_id not in vat_report[tag_or_group_id].keys(): + vat_report[tag_or_group_id][tax_id] = dict(tax_data[tax_id]) + vat_report[tag_or_group_id][tax_id].update( + {'net': 0.0, 'tax': 0.0} + ) + return vat_report + + def _get_vat_report_group_data(self, vat_report_data, tax_data, + tax_detail, hide_tax_at_zero): vat_report = {} + if not hide_tax_at_zero: + for tax_id in tax_data.keys(): + if tax_data[tax_id]['amount_type'] == 'group': + continue + tax_group_id = tax_data[tax_id]['tax_group_id'] + vat_report = self._initialize_vat_report( + tax_id, tax_group_id, vat_report, tax_data + ) for tax_move_line in vat_report_data: tax_id = tax_move_line['tax_line_id'] if tax_data[tax_id]['amount_type'] == 'group': - pass - else: - tax_group_id = tax_data[tax_id]['tax_group_id'] - if tax_group_id not in vat_report.keys(): - vat_report[tax_group_id] = {} - vat_report[tax_group_id]['net'] = 0.0 - vat_report[tax_group_id]['tax'] = 0.0 - vat_report[tax_group_id][tax_id] = dict(tax_data[tax_id]) - vat_report[tax_group_id][tax_id].update( - {'net': 0.0, 'tax': 0.0} - ) - else: - if tax_id not in vat_report[tax_group_id].keys(): - vat_report[tax_group_id][tax_id] = dict(tax_data[ - tax_id]) - vat_report[tax_group_id][tax_id].update( - {'net': 0.0, 'tax': 0.0} - ) - vat_report[tax_group_id]['net'] += tax_move_line['net'] - vat_report[tax_group_id]['tax'] += tax_move_line['tax'] - vat_report[tax_group_id][tax_id]['net'] += tax_move_line['net'] - vat_report[tax_group_id][tax_id]['tax'] += tax_move_line['tax'] + continue + tax_group_id = tax_data[tax_id]['tax_group_id'] + vat_report = self._initialize_vat_report( + tax_id, tax_group_id, vat_report, tax_data + ) + vat_report[tax_group_id]['net'] += tax_move_line['net'] + vat_report[tax_group_id]['tax'] += tax_move_line['tax'] + vat_report[tax_group_id][tax_id]['net'] += tax_move_line['net'] + vat_report[tax_group_id][tax_id]['tax'] += tax_move_line['tax'] tax_group_data = self._get_tax_group_data(vat_report.keys()) vat_report_list = [] for tax_group_id in vat_report.keys(): - vat_report[tax_group_id]['name'] = tax_group_data[ - tax_group_id]['name'] - vat_report[tax_group_id]['code'] = tax_group_data[ - tax_group_id]['code'] + vat_report_group = {} + vat_report_group.update({tax_group_id: { + 'name': tax_group_data[tax_group_id]['name'], + 'code': tax_group_data[tax_group_id]['code'], + 'net': vat_report[tax_group_id]['net'], + 'tax': vat_report[tax_group_id]['tax'], + } + }) if tax_detail: - vat_report[tax_group_id]['taxes'] = [] + vat_report_group[tax_group_id]['taxes'] = [] for tax_id in vat_report[tax_group_id]: if isinstance(tax_id, int): - vat_report[tax_group_id]['taxes'].append( + vat_report_group[tax_group_id]['taxes'].append( vat_report[tax_group_id][tax_id] ) - vat_report_list.append(vat_report[tax_group_id]) + vat_report_list.append(vat_report_group[tax_group_id]) return vat_report_list def _get_tags_data(self, tags_ids): @@ -155,8 +178,20 @@ class VATReport(models.AbstractModel): }) return tags_data - def _get_vat_report_tag_data(self, vat_report_data, tax_data, tax_detail): + def _get_vat_report_tag_data( + self, vat_report_data, tax_data, tax_detail, hide_tax_at_zero + ): vat_report = {} + if not hide_tax_at_zero: + for tax_id in tax_data.keys(): + if tax_data[tax_id]['amount_type'] == 'group': + continue + tags_ids = tax_data[tax_id]['tags_ids'] + if tags_ids: + for tag_id in tags_ids: + vat_report = self._initialize_vat_report( + tax_id, tag_id, vat_report, tax_data + ) for tax_move_line in vat_report_data: tax_id = tax_move_line['tax_line_id'] if tax_data[tax_id]['amount_type'] == 'group': @@ -165,21 +200,9 @@ class VATReport(models.AbstractModel): tags_ids = tax_data[tax_id]['tags_ids'] if tags_ids: for tag_id in tags_ids: - if tag_id not in vat_report.keys(): - vat_report[tag_id] = {} - vat_report[tag_id]['net'] = 0.0 - vat_report[tag_id]['tax'] = 0.0 - vat_report[tag_id][tax_id] = dict(tax_data[tax_id]) - vat_report[tag_id][tax_id].update( - {'net': 0.0, 'tax': 0.0} - ) - else: - if tax_id not in vat_report[tag_id].keys(): - vat_report[tag_id][tax_id] = dict(tax_data[ - tax_id]) - vat_report[tag_id][tax_id].update( - {'net': 0.0, 'tax': 0.0} - ) + vat_report = self._initialize_vat_report( + tax_id, tag_id, vat_report, tax_data + ) vat_report[tag_id][tax_id]['net'] += tax_move_line['net'] vat_report[tag_id][tax_id]['tax'] += tax_move_line['tax'] vat_report[tag_id]['net'] += tax_move_line['net'] @@ -187,16 +210,22 @@ class VATReport(models.AbstractModel): tags_data = self._get_tags_data(vat_report.keys()) vat_report_list = [] for tag_id in vat_report.keys(): - vat_report[tag_id]['name'] = tags_data[tag_id]['name'] - vat_report[tag_id]['code'] = tags_data[tag_id]['code'] + vat_report_tag = {} + vat_report_tag.update({tag_id: { + 'name': tags_data[tag_id]['name'], + 'code': tags_data[tag_id]['code'], + 'net': vat_report[tag_id]['net'], + 'tax': vat_report[tag_id]['tax'], + } + }) if tax_detail: - vat_report[tag_id]['taxes'] = [] + vat_report_tag[tag_id]['taxes'] = [] for tax_id in vat_report[tag_id]: if isinstance(tax_id, int): - vat_report[tag_id]['taxes'].append( + vat_report_tag[tag_id]['taxes'].append( vat_report[tag_id][tax_id] ) - vat_report_list.append(vat_report[tag_id]) + vat_report_list.append(vat_report_tag[tag_id]) return vat_report_list @api.multi @@ -209,15 +238,18 @@ class VATReport(models.AbstractModel): based_on = data['based_on'] tax_detail = data['tax_detail'] only_posted_moves = data['only_posted_moves'] + hide_tax_at_zero = data['hide_tax_at_zero'] vat_report_data, tax_data = self._get_vat_report_data( - company_id, date_from, date_to, only_posted_moves + company_id, date_from, date_to, only_posted_moves, hide_tax_at_zero ) if based_on == 'taxgroups': vat_report = self._get_vat_report_group_data( - vat_report_data, tax_data, tax_detail) + vat_report_data, tax_data, tax_detail, hide_tax_at_zero + ) else: vat_report = self._get_vat_report_tag_data( - vat_report_data, tax_data, tax_detail) + vat_report_data, tax_data, tax_detail, hide_tax_at_zero + ) return { 'doc_ids': [wizard_id], 'doc_model': 'open.items.report.wizard', diff --git a/account_financial_report/wizard/vat_report_wizard.py b/account_financial_report/wizard/vat_report_wizard.py index 58aa6e58..4b8bcc32 100644 --- a/account_financial_report/wizard/vat_report_wizard.py +++ b/account_financial_report/wizard/vat_report_wizard.py @@ -32,6 +32,11 @@ class VATReportWizard(models.TransientModel): string='Target Moves', required=True, default='posted') + hide_tax_at_zero = fields.Boolean( + string='Hide taxes with balance at 0', + default=True, + help='Use this filter to hide taxes with a balance at 0.' + ) @api.onchange('company_id') def onchange_company_id(self): @@ -106,6 +111,7 @@ class VATReportWizard(models.TransientModel): 'based_on': self.based_on, 'only_posted_moves': self.target_move == 'posted', 'tax_detail': self.tax_detail, + 'hide_tax_at_zero': self.hide_tax_at_zero, } def _export(self, report_type): diff --git a/account_financial_report/wizard/vat_report_wizard_view.xml b/account_financial_report/wizard/vat_report_wizard_view.xml index fe660e90..1476b176 100755 --- a/account_financial_report/wizard/vat_report_wizard_view.xml +++ b/account_financial_report/wizard/vat_report_wizard_view.xml @@ -19,6 +19,7 @@ +