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