23
23
from osv import osv, fields
28
from tools.translate import _
30
25
class account_journal(osv.osv):
31
26
_inherit = "account.journal"
35
Load demo.xml before addons
37
if hasattr(super(account_journal, self), 'init'):
38
super(account_journal, self).init(cr)
40
mod_obj = self.pool.get('ir.module.module')
42
mod_id = mod_obj.search(cr, 1, [('name', '=', 'account_journal')])
44
demo = mod_obj.read(cr, 1, mod_id, ['demo'])[0]['demo']
47
# Search if an engagement journal exists
48
eng_ids = self.pool.get('account.analytic.journal').search(cr, 1, [('type', '=', 'engagement')])
50
logging.getLogger('init').info('HOOK: module account_journal: loading account_journal_demo.xml')
51
pathname = path.join('account_journal', 'account_journal_demo.xml')
52
file = tools.file_open(pathname)
53
tools.convert_xml_import(cr, 'account_journal', file, {}, mode='init', noupdate=False)
56
28
def get_journal_type(self, cursor, user_id, context=None):
57
return [('accrual', 'Accrual'),
61
('correction','Correction'),
62
('cur_adj', 'Currency Adjustement'),
63
('depreciation', 'Depreciation'),
64
('extra', 'OD-Extra Accounting'),
65
('general', 'General'),
68
('inkind', 'In-kind Donation'),
69
('intermission', 'Intermission'),
70
('situation', 'Opening/Closing Situation'),
71
('purchase', 'Purchase'),
72
('purchase_refund','Purchase Refund'),
74
('sale_refund','Sale Refund'),
76
('migration', 'Migration'),
29
return [('bank', 'Bank'), \
31
('purchase', 'Purchase'), \
32
('correction','Correction'), \
33
('cheque', 'Cheque'), \
36
('accrual', 'Accrual'), \
38
('depreciation', 'Depreciation'), \
39
# Old journal types: not used, but kept to
40
# not break OpenERP's demo/install data
42
('sale_refund','Sale Refund'), \
43
('purchase_refund','Purchase Refund'), \
44
('general', 'General'), \
45
('situation', 'Opening/Closing Situation')]
80
48
'type': fields.selection(get_journal_type, 'Type', size=32, required=True),
49
'instance_id': fields.char('Proprietary instance', size=32, required=True),
81
50
'code': fields.char('Code', size=10, required=True, help="The code will be used to generate the numbers of the journal entries of this journal."),
98
68
def name_get(self, cr, user, ids, context=None):
100
Get code for journals
102
result = self.read(cr, user, ids, ['code'])
69
result = self.browse(cr, user, ids, context=context)
105
txt = rs.get('code', '')
106
res += [(rs.get('id'), txt)]
73
res += [(rs.id, code)]
109
76
def onchange_type(self, cr, uid, ids, type, currency, context=None):
110
analytic_journal_obj = self.pool.get('account.analytic.journal')
111
# value = super(account_journal, self).onchange_type(cr, uid, ids, type, currency, context)
77
value = super(account_journal, self).onchange_type(cr, uid, ids, type, currency, context)
112
78
default_dom = [('type','<>','view'),('type','<>','consolidation')]
113
value = {'value': {}, 'domain': {}}
79
value.setdefault('domain',{})
114
80
if type in ('cash', 'bank', 'cheque'):
116
xml_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'account_type_cash_moves')
117
default_dom += [('user_type', '=', xml_id[1])]
81
default_dom += [('code', '=like', '5%' )]
120
82
value['domain']['default_debit_account_id'] = default_dom
121
value['domain']['default_credit_account_id'] = default_dom
122
# Analytic journal associated
124
analytic_cash_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'CAS'),
125
('is_current_instance', '=', True)], context=context)[0]
126
value['value']['analytic_journal_id'] = analytic_cash_journal
128
analytic_bank_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'BNK'),
129
('is_current_instance', '=', True)], context=context)[0]
130
value['value']['analytic_journal_id'] = analytic_bank_journal
131
elif type == 'cheque':
132
analytic_cheque_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'CHK'),
133
('is_current_instance', '=', True)], context=context)[0]
134
value['value']['analytic_journal_id'] = analytic_cheque_journal
135
elif type == 'cur_adj':
136
debit_default_dom = [('type','<>','view'),('type','<>','consolidation')]
137
credit_default_dom = [('type','<>','view'),('type','<>','consolidation')]
139
xml_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'account_type_expense')
140
debit_default_dom += [('user_type', '=', xml_id[1])]
144
xml_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'account_type_income')
145
credit_default_dom += [('user_type', '=', xml_id[1])]
148
value['domain']['default_debit_account_id'] = debit_default_dom
149
value['domain']['default_credit_account_id'] = credit_default_dom
83
value['domain']['default_crebit_account_id'] = default_dom
152
87
def create(self, cr, uid, vals, context=None):
154
89
# TODO: add default accounts
159
91
# Create associated sequence
160
92
seq_pool = self.pool.get('ir.sequence')
161
93
seq_typ_pool = self.pool.get('ir.sequence.type')
188
118
journal_obj = super(account_journal, self).create(cr, uid, vals, context)
190
# Some verification for cash, bank, cheque and cur_adj type
191
if vals['type'] in ['cash', 'bank', 'cheque', 'cur_adj']:
192
if not vals.get('default_debit_account_id'):
193
raise osv.except_osv(_('Warning'), _('Default Debit Account is missing.'))
195
120
# if the journal can be linked to a register, the register is also created
196
# UTP-182: but not create if the journal came from another instance via the synchronization
197
if vals['type'] in ('cash','bank','cheque') and not context.get('sync_data', False):
198
# 'from_journal_creation' in context permits to pass register creation that have a
199
# 'prev_reg_id' mandatory field. This is because this register is the first register from this journal.
200
context.update({'from_journal_creation': True})
121
if vals['type'] in ('cash','bank','cheque'):
201
122
self.pool.get('account.bank.statement') \
202
123
.create(cr, uid, {'journal_id': journal_obj,
203
'name': vals['name'],
124
'name': "REG1" + vals['code'],
204
125
'period_id': self.get_current_period(cr, uid, context),
205
126
'currency': vals.get('currency')}, \
208
# Prevent user that default account for cur_adj type should be an expense account
209
if vals['type'] in ['cur_adj']:
210
account_id = vals['default_debit_account_id']
211
user_type_code = self.pool.get('account.account').read(cr, uid, account_id, ['user_type_code']).get('user_type_code', False)
212
if user_type_code != 'expense':
213
raise osv.except_osv(_('Warning'), _('Default Debit Account should be an expense account for Adjustement Journals!'))
214
129
return journal_obj
216
def write(self, cr, uid, ids, vals, context=None):
218
Verify default debit account for adjustement journals
220
res = super(account_journal, self).write(cr, uid, ids, vals, context=context)
221
for j in self.browse(cr, uid, ids):
222
if j.type == 'cur_adj' and j.default_debit_account_id.user_type_code != 'expense':
223
raise osv.except_osv(_('Warning'), _('Default Debit Account should be an expense account for Adjustement Journals!'))
226
def button_delete_journal(self, cr, uid, ids, context=None):
228
Delete all linked register and this journal except:
229
- if another register is linked to one of attached register
230
- if one of register's balance is not null
231
- if one of register is not draft
236
all_register_ids = self.pool.get('account.bank.statement').search(cr, uid, [('journal_id', '=', id)])
237
criteria_register_ids = self.pool.get('account.bank.statement').search(cr, uid, [('journal_id', '=', id), ('state', '=', 'draft'), ('balance_end', '=', 0)])
238
if not all_register_ids:
239
raise osv.except_osv(_('Error'), _('No register found. You can manually delete this journal.'))
240
if all_register_ids != criteria_register_ids:
241
raise osv.except_osv(_('Warning'), _('Deletion is not possible. All registers are not in draft state!'))
242
# Delete all registers
243
context.update({'from': 'journal_deletion'})
244
self.pool.get('account.bank.statement').unlink(cr, uid, all_register_ids, context) # Needs context to permit register deletion
245
# Delete this journal
246
self.unlink(cr, uid, id)
247
# Return to the journal view list
248
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'view_account_journal_tree')
249
view_id = view_id and view_id[1] or False
250
search_view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'account', 'view_account_journal_search')
251
search_view_id = search_view_id and search_view_id[1] or False
253
'name': _('Journal list'),
254
'type': 'ir.actions.act_window',
255
'res_model': 'account.journal',
257
'view_mode': 'tree,form',
258
'view_id': [view_id],
259
'search_view_id': search_view_id,
263
132
account_journal()
264
133
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: