account_fiscal_position.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. SOUTH_SOUTHEAST = {"PR", "RS", "SC", "SP", "ES", "MG", "RJ"}
  5. NORTH_NORTHEAST_MIDWEST = {
  6. "AC", "AP", "AM", "PA", "RO", "RR", "TO", "AL", "BA", "CE",
  7. "MA", "PB", "PE", "PI", "RN", "SE", "DF", "GO", "MT", "MS"
  8. }
  9. class AccountFiscalPosition(models.Model):
  10. _inherit = 'account.fiscal.position'
  11. l10n_br_fp_type = fields.Selection(
  12. selection=[
  13. ('internal', 'Internal'),
  14. ('ss_nnm', 'South/Southeast selling to North/Northeast/Midwest'),
  15. ('interstate', 'Other interstate'),
  16. ],
  17. string='Interstate Fiscal Position Type',
  18. )
  19. @api.model
  20. def _get_fiscal_position(self, partner, delivery=None):
  21. if not delivery:
  22. delivery = partner
  23. if self.env.company.country_id.code != "BR" or delivery.country_id.code != 'BR':
  24. return super()._get_fiscal_position(partner, delivery=delivery)
  25. # manually set fiscal position on partner has a higher priority
  26. manual_fiscal_position = delivery.property_account_position_id or partner.property_account_position_id
  27. if manual_fiscal_position:
  28. return manual_fiscal_position
  29. # Taxation in Brazil depends on both the state of the partner and the state of the company
  30. if self.env.company.state_id == delivery.state_id:
  31. return self.search([('l10n_br_fp_type', '=', 'internal'), ('company_id', '=', self.env.company.id)], limit=1)
  32. if self.env.company.state_id.code in SOUTH_SOUTHEAST and delivery.state_id.code in NORTH_NORTHEAST_MIDWEST:
  33. return self.search([('l10n_br_fp_type', '=', 'ss_nnm'), ('company_id', '=', self.env.company.id)], limit=1)
  34. return self.search([('l10n_br_fp_type', '=', 'interstate'), ('company_id', '=', self.env.company.id)], limit=1)