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