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