[FIX] Merge fix in saas1 for the problem that made the server crash when install...
[odoo/odoo.git] / addons / l10n_br / account.py
1 # -*- encoding: utf-8 -*-
2 #################################################################################
3 #                                                                               #
4 # Copyright (C) 2009  Renato Lima - Akretion                                    #
5 #                                                                               #
6 #This program is free software: you can redistribute it and/or modify           #
7 #it under the terms of the GNU Affero General Public License as published by    #
8 #the Free Software Foundation, either version 3 of the License, or              #
9 #(at your option) any later version.                                            #
10 #                                                                               #
11 #This program is distributed in the hope that it will be useful,                #
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of                 #
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                  #
14 #GNU General Public License for more details.                                   #
15 #                                                                               #
16 #You should have received a copy of the GNU General Public License              #
17 #along with this program.  If not, see <http://www.gnu.org/licenses/>.          #
18 #################################################################################
19
20 import openerp
21 from openerp.osv import fields, osv
22
23 TAX_CODE_COLUMNS = {
24                     'domain':fields.char('Domain', size=32, 
25                                          help="This field is only used if you develop your own module allowing developers to create specific taxes in a custom domain."),
26                     'tax_discount': fields.boolean('Discount this Tax in Prince', 
27                                                    help="Mark it for (ICMS, PIS, COFINS and others taxes included)."),
28                     }
29
30 TAX_DEFAULTS = {
31                 'base_reduction': 0,
32                 'amount_mva': 0,
33                 }
34
35 class account_tax_code_template(osv.osv):
36     """ Add fields used to define some brazilian taxes """
37     _inherit = 'account.tax.code.template'
38     _columns = TAX_CODE_COLUMNS
39
40     def generate_tax_code(self, cr, uid, tax_code_root_id, company_id, 
41                          context=None):
42         """This function generates the tax codes from the templates of tax 
43         code that are children of the given one passed in argument. Then it 
44         returns a dictionary with the mappping between the templates and the 
45         real objects.
46
47         :param tax_code_root_id: id of the root of all the tax code templates 
48                                  to process.
49         :param company_id: id of the company the wizard is running for
50         :returns: dictionary with the mappping between the templates and the 
51                   real objects.
52         :rtype: dict
53         """
54         obj_tax_code_template = self.pool.get('account.tax.code.template')
55         obj_tax_code = self.pool.get('account.tax.code')
56         tax_code_template_ref = {}
57         company = self.pool.get('res.company').browse(cr, uid, company_id, context=context)
58
59         #find all the children of the tax_code_root_id
60         children_tax_code_template = tax_code_root_id and obj_tax_code_template.search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id') or []
61         for tax_code_template in obj_tax_code_template.browse(cr, uid, children_tax_code_template, context=context):
62             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
63             vals = {
64                 'name': (tax_code_root_id == tax_code_template.id) and company.name or tax_code_template.name,
65                 'code': tax_code_template.code,
66                 'info': tax_code_template.info,
67                 'parent_id': parent_id,
68                 'company_id': company_id,
69                 'sign': tax_code_template.sign,
70                 'domain': tax_code_template.domain,
71                 'tax_discount': tax_code_template.tax_discount,
72             }
73             #check if this tax code already exists
74             rec_list = obj_tax_code.search(cr, uid, [('name', '=', vals['name']),
75                                                      ('parent_id','=',parent_id),
76                                                      ('code', '=', vals['code']),
77                                                      ('company_id', '=', vals['company_id'])], context=context)
78             if not rec_list:
79                 #if not yet, create it
80                 new_tax_code = obj_tax_code.create(cr, uid, vals)
81                 #recording the new tax code to do the mapping
82                 tax_code_template_ref[tax_code_template.id] = new_tax_code
83         return tax_code_template_ref
84     
85
86
87 class account_tax_code(osv.osv):
88     """ Add fields used to define some brazilian taxes """
89     _inherit = 'account.tax.code'
90     _columns = TAX_CODE_COLUMNS
91
92
93 def get_precision_tax():
94     def change_digit_tax(cr):
95         decimal_precision = openerp.registry(cr.dbname)['decimal.precision']
96         res = decimal_precision.precision_get(cr, 1, 'Account')
97         return (16, res+2)
98     return change_digit_tax
99
100 class account_tax_template(osv.osv):
101     """ Add fields used to define some brazilian taxes """
102     _inherit = 'account.tax.template'
103     
104     _columns = {
105                'tax_discount': fields.boolean('Discount this Tax in Prince', 
106                                               help="Mark it for (ICMS, PIS e etc.)."),
107                'base_reduction': fields.float('Redution', required=True, 
108                                               digits_compute=get_precision_tax(), 
109                                               help="Um percentual decimal em % entre 0-1."),
110                'amount_mva': fields.float('MVA Percent', required=True, 
111                                           digits_compute=get_precision_tax(), 
112                                           help="Um percentual decimal em % entre 0-1."),
113                'type': fields.selection([('percent','Percentage'), 
114                                          ('fixed','Fixed Amount'), 
115                                          ('none','None'), 
116                                          ('code','Python Code'), 
117                                          ('balance','Balance'), 
118                                          ('quantity','Quantity')], 'Tax Type', required=True,
119                                         help="The computation method for the tax amount."),
120                }
121     _defaults = TAX_DEFAULTS
122     
123     def _generate_tax(self, cr, uid, tax_templates, tax_code_template_ref, company_id, context=None):
124         """
125         This method generate taxes from templates.
126
127         :param tax_templates: list of browse record of the tax templates to process
128         :param tax_code_template_ref: Taxcode templates reference.
129         :param company_id: id of the company the wizard is running for
130         :returns:
131             {
132             'tax_template_to_tax': mapping between tax template and the newly generated taxes corresponding,
133             'account_dict': dictionary containing a to-do list with all the accounts to assign on new taxes
134             }
135         """
136         result = super(account_tax_template, self)._generate_tax(cr, uid, 
137                                                                  tax_templates, 
138                                                                  tax_code_template_ref, 
139                                                                  company_id, 
140                                                                  context)
141         tax_templates = self.browse(cr, uid, result['tax_template_to_tax'].keys(), context)   
142         obj_acc_tax = self.pool.get('account.tax')
143         for tax_template in tax_templates:
144             if tax_template.tax_code_id:
145                 obj_acc_tax.write(cr, uid, result['tax_template_to_tax'][tax_template.id], {'domain': tax_template.tax_code_id.domain,
146                                                                                             'tax_discount': tax_template.tax_code_id.tax_discount})    
147         return result
148     
149     def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
150
151         result = {'value': {}}
152
153         if not tax_code_id:
154             return result
155
156         obj_tax_code = self.pool.get('account.tax.code.template').browse(cr, uid, tax_code_id)     
157
158         if obj_tax_code:
159             result['value']['tax_discount'] = obj_tax_code.tax_discount
160             result['value']['domain'] = obj_tax_code.domain
161
162         return result
163
164
165
166 class account_tax(osv.osv):
167     """ Add fields used to define some brazilian taxes """
168     _inherit = 'account.tax'
169     
170     _columns = {
171                'tax_discount': fields.boolean('Discount this Tax in Prince', 
172                                               help="Mark it for (ICMS, PIS e etc.)."),
173                'base_reduction': fields.float('Redution', required=True, 
174                                               digits_compute=get_precision_tax(), 
175                                               help="Um percentual decimal em % entre 0-1."),
176                'amount_mva': fields.float('MVA Percent', required=True, 
177                                           digits_compute=get_precision_tax(), 
178                                           help="Um percentual decimal em % entre 0-1."),
179                'type': fields.selection([('percent','Percentage'), 
180                                          ('fixed','Fixed Amount'), 
181                                          ('none','None'), 
182                                          ('code','Python Code'), 
183                                          ('balance','Balance'), 
184                                          ('quantity','Quantity')], 'Tax Type', required=True,
185                                         help="The computation method for the tax amount."),
186                }
187     _defaults = TAX_DEFAULTS
188     
189     def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
190
191         result = {'value': {}}
192
193         if not tax_code_id:
194             return result
195         
196         obj_tax_code = self.pool.get('account.tax.code').browse(cr, uid, tax_code_id)      
197     
198         if obj_tax_code:
199             result['value']['tax_discount'] = obj_tax_code.tax_discount
200             result['value']['domain'] = obj_tax_code.domain
201
202         return result
203
204