62
61
('correction','Correction'),
63
62
('cur_adj', 'Currency Adjustement'),
64
63
('depreciation', 'Depreciation'),
64
('extra', 'OD-Extra Accounting'),
65
65
('general', 'General'),
68
68
('inkind', 'In-kind Donation'),
69
69
('intermission', 'Intermission'),
70
('migration', 'Migration'),
71
('extra', 'OD-Extra Accounting'),
72
70
('situation', 'Opening/Closing Situation'),
73
71
('purchase', 'Purchase'),
74
72
('purchase_refund','Purchase Refund'),
75
('revaluation', 'Revaluation'),
77
74
('sale_refund','Sale Refund'),
78
75
('stock', 'Stock'),
82
def _get_has_entries(self, cr, uid, ids, field_name, arg, context=None):
83
def count_entries(journal_id):
84
return am_obj.search(cr, uid, [('journal_id', '=', journal_id)],
85
limit=1, context=context)
90
am_obj = self.pool.get('account.move')
91
if isinstance(ids, (int, long)):
94
res[id] = bool(count_entries(id))
98
79
'type': fields.selection(get_journal_type, 'Type', size=32, required=True),
99
80
'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."),
100
'bank_journal_id': fields.many2one('account.journal', _("Corresponding bank journal"), domain="[('type', '=', 'bank'), ('currency', '=', currency)]"),
101
'cheque_journal_id': fields.one2many('account.journal', 'bank_journal_id', 'Linked cheque'),
102
'has_entries': fields.function(_get_has_entries, type='boolean', method=True, string='Has journal entries'),
183
145
value['domain']['default_credit_account_id'] = credit_default_dom
186
def create_fiscalyear_sequence(self, cr, uid, fiscalyear, name, code, date, main_sequence=False, context=None):
188
Create a fiscalyear sequence between journal and fiscalyear.
194
raise osv.except_osv(_('Error'), _('Fiscalyear is missing'))
196
raise osv.except_osv(_('Error'), _('Name is missing!'))
198
raise osv.except_osv(_('Error'), _('Code is missing!'))
200
raise osv.except_osv(_('Error'), _('Date is missing!'))
201
# create a new sequence
206
'prefix': "%s" % str(date)[2:4], # take last 2 number of year
208
'number_increment': 1
210
# check if code exists
211
if not self.pool.get('ir.sequence.type').search(cr, uid, [('code', '=' , code)], limit=1, context=context):
212
self.pool.get('ir.sequence.type').create(cr, uid, {'name': code, 'code': code}, context=context)
213
sequence_id = self.pool.get('ir.sequence').create(cr, uid, seq)
214
if not main_sequence:
215
main_sequence = sequence_id
216
self.pool.get('account.sequence.fiscalyear').create(cr, uid, {'sequence_id': sequence_id, 'fiscalyear_id': fiscalyear, 'sequence_main_id': main_sequence,})
219
def check_linked_journal(self, cr, uid, journal_id, context=None):
221
Check that used linked journal is not used twice.
226
raise osv.except_osv(_('Error'), _('Programming error.'))
227
res = self.search(cr, uid, [('bank_journal_id', '=', journal_id)], count=1)
229
raise osv.except_osv(_('Error'), _('Corresponding bank journal already used. Choose another one.'))
232
148
def create(self, cr, uid, vals, context=None):
234
Create the journal with its sequence, a sequence linked to the fiscalyear and some register if this journal type is bank, cash or cheque.
150
# TODO: add default accounts
237
152
if context is None:
240
if not context.get('sync_update_execution', False) and \
241
not context.get('allow_journal_system_create', False) and \
242
vals.get('type', '') == 'system':
243
# user not allowed to create 'system' journal
244
raise osv.except_osv(_('Warning'),
245
_('You can not create a System journal'))
247
# Prepare some values
155
# Create associated sequence
248
156
seq_pool = self.pool.get('ir.sequence')
249
157
seq_typ_pool = self.pool.get('ir.sequence.type')
250
seq_fiscal_pool = self.pool.get('account.sequence.fiscalyear')
251
158
name = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.name
252
159
code = vals['code'].lower()
253
fy_ids = self.pool.get('account.fiscalyear').search(cr, uid, [('state', '=', 'draft')])
258
# Create main sequence
259
164
seq_typ_pool.create(cr, uid, types)
264
169
# UF-433: sequence is now only the number, no more prefix
170
#'prefix': "%(year)s%(month)s-" + name + "-" + code + "-",
267
173
'number_increment': 1
269
vals['sequence_id'] = seq_pool.create(cr, uid, main_seq)
271
for fy in self.pool.get('account.fiscalyear').browse(cr, uid, fy_ids, context=context):
272
# Create associated sequence FY/JOURNAL
273
self.create_fiscalyear_sequence(cr, uid, fy.id, name, code, fy.date_start, main_sequence=vals['sequence_id'], context=context)
175
vals['sequence_id'] = seq_pool.create(cr, uid, seq)
274
177
# View is set by default, since every journal will display the same thing
275
178
obj_data = self.pool.get('ir.model.data')
276
179
data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_view')])
277
180
data = obj_data.browse(cr, uid, data_id[0], context=context)
278
181
vals['view_id'] = data.res_id
281
journal_id = super(account_journal, self).create(cr, uid, vals, context)
282
# Check that linked bank journal if cheque
283
if vals['type'] == 'cheque':
284
self.check_linked_journal(cr, uid, vals['bank_journal_id'] or False)
184
journal_obj = super(account_journal, self).create(cr, uid, vals, context)
286
186
# Some verification for cash, bank, cheque and cur_adj type
287
187
if vals['type'] in ['cash', 'bank', 'cheque', 'cur_adj']:
288
188
if not vals.get('default_debit_account_id'):
289
189
raise osv.except_osv(_('Warning'), _('Default Debit Account is missing.'))
291
191
# if the journal can be linked to a register, the register is also created
292
# UTP-182: but not create if the journal came from another instance via the synchronization
293
if vals['type'] in ('cash','bank','cheque') and not context.get('sync_update_execution', False):
192
# UTP-182: but not create if the journal came from another instance via the synchronization
193
if vals['type'] in ('cash','bank','cheque') and not context.get('sync_data', False):
294
194
# 'from_journal_creation' in context permits to pass register creation that have a
295
195
# 'prev_reg_id' mandatory field. This is because this register is the first register from this journal.
296
196
context.update({'from_journal_creation': True})
298
#BKLG-53 get the next draft period from today
299
current_date = datetime.date.today().strftime('%Y-%m-%d')
300
periods = self.pool.get('account.period').search(cr, uid, [
301
('date_stop','>=',current_date),
302
('state','=','draft'),
303
('special', '=', False),
304
], context=context, limit=1, order='date_stop')
306
raise osv.except_osv(_('Warning'), _('Sorry, No open period for creating the register!'))
307
197
self.pool.get('account.bank.statement') \
308
.create(cr, uid, {'journal_id': journal_id,
198
.create(cr, uid, {'journal_id': journal_obj,
309
199
'name': vals['name'],
310
'period_id': periods[0],
200
'period_id': self.get_current_period(cr, uid, context),
311
201
'currency': vals.get('currency')}, \
314
204
# Prevent user that default account for cur_adj type should be an expense account
315
205
if vals['type'] in ['cur_adj']:
316
206
account_id = vals['default_debit_account_id']
317
207
user_type_code = self.pool.get('account.account').read(cr, uid, account_id, ['user_type_code']).get('user_type_code', False)
318
208
if user_type_code != 'expense':
319
209
raise osv.except_osv(_('Warning'), _('Default Debit Account should be an expense account for Adjustement Journals!'))
322
212
def write(self, cr, uid, ids, vals, context=None):
324
214
Verify default debit account for adjustement journals
329
if not context.get('sync_update_execution', False):
330
if vals.get('type', '') == 'system':
331
# not from sync, user not allowed to update 'system' journal
332
# (note: noteditable not usable on journal form)
333
raise osv.except_osv(_('Warning'),
334
_('System journal not updatable'))
336
216
res = super(account_journal, self).write(cr, uid, ids, vals, context=context)
337
217
for j in self.browse(cr, uid, ids):
338
218
if j.type == 'cur_adj' and j.default_debit_account_id.user_type_code != 'expense':
339
219
raise osv.except_osv(_('Warning'), _('Default Debit Account should be an expense account for Adjustement Journals!'))
340
# Check linked bank journal if type is cheque
341
if j.type == 'cheque':
342
self.check_linked_journal(cr, uid, j.bank_journal_id.id, context=context)
343
# US-265: Check account bank statements if name change
344
if not context.get('sync_update_execution'):
345
if vals.get('name', False):
346
abs_obj = self.pool.get('account.bank.statement')
347
s_ids = abs_obj.search(cr, uid, [('journal_id', '=', j.id)], context=context)
349
abs_obj.write(cr, uid, s_ids, {'name': vals['name']}, context=context)
352
def unlink(self, cr, uid, ids, context=None):
355
if isinstance(ids, (int, long, )):
358
is_system = [ rec.type == 'system' \
359
for rec in self.browse(cr, uid, ids, context=context) ]
361
raise osv.except_osv(_('Warning'),
362
_('System journal not deletable'))
363
return super(account_journal, self).unlink(cr, uid, ids,
366
222
def button_delete_journal(self, cr, uid, ids, context=None):
368
224
Delete all linked register and this journal except: