| 1 |
from odoo import _, api, fields, models |
| 2 |
|
| 3 |
|
| 4 |
class AccountTax(models.Model): |
| 5 |
_inherit = 'account.tax' |
| 6 |
|
| 7 |
l10n_es_applicability = fields.Selection( |
| 8 |
selection=[ |
| 9 |
('01', "VAT"), |
| 10 |
('02', "IPSI"), |
| 11 |
('03', "IGIC"), |
| 12 |
], |
| 13 |
string="Applicability (Spain)", |
| 14 |
) |
| 15 |
|
| 16 |
@api.model |
| 17 |
def _l10n_es_edi_verifactu_get_applicability_name_map(self): |
| 18 |
"""Return dict: l10n_es_applicability -> human readable string |
| 19 |
""" |
| 20 |
|
| 21 |
applicability_string = dict(self.env['account.tax']._fields['l10n_es_applicability'].get_description(self.env)['selection']) |
| 22 |
return { |
| 23 |
'01': applicability_string['01'], |
| 24 |
'02': applicability_string['02'], |
| 25 |
'03': applicability_string['03'], |
| 26 |
'05': _("Other"), |
| 27 |
} |
| 28 |
|
| 29 |
def _l10n_es_edi_verifactu_get_applicability(self): |
| 30 |
""" |
| 31 |
Return the Veri*Factu Tax Applicability for the "first" main tax in self. |
| 32 |
Fallback to '05' ("Other") if there is no main tax or the applicability is not set on the "first" one. |
| 33 |
Note: Currently we only support one Veri*Factu Tax Applicability for the whole invoice. |
| 34 |
""" |
| 35 |
main_tax_types = self._l10n_es_get_main_tax_types() |
| 36 |
main_taxes = self.filtered(lambda tax: tax.l10n_es_type in main_tax_types) |
| 37 |
if not main_taxes: |
| 38 |
return '05' |
| 39 |
return main_taxes[0].l10n_es_applicability or '05' |
| 40 |
|
| 41 |
def _l10n_es_edi_verifactu_get_suggested_clave_regimen(self, special_regime, forced_tax_applicability=None): |
| 42 |
""" |
| 43 |
Return a suggested Clave Regimen for the taxes in `self` to be used for the Veri*Factu document. |
| 44 |
Note: Currently we only support one Clave Regimen for a whole Veri*Factu document. |
| 45 |
""" |
| 46 |
taxes = self |
| 47 |
if forced_tax_applicability: |
| 48 |
|
| 49 |
main_tax_types = self._l10n_es_get_main_tax_types() |
| 50 |
taxes = taxes.filtered( |
| 51 |
lambda tax: (tax.l10n_es_type not in main_tax_types |
| 52 |
or tax._l10n_es_edi_verifactu_get_applicability() == forced_tax_applicability) |
| 53 |
) |
| 54 |
|
| 55 |
tax_applicability = forced_tax_applicability or taxes._l10n_es_edi_verifactu_get_applicability() |
| 56 |
if not tax_applicability: |
| 57 |
return False |
| 58 |
|
| 59 |
VAT = tax_applicability == '01' |
| 60 |
IGIC = tax_applicability == '03' |
| 61 |
if not (VAT or IGIC): |
| 62 |
return False |
| 63 |
|
| 64 |
recargo_taxes = taxes.filtered(lambda tax: tax.l10n_es_type == 'recargo') |
| 65 |
oss_tag = self.env.ref('l10n_eu_oss.tag_oss', raise_if_not_found=False) |
| 66 |
|
| 67 |
regimen_key = None |
| 68 |
if VAT and oss_tag and oss_tag.id in taxes.repartition_line_ids.tag_ids.ids: |
| 69 |
|
| 70 |
regimen_key = '17_iva' |
| 71 |
elif taxes.filtered(lambda tax: tax.l10n_es_type == 'exento' and tax.l10n_es_exempt_reason == 'E2'): |
| 72 |
|
| 73 |
regimen_key = '02' |
| 74 |
elif VAT and special_regime == 'simplified': |
| 75 |
|
| 76 |
regimen_key = '20_iva' |
| 77 |
elif VAT and special_regime == 'reagyp': |
| 78 |
|
| 79 |
regimen_key = '19_iva' |
| 80 |
elif VAT and (recargo_taxes or special_regime == 'recargo'): |
| 81 |
|
| 82 |
regimen_key = '18_iva' |
| 83 |
else: |
| 84 |
regimen_key = '01' |
| 85 |
|
| 86 |
return regimen_key |
| 87 |
|
| 88 |
@api.model |
| 89 |
def _l10n_es_edi_verifactu_get_tax_details_functions(self, company): |
| 90 |
def base_line_filter(base_line): |
| 91 |
return any(t != 'ignore' for t in base_line['tax_ids'].flatten_taxes_hierarchy().mapped('l10n_es_type')) |
| 92 |
|
| 93 |
def total_grouping_function(base_line, tax_data): |
| 94 |
return (tax_data |
| 95 |
and not tax_data['is_reverse_charge'] |
| 96 |
and tax_data['tax'].amount != -100.0 |
| 97 |
and tax_data['tax'].l10n_es_type not in ('ignore', 'retencion')) |
| 98 |
|
| 99 |
def tax_details_grouping_function(base_line, tax_data): |
| 100 |
if not total_grouping_function(base_line, tax_data): |
| 101 |
return None |
| 102 |
|
| 103 |
tax = tax_data['tax'] |
| 104 |
l10n_es_exempt_reason = tax.l10n_es_exempt_reason if tax.l10n_es_type == 'exento' else False |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
recargo_taxes = self.env['account.tax'] |
| 109 |
if tax.l10n_es_type in self.env['account.tax']._l10n_es_get_sujeto_tax_types(): |
| 110 |
recargo_taxes = base_line['tax_ids'].filtered(lambda t: t.l10n_es_type == 'recargo') |
| 111 |
|
| 112 |
grouping_key = { |
| 113 |
'amount': tax.amount, |
| 114 |
'recargo_taxes': recargo_taxes, |
| 115 |
'l10n_es_bien_inversion': tax.l10n_es_bien_inversion, |
| 116 |
'l10n_es_exempt_reason': l10n_es_exempt_reason, |
| 117 |
'l10n_es_type': tax.l10n_es_type, |
| 118 |
'l10n_es_applicability': tax._l10n_es_edi_verifactu_get_applicability(), |
| 119 |
} |
| 120 |
return grouping_key |
| 121 |
|
| 122 |
return { |
| 123 |
'base_line_filter': base_line_filter, |
| 124 |
'total_grouping_function': total_grouping_function, |
| 125 |
'tax_details_grouping_function': tax_details_grouping_function, |
| 126 |
} |