1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import datetime
from osv import osv, fields
import logging
from os import path
import tools
class account_journal(osv.osv):
_inherit = "account.journal"
def init(self, cr):
"""
Load demo.xml before addons
"""
if hasattr(super(account_journal, self), 'init'):
super(account_journal, self).init(cr)
mod_obj = self.pool.get('ir.module.module')
demo = False
mod_id = mod_obj.search(cr, 1, [('name', '=', 'account_journal')])
if mod_id:
demo = mod_obj.read(cr, 1, mod_id, ['demo'])[0]['demo']
if demo:
# Search if an engagement journal exists
eng_ids = self.pool.get('account.analytic.journal').search(cr, 1, [('type', '=', 'engagement')])
if not len(eng_ids):
logging.getLogger('init').info('HOOK: module account_journal: loading account_journal_demo.xml')
pathname = path.join('account_journal', 'account_journal_demo.xml')
file = tools.file_open(pathname)
tools.convert_xml_import(cr, 'account_journal', file, {}, mode='init', noupdate=False)
def get_journal_type(self, cursor, user_id, context=None):
return [('bank', 'Bank'),
('cash','Cash'),
('purchase', 'Purchase'),
('correction','Correction'),
('cheque', 'Cheque'),
('hq', 'HQ'),
('hr', 'HR'),
('accrual', 'Accrual'),
('stock', 'Stock'),
('depreciation', 'Depreciation'),
# Old journal types: not used, but kept to
# not break OpenERP's demo/install data
('sale', 'Sale'),
('sale_refund','Sale Refund'),
('purchase_refund','Purchase Refund'),
('general', 'General'),
('situation', 'Opening/Closing Situation'),
('cur_adj', 'Currency Adjustement'),
]
_columns = {
'type': fields.selection(get_journal_type, 'Type', size=32, required=True),
'instance_id': fields.char('Proprietary instance', size=32, required=True),
'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."),
}
_defaults = {
'allow_date': False,
'centralisation': False,
'entry_posted': False,
'update_posted': True,
'group_invoice_lines': False,
'instance_id': lambda self, cr, uid, c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.name,
}
def get_current_period(self, cr, uid, context=None):
periods = self.pool.get('account.period').find(cr, uid, datetime.date.today())
if periods:
return periods[0]
return False
def name_get(self, cr, user, ids, context=None):
result = self.browse(cr, user, ids, context=context)
res = []
for rs in result:
code = rs.code
res += [(rs.id, code)]
return res
def onchange_type(self, cr, uid, ids, type, currency, context=None):
analytic_journal_obj = self.pool.get('account.analytic.journal')
value = super(account_journal, self).onchange_type(cr, uid, ids, type, currency, context)
default_dom = [('type','<>','view'),('type','<>','consolidation')]
value = {'value': {}, 'domain': {}}
if type in ('cash', 'bank', 'cheque'):
default_dom += [('code', '=like', '5%' )]
value['domain']['default_debit_account_id'] = default_dom
value['domain']['default_crebit_account_id'] = default_dom
# Analytic journal associated
if type == 'cash':
analytic_cash_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'CAS')], context=context)[0]
value['value']['analytic_journal_id'] = analytic_cash_journal
elif type == 'bank':
analytic_bank_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'BNK')], context=context)[0]
value['value']['analytic_journal_id'] = analytic_bank_journal
elif type == 'cheque':
analytic_cheque_journal = analytic_journal_obj.search(cr, uid, [('code', '=', 'CHK')], context=context)[0]
value['value']['analytic_journal_id'] = analytic_cheque_journal
return value
def create(self, cr, uid, vals, context=None):
# TODO: add default accounts
if context is None:
context = {}
# Create associated sequence
seq_pool = self.pool.get('ir.sequence')
seq_typ_pool = self.pool.get('ir.sequence.type')
name = self.pool.get('res.users').browse(cr, uid, uid, context).company_id.name
code = vals['code'].lower()
types = {
'name': name,
'code': code
}
seq_typ_pool.create(cr, uid, types)
seq = {
'name': name,
'code': code,
'active': True,
# UF-433: sequence is now only the number, no more prefix
#'prefix': "%(year)s%(month)s-" + name + "-" + code + "-",
'prefix': "",
'padding': 6,
'number_increment': 1
}
vals['sequence_id'] = seq_pool.create(cr, uid, seq)
# View is set by default, since every journal will display the same thing
obj_data = self.pool.get('ir.model.data')
data_id = obj_data.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_journal_view')])
data = obj_data.browse(cr, uid, data_id[0], context=context)
vals['view_id'] = data.res_id
# create journal
journal_obj = super(account_journal, self).create(cr, uid, vals, context)
# if the journal can be linked to a register, the register is also created
if vals['type'] in ('cash','bank','cheque'):
# 'from_journal_creation' in context permits to pass register creation that have a
# 'prev_reg_id' mandatory field. This is because this register is the first register from this journal.
context.update({'from_journal_creation': True})
self.pool.get('account.bank.statement') \
.create(cr, uid, {'journal_id': journal_obj,
'name': "REG1" + vals['code'],
'period_id': self.get_current_period(cr, uid, context),
'currency': vals.get('currency')}, \
context=context)
return journal_obj
account_journal()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|