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