[ADD] Account: add account_voucher fields again
[odoo/odoo.git] / addons / account / installer.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 import datetime
24 from dateutil.relativedelta import relativedelta
25 from os.path import join as opj
26 from operator import itemgetter
27
28 from tools.translate import _
29 from osv import fields, osv
30 import netsvc
31 import tools
32
33 class account_installer(osv.osv_memory):
34     _name = 'account.installer'
35     _inherit = 'res.config.installer'
36
37     def _get_charts(self, cr, uid, context=None):
38         modules = self.pool.get('ir.module.module')
39         ids = modules.search(cr, uid, [('name', 'like', 'l10n_')], context=context)
40         charts = list(
41             sorted(((m.name, m.shortdesc)
42                     for m in modules.browse(cr, uid, ids, context=context)),
43                    key=itemgetter(1)))
44         charts.insert(0, ('configurable', 'Generic Chart Of Account'))
45         return charts
46
47     _columns = {
48         # Accounting
49         'charts': fields.selection(_get_charts, 'Chart of Accounts',
50             required=True,
51             help="Installs localized accounting charts to match as closely as "
52                  "possible the accounting needs of your company based on your "
53                  "country."),
54         'date_start': fields.date('Start Date', required=True),
55         'date_stop': fields.date('End Date', required=True),
56         'period': fields.selection([('month', 'Monthly'), ('3months','3 Monthly')], 'Periods', required=True),
57         'sale_tax': fields.float('Sale Tax(%)'),
58         'purchase_tax': fields.float('Purchase Tax(%)'),
59         'company_id': fields.many2one('res.company', 'Company', required=True),
60     }
61
62     def _default_company(self, cr, uid, context=None):
63         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
64         return user.company_id and user.company_id.id or False
65
66     def _get_default_charts(self, cr, uid, context=None):
67         module_name = False
68         company_id = self._default_company(cr, uid, context=context)
69         company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
70         address_id = self.pool.get('res.partner').address_get(cr, uid, [company.partner_id.id])
71         if address_id['default']:
72             address = self.pool.get('res.partner.address').browse(cr, uid, address_id['default'], context=context)
73             code = address.country_id.code
74             module_name = (code and 'l10n_' + code.lower()) or False
75         if module_name:
76             module_id = self.pool.get('ir.module.module').search(cr, uid, [('name', '=', module_name)], context=context)
77             if module_id:
78                 return module_name
79         return 'configurable'
80
81     _defaults = {
82         'date_start': lambda *a: time.strftime('%Y-01-01'),
83         'date_stop': lambda *a: time.strftime('%Y-12-31'),
84         'period': 'month',
85         'sale_tax': 0.0,
86         'purchase_tax': 0.0,
87         'company_id': _default_company,
88         'charts': _get_default_charts
89     }
90
91     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
92         res = super(account_installer, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
93         configured_cmp = []
94         unconfigured_cmp = []
95         cmp_select = []
96         company_ids = self.pool.get('res.company').search(cr, uid, [], context=context)
97         #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)
98         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",))
99         configured_cmp = [r[0] for r in cr.fetchall()]
100         unconfigured_cmp = list(set(company_ids)-set(configured_cmp))
101         for field in res['fields']:
102            if field == 'company_id':
103                res['fields'][field]['domain'] = unconfigured_cmp
104                res['fields'][field]['selection'] = [('', '')]
105                if unconfigured_cmp:
106                    cmp_select = [(line.id, line.name) for line in self.pool.get('res.company').browse(cr, uid, unconfigured_cmp)]
107                    res['fields'][field]['selection'] = cmp_select
108         return res
109
110     def on_change_tax(self, cr, uid, id, tax):
111         return {'value': {'purchase_tax': tax}}
112
113     def on_change_start_date(self, cr, uid, id, start_date=False):
114         if start_date:
115             start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
116             end_date = (start_date + relativedelta(months=12)) - relativedelta(days=1)
117             return {'value': {'date_stop': end_date.strftime('%Y-%m-%d')}}
118         return {}
119
120     def execute(self, cr, uid, ids, context=None):
121         if context is None:
122             context = {}
123         super(account_installer, self).execute(cr, uid, ids, context=context)
124         fy_obj = self.pool.get('account.fiscalyear')
125         mod_obj = self.pool.get('ir.model.data')
126         obj_acc_temp = self.pool.get('account.account.template')
127         obj_tax_code_temp = self.pool.get('account.tax.code.template')
128         obj_tax_temp = self.pool.get('account.tax.template')
129         obj_product = self.pool.get('product.product')
130         ir_values = self.pool.get('ir.values')
131         obj_acc_chart_temp = self.pool.get('account.chart.template')
132         record = self.browse(cr, uid, ids, context=context)[0]
133         company_id = record.company_id
134         for res in self.read(cr, uid, ids, context=context):
135             if record.charts == 'configurable':
136                 fp = tools.file_open(opj('account', 'configurable_account_chart.xml'))
137                 tools.convert_xml_import(cr, 'account', fp, {}, 'init', True, None)
138                 fp.close()
139                 s_tax = (res.get('sale_tax', 0.0))/100
140                 p_tax = (res.get('purchase_tax', 0.0))/100
141                 pur_temp_tax = mod_obj.get_object_reference(cr, uid, 'account', 'tax_code_base_purchases')
142                 pur_temp_tax_id = pur_temp_tax and pur_temp_tax[1] or False
143
144                 pur_temp_tax_paid = mod_obj.get_object_reference(cr, uid, 'account', 'tax_code_output')
145                 pur_temp_tax_paid_id = pur_temp_tax_paid and pur_temp_tax_paid[1] or False
146
147                 sale_temp_tax = mod_obj.get_object_reference(cr, uid, 'account', 'tax_code_base_sales')
148                 sale_temp_tax_id = sale_temp_tax and sale_temp_tax[1] or False
149
150                 sale_temp_tax_paid = mod_obj.get_object_reference(cr, uid, 'account', 'tax_code_input')
151                 sale_temp_tax_paid_id = sale_temp_tax_paid and sale_temp_tax_paid[1] or False
152
153                 chart_temp_ids = obj_acc_chart_temp.search(cr, uid, [('name','=','Configurable Account Chart Template')], context=context)
154                 chart_temp_id = chart_temp_ids and chart_temp_ids[0] or False
155                 if s_tax * 100 > 0.0:
156                     tax_account_ids = obj_acc_temp.search(cr, uid, [('name', '=', 'Tax Received')], context=context)
157                     sales_tax_account_id = tax_account_ids and tax_account_ids[0] or False
158                     vals_tax_code_temp = {
159                         'name': _('TAX %s%%') % (s_tax*100),
160                         'code': _('TAX %s%%') % (s_tax*100),
161                         'parent_id': sale_temp_tax_id
162                     }
163                     new_tax_code_temp = obj_tax_code_temp.create(cr, uid, vals_tax_code_temp, context=context)
164                     vals_paid_tax_code_temp = {
165                         'name': _('TAX Received %s%%') % (s_tax*100),
166                         'code': _('TAX Received %s%%') % (s_tax*100),
167                         'parent_id': sale_temp_tax_paid_id
168                     }
169                     new_paid_tax_code_temp = obj_tax_code_temp.create(cr, uid, vals_paid_tax_code_temp, context=context)
170                     sales_tax_temp = obj_tax_temp.create(cr, uid, {
171                                             'name': _('TAX %s%%') % (s_tax*100),
172                                             'amount': s_tax,
173                                             'base_code_id': new_tax_code_temp,
174                                             'tax_code_id': new_paid_tax_code_temp,
175                                             'ref_base_code_id': new_tax_code_temp,
176                                             'ref_tax_code_id': new_paid_tax_code_temp,
177                                             'type_tax_use': 'sale',
178                                             'type': 'percent',
179                                             'sequence': 0,
180                                             'account_collected_id': sales_tax_account_id,
181                                             'account_paid_id': sales_tax_account_id,
182                                             'chart_template_id': chart_temp_id,
183                                 }, context=context)
184                 if p_tax * 100 > 0.0:
185                     tax_account_ids = obj_acc_temp.search(cr, uid, [('name', '=', 'Tax Paid')], context=context)
186                     purchase_tax_account_id = tax_account_ids and tax_account_ids[0] or False
187                     vals_tax_code_temp = {
188                         'name': _('TAX %s%%') % (p_tax*100),
189                         'code': _('TAX %s%%') % (p_tax*100),
190                         'parent_id': pur_temp_tax_id
191                     }
192                     new_tax_code_temp = obj_tax_code_temp.create(cr, uid, vals_tax_code_temp, context=context)
193                     vals_paid_tax_code_temp = {
194                         'name': _('TAX Paid %s%%') % (p_tax*100),
195                         'code': _('TAX Paid %s%%') % (p_tax*100),
196                         'parent_id': pur_temp_tax_paid_id
197                     }
198                     new_paid_tax_code_temp = obj_tax_code_temp.create(cr, uid, vals_paid_tax_code_temp, context=context)
199                     purchase_tax_temp = obj_tax_temp.create(cr, uid, {
200                                              'name': _('TAX %s%%') % (p_tax*100),
201                                              'description': _('TAX %s%%') % (p_tax*100),
202                                              'amount': p_tax,
203                                              'base_code_id': new_tax_code_temp,
204                                              'tax_code_id': new_paid_tax_code_temp,
205                                              'ref_base_code_id': new_tax_code_temp,
206                                              'ref_tax_code_id': new_paid_tax_code_temp,
207                                              'type_tax_use': 'purchase',
208                                              'type': 'percent',
209                                              'sequence': 0,
210                                              'account_collected_id': purchase_tax_account_id,
211                                              'account_paid_id': purchase_tax_account_id,
212                                              'chart_template_id': chart_temp_id,
213                                     }, context=context)
214
215             if 'date_start' in res and 'date_stop' in res:
216                 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)
217                 if not f_ids:
218                     name = code = res['date_start'][:4]
219                     if int(name) != int(res['date_stop'][:4]):
220                         name = res['date_start'][:4] +'-'+ res['date_stop'][:4]
221                         code = res['date_start'][2:4] +'-'+ res['date_stop'][2:4]
222                     vals = {
223                         'name': name,
224                         'code': code,
225                         'date_start': res['date_start'],
226                         'date_stop': res['date_stop'],
227                         'company_id': res['company_id'][0]
228                     }
229                     fiscal_id = fy_obj.create(cr, uid, vals, context=context)
230                     if res['period'] == 'month':
231                         fy_obj.create_period(cr, uid, [fiscal_id])
232                     elif res['period'] == '3months':
233                         fy_obj.create_period3(cr, uid, [fiscal_id])
234
235     def modules_to_install(self, cr, uid, ids, context=None):
236         modules = super(account_installer, self).modules_to_install(
237             cr, uid, ids, context=context)
238         chart = self.read(cr, uid, ids, ['charts'],
239                           context=context)[0]['charts']
240         self.logger.notifyChannel(
241             'installer', netsvc.LOG_DEBUG,
242             'Installing chart of accounts %s'%chart)
243         return modules | set([chart])
244
245 account_installer()
246
247 class account_installer_modules(osv.osv_memory):
248     _inherit = 'base.setup.installer'
249     _columns = {
250         'account_analytic_plans': fields.boolean('Multiple Analytic Plans',
251             help="Allows invoice lines to impact multiple analytic accounts "
252                  "simultaneously."),
253         'account_payment': fields.boolean('Suppliers Payment Management',
254             help="Streamlines invoice payment and creates hooks to plug "
255                  "automated payment systems in."),
256         'account_followup': fields.boolean('Followups Management',
257             help="Helps you generate reminder letters for unpaid invoices, "
258                  "including multiple levels of reminding and customized "
259                  "per-partner policies."),
260         'account_voucher': fields.boolean('Voucher Management',
261             help="Account Voucher module includes all the basic requirements of "
262                  "Voucher Entries for Bank, Cash, Sales, Purchase, Expenses, Contra, etc... "),
263         'account_anglo_saxon': fields.boolean('Anglo-Saxon Accounting',
264             help="This module will support the Anglo-Saxons accounting methodology by "
265                 "changing the accounting logic with stock transactions."),
266     }
267
268 account_installer_modules()
269
270 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: