1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 MSF, TeMPO Consulting.
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.
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.
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/>.
20
##############################################################################
22
from osv import fields, osv
24
class account_analytic_journal(osv.osv):
25
_name = 'account.analytic.journal'
26
_inherit = 'account.analytic.journal'
28
def _get_current_instance(self, cr, uid, ids, name, args, context=None):
30
Get True if the journal was created by this instance.
31
NOT TO BE SYNCHRONIZED!!!
34
current_instance_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.instance_id.id
35
for journal in self.browse(cr, uid, ids, context=context):
36
res[journal.id] = (current_instance_id == journal.instance_id.id)
40
'name': fields.char('Journal Name', size=64, required=True, translate=True),
41
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
42
'is_current_instance': fields.function(_get_current_instance, type='boolean', method=True, readonly=True, store=True, string="Current Instance", help="Is this journal from my instance?")
46
'instance_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.instance_id.id,
49
def _check_engagement_count(self, cr, uid, ids, context=None):
51
Check that no more than one engagement journal exists for one instance
55
instance_ids = self.pool.get('msf.instance').search(cr, uid, [], context=context)
56
for instance_id in instance_ids:
57
eng_ids = self.search(cr, uid, [('type', '=', 'engagement'), ('instance_id', '=', instance_id)])
58
if len(eng_ids) and len(eng_ids) > 1:
63
(_check_engagement_count, 'You cannot have more than one engagement journal per instance!', ['type', 'instance_id']),
66
account_analytic_journal()
68
class account_journal(osv.osv):
69
_name = 'account.journal'
70
_inherit = 'account.journal'
72
def _get_current_instance(self, cr, uid, ids, name, args, context=None):
74
Get True if the journal was created by this instance.
75
NOT TO BE SYNCHRONIZED!!!
78
current_instance_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.instance_id.id
79
for journal in self.browse(cr, uid, ids, context=context):
80
res[journal.id] = (current_instance_id == journal.instance_id.id)
84
'name': fields.char('Journal Name', size=64, required=True, translate=True),
85
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
86
'is_current_instance': fields.function(_get_current_instance, type='boolean', method=True, readonly=True, store=True, string="Current Instance", help="Is this journal from my instance?")
90
'instance_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.instance_id.id,
94
('code_company_uniq', 'unique (code, company_id, instance_id)', 'The code of the journal must be unique per company and instance !'),
95
('name_company_uniq', 'unique (name, company_id, instance_id)', 'The name of the journal must be unique per company and instance !'),
98
# SP-72: in order to always get an analytic journal with the same instance,
99
# the create and write check and replace with the "good" journal if necessary.
100
def create(self, cr, uid, vals, context=None):
101
analytic_obj = self.pool.get('account.analytic.journal')
102
if vals.get('type') and vals.get('type') not in ['situation', 'stock'] and vals.get('analytic_journal_id'):
103
analytic_journal = analytic_obj.browse(cr, uid, vals['analytic_journal_id'], context=context)
106
if 'instance_id' in vals:
107
instance_id = vals['instance_id']
109
instance_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.instance_id.id
111
if analytic_journal and \
112
analytic_journal.name and \
113
analytic_journal.instance_id and \
114
analytic_journal.instance_id.id != instance_id:
115
# replace the journal with the one with the same name, and the wanted instance
116
new_journal_ids = analytic_obj.search(cr, uid, [('name','=', analytic_journal.name),
117
('instance_id','=',instance_id)], context=context)
118
if len(new_journal_ids) > 0:
119
vals['analytic_journal_id'] = new_journal_ids[0]
120
return super(account_journal, self).create(cr, uid, vals, context=context)
122
def write(self, cr, uid, ids, vals, context=None):
123
analytic_obj = self.pool.get('account.analytic.journal')
124
if vals.get('type') and vals.get('type') not in ['situation', 'stock'] and vals.get('analytic_journal_id'):
125
analytic_journal = analytic_obj.browse(cr, uid, vals['analytic_journal_id'], context=context)
128
if 'instance_id' in vals:
129
instance_id = vals['instance_id']
131
instance_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.instance_id.id
133
if analytic_journal and \
134
analytic_journal.name and \
135
analytic_journal.instance_id and \
136
analytic_journal.instance_id.id != instance_id:
137
# replace the journal with the one with the same name, and the wanted instance
138
new_journal_ids = analytic_obj.search(cr, uid, [('name','=', analytic_journal.name),
139
('instance_id','=',instance_id)], context=context)
140
if len(new_journal_ids) > 0:
141
vals['analytic_journal_id'] = new_journal_ids[0]
142
return super(account_journal, self).write(cr, uid, ids, vals, context=context)
146
class account_analytic_journal_fake(osv.osv):
147
""" Workaround class used in account.analytic.line search view, because context is lost in m2o search view """
148
_inherit = 'account.analytic.journal'
149
_name = 'account.analytic.journal.fake'
150
_table = 'account_analytic_journal'
152
def name_get(self, cr, uid, ids, context=None):
157
for journal in self.read(cr, uid, ids, ['code', 'instance_id']):
158
ret.append((journal['id'], '%s / %s'%(journal['instance_id'] and journal['instance_id'][1] or '', journal['code'])))
162
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
163
return self.pool.get('account.analytic.journal').fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
165
account_analytic_journal_fake()
167
class account_journal_fake(osv.osv):
168
""" Workaround class used in account.move search view, because context is lost in m2o search view """
170
_inherit = 'account.journal'
171
_name = 'account.journal.fake'
172
_table = 'account_journal'
174
def name_get(self, cr, uid, ids, context=None):
179
for journal in self.read(cr, uid, ids, ['code', 'instance_id']):
180
ret.append((journal['id'], '%s / %s'%(journal['instance_id'] and journal['instance_id'][1] or '', journal['code'])))
184
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
185
return self.pool.get('account.journal').fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
187
account_journal_fake()
189
def _get_journal_id_fake(self, cr, uid, ids, field_name, args, context=None):
193
for i in self.read(cr, uid, ids, ['journal_id']):
194
res[i['id']] = i['journal_id']
197
def _search_journal_id_fake(self, cr, uid, obj, name, args, context=None):
200
if arg[0] == 'journal_id_fake':
201
res.append(('journal_id', arg[1], arg[2]))
206
class account_analytic_line(osv.osv):
207
_name = 'account.analytic.line'
208
_inherit = 'account.analytic.line'
211
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
212
'journal_id_fake': fields.function(_get_journal_id_fake, method=True, string='Journal', type='many2one', relation='account.analytic.journal.fake', fnct_search=_search_journal_id_fake)
215
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
219
dom = [('instance_id', '=', instance_id)]
220
if journal_id and not self.pool.get('account.analytic.journal').search(cr, uid, [('id', '=', journal_id), ('instance_id', '=', instance_id)]):
221
value['journal_id_fake'] = False
223
return {'domain': {'journal_id_fake': dom}, 'value': value}
225
def create(self, cr, uid, vals, context=None):
226
if 'journal_id' in vals:
227
journal = self.pool.get('account.analytic.journal').browse(cr, uid, vals['journal_id'], context=context)
228
vals['instance_id'] = journal.instance_id.id
229
return super(account_analytic_line, self).create(cr, uid, vals, context=context)
231
def write(self, cr, uid, ids, vals, context=None):
232
if 'journal_id' in vals:
233
journal = self.pool.get('account.analytic.journal').browse(cr, uid, vals['journal_id'], context=context)
234
vals['instance_id'] = journal.instance_id.id
235
return super(account_analytic_line, self).write(cr, uid, ids, vals, context=context)
237
account_analytic_line()
239
class account_move(osv.osv):
240
_name = 'account.move'
241
_inherit = 'account.move'
244
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
245
'journal_id_fake': fields.function(_get_journal_id_fake, method=True, string='Journal', type='many2one', relation='account.journal.fake', fnct_search=_search_journal_id_fake)
248
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
252
dom = [('instance_id', '=', instance_id)]
253
if journal_id and not self.pool.get('account.journal').search(cr, uid, [('id', '=', journal_id), ('instance_id', '=', instance_id)]):
254
value['journal_id_fake'] = False
256
return {'domain': {'journal_id_fake': dom}, 'value': value}
258
def create(self, cr, uid, vals, context=None):
259
if 'journal_id' in vals:
260
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
261
vals['instance_id'] = journal and journal.instance_id and journal.instance_id.id or False
262
return super(account_move, self).create(cr, uid, vals, context=context)
264
def write(self, cr, uid, ids, vals, context=None):
265
if 'journal_id' in vals:
266
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
267
vals['instance_id'] = journal and journal.instance_id and journal.instance_id.id or False
268
return super(account_move, self).write(cr, uid, ids, vals, context=context)
270
def onchange_journal_id(self, cr, uid, ids, journal_id=False, context=None):
272
Change msf instance @journal_id change
274
res = super(account_move, self).onchange_journal_id(cr, uid, ids, journal_id, context)
276
journal_data = self.pool.get('account.journal').read(cr, uid, [journal_id], ['instance_id'])
277
if journal_data and journal_data[0] and journal_data[0].get('instance_id', False):
278
if 'value' not in res:
280
res['value'].update({'instance_id': journal_data[0].get('instance_id')})
285
class account_move_line(osv.osv):
286
_name = 'account.move.line'
287
_inherit = 'account.move.line'
290
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
291
'journal_id_fake': fields.function(_get_journal_id_fake, method=True, string='Journal', type='many2one', relation='account.journal.fake', fnct_search=_search_journal_id_fake)
294
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
295
return self.pool.get('account.move').onchange_filter_journal(cr, uid, ids, instance_id, journal_id, context)
297
def create(self, cr, uid, vals, context=None, check=True):
298
if 'journal_id' in vals:
299
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
300
vals['instance_id'] = journal.instance_id.id
301
return super(account_move_line, self).create(cr, uid, vals, context=context, check=check)
303
def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
304
if 'journal_id' in vals:
305
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
306
vals['instance_id'] = journal.instance_id.id
307
return super(account_move_line, self).write(cr, uid, ids, vals, context=context, check=check, update_check=update_check)
311
class account_bank_statement(osv.osv):
312
_name = 'account.bank.statement'
313
_inherit = 'account.bank.statement'
316
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
319
def create(self, cr, uid, vals, context=None):
320
if 'journal_id' in vals:
321
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
322
vals['instance_id'] = journal.instance_id.id
323
return super(account_bank_statement, self).create(cr, uid, vals, context=context)
325
def write(self, cr, uid, ids, vals, context=None):
326
if 'journal_id' in vals:
327
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
328
vals['instance_id'] = journal.instance_id.id
329
return super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
331
account_bank_statement()
333
class account_bank_statement_line(osv.osv):
334
_name = 'account.bank.statement.line'
335
_inherit = 'account.bank.statement.line'
338
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
341
def create(self, cr, uid, vals, context=None):
342
if 'statement_id' in vals:
343
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['statement_id'], context=context)
344
vals['instance_id'] = register.instance_id.id
345
return super(account_bank_statement_line, self).create(cr, uid, vals, context=context)
347
def write(self, cr, uid, ids, vals, context=None):
348
if 'statement_id' in vals:
349
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['statement_id'], context=context)
350
vals['instance_id'] = register.instance_id.id
351
return super(account_bank_statement_line, self).write(cr, uid, ids, vals, context=context)
353
account_bank_statement_line()
355
class account_cashbox_line(osv.osv):
356
_name = 'account.cashbox.line'
357
_inherit = 'account.cashbox.line'
360
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
363
def create(self, cr, uid, vals, context=None):
364
if 'starting_id' in vals:
365
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['starting_id'], context=context)
366
vals['instance_id'] = register.instance_id.id
367
elif 'ending_id' in vals:
368
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['ending_id'], context=context)
369
vals['instance_id'] = register.instance_id.id
370
return super(account_cashbox_line, self).create(cr, uid, vals, context=context)
372
def write(self, cr, uid, ids, vals, context=None):
373
if 'starting_id' in vals:
374
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['starting_id'], context=context)
375
vals['instance_id'] = register.instance_id.id
376
elif 'ending_id' in vals:
377
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['ending_id'], context=context)
378
vals['instance_id'] = register.instance_id.id
379
return super(account_cashbox_line, self).write(cr, uid, ids, vals, context=context)
381
account_cashbox_line()
382
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: