1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 ##############################################################################
24 from dateutil.relativedelta import relativedelta
25 from operator import itemgetter
27 from tools.translate import _
28 from osv import fields, osv
32 class account_installer(osv.osv_memory):
33 _name = 'account.installer'
34 _inherit = 'res.config.installer'
36 def _get_charts(self, cr, uid, context=None):
37 modules = self.pool.get('ir.module.module')
38 ids = modules.search(cr, uid, [('name', 'like', 'l10n_')], context=context)
40 sorted(((m.name, m.shortdesc)
41 for m in modules.browse(cr, uid, ids, context=context)),
43 charts.insert(0, ('configurable', 'Generic Chart Of Account'))
48 'charts': fields.selection(_get_charts, 'Chart of Accounts',
50 help="Installs localized accounting charts to match as closely as "
51 "possible the accounting needs of your company based on your "
53 'date_start': fields.date('Start Date', required=True),
54 'date_stop': fields.date('End Date', required=True),
55 'period': fields.selection([('month', 'Monthly'), ('3months','3 Monthly')], 'Periods', required=True),
56 'company_id': fields.many2one('res.company', 'Company', required=True),
59 def _default_company(self, cr, uid, context=None):
60 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
61 return user.company_id and user.company_id.id or False
63 def _get_default_charts(self, cr, uid, context=None):
65 company_id = self._default_company(cr, uid, context=context)
66 company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
67 address_id = self.pool.get('res.partner').address_get(cr, uid, [company.partner_id.id])
68 if address_id['default']:
69 address = self.pool.get('res.partner.address').browse(cr, uid, address_id['default'], context=context)
70 code = address.country_id.code
71 module_name = (code and 'l10n_' + code.lower()) or False
73 module_id = self.pool.get('ir.module.module').search(cr, uid, [('name', '=', module_name)], context=context)
79 'date_start': lambda *a: time.strftime('%Y-01-01'),
80 'date_stop': lambda *a: time.strftime('%Y-12-31'),
82 'company_id': _default_company,
83 'charts': _get_default_charts
86 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
87 res = super(account_installer, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
89 company_ids = self.pool.get('res.company').search(cr, uid, [], context=context)
90 #display in the widget selection of companies, only the companies that haven't been configured yet (but don't care about the demo chart of accounts)
91 cr.execute("SELECT company_id FROM account_account WHERE active = 't' AND account_account.parent_id IS NULL AND name != %s", ("Chart For Automated Tests",))
92 configured_cmp = [r[0] for r in cr.fetchall()]
93 unconfigured_cmp = list(set(company_ids)-set(configured_cmp))
94 for field in res['fields']:
95 if field == 'company_id':
96 res['fields'][field]['domain'] = unconfigured_cmp
97 res['fields'][field]['selection'] = [('', '')]
99 cmp_select = [(line.id, line.name) for line in self.pool.get('res.company').browse(cr, uid, unconfigured_cmp)]
100 res['fields'][field]['selection'] = cmp_select
103 def on_change_tax(self, cr, uid, id, tax):
104 return {'value': {'purchase_tax': tax}}
106 def on_change_start_date(self, cr, uid, id, start_date=False):
108 start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
109 end_date = (start_date + relativedelta(months=12)) - relativedelta(days=1)
110 return {'value': {'date_stop': end_date.strftime('%Y-%m-%d')}}
113 def execute(self, cr, uid, ids, context=None):
116 fy_obj = self.pool.get('account.fiscalyear')
117 mod_obj = self.pool.get('ir.model.data')
118 obj_acc_temp = self.pool.get('account.account.template')
119 obj_tax_code_temp = self.pool.get('account.tax.code.template')
120 obj_tax_temp = self.pool.get('account.tax.template')
121 obj_acc_chart_temp = self.pool.get('account.chart.template')
122 record = self.browse(cr, uid, ids, context=context)[0]
123 for res in self.read(cr, uid, ids, context=context):
124 if 'date_start' in res and 'date_stop' in res:
125 f_ids = fy_obj.search(cr, uid, [('date_start', '<=', res['date_start']), ('date_stop', '>=', res['date_stop']), ('company_id', '=', res['company_id'][0])], context=context)
127 name = code = res['date_start'][:4]
128 if int(name) != int(res['date_stop'][:4]):
129 name = res['date_start'][:4] +'-'+ res['date_stop'][:4]
130 code = res['date_start'][2:4] +'-'+ res['date_stop'][2:4]
134 'date_start': res['date_start'],
135 'date_stop': res['date_stop'],
136 'company_id': res['company_id'][0]
138 fiscal_id = fy_obj.create(cr, uid, vals, context=context)
139 if res['period'] == 'month':
140 fy_obj.create_period(cr, uid, [fiscal_id])
141 elif res['period'] == '3months':
142 fy_obj.create_period3(cr, uid, [fiscal_id])
143 super(account_installer, self).execute(cr, uid, ids, context=context)
145 def modules_to_install(self, cr, uid, ids, context=None):
146 modules = super(account_installer, self).modules_to_install(
147 cr, uid, ids, context=context)
148 chart = self.read(cr, uid, ids, ['charts'],
149 context=context)[0]['charts']
150 self.logger.notifyChannel(
151 'installer', netsvc.LOG_DEBUG,
152 'Installing chart of accounts %s'%chart)
153 return modules | set([chart])
157 class account_installer_modules(osv.osv_memory):
158 _name = 'account.installer.modules'
159 _inherit = 'res.config.installer'
161 'account_analytic_plans': fields.boolean('Multiple Analytic Plans',
162 help="Allows invoice lines to impact multiple analytic accounts "
164 'account_payment': fields.boolean('Suppliers Payment Management',
165 help="Streamlines invoice payment and creates hooks to plug "
166 "automated payment systems in."),
167 'account_followup': fields.boolean('Followups Management',
168 help="Helps you generate reminder letters for unpaid invoices, "
169 "including multiple levels of reminding and customized "
170 "per-partner policies."),
171 'account_voucher': fields.boolean('Voucher Management',
172 help="Account Voucher module includes all the basic requirements of "
173 "Voucher Entries for Bank, Cash, Sales, Purchase, Expenses, Contra, etc... "),
174 'account_anglo_saxon': fields.boolean('Anglo-Saxon Accounting',
175 help="This module will support the Anglo-Saxons accounting methodology by "
176 "changing the accounting logic with stock transactions."),
180 'account_voucher': True,
183 account_installer_modules()
185 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: