diff --git a/account_statement_completion_label/__init__.py b/account_statement_completion_label/__init__.py new file mode 100644 index 00000000..9b4656e6 --- /dev/null +++ b/account_statement_completion_label/__init__.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# account_statement_completion_label for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com). All Rights Reserved +# @author Benoît GUILLOT +# +# 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 . import partner +from . import statement + diff --git a/account_statement_completion_label/__openerp__.py b/account_statement_completion_label/__openerp__.py new file mode 100644 index 00000000..96a29127 --- /dev/null +++ b/account_statement_completion_label/__openerp__.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# account_statement_completion_label for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com). All Rights Reserved +# @author Benoît GUILLOT +# +# 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': 'Bank statement completion from label', + 'version': '0.1', + 'category': 'Generic Modules/Others', + 'license': 'AGPL-3', + 'description': """ + Improve the basic rule "Match from statement line label (based on partner field 'Bank Statement Label')" provided by the + Bank statement base completion module. The goal is to match the label field from the bank statement line with a partner and + an account. + For this, you have to create your record in the new class account.statement.label where you can link the label you want with a + partner and an account. + + """, + 'author': 'Akretion', + 'website': 'http://www.akretion.com/', + 'depends': ['account_statement_base_completion'], + 'data': [ + 'partner_view.xml', + 'statement_view.xml', + 'security/ir.model.access.csv', + 'security/ir_rule.xml', + ], + 'demo': [], + 'installable': True, + 'active': False, +} + diff --git a/account_statement_completion_label/partner.py b/account_statement_completion_label/partner.py new file mode 100644 index 00000000..f2e85cb4 --- /dev/null +++ b/account_statement_completion_label/partner.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# account_statement_completion_label for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com). All Rights Reserved +# @author Benoît GUILLOT +# +# 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 res_partner(orm.Model): + _inherit = "res.partner" + + _columns = { + 'bank_statement_label': fields.one2many('account.statement.label', + 'partner_id', + 'Bank Statement Label'), + } diff --git a/account_statement_completion_label/partner_view.xml b/account_statement_completion_label/partner_view.xml new file mode 100644 index 00000000..9a7b35b3 --- /dev/null +++ b/account_statement_completion_label/partner_view.xml @@ -0,0 +1,33 @@ + + + + + + + account_bank_statement_import.view.partner.form + res.partner + form + 20 + + + + + + + + + + + + + + + + + + diff --git a/account_statement_completion_label/security/ir.model.access.csv b/account_statement_completion_label/security/ir.model.access.csv new file mode 100644 index 00000000..6a42a84a --- /dev/null +++ b/account_statement_completion_label/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_account_statement_label_user,account.statement.label,model_account_statement_label,account.group_account_user,1,0,0,0 +access_account_statement_label_manager,account.statement.label,model_account_statement_label,account.group_account_manager,1,1,1,1 diff --git a/account_statement_completion_label/security/ir_rule.xml b/account_statement_completion_label/security/ir_rule.xml new file mode 100644 index 00000000..205c4ef7 --- /dev/null +++ b/account_statement_completion_label/security/ir_rule.xml @@ -0,0 +1,14 @@ + + + + + + Multicompany label + + ['|',('company_id','=',False),('company_id','child_of',[user.company_id.id])] + + + + + + diff --git a/account_statement_completion_label/statement.py b/account_statement_completion_label/statement.py new file mode 100644 index 00000000..0d50ae56 --- /dev/null +++ b/account_statement_completion_label/statement.py @@ -0,0 +1,154 @@ +# -*- coding: utf-8 -*- +############################################################################### +# +# account_statement_completion_label for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com). All Rights Reserved +# @author Benoît GUILLOT +# +# 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 +from collections import defaultdict +from openerp.addons.account_statement_base_completion.statement import ErrorTooManyPartner + + +class ErrorTooManyLabel(Exception): + """ + New Exception definition that is raised when more than one label is matched + by the completion rule. + """ + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + + +class AccountBankSatement(orm.Model): + """ + We add a basic button and stuff to support the auto-completion + of the bank statement once line have been imported or manually fullfill. + """ + _inherit = "account.bank.statement" + + def open_completion_label(self, cr, uid, ids, context=None): + return { + 'name': 'Statement Label', + 'view_type': 'form', + 'view_mode': 'tree,form', + 'res_model': 'account.statement.label', + 'type': 'ir.actions.act_window', + 'nodestroy': True, + 'target': False, + } + + +class AccountStatementCompletionRule(orm.Model): + _inherit = "account.statement.completion.rule" + + def get_from_label_and_partner_field(self, cr, uid, st_line, context=None): + """ + Match the partner and the account based on the name field of the + statement line and the table account.statement.label. + If more than one statement label matched, raise the ErrorTooManylabel + error. + + :param int line_id: id of the concerned account.bank.statement.line + :return: + A dict of value that can be passed directly to the write method of + the statement line or {} + {'partner_id': value, + 'account_id': value, + + ...} + """ + st_obj = self.pool.get('account.bank.statement') + statement = st_obj.browse(cr, uid, st_line['statement_id'][0], + context=context) + res = {} + if not context.get('label_memorizer'): + context['label_memorizer'] = defaultdict(list) + for line in statement.line_ids: + cr.execute(""" + SELECT l.partner_id, + l.account_id + FROM account_statement_label as l, + account_bank_statement as s + LEFT JOIN + account_bank_statement_line as st_l + ON + st_l.statement_id = s.id + WHERE + st_l.name ~* l.label + AND + l.profile_id = s.profile_id + AND + st_l.id = %s + """, (line.id,)) + for partner, account in cr.fetchall(): + context['label_memorizer'][line.id].append({'partner_id': partner, + 'account_id': account}) + if st_line['id'] in context['label_memorizer']: + label_info = context['label_memorizer'][st_line['id']] + if len(label_info) > 1: + raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by ' + 'more than one statement label.') % + (st_line['name'], st_line['ref'])) + if label_info[0]['partner_id']: + res['partner_id'] = label_info[0]['partner_id'] + res['account_id'] = label_info[0]['account_id'] + return res + + +class AccountStatementLabel(orm.Model): + """Create a new class to map an account statement label to a partner + and a specific account + """ + _name = "account.statement.label" + + _description = "Account Statement Label" + + _columns = { + 'partner_id': fields.many2one('res.partner', 'Partner'), + 'label': fields.char('Bank Statement Label', size=100), + 'account_id': fields.many2one('account.account', 'Account', + required = True, + help='Account corresponding to the label ' + 'for a given partner'), + 'company_id': fields.related('account_id', 'company_id', + type='many2one', + relation='res.company', + string='Company', + store=True, + readonly=True), + 'profile_id': fields.many2one('account.statement.profile', + 'Account Profile'), + } + + _defaults = { + 'company_id': lambda s,cr,uid,c: + s.pool.get('res.company')._company_default_get(cr, uid, + 'account.statement.label', + context=c), + } + + _sql_constraints = [ + ('profile_label_unique', 'unique (label, profile_id, company_id)', + 'You cannot have similar label for the same profile and company'), + ] + + def save_and_close_label(self, cr, uid, ids, context=None): + return {'type': 'ir.actions.act_window_close'} diff --git a/account_statement_completion_label/statement_view.xml b/account_statement_completion_label/statement_view.xml new file mode 100644 index 00000000..d965433c --- /dev/null +++ b/account_statement_completion_label/statement_view.xml @@ -0,0 +1,79 @@ + + + + + + + + account_statement_completion_label.bank_statement.view_form + account.bank.statement + + + + +