[fix] point of sale: problem when installing the addon
[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 osv import osv, fields
25 import decimal_precision as dp
26 import netsvc
27 from tools.translate import _
28
29 class account_bank_statement(osv.osv):
30     _inherit = 'account.bank.statement'
31
32     def write(self, cr, uid, ids, vals, context=None):
33         if context is None:
34             context = {}
35         # bypass obsolete statement line resequencing
36         if vals.get('line_ids', False) or context.get('ebanking_import', False):
37             res = super(osv.osv, self).write(cr, uid, ids, vals, context=context)
38         else: 
39             res = super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
40         return res
41
42     def button_confirm_bank(self, cr, uid, ids, context=None):
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             cr.execute("UPDATE account_bank_statement_line  \
46                 SET state='confirm' WHERE id in %s ",
47                 (tuple([x.id for x in st.line_ids]),))
48         return True
49
50     def button_cancel(self, cr, uid, ids, context=None):
51         super(account_bank_statement, self).button_cancel(cr, uid, ids, context=context)
52         for st in self.browse(cr, uid, ids, context=context):
53             if st.line_ids:
54                 cr.execute("UPDATE account_bank_statement_line  \
55                     SET state='draft' WHERE id in %s ",
56                     (tuple([x.id for x in st.line_ids]),))
57         return True
58
59 account_bank_statement()
60
61 class account_bank_statement_line_global(osv.osv):
62     _name = 'account.bank.statement.line.global'
63     _description = 'Batch Payment Info'
64
65     _columns = {
66         'name': fields.char('Communication', size=128, required=True),
67         'code': fields.char('Code', size=64, required=True),
68         'parent_id': fields.many2one('account.bank.statement.line.global', 'Parent Code', ondelete='cascade'),
69         'child_ids': fields.one2many('account.bank.statement.line.global', 'parent_id', 'Child Codes'),
70         'type': fields.selection([
71             ('iso20022', 'ISO 20022'),
72             ('coda', 'CODA'),
73             ('manual', 'Manual'), 
74             ], 'Type', required=True),
75         'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
76         'bank_statement_line_ids': fields.one2many('account.bank.statement.line', 'globalisation_id', 'Bank Statement Lines'),
77     }
78     _rec_name = 'code'
79     _defaults = {
80         'code': lambda s,c,u,ctx={}: s.pool.get('ir.sequence').get(c, u, 'account.bank.statement.line.global'),
81         'name': '/',
82     }
83     _sql_constraints = [
84         ('code_uniq', 'unique (code)', 'The code must be unique !'),
85     ]
86
87     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
88         if not args:
89             args = []
90         ids = []
91         if name:
92             ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit)
93             if not ids:
94                 ids = self.search(cr, user, [('name', operator, name)] + args, limit=limit)
95             if not ids and len(name.split()) >= 2:
96                 #Separating code and name for searching
97                 operand1, operand2 = name.split(' ', 1) #name can contain spaces
98                 ids = self.search(cr, user, [('code', 'like', operand1), ('name', operator, operand2)] + args, limit=limit)
99         else:
100             ids = self.search(cr, user, args, context=context, limit=limit)
101         return self.name_get(cr, user, ids, context=context)
102
103 account_bank_statement_line_global()
104
105 class account_bank_statement_line(osv.osv):
106     _inherit = 'account.bank.statement.line'
107     _columns = {
108         'date': fields.date('Entry Date', required=True, states={'confirm': [('readonly', True)]}),
109         'val_date': fields.date('Valuta Date', states={'confirm': [('readonly', True)]}),
110         'globalisation_id': fields.many2one('account.bank.statement.line.global', 'Globalisation ID',
111             states={'confirm': [('readonly', True)]}, 
112             help="Code to identify transactions belonging to the same globalisation level within a batch payment"),
113         'globalisation_amount': fields.related('globalisation_id', 'amount', type='float',
114             relation='account.bank.statement.line.global', string='Glob. Amount', readonly=True),
115         'journal_id': fields.related('statement_id', 'journal_id', type='many2one', relation='account.journal', string='Journal', store=True, readonly=True),
116         'state': fields.selection([('draft', 'Draft'), ('confirm', 'Confirmed')],
117             'State', required=True, readonly=True),    
118         'counterparty_name': fields.char('Counterparty Name', size=35),
119         'counterparty_bic': fields.char('Counterparty BIC', size=11),
120         'counterparty_number': fields.char('Counterparty Number', size=34),
121         'counterparty_currency': fields.char('Counterparty Currency', size=3),
122     }
123     _defaults = {
124         'state': 'draft',
125     }
126
127     def unlink(self, cr, uid, ids, context=None):
128         if context is None:
129             context = {}
130         if context.get('block_statement_line_delete', False):
131             raise osv.except_osv(_('Warning'), _('Delete operation not allowed ! \
132             Please go to the associated bank statement in order to delete and/or modify this bank statement line'))
133         return super(account_bank_statement_line, self).unlink(cr, uid, ids, context=context)
134
135 account_bank_statement_line()
136
137 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: