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 #################################################################################
20 from openerp import pooler
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 res = pooler.get_pool(cr.dbname).get('decimal.precision').precision_get(cr, 1, 'Account')
97 return change_digit_tax
99 class account_tax_template(osv.osv):
100 """ Add fields used to define some brazilian taxes """
101 _inherit = 'account.tax.template'
104 'tax_discount': fields.boolean('Discount this Tax in Prince',
105 help="Mark it for (ICMS, PIS e etc.)."),
106 'base_reduction': fields.float('Redution', required=True,
107 digits_compute=get_precision_tax(),
108 help="Um percentual decimal em % entre 0-1."),
109 'amount_mva': fields.float('MVA Percent', required=True,
110 digits_compute=get_precision_tax(),
111 help="Um percentual decimal em % entre 0-1."),
112 'type': fields.selection([('percent','Percentage'),
113 ('fixed','Fixed Amount'),
115 ('code','Python Code'),
116 ('balance','Balance'),
117 ('quantity','Quantity')], 'Tax Type', required=True,
118 help="The computation method for the tax amount."),
120 _defaults = TAX_DEFAULTS
122 def _generate_tax(self, cr, uid, tax_templates, tax_code_template_ref, company_id, context=None):
124 This method generate taxes from templates.
126 :param tax_templates: list of browse record of the tax templates to process
127 :param tax_code_template_ref: Taxcode templates reference.
128 :param company_id: id of the company the wizard is running for
131 'tax_template_to_tax': mapping between tax template and the newly generated taxes corresponding,
132 'account_dict': dictionary containing a to-do list with all the accounts to assign on new taxes
135 result = super(account_tax_template, self)._generate_tax(cr, uid,
137 tax_code_template_ref,
140 tax_templates = self.browse(cr, uid, result['tax_template_to_tax'].keys(), context)
141 obj_acc_tax = self.pool.get('account.tax')
142 for tax_template in tax_templates:
143 if tax_template.tax_code_id:
144 obj_acc_tax.write(cr, uid, result['tax_template_to_tax'][tax_template.id], {'domain': tax_template.tax_code_id.domain,
145 'tax_discount': tax_template.tax_code_id.tax_discount})
148 def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
150 result = {'value': {}}
155 obj_tax_code = self.pool.get('account.tax.code.template').browse(cr, uid, tax_code_id)
158 result['value']['tax_discount'] = obj_tax_code.tax_discount
159 result['value']['domain'] = obj_tax_code.domain
165 class account_tax(osv.osv):
166 """ Add fields used to define some brazilian taxes """
167 _inherit = 'account.tax'
170 'tax_discount': fields.boolean('Discount this Tax in Prince',
171 help="Mark it for (ICMS, PIS e etc.)."),
172 'base_reduction': fields.float('Redution', required=True,
173 digits_compute=get_precision_tax(),
174 help="Um percentual decimal em % entre 0-1."),
175 'amount_mva': fields.float('MVA Percent', required=True,
176 digits_compute=get_precision_tax(),
177 help="Um percentual decimal em % entre 0-1."),
178 'type': fields.selection([('percent','Percentage'),
179 ('fixed','Fixed Amount'),
181 ('code','Python Code'),
182 ('balance','Balance'),
183 ('quantity','Quantity')], 'Tax Type', required=True,
184 help="The computation method for the tax amount."),
186 _defaults = TAX_DEFAULTS
188 def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
190 result = {'value': {}}
195 obj_tax_code = self.pool.get('account.tax.code').browse(cr, uid, tax_code_id)
198 result['value']['tax_discount'] = obj_tax_code.tax_discount
199 result['value']['domain'] = obj_tax_code.domain