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
# UTP-827: exception: another engagement journal, ENGI, may exist
58
eng_ids = self.search(cr, uid, [('type', '=', 'engagement'), ('instance_id', '=', instance_id), ('code', '!=', 'ENGI')])
59
if len(eng_ids) and len(eng_ids) > 1:
64
(_check_engagement_count, 'You cannot have more than one engagement journal per instance!', ['type', 'instance_id']),
67
account_analytic_journal()
69
class account_journal(osv.osv):
70
_name = 'account.journal'
71
_inherit = 'account.journal'
73
def _get_current_instance(self, cr, uid, ids, name, args, context=None):
75
Get True if the journal was created by this instance.
76
NOT TO BE SYNCHRONIZED!!!
79
current_instance_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.instance_id.id
80
for journal in self.browse(cr, uid, ids, context=context):
81
res[journal.id] = (current_instance_id == journal.instance_id.id)
85
'name': fields.char('Journal Name', size=64, required=True, translate=True),
86
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
87
'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?")
91
'instance_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.instance_id.id,
95
('code_company_uniq', 'unique (code, company_id, instance_id)', 'The code of the journal must be unique per company and instance !'),
96
('name_company_uniq', 'unique (name, company_id, instance_id)', 'The name of the journal must be unique per company and instance !'),
99
# SP-72: in order to always get an analytic journal with the same instance,
100
# the create and write check and replace with the "good" journal if necessary.
101
def create(self, cr, uid, vals, context=None):
102
analytic_obj = self.pool.get('account.analytic.journal')
103
if vals.get('type') and vals.get('type') not in ['situation', 'stock'] and vals.get('analytic_journal_id'):
104
analytic_journal = analytic_obj.browse(cr, uid, vals['analytic_journal_id'], context=context)
107
if 'instance_id' in vals:
108
instance_id = vals['instance_id']
110
instance_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.instance_id.id
112
if analytic_journal and \
113
analytic_journal.name and \
114
analytic_journal.instance_id and \
115
analytic_journal.instance_id.id != instance_id:
116
# replace the journal with the one with the same name, and the wanted instance
117
new_journal_ids = analytic_obj.search(cr, uid, [('name','=', analytic_journal.name),
118
('instance_id','=',instance_id)], context=context)
119
if len(new_journal_ids) > 0:
120
vals['analytic_journal_id'] = new_journal_ids[0]
121
return super(account_journal, self).create(cr, uid, vals, context=context)
123
def write(self, cr, uid, ids, vals, context=None):
124
analytic_obj = self.pool.get('account.analytic.journal')
125
if vals.get('type') and vals.get('type') not in ['situation', 'stock'] and vals.get('analytic_journal_id'):
126
analytic_journal = analytic_obj.browse(cr, uid, vals['analytic_journal_id'], context=context)
129
if 'instance_id' in vals:
130
instance_id = vals['instance_id']
132
instance_id = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.instance_id.id
134
if analytic_journal and \
135
analytic_journal.name and \
136
analytic_journal.instance_id and \
137
analytic_journal.instance_id.id != instance_id:
138
# replace the journal with the one with the same name, and the wanted instance
139
new_journal_ids = analytic_obj.search(cr, uid, [('name','=', analytic_journal.name),
140
('instance_id','=',instance_id)], context=context)
141
if len(new_journal_ids) > 0:
142
vals['analytic_journal_id'] = new_journal_ids[0]
143
return super(account_journal, self).write(cr, uid, ids, vals, context=context)
147
class account_analytic_journal_fake(osv.osv):
148
""" Workaround class used in account.analytic.line search view, because context is lost in m2o search view """
149
_inherit = 'account.analytic.journal'
150
_name = 'account.analytic.journal.fake'
151
_table = 'account_analytic_journal'
153
def name_get(self, cr, uid, ids, context=None):
158
for journal in self.read(cr, uid, ids, ['code', 'instance_id']):
159
ret.append((journal['id'], '%s / %s'%(journal['instance_id'] and journal['instance_id'][1] or '', journal['code'])))
163
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
164
return self.pool.get('account.analytic.journal').fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
166
account_analytic_journal_fake()
168
class account_journal_fake(osv.osv):
169
""" Workaround class used in account.move search view, because context is lost in m2o search view """
171
_inherit = 'account.journal'
172
_name = 'account.journal.fake'
173
_table = 'account_journal'
175
def name_get(self, cr, uid, ids, context=None):
180
for journal in self.read(cr, uid, ids, ['code', 'instance_id']):
181
ret.append((journal['id'], '%s / %s'%(journal['instance_id'] and journal['instance_id'][1] or '', journal['code'])))
185
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
186
return self.pool.get('account.journal').fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
188
account_journal_fake()
190
def _get_journal_id_fake(self, cr, uid, ids, field_name, args, context=None):
194
for i in self.read(cr, uid, ids, ['journal_id']):
195
res[i['id']] = i['journal_id']
198
def _search_journal_id_fake(self, cr, uid, obj, name, args, context=None):
201
if arg[0] == 'journal_id_fake':
202
res.append(('journal_id', arg[1], arg[2]))
207
class account_analytic_line(osv.osv):
208
_name = 'account.analytic.line'
209
_inherit = 'account.analytic.line'
212
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
213
'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)
216
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
220
dom = [('instance_id', '=', instance_id)]
221
if journal_id and not self.pool.get('account.analytic.journal').search(cr, uid, [('id', '=', journal_id), ('instance_id', '=', instance_id)]):
222
value['journal_id_fake'] = False
224
return {'domain': {'journal_id_fake': dom}, 'value': value}
226
def create(self, cr, uid, vals, context=None):
227
if 'journal_id' in vals:
228
journal = self.pool.get('account.analytic.journal').browse(cr, uid, vals['journal_id'], context=context)
229
vals['instance_id'] = journal.instance_id.id
230
return super(account_analytic_line, self).create(cr, uid, vals, context=context)
232
def write(self, cr, uid, ids, vals, context=None):
233
if 'journal_id' in vals:
234
journal = self.pool.get('account.analytic.journal').browse(cr, uid, vals['journal_id'], context=context)
235
vals['instance_id'] = journal.instance_id.id
236
return super(account_analytic_line, self).write(cr, uid, ids, vals, context=context)
238
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
240
Filtering regarding context
244
if context.get('instance_ids'):
245
instance_ids = context.get('instance_ids')
246
if isinstance(instance_ids, (int, long)):
247
instance_ids = [instance_ids]
248
args.append(('instance_id', 'in', instance_ids))
249
return super(account_analytic_line, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
251
account_analytic_line()
253
class account_move(osv.osv):
254
_name = 'account.move'
255
_inherit = 'account.move'
258
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
259
'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)
262
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
266
dom = [('instance_id', '=', instance_id)]
267
if journal_id and not self.pool.get('account.journal').search(cr, uid, [('id', '=', journal_id), ('instance_id', '=', instance_id)]):
268
value['journal_id_fake'] = False
270
return {'domain': {'journal_id_fake': dom}, 'value': value}
272
def create(self, cr, uid, vals, context=None):
273
if 'journal_id' in vals:
274
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
275
vals['instance_id'] = journal and journal.instance_id and journal.instance_id.id or False
276
return super(account_move, self).create(cr, uid, vals, context=context)
278
def write(self, cr, uid, ids, vals, context=None):
279
if 'journal_id' in vals:
280
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
281
vals['instance_id'] = journal and journal.instance_id and journal.instance_id.id or False
282
return super(account_move, self).write(cr, uid, ids, vals, context=context)
284
def onchange_journal_id(self, cr, uid, ids, journal_id=False, context=None):
286
Change msf instance @journal_id change
288
res = super(account_move, self).onchange_journal_id(cr, uid, ids, journal_id, context)
290
journal_data = self.pool.get('account.journal').read(cr, uid, [journal_id], ['instance_id'])
291
if journal_data and journal_data[0] and journal_data[0].get('instance_id', False):
292
if 'value' not in res:
294
res['value'].update({'instance_id': journal_data[0].get('instance_id')})
299
class account_move_line(osv.osv):
300
_name = 'account.move.line'
301
_inherit = 'account.move.line'
304
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
305
'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)
308
def onchange_filter_journal(self, cr, uid, ids, instance_id, journal_id, context=None):
309
return self.pool.get('account.move').onchange_filter_journal(cr, uid, ids, instance_id, journal_id, context)
311
def create(self, cr, uid, vals, context=None, check=True):
312
if 'journal_id' in vals:
313
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
314
vals['instance_id'] = journal.instance_id.id
315
return super(account_move_line, self).create(cr, uid, vals, context=context, check=check)
317
def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
318
if 'journal_id' in vals:
319
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
320
vals['instance_id'] = journal.instance_id.id
321
return super(account_move_line, self).write(cr, uid, ids, vals, context=context, check=check, update_check=update_check)
323
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
325
Filtering regarding context
329
if context.get('instance_ids'):
330
instance_ids = context.get('instance_ids')
331
if isinstance(instance_ids, (int, long)):
332
instance_ids = [instance_ids]
333
args.append(('instance_id', 'in', instance_ids))
334
return super(account_move_line, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
338
class account_move_reconcile(osv.osv):
339
_name = 'account.move.reconcile'
340
_inherit = 'account.move.reconcile'
343
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
347
'instance_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.instance_id.id,
350
account_move_reconcile()
352
class account_bank_statement(osv.osv):
353
_name = 'account.bank.statement'
354
_inherit = 'account.bank.statement'
357
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
360
def create(self, cr, uid, vals, context=None):
361
if 'journal_id' in vals:
362
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
363
vals['instance_id'] = journal.instance_id.id
364
return super(account_bank_statement, self).create(cr, uid, vals, context=context)
366
def write(self, cr, uid, ids, vals, context=None):
367
if 'journal_id' in vals:
368
journal = self.pool.get('account.journal').browse(cr, uid, vals['journal_id'], context=context)
369
vals['instance_id'] = journal.instance_id.id
370
return super(account_bank_statement, self).write(cr, uid, ids, vals, context=context)
372
account_bank_statement()
374
class account_bank_statement_line(osv.osv):
375
_name = 'account.bank.statement.line'
376
_inherit = 'account.bank.statement.line'
379
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
382
def create(self, cr, uid, vals, context=None):
383
if 'statement_id' in vals:
384
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['statement_id'], context=context)
385
vals['instance_id'] = register.instance_id.id
386
return super(account_bank_statement_line, self).create(cr, uid, vals, context=context)
388
def write(self, cr, uid, ids, vals, context=None):
389
if 'statement_id' in vals:
390
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['statement_id'], context=context)
391
vals['instance_id'] = register.instance_id.id
392
return super(account_bank_statement_line, self).write(cr, uid, ids, vals, context=context)
394
account_bank_statement_line()
396
class account_cashbox_line(osv.osv):
397
_name = 'account.cashbox.line'
398
_inherit = 'account.cashbox.line'
401
'instance_id': fields.many2one('msf.instance', 'Proprietary Instance'),
404
def create(self, cr, uid, vals, context=None):
405
if 'starting_id' in vals:
406
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['starting_id'], context=context)
407
vals['instance_id'] = register.instance_id.id
408
elif 'ending_id' in vals:
409
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['ending_id'], context=context)
410
vals['instance_id'] = register.instance_id.id
411
return super(account_cashbox_line, self).create(cr, uid, vals, context=context)
413
def write(self, cr, uid, ids, vals, context=None):
414
if 'starting_id' in vals:
415
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['starting_id'], context=context)
416
vals['instance_id'] = register.instance_id.id
417
elif 'ending_id' in vals:
418
register = self.pool.get('account.bank.statement').browse(cr, uid, vals['ending_id'], context=context)
419
vals['instance_id'] = register.instance_id.id
420
return super(account_cashbox_line, self).write(cr, uid, ids, vals, context=context)
422
account_cashbox_line()
423
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: