[IMP] account: add a sequence to account journals to allow reordering. This is used...
[odoo/odoo.git] / addons / account_bank_statement_extensions / account_bank_statement.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #
6 #    Copyright (c) 2011 Noviat nv/sa (www.noviat.be). All rights reserved.
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 from openerp.osv import fields, osv
25 import openerp.addons.decimal_precision as dp
26 from openerp.tools.translate import _
27
28 class account_bank_statement(osv.osv):
29     _inherit = 'account.bank.statement'
30
31     def write(self, cr, uid, ids, vals, context=None):
32         if context is None:
33             context = {}
34         # bypass obsolete statement line resequencing
35         if vals.get('line_ids', False) or context.get('ebanking_import', False):
36             res = super(osv.osv, self).write(cr, uid, ids, vals, context=context)
37         else:
38             res = super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
39         return res
40
41     def button_confirm_bank(self, cr, uid, ids, context=None):
42         bank_statement_line_obj = self.pool.get('account.bank.statement.line')
43         super(account_bank_statement, self).button_confirm_bank(cr, uid, ids, context=context)
44         for st in self.browse(cr, uid, ids, context=context):
45             if st.line_ids:
46                 line_ids = [l.id for l in st.line_ids]
47                 cr.execute("UPDATE account_bank_statement_line  \
48                     SET state='confirm' WHERE id in %s ",
49                     (tuple(line_ids),))
50                 bank_statement_line_obj.invalidate_cache(cr, uid, ['state'], line_ids, context=context)
51         return True
52
53     def button_cancel(self, cr, uid, ids, context=None):
54         bank_statement_line_obj = self.pool.get('account.bank.statement.line')
55         super(account_bank_statement, self).button_cancel(cr, uid, ids, context=context)
56         for st in self.browse(cr, uid, ids, context=context):
57             if st.line_ids:
58                 line_ids = [l.id for l in st.line_ids]
59                 cr.execute("UPDATE account_bank_statement_line  \
60                     SET state='draft' WHERE id in %s ",
61                     (tuple(line_ids),))
62                 bank_statement_line_obj.invalidate_cache(cr, uid, ['state'], line_ids, context=context)
63         return True
64
65
66 class account_bank_statement_line_global(osv.osv):
67     _name = 'account.bank.statement.line.global'
68     _description = 'Batch Payment Info'
69
70     _columns = {
71         'name': fields.char('OBI', required=True, help="Originator to Beneficiary Information"),
72         'code': fields.char('Code', size=64, required=True),
73         'parent_id': fields.many2one('account.bank.statement.line.global', 'Parent Code', ondelete='cascade'),
74         'child_ids': fields.one2many('account.bank.statement.line.global', 'parent_id', 'Child Codes', copy=True),
75         'type': fields.selection([
76             ('iso20022', 'ISO 20022'),
77             ('coda', 'CODA'),
78             ('manual', 'Manual'),
79             ], 'Type', required=True),
80         'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
81         'bank_statement_line_ids': fields.one2many('account.bank.statement.line', 'globalisation_id', 'Bank Statement Lines'),
82     }
83     _rec_name = 'code'
84     _defaults = {
85         'code': lambda s,c,u,ctx={}: s.pool.get('ir.sequence').next_by_code(c, u, 'account.bank.statement.line.global'),
86         'name': '/',
87     }
88     _sql_constraints = [
89         ('code_uniq', 'unique (code)', 'The code must be unique !'),
90     ]
91
92     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
93         if not args:
94             args = []
95         ids = []
96         if name:
97             ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit)
98             if not ids:
99                 ids = self.search(cr, user, [('name', operator, name)] + args, limit=limit)
100             if not ids and len(name.split()) >= 2:
101                 #Separating code and name for searching
102                 operand1, operand2 = name.split(' ', 1) #name can contain spaces
103                 ids = self.search(cr, user, [('code', 'like', operand1), ('name', operator, operand2)] + args, limit=limit)
104         else:
105             ids = self.search(cr, user, args, context=context, limit=limit)
106         return self.name_get(cr, user, ids, context=context)
107
108
109 class account_bank_statement_line(osv.osv):
110     _inherit = 'account.bank.statement.line'
111     _columns = {
112         'val_date': fields.date('Value Date', states={'confirm': [('readonly', True)]}),
113         'globalisation_id': fields.many2one('account.bank.statement.line.global', 'Globalisation ID',
114             states={'confirm': [('readonly', True)]},
115             help="Code to identify transactions belonging to the same globalisation level within a batch payment"),
116         'globalisation_amount': fields.related('globalisation_id', 'amount', type='float',
117             relation='account.bank.statement.line.global', string='Glob. Amount', readonly=True),
118         'state': fields.selection([('draft', 'Draft'), ('confirm', 'Confirmed')],
119             'Status', required=True, readonly=True, copy=False),
120         'counterparty_name': fields.char('Counterparty Name', size=35),
121         'counterparty_bic': fields.char('Counterparty BIC', size=11),
122         'counterparty_number': fields.char('Counterparty Number', size=34),
123         'counterparty_currency': fields.char('Counterparty Currency', size=3),
124     }
125     _defaults = {
126         'state': 'draft',
127     }
128
129     def unlink(self, cr, uid, ids, context=None):
130         if context is None:
131             context = {}
132         if context.get('block_statement_line_delete', False):
133             raise osv.except_osv(_('Warning!'), _('Delete operation not allowed. \
134             Please go to the associated bank statement in order to delete and/or modify bank statement line.'))
135         return super(account_bank_statement_line, self).unlink(cr, uid, ids, context=context)
136
137
138 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: