1 # -*- encoding: utf-8 -*-
2 #################################################################################
4 # Copyright (C) 2009 Renato Lima - Akretion #
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. #
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. #
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 #################################################################################
21 from openerp.osv import fields, osv
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)."),
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
40 def generate_tax_code(self, cr, uid, tax_code_root_id, company_id,
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
47 :param tax_code_root_id: id of the root of all the tax code templates
49 :param company_id: id of the company the wizard is running for
50 :returns: dictionary with the mappping between the templates and the
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)
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
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,
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)
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
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
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')
98 return change_digit_tax
100 class account_tax_template(osv.osv):
101 """ Add fields used to define some brazilian taxes """
102 _inherit = 'account.tax.template'
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'),
116 ('code','Python Code'),
117 ('balance','Balance'),
118 ('quantity','Quantity')], 'Tax Type', required=True,
119 help="The computation method for the tax amount."),
121 _defaults = TAX_DEFAULTS
123 def _generate_tax(self, cr, uid, tax_templates, tax_code_template_ref, company_id, context=None):
125 This method generate taxes from templates.
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
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
136 result = super(account_tax_template, self)._generate_tax(cr, uid,
138 tax_code_template_ref,
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})
149 def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
151 result = {'value': {}}
156 obj_tax_code = self.pool.get('account.tax.code.template').browse(cr, uid, tax_code_id)
159 result['value']['tax_discount'] = obj_tax_code.tax_discount
160 result['value']['domain'] = obj_tax_code.domain
166 class account_tax(osv.osv):
167 """ Add fields used to define some brazilian taxes """
168 _inherit = 'account.tax'
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'),
182 ('code','Python Code'),
183 ('balance','Balance'),
184 ('quantity','Quantity')], 'Tax Type', required=True,
185 help="The computation method for the tax amount."),
187 _defaults = TAX_DEFAULTS
189 def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
191 result = {'value': {}}
196 obj_tax_code = self.pool.get('account.tax.code').browse(cr, uid, tax_code_id)
199 result['value']['tax_discount'] = obj_tax_code.tax_discount
200 result['value']['domain'] = obj_tax_code.domain