~camptocamp/banking-addons/bank-statement-reconcile-7.0-add-cancel-line-lep

« back to all changes in this revision

Viewing changes to account_statement_bankaccount_completion/statement.py

  • Committer: Laurent Mignon (Acsone)
  • Date: 2013-08-22 15:09:03 UTC
  • mto: This revision was merged to the branch mainline in revision 99.
  • Revision ID: laurent.mignon@acsone.eu-20130822150903-qhpq393xhutv6a1h
add completion logic based on the bank account number to link the partner to a statement line

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#
 
4
#    Author: Laurent Mignon
 
5
#    Copyright 2013 'ACSONE SA/NV'
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
#
 
21
 
 
22
 
 
23
from openerp.tools.translate import _
 
24
from openerp.osv.orm import Model
 
25
from openerp.osv import fields
 
26
from openerp.addons.account_statement_base_completion.statement import ErrorTooManyPartner
 
27
 
 
28
 
 
29
class AccountStatementCompletionRule(Model):
 
30
 
 
31
    """Add a rule based on transaction ID"""
 
32
 
 
33
    _inherit = "account.statement.completion.rule"
 
34
 
 
35
    def _get_functions(self, cr, uid, context=None):
 
36
        res = super(AccountStatementCompletionRule, self)._get_functions(
 
37
            cr, uid, context=context)
 
38
        res.append(('get_from_bank_account',
 
39
                    'From bank account number (Nomal or IBAN)'))
 
40
        return res
 
41
 
 
42
    _columns = {
 
43
        'function_to_call': fields.selection(_get_functions, 'Method'),
 
44
    }
 
45
 
 
46
    def get_from_bank_account(self, cr, uid, st_line, context=None):
 
47
        """
 
48
        Match the partner based on the partner account number field
 
49
        Then, call the generic st_line method to complete other values.
 
50
        In that case, we always fullfill the reference of the line with the SO name.
 
51
        :param dict st_line: read of the concerned account.bank.statement.line
 
52
        :return:
 
53
            A dict of value that can be passed directly to the write method of
 
54
            the statement line or {}
 
55
           {'partner_id': value,
 
56
            'account_id' : value,
 
57
            ...}
 
58
            """
 
59
        if st_line['partner_acc_number'] == False:
 
60
            return {}
 
61
        st_obj = self.pool.get('account.bank.statement.line')
 
62
        res = {}
 
63
        res_bank = self.pool.get('res.partner.bank')
 
64
        ids = res_bank.search(cr,
 
65
                              uid,
 
66
                              [('acc_number', '=', st_line['partner_acc_number'])],
 
67
                              context=context)
 
68
        if len(ids) > 1:
 
69
            raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by more than '
 
70
                                        'one partner.') % (st_line['name'], st_line['ref']))
 
71
        if len(ids) == 1:
 
72
            partner = self.pool.get('res.partner.bank').browse(cr, uid, ids[0], context=context).partner_id
 
73
            res['partner_id'] = partner.id
 
74
            st_vals = st_obj.get_values_for_line(cr,
 
75
                                                 uid,
 
76
                                                 profile_id=st_line['profile_id'],
 
77
                                                 master_account_id=st_line['master_account_id'],
 
78
                                                 partner_id=res.get('partner_id', False),
 
79
                                                 line_type=st_line['type'],
 
80
                                                 amount=st_line['amount'] if st_line['amount'] else 0.0,
 
81
                                                 context=context)
 
82
            res.update(st_vals)
 
83
        return res
 
84
 
 
85
 
 
86
class AccountStatementLine(Model):
 
87
    _inherit = "account.bank.statement.line"
 
88
 
 
89
    _columns = {
 
90
        # 'additionnal_bank_fields' : fields.serialized('Additionnal infos from bank', help="Used by completion and import system."),
 
91
        'partner_acc_number': fields.sparse(
 
92
            type='char',
 
93
            string='Account Number',
 
94
            size=64,
 
95
            serialization_field='additionnal_bank_fields',
 
96
            help="Account number of the partner"),
 
97
        'partner_bank_bic': fields.sparse(
 
98
            type='char',
 
99
            string='Bank Identifier Code',
 
100
            size=16,
 
101
            serialization_field='additionnal_bank_fields',
 
102
            help="Bank Identifier Code"),
 
103
    }