diff --git a/account_constraints/__init__.py b/account_constraints/__init__.py new file mode 100644 index 000000000..ef69cef93 --- /dev/null +++ b/account_constraints/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author Joel Grand-Guillaume. Copyright 2012 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import account_constraints \ No newline at end of file diff --git a/account_constraints/__openerp__.py b/account_constraints/__openerp__.py new file mode 100644 index 000000000..222b3e365 --- /dev/null +++ b/account_constraints/__openerp__.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author Joel Grand-Guillaume. Copyright 2012 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + 'name' : 'Account Constraints', + 'version' : '1.0', + 'depends' : [ + 'account', + ], + 'author' : 'Camptocamp', + 'license': 'AGPL-3', + 'category': 'Generic Modules/Accounting', + 'description': """ +Account Constraints +=================== + +Add constraints in the accounting module of OpenERP to avoid bad usage +by users that lead to corrupted datas. This is based on our experiences +and legal state of the art in other software. + +Summary of constraints are: + +* Add a constraint on account move: you cannot pickup a date that is not + in the fiscal year of the concerned period + +* For manual entries when multicurrency: + + a. Validation on the use of the 'Currency' and 'Currency Amount' + fields as it is possible to enter one without the other + b. Validation to prevent a Credit amount with a positive + 'Currency Amount', or a Debit with a negative 'Currency Amount' + +* Add a check on entries that user cannot provide a secondary currency + if the same than the company one. + + """, + 'website': 'http://www.camptocamp.com', + 'data': [], + 'installable': True, +} diff --git a/account_constraints/account_constraints.py b/account_constraints/account_constraints.py new file mode 100644 index 000000000..861005daa --- /dev/null +++ b/account_constraints/account_constraints.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Author Joel Grand-Guillaume. Copyright 2012 Camptocamp SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import fields, orm + + +class AccountMove(orm.Model): + _inherit = "account.move" + + def _check_fiscal_year(self, cr, uid, ids): + for move in self.browse(cr, uid, ids): + date_start = move.period_id.fiscalyear_id.date_start + date_stop = move.period_id.fiscalyear_id.date_stop + if not date_start <= move.date <= date_stop: + return False + return True + + _constraints = [ + (_check_fiscal_year, + 'You cannot create entries with date not in the ' + 'fiscal year of the chosen period', + ['line_id']), + ] + + +class AccountMoveLine(orm.Model): + _inherit = 'account.move.line' + + def _check_currency_and_amount(self, cr, uid, ids, context=None): + for l in self.browse(cr, uid, ids, context=context): + if ((l.currency_id and not l.amount_currency) or + (not l.currency_id and l.amount_currency)): + return False + return True + + def _check_currency_amount(self, cr, uid, ids, context=None): + for l in self.browse(cr, uid, ids, context=context): + if l.amount_currency: + if ((l.amount_currency > 0.0 and l.credit > 0.0) or + (l.amount_currency < 0.0 and l.debit > 0.0)): + return False + return True + + def _check_currency_company(self, cr, uid, ids, context=None): + for l in self.browse(cr, uid, ids, context=context): + if l.currency_id.id == l.company_id.currency_id.id: + return False + return True + + _constraints = [ + (_check_currency_and_amount, + "You cannot create journal items with a secondary currency " + "without recording both 'currency' and 'amount currency' field.", + ['currency_id','amount_currency'] + ), + (_check_currency_amount, + "The amount expressed in the secondary currency must be positive " + "when journal item are debit and negatif when journal item are " + "credit.", + ['amount_currency'] + ), + (_check_currency_company, + "You can't provide a secondary currency if " + "the same than the company one.", + ['currency_id'] + ), + ]