Skip to content

Commit

Permalink
[MIG] account_fiscal_position_constraints: Migration to 11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-demeyer committed Feb 27, 2019
1 parent 31a2ac5 commit ec1c0ac
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions account_fiscal_position_constraints/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png
:target: https://www.gnu.org/licenses/agpl
:alt: License: AGPL-3

===================================
Account Fiscal Position constraints
===================================

This module prohibits the removal of a fiscal position when this position
is already used in invoices or partner records.

The module also enforces fiscal position names to be unique per company.
1 change: 1 addition & 0 deletions account_fiscal_position_constraints/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions account_fiscal_position_constraints/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2009-2019 Noviat.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Account Fiscal Position constraints',
'version': '11.0.1.0.0',
'category': 'Accounting & Finance',
'website': 'https://www.noviat.com',
'author': 'Noviat',
'license': 'AGPL-3',
'installable': True,
'depends': [
'account',
],
}
1 change: 1 addition & 0 deletions account_fiscal_position_constraints/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_fiscal_position
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2009-2019 Noviat.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, _
from odoo.exceptions import UserError


class AccountFiscalPosition(models.Model):
_inherit = 'account.fiscal.position'

company_id = fields.Many2one(
required=True, default=lambda self: self.env.user.company_id)

_sql_constraints = [(
'name_company_uniq',
'unique (name, company_id)',
'The name of the fiscal position must be unique per company !')]

@api.one
@api.returns('self', lambda value: value.id)
def copy(self, default=None):
default = dict(default or {})
default.setdefault('name', _("%s (copy)") % (self.name or ''))
return super().copy(default)

@api.multi
def unlink(self):
for fpos in self:

partners = self.env['res.partner'].with_context(
active_test=False).search(
[('property_account_position_id', '=', fpos.id)])
if partners:
partner_list = [
'%s (ID:%s)' % (x.name, x.id) for x in partners]
raise UserError(_(
"You cannot delete a fiscal position that "
"has been set on partner records"
"\nAs an alterative, you can disable a "
"fiscal position via the 'active' flag."
"\n\nPartner records: %s") % partner_list)

invoices = self.env['account.invoice'].with_context(
active_test=False).search(
[('fiscal_position_id', '=', fpos.id)])
if invoices:
invoice_list = [
'%s (ID:%s)' % (x.number or 'n/a', x.id)
for x in invoices]
raise UserError(_(
"You cannot delete a fiscal position that "
"has been used on invoices"
"\nAs an alterative, you can disable a "
"fiscal position via the 'active' flag."
"\n\nInvoices: %s") % invoice_list)

return super().unlink()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ec1c0ac

Please sign in to comment.