useability
[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_default_accounts(self, cr, uid, context=None):
38         accounts = [{'acc_name':'Current','account_type':'bank'},
39                     {'acc_name':'Deposit','account_type':'bank'},
40                     {'acc_name':'Cash','account_type':'cash'}]
41         return accounts
42
43     def _get_charts(self, cr, uid, context=None):
44         modules = self.pool.get('ir.module.module')
45         ids = modules.search(cr, uid, [('category_id','=','Account Charts')])
46         charts = list(
47             sorted(((m.name, m.shortdesc)
48                     for m in modules.browse(cr, uid, ids)),
49                    key=itemgetter(1)))
50         charts.insert(0,('configurable','Generic Chart Of Account'))
51         return charts
52
53     _columns = {
54         # Accounting
55         'charts':fields.selection(_get_charts, 'Chart of Accounts',
56             required=True,
57             help="Installs localized accounting charts to match as closely as "
58                  "possible the accounting needs of your company based on your "
59                  "country."),
60         'date_start': fields.date('Start Date', required=True),
61         'date_stop': fields.date('End Date', required=True),
62         'period':fields.selection([('month','Monthly'), ('3months','3 Monthly')],
63                                   'Periods', required=True),
64         'bank_accounts_id': fields.one2many('account.bank.accounts.wizard', 'bank_account_id', 'Your Bank and Cash Accounts',required=True),
65         'sale_tax':fields.float('Sale Tax(%)'),
66         'purchase_tax':fields.float('Purchase Tax(%)'),
67         'company_id': fields.many2one('res.company', 'Company'),
68     }
69
70     def _default_company(self, cr, uid, context=None):
71         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
72         if user.company_id:
73             return user.company_id.id
74         return False
75
76     def _get_default_charts(self, cr, uid, context=None):
77         module_name = False
78         company_id = self._default_company(cr, uid, context=context)
79         company = self.pool.get('res.company').browse(cr, uid, company_id)
80         address_id = self.pool.get('res.partner').address_get(cr, uid, [company.partner_id.id])
81         if address_id['default']:
82             address = self.pool.get('res.partner.address').browse(cr, uid, address_id['default'])
83             code = address.country_id.code
84             module_name = (code and 'l10n_' + code.lower()) or False
85         if module_name:
86             module_id = self.pool.get('ir.module.module').search(cr, uid, [('name', '=', module_name)])
87             if module_id:
88                 return module_name
89         return 'configurable'
90
91     _defaults = {
92         'date_start': lambda *a: time.strftime('%Y-01-01'),
93         'date_stop': lambda *a: time.strftime('%Y-12-31'),
94         'period': lambda *a:'month',
95         'sale_tax': lambda *a:0.0,
96         'purchase_tax': lambda *a:0.0,
97         'company_id': _default_company,
98         'bank_accounts_id': _get_default_accounts,
99         'charts': _get_default_charts
100     }
101
102     def on_change_tax(self, cr, uid, id, tax):
103         return {'value':{'purchase_tax':tax}}
104
105     def on_change_start_date(self, cr, uid, id, start_date=False):
106         if start_date:
107             start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d")
108             end_date = (start_date + relativedelta(months=12)) - relativedelta(days=1)
109             return {'value':{'date_stop':end_date.strftime('%Y-%m-%d')}}
110         return {}
111
112     def generate_configurable_chart(self, cr, uid, ids, context=None):
113         obj_acc = self.pool.get('account.account')
114         obj_acc_tax = self.pool.get('account.tax')
115         obj_journal = self.pool.get('account.journal')
116         obj_sequence = self.pool.get('ir.sequence')
117         obj_acc_template = self.pool.get('account.account.template')
118         obj_fiscal_position_template = self.pool.get('account.fiscal.position.template')
119         obj_fiscal_position = self.pool.get('account.fiscal.position')
120         mod_obj = self.pool.get('ir.model.data')
121         analytic_journal_obj = self.pool.get('account.analytic.journal')
122
123         result = mod_obj._get_id(cr, uid, 'account', 'configurable_chart_template')
124         id = mod_obj.read(cr, uid, [result], ['res_id'])[0]['res_id']
125         obj_multi = self.pool.get('account.chart.template').browse(cr, uid, id)
126
127         record = self.browse(cr, uid, ids, context=context)[0]
128
129         if context is None:
130             context = {}
131         company_id = self.browse(cr, uid, ids, context)[0].company_id
132         seq_journal = True
133
134         # Creating Account
135         obj_acc_root = obj_multi.account_root_id
136         tax_code_root_id = obj_multi.tax_code_root_id.id
137
138         #new code
139         acc_template_ref = {}
140         tax_template_ref = {}
141         tax_code_template_ref = {}
142         todo_dict = {}
143
144         #create all the tax code
145         children_tax_code_template = self.pool.get('account.tax.code.template').search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id')
146         children_tax_code_template.sort()
147         for tax_code_template in self.pool.get('account.tax.code.template').browse(cr, uid, children_tax_code_template):
148             vals={
149                 'name': (tax_code_root_id == tax_code_template.id) and company_id.name or tax_code_template.name,
150                 'code': tax_code_template.code,
151                 'info': tax_code_template.info,
152                 'parent_id': tax_code_template.parent_id and ((tax_code_template.parent_id.id in tax_code_template_ref) and tax_code_template_ref[tax_code_template.parent_id.id]) or False,
153                 'company_id': company_id.id,
154                 'sign': tax_code_template.sign,
155             }
156             new_tax_code = self.pool.get('account.tax.code').create(cr, uid, vals)
157             #recording the new tax code to do the mapping
158             tax_code_template_ref[tax_code_template.id] = new_tax_code
159
160         #create all the tax
161         for tax in obj_multi.tax_template_ids:
162             #create it
163             vals_tax = {
164                 'name':tax.name,
165                 'sequence': tax.sequence,
166                 'amount':tax.amount,
167                 'type':tax.type,
168                 'applicable_type': tax.applicable_type,
169                 'domain':tax.domain,
170                 'parent_id': tax.parent_id and ((tax.parent_id.id in tax_template_ref) and tax_template_ref[tax.parent_id.id]) or False,
171                 'child_depend': tax.child_depend,
172                 'python_compute': tax.python_compute,
173                 'python_compute_inv': tax.python_compute_inv,
174                 'python_applicable': tax.python_applicable,
175                 'base_code_id': tax.base_code_id and ((tax.base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.base_code_id.id]) or False,
176                 'tax_code_id': tax.tax_code_id and ((tax.tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.tax_code_id.id]) or False,
177                 'base_sign': tax.base_sign,
178                 'tax_sign': tax.tax_sign,
179                 'ref_base_code_id': tax.ref_base_code_id and ((tax.ref_base_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_base_code_id.id]) or False,
180                 'ref_tax_code_id': tax.ref_tax_code_id and ((tax.ref_tax_code_id.id in tax_code_template_ref) and tax_code_template_ref[tax.ref_tax_code_id.id]) or False,
181                 'ref_base_sign': tax.ref_base_sign,
182                 'ref_tax_sign': tax.ref_tax_sign,
183                 'include_base_amount': tax.include_base_amount,
184                 'description':tax.description,
185                 'company_id': company_id.id,
186                 'type_tax_use': tax.type_tax_use
187             }
188             new_tax = obj_acc_tax.create(cr, uid, vals_tax)
189             #as the accounts have not been created yet, we have to wait before filling these fields
190             todo_dict[new_tax] = {
191                 'account_collected_id': tax.account_collected_id and tax.account_collected_id.id or False,
192                 'account_paid_id': tax.account_paid_id and tax.account_paid_id.id or False,
193             }
194             tax_template_ref[tax.id] = new_tax
195
196         #deactivate the parent_store functionnality on account_account for rapidity purpose
197         self.pool._init = True
198
199         children_acc_template = obj_acc_template.search(cr, uid, [('parent_id','child_of',[obj_acc_root.id]),('nocreate','!=',True)])
200         children_acc_template.sort()
201         for account_template in obj_acc_template.browse(cr, uid, children_acc_template):
202             tax_ids = []
203             for tax in account_template.tax_ids:
204                 tax_ids.append(tax_template_ref[tax.id])
205             #create the account_account
206
207             dig = 6
208             code_main = account_template.code and len(account_template.code) or 0
209             code_acc = account_template.code or ''
210             if code_main>0 and code_main<=dig and account_template.type != 'view':
211                 code_acc=str(code_acc) + (str('0'*(dig-code_main)))
212             vals={
213                 'name': (obj_acc_root.id == account_template.id) and company_id.name or account_template.name,
214                 #'sign': account_template.sign,
215                 'currency_id': account_template.currency_id and account_template.currency_id.id or False,
216                 'code': code_acc,
217                 'type': account_template.type,
218                 'user_type': account_template.user_type and account_template.user_type.id or False,
219                 'reconcile': account_template.reconcile,
220                 'shortcut': account_template.shortcut,
221                 'note': account_template.note,
222                 'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False,
223                 'tax_ids': [(6,0,tax_ids)],
224                 'company_id': company_id.id,
225             }
226             new_account = obj_acc.create(cr, uid, vals)
227             acc_template_ref[account_template.id] = new_account
228             if account_template.name == 'Bank Current Account':
229                 b_vals={
230                     'name': 'Bank Accounts',
231                     'code': '110500',
232                     'type': 'view',
233                     'user_type': account_template.parent_id.user_type and account_template.user_type.id or False,
234                     'shortcut': account_template.shortcut,
235                     'note': account_template.note,
236                     'parent_id': account_template.parent_id and ((account_template.parent_id.id in acc_template_ref) and acc_template_ref[account_template.parent_id.id]) or False,
237                     'tax_ids': [(6,0,tax_ids)],
238                     'company_id': company_id.id,
239                 }
240                 bank_account = obj_acc.create(cr, uid, b_vals)
241
242                 view_id_cash = self.pool.get('account.journal.view').search(cr,uid,[('name','=','Bank/Cash Journal View')])[0] #why fixed name here?
243                 view_id_cur = self.pool.get('account.journal.view').search(cr,uid,[('name','=','Bank/Cash Journal (Multi-Currency) View')])[0] #Why Fixed name here?
244
245                 cash_result = mod_obj._get_id(cr, uid, 'account', 'conf_account_type_cash')
246                 cash_type_id = mod_obj.read(cr, uid, [cash_result], ['res_id'])[0]['res_id']
247
248                 bank_result = mod_obj._get_id(cr, uid, 'account', 'conf_account_type_bnk')
249                 bank_type_id = mod_obj.read(cr, uid, [bank_result], ['res_id'])[0]['res_id']
250
251                 check_result = mod_obj._get_id(cr, uid, 'account', 'conf_account_type_chk')
252                 check_type_id = mod_obj.read(cr, uid, [check_result], ['res_id'])[0]['res_id']
253
254 #                record = self.browse(cr, uid, ids, context=context)[0]
255                 code_cnt = 1
256                 vals_seq = {
257                         'name': _('Bank Journal '),
258                         'code': 'account.journal',
259                         'prefix': 'BNK/%(year)s/',
260                         'padding': 5
261                         }
262                 seq_id = obj_sequence.create(cr,uid,vals_seq)
263
264                 #create the bank journals
265                 analitical_bank_ids = analytic_journal_obj.search(cr,uid,[('type','=','situation')])
266                 analitical_journal_bank = analitical_bank_ids and analitical_bank_ids[0] or False
267                 vals_journal = {}
268                 vals_journal['name']= _('Bank Journal ')
269                 vals_journal['code']= _('BNK')
270                 vals_journal['sequence_id'] = seq_id
271                 vals_journal['type'] = 'bank'
272                 vals_journal['analytic_journal_id'] = analitical_journal_bank
273                 if vals.get('currency_id', False):
274                     vals_journal['view_id'] = view_id_cur
275                     vals_journal['currency'] = vals.get('currency_id', False)
276                 else:
277                     vals_journal['view_id'] = view_id_cash
278                 vals_journal['default_credit_account_id'] = new_account
279                 vals_journal['default_debit_account_id'] = new_account
280                 obj_journal.create(cr,uid,vals_journal)
281
282                 for val in record.bank_accounts_id:
283                     seq_padding = 5
284                     if val.account_type == 'cash':
285                         type = cash_type_id
286                     elif val.account_type == 'bank':
287                         type = bank_type_id
288                     elif val.account_type == 'check':
289                         type = check_type_id
290                     else:
291                         type = check_type_id
292                         seq_padding = None
293
294                     vals_bnk = {'name': val.acc_name or '',
295                         'currency_id': val.currency_id.id or False,
296                         'code': str(110500 + code_cnt),
297                         'type': 'liquidity',
298                         'user_type': type,
299                         'parent_id':bank_account,
300                         'company_id': company_id.id }
301                     child_bnk_acc = obj_acc.create(cr, uid, vals_bnk)
302                     vals_seq_child = {
303                         'name': _(vals_bnk['name'] + ' ' + 'Journal'),
304                         'code': 'account.journal',
305                         'prefix': _((vals_bnk['name'][:3].upper()) + '/%(year)s/'),
306                         'padding': seq_padding
307                         }
308                     seq_id = obj_sequence.create(cr, uid, vals_seq_child)
309
310                     #create the bank journal
311                     vals_journal = {}
312                     vals_journal['name']= vals_bnk['name'] + ' Journal'
313                     vals_journal['code']= _(vals_bnk['name'][:3]).upper()
314                     vals_journal['sequence_id'] = seq_id
315                     vals_journal['type'] = 'cash'
316                     if vals.get('currency_id', False):
317                         vals_journal['view_id'] = view_id_cur
318                         vals_journal['currency'] = vals_bnk.get('currency_id', False)
319                     else:
320                         vals_journal['view_id'] = view_id_cash
321                     vals_journal['default_credit_account_id'] = child_bnk_acc
322                     vals_journal['default_debit_account_id'] = child_bnk_acc
323                     vals_journal['analytic_journal_id'] = analitical_journal_bank
324                     obj_journal.create(cr,uid,vals_journal)
325                     code_cnt += 1
326
327         #reactivate the parent_store functionality on account_account
328         self.pool._init = False
329         self.pool.get('account.account')._parent_store_compute(cr)
330
331         for key,value in todo_dict.items():
332             if value['account_collected_id'] or value['account_paid_id']:
333                 obj_acc_tax.write(cr, uid, [key], {
334                     'account_collected_id': acc_template_ref[value['account_collected_id']],
335                     'account_paid_id': acc_template_ref[value['account_paid_id']],
336                 })
337
338         # Creating Journals Sales and Purchase
339         vals_journal={}
340         data_id = mod_obj.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_sp_journal_view')])
341         data = mod_obj.browse(cr, uid, data_id[0])
342         view_id = data.res_id
343
344         seq_id = obj_sequence.search(cr,uid,[('name','=','Account Journal')])[0]
345
346         if seq_journal:
347             seq_sale = {
348                         'name': 'Sale Journal',
349                         'code': 'account.journal',
350                         'prefix': 'SAJ/%(year)s/',
351                         'padding': 3
352                         }
353             seq_id_sale = obj_sequence.create(cr, uid, seq_sale)
354             seq_purchase = {
355                         'name': 'Purchase Journal',
356                         'code': 'account.journal',
357                         'prefix': 'EXJ/%(year)s/',
358                         'padding': 3
359                         }
360             seq_id_purchase = obj_sequence.create(cr, uid, seq_purchase)
361             seq_refund_sale = {
362                         'name': 'Sales Refund Journal',
363                         'code': 'account.journal',
364                         'prefix': 'SCNJ/%(year)s/',
365                         'padding': 3
366                         }
367             seq_id_sale_refund = obj_sequence.create(cr, uid, seq_refund_sale)
368             seq_refund_purchase = {
369                         'name': 'Purchase Refund Journal',
370                         'code': 'account.journal',
371                         'prefix': 'ECNJ/%(year)s/',
372                         'padding': 3
373                         }
374             seq_id_purchase_refund = obj_sequence.create(cr, uid, seq_refund_purchase)
375         else:
376             seq_id_sale = seq_id
377             seq_id_purchase = seq_id
378             seq_id_sale_refund = seq_id
379             seq_id_purchase_refund = seq_id
380
381         vals_journal['view_id'] = view_id
382
383         #Sales Journal
384         analitical_sale_ids = analytic_journal_obj.search(cr,uid,[('type','=','sale')])
385         analitical_journal_sale = analitical_sale_ids and analitical_sale_ids[0] or False
386
387         vals_journal['name'] = _('Sales Journal')
388         vals_journal['type'] = 'sale'
389         vals_journal['code'] = _('SAJ')
390         vals_journal['sequence_id'] = seq_id_sale
391         vals_journal['analytic_journal_id'] = analitical_journal_sale
392
393
394         if obj_multi.property_account_receivable:
395             vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.property_account_income_categ.id]
396             vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.property_account_income_categ.id]
397
398         obj_journal.create(cr,uid,vals_journal)
399
400         # Purchase Journal
401         analitical_purchase_ids = analytic_journal_obj.search(cr,uid,[('type','=','purchase')])
402         analitical_journal_purchase = analitical_purchase_ids and analitical_purchase_ids[0] or False
403
404         vals_journal['name'] = _('Purchase Journal')
405         vals_journal['type'] = 'purchase'
406         vals_journal['code'] = _('EXJ')
407         vals_journal['sequence_id'] = seq_id_purchase
408         vals_journal['analytic_journal_id'] = analitical_journal_purchase
409
410         if obj_multi.property_account_payable:
411             vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.property_account_expense_categ.id]
412             vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.property_account_expense_categ.id]
413
414         obj_journal.create(cr,uid,vals_journal)
415
416         # Creating Journals Sales Refund and Purchase Refund
417         vals_journal={}
418         data_id = mod_obj.search(cr, uid, [('model','=','account.journal.view'), ('name','=','account_sp_refund_journal_view')])
419         data = mod_obj.browse(cr, uid, data_id[0])
420         view_id = data.res_id
421
422         vals_journal['view_id'] = view_id
423
424         #Sales Refund Journal
425         vals_journal['name'] = _('Sales Refund Journal')
426         vals_journal['type'] = 'sale_refund'
427         vals_journal['refund_journal'] = True
428         vals_journal['code'] = _('SCNJ')
429         vals_journal['sequence_id'] = seq_id_sale_refund
430         vals_journal['analytic_journal_id'] = analitical_journal_sale
431
432         if obj_multi.property_account_receivable:
433             vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.property_account_income_categ.id]
434             vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.property_account_income_categ.id]
435
436         obj_journal.create(cr,uid,vals_journal)
437
438         # Purchase Refund Journal
439         vals_journal['name'] = _('Purchase Refund Journal')
440         vals_journal['type'] = 'purchase_refund'
441         vals_journal['refund_journal'] = True
442         vals_journal['code'] = _('ECNJ')
443         vals_journal['sequence_id'] = seq_id_purchase_refund
444         vals_journal['analytic_journal_id'] = analitical_journal_purchase
445
446         if obj_multi.property_account_payable:
447             vals_journal['default_credit_account_id'] = acc_template_ref[obj_multi.property_account_expense_categ.id]
448             vals_journal['default_debit_account_id'] = acc_template_ref[obj_multi.property_account_expense_categ.id]
449
450         obj_journal.create(cr,uid,vals_journal)
451
452         # Bank Journals
453         view_id_cash = self.pool.get('account.journal.view').search(cr, uid, [('name','=','Bank/Cash Journal View')])[0] #TOFIX: Why put fixed name ?
454         view_id_cur = self.pool.get('account.journal.view').search(cr, uid, [('name','=','Bank/Cash Journal (Multi-Currency) View')])[0] #TOFIX: why put fixed name?
455
456
457         #create the properties
458         property_obj = self.pool.get('ir.property')
459         fields_obj = self.pool.get('ir.model.fields')
460
461         todo_list = [
462             ('property_account_receivable','res.partner','account.account'),
463             ('property_account_payable','res.partner','account.account'),
464             ('property_account_expense_categ','product.category','account.account'),
465             ('property_account_income_categ','product.category','account.account'),
466             ('property_account_expense','product.template','account.account'),
467             ('property_account_income','product.template','account.account'),
468             ('property_reserve_and_surplus_account','res.company','account.account'),
469         ]
470
471         for record in todo_list:
472             r = []
473             r = property_obj.search(cr, uid, [('name','=', record[0] ),('company_id','=',company_id.id)])
474             account = getattr(obj_multi, record[0])
475             field = fields_obj.search(cr, uid, [('name','=',record[0]),('model','=',record[1]),('relation','=',record[2])])
476             vals = {
477                 'name': record[0],
478                 'company_id': company_id.id,
479                 'fields_id': field[0],
480                 'value': account and 'account.account,'+str(acc_template_ref[account.id]) or False,
481             }
482
483             if r:
484                 #the property exist: modify it
485                 property_obj.write(cr, uid, r, vals)
486             else:
487                 #create the property
488                 property_obj.create(cr, uid, vals)
489
490         fp_ids = obj_fiscal_position_template.search(cr, uid,[('chart_template_id', '=', obj_multi.id)])
491
492         if fp_ids:
493             for position in obj_fiscal_position_template.browse(cr, uid, fp_ids):
494
495                 vals_fp = {
496                            'company_id': company_id.id,
497                            'name': position.name,
498                            }
499                 new_fp = obj_fiscal_position.create(cr, uid, vals_fp)
500
501                 obj_tax_fp = self.pool.get('account.fiscal.position.tax')
502                 obj_ac_fp = self.pool.get('account.fiscal.position.account')
503
504                 for tax in position.tax_ids:
505                     vals_tax = {
506                                 'tax_src_id': tax_template_ref[tax.tax_src_id.id],
507                                 'tax_dest_id': tax.tax_dest_id and tax_template_ref[tax.tax_dest_id.id] or False,
508                                 'position_id': new_fp,
509                                 }
510                     obj_tax_fp.create(cr, uid, vals_tax)
511
512                 for acc in position.account_ids:
513                     vals_acc = {
514                                 'account_src_id': acc_template_ref[acc.account_src_id.id],
515                                 'account_dest_id': acc_template_ref[acc.account_dest_id.id],
516                                 'position_id': new_fp,
517                                 }
518                     obj_ac_fp.create(cr, uid, vals_acc)
519
520     def execute(self, cr, uid, ids, context=None):
521         if context is None:
522             context = {}
523         fy_obj = self.pool.get('account.fiscalyear')
524         mod_obj = self.pool.get('ir.model.data')
525         obj_acc = self.pool.get('account.account')
526         obj_tax_code = self.pool.get('account.tax.code')
527         obj_temp_tax_code = self.pool.get('account.tax.code.template')
528         super(account_installer, self).execute(cr, uid, ids, context=context)
529         record = self.browse(cr, uid, ids, context=context)[0]
530         company_id = record.company_id
531         for res in self.read(cr, uid, ids):
532             if record.charts == 'configurable':
533                 fp = tools.file_open(opj('account','configurable_account_chart.xml'))
534                 tools.convert_xml_import(cr, 'account', fp, {}, 'init',True, None)
535                 fp.close()
536                 self.generate_configurable_chart(cr, uid, ids, context=context)
537                 obj_tax = self.pool.get('account.tax')
538                 obj_product = self.pool.get('product.product')
539                 ir_values = self.pool.get('ir.values')
540                 s_tax = (res.get('sale_tax',0.0))/100
541                 p_tax = (res.get('purchase_tax',0.0))/100
542                 tax_val = {}
543                 default_tax = []
544
545                 pur_temp_tax = mod_obj._get_id(cr, uid, 'account', 'tax_code_base_purchases')
546                 pur_temp_tax_id = mod_obj.read(cr, uid, [pur_temp_tax], ['res_id'])[0]['res_id']
547                 pur_temp_tax_names = obj_temp_tax_code.read(cr, uid, [pur_temp_tax_id], ['name'])
548                 pur_tax_parent_name = pur_temp_tax_names and pur_temp_tax_names[0]['name'] or False
549                 pur_taxcode_parent_id = obj_tax_code.search(cr, uid, [('name', 'ilike', pur_tax_parent_name)])
550                 if pur_taxcode_parent_id:
551                     pur_taxcode_parent_id = pur_taxcode_parent_id[0]
552                 else:
553                     pur_taxcode_parent_id = False
554
555                 pur_temp_tax_paid = mod_obj._get_id(cr, uid, 'account', 'tax_code_input')
556                 pur_temp_tax_paid_id = mod_obj.read(cr, uid, [pur_temp_tax_paid], ['res_id'])[0]['res_id']
557                 pur_temp_tax_paid_names = obj_temp_tax_code.read(cr, uid, [pur_temp_tax_paid_id], ['name'])
558                 pur_tax_paid_parent_name = pur_temp_tax_names and pur_temp_tax_paid_names[0]['name'] or False
559                 pur_taxcode_paid_parent_id = obj_tax_code.search(cr, uid, [('name', 'ilike', pur_tax_paid_parent_name)])
560                 if pur_taxcode_paid_parent_id:
561                     pur_taxcode_paid_parent_id = pur_taxcode_paid_parent_id[0]
562                 else:
563                     pur_taxcode_paid_parent_id = False
564
565                 sale_temp_tax = mod_obj._get_id(cr, uid, 'account', 'tax_code_base_sales')
566                 sale_temp_tax_id = mod_obj.read(cr, uid, [sale_temp_tax], ['res_id'])[0]['res_id']
567                 sale_temp_tax_names = obj_temp_tax_code.read(cr, uid, [sale_temp_tax_id], ['name'])
568                 sale_tax_parent_name = sale_temp_tax_names and sale_temp_tax_names[0]['name'] or False
569                 sale_taxcode_parent_id = obj_tax_code.search(cr, uid, [('name', 'ilike', sale_tax_parent_name)])
570                 if sale_taxcode_parent_id:
571                     sale_taxcode_parent_id = sale_taxcode_parent_id[0]
572                 else:
573                     sale_taxcode_parent_id = False
574
575                 sale_temp_tax_paid = mod_obj._get_id(cr, uid, 'account', 'tax_code_output')
576                 sale_temp_tax_paid_id = mod_obj.read(cr, uid, [sale_temp_tax_paid], ['res_id'])[0]['res_id']
577                 sale_temp_tax_paid_names = obj_temp_tax_code.read(cr, uid, [sale_temp_tax_paid_id], ['name'])
578                 sale_tax_paid_parent_name = sale_temp_tax_paid_names and sale_temp_tax_paid_names[0]['name'] or False
579                 sale_taxcode_paid_parent_id = obj_tax_code.search(cr, uid, [('name', 'ilike', sale_tax_paid_parent_name)])
580                 if sale_taxcode_paid_parent_id:
581                     sale_taxcode_paid_parent_id = sale_taxcode_paid_parent_id[0]
582                 else:
583                     sale_taxcode_paid_parent_id = False
584
585                 if s_tax*100 > 0.0:
586                     tax_account_ids = obj_acc.search(cr, uid, [('name','=','Tax Received')], context=context)
587                     sales_tax_account_id = tax_account_ids and tax_account_ids[0] or False
588                     vals_tax_code = {
589                         'name': 'TAX%s%%'%(s_tax*100),
590                         'code': 'TAX%s%%'%(s_tax*100),
591                         'company_id': company_id.id,
592                         'sign': 1,
593                         'parent_id': sale_taxcode_parent_id
594                         }
595                     new_tax_code = self.pool.get('account.tax.code').create(cr, uid, vals_tax_code)
596
597                     vals_paid_tax_code = {
598                         'name': 'TAX Received %s%%'%(s_tax*100),
599                         'code': 'TAX Received %s%%'%(s_tax*100),
600                         'company_id': company_id.id,
601                         'sign': 1,
602                         'parent_id': sale_taxcode_paid_parent_id
603                         }
604                     new_paid_tax_code = self.pool.get('account.tax.code').create(cr, uid, vals_paid_tax_code)
605
606                     sales_tax = obj_tax.create(cr, uid,
607                                            {'name':'TAX %s%%'%(s_tax*100),
608                                             'amount':s_tax,
609                                             'base_code_id':new_tax_code,
610                                             'tax_code_id':new_paid_tax_code,
611                                             'type_tax_use':'sale',
612                                             'account_collected_id':sales_tax_account_id,
613                                             'account_paid_id':sales_tax_account_id
614                                             })
615                     default_account_ids = obj_acc.search(cr, uid, [('name','=','Product Sales')],context=context)
616                     if default_account_ids:
617                         obj_acc.write(cr, uid, default_account_ids, {'tax_ids':[(6,0,[sales_tax])]})
618                     tax_val.update({'taxes_id':[(6,0,[sales_tax])]})
619                     default_tax.append(('taxes_id',sales_tax))
620                 if p_tax*100 > 0.0:
621                     tax_account_ids = obj_acc.search(cr, uid, [('name','=','Tax Paid')], context=context)
622                     purchase_tax_account_id = tax_account_ids and tax_account_ids[0] or False
623                     vals_tax_code = {
624                         'name': 'TAX%s%%'%(p_tax*100),
625                         'code': 'TAX%s%%'%(p_tax*100),
626                         'company_id': company_id.id,
627                         'sign': 1,
628                         'parent_id': pur_taxcode_parent_id
629                     }
630                     new_tax_code = self.pool.get('account.tax.code').create(cr, uid, vals_tax_code)
631
632                     vals_paid_tax_code = {
633                         'name': 'TAX Paid %s%%'%(p_tax*100),
634                         'code': 'TAX Paid %s%%'%(p_tax*100),
635                         'company_id': company_id.id,
636                         'sign': 1,
637                         'parent_id': pur_taxcode_paid_parent_id
638                     }
639                     new_paid_tax_code = self.pool.get('account.tax.code').create(cr, uid, vals_paid_tax_code)
640
641                     purchase_tax = obj_tax.create(cr, uid,
642                                             {'name':'TAX %s%%'%(p_tax*100),
643                                              'amount':p_tax,
644                                              'base_code_id':new_tax_code,
645                                             'tax_code_id':new_paid_tax_code,
646                                             'type_tax_use':'purchase',
647                                             'account_collected_id':purchase_tax_account_id,
648                                             'account_paid_id':purchase_tax_account_id
649                                              })
650                     default_account_ids = obj_acc.search(cr, uid, [('name','=','Expenses')], context=context)
651                     if default_account_ids:
652                         obj_acc.write(cr, uid, default_account_ids, {'tax_ids':[(6,0,[purchase_tax])]})
653                     tax_val.update({'supplier_taxes_id':[(6,0,[purchase_tax])]})
654                     default_tax.append(('supplier_taxes_id',purchase_tax))
655                 if tax_val:
656                     product_ids = obj_product.search(cr, uid, [])
657                     for product in obj_product.browse(cr, uid, product_ids):
658                         obj_product.write(cr, uid, product.id, tax_val)
659                     for name, value in default_tax:
660                         ir_values.set(cr, uid, key='default', key2=False, name=name, models =[('product.product',False)], value=[value])
661
662             if 'date_start' in res and 'date_stop' in res:
663                 f_ids = fy_obj.search(cr, uid, [('date_start', '<=', res['date_start']), ('date_stop', '>=', res['date_stop']), ('company_id','=',res['company_id'])])
664                 if not f_ids:
665                     name = code = res['date_start'][:4]
666                     if int(name) != int(res['date_stop'][:4]):
667                         name = res['date_start'][:4] +'-'+ res['date_stop'][:4]
668                         code = res['date_start'][2:4] +'-'+ res['date_stop'][2:4]
669                     vals = {'name': name,
670                             'code': code,
671                             'date_start': res['date_start'],
672                             'date_stop': res['date_stop'],
673                             'company_id': res['company_id']
674                            }
675                     fiscal_id = fy_obj.create(cr, uid, vals, context=context)
676                     if res['period'] == 'month':
677                         fy_obj.create_period(cr, uid, [fiscal_id])
678                     elif res['period'] == '3months':
679                         fy_obj.create_period3(cr, uid, [fiscal_id])
680
681
682     def modules_to_install(self, cr, uid, ids, context=None):
683         modules = super(account_installer, self).modules_to_install(
684             cr, uid, ids, context=context)
685         chart = self.read(cr, uid, ids, ['charts'],
686                           context=context)[0]['charts']
687         self.logger.notifyChannel(
688             'installer', netsvc.LOG_DEBUG,
689             'Installing chart of accounts %s'%chart)
690         return modules | set([chart])
691
692 account_installer()
693
694 class account_bank_accounts_wizard(osv.osv_memory):
695     _name='account.bank.accounts.wizard'
696
697     _columns = {
698         'acc_name': fields.char('Account Name.', size=64, required=True),
699         'bank_account_id': fields.many2one('wizard.multi.charts.accounts', 'Bank Account', required=True),
700         'currency_id': fields.many2one('res.currency', 'Secondary Currency', help="Forces all moves for this account to have this secondary currency."),
701         'account_type': fields.selection([('cash','Cash'),('check','Check'),('bank','Bank')], 'Account Type', size=32),
702     }
703 #    _defaults = {
704 #        'currency_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
705 #        }
706
707 account_bank_accounts_wizard()
708
709 class account_installer_modules(osv.osv_memory):
710     _name = 'account.installer.modules'
711     _inherit = 'res.config.installer'
712     _columns = {
713         # Accounting
714         'account_analytic_plans':fields.boolean('Multiple Analytic Plans',
715             help="Allows invoice lines to impact multiple analytic accounts "
716                  "simultaneously."),
717         'account_payment':fields.boolean('Suppliers Payment Management',
718             help="Streamlines invoice payment and creates hooks to plug "
719                  "automated payment systems in."),
720         'account_followup':fields.boolean('Followups Management',
721             help="Helps you generate reminder letters for unpaid invoices, "
722                  "including multiple levels of reminding and customized "
723                  "per-partner policies."),
724         'account_voucher':fields.boolean('Voucher Management',
725             help="Account Voucher module includes all the basic requirements of "
726                  "Voucher Entries for Bank, Cash, Sales, Purchase, Expenses, Contra, etc... "),
727         'account_anglo_saxon': fields.boolean('Anglo-Saxon Accounting',
728             help="This module will support the Anglo-Saxons accounting methodology by "
729                 "changing the accounting logic with stock transactions."),
730 #        'account_voucher_payment':fields.boolean('Voucher and Reconcile Management',
731 #            help="Extension Account Voucher module includes allows to link payment / receipt "
732 #                 "entries with voucher, also automatically reconcile during the payment and receipt entries."),
733     }
734
735     _defaults = {
736         'account_voucher': True,
737     }
738
739 account_installer_modules()
740
741 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: