[merge]
[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 time
21 from datetime import datetime, timedelta
22 from dateutil.relativedelta import relativedelta
23 from operator import itemgetter
24
25 import netsvc
26 import pooler
27 from osv import fields, osv
28 import decimal_precision as dp
29 from tools.misc import currency
30 from tools.translate import _
31 from tools import config
32
33 class account_tax_code_template(osv.osv):
34
35     _inherit = 'account.tax.code.template'
36     _columns = {
37                 'domain':fields.char('Domain', size=32, help="This field is only used if you develop your own module allowing developers to create specific taxes in a custom domain."),
38                 'tax_discount': fields.boolean('Discount this Tax in Prince', help="Mark it for (ICMS, PIS e etc.)."),
39         }
40 account_tax_code_template()
41
42 class account_tax_code(osv.osv):
43
44     _inherit = 'account.tax.code'
45     _columns = {
46                 'domain':fields.char('Domain', size=32, help="This field is only used if you develop your own module allowing developers to create specific taxes in a custom domain."),
47                 'tax_discount': fields.boolean('Discount this Tax in Prince', help="Mark it for (ICMS, PIS e etc.)."),
48         }
49 account_tax_code()
50
51 class account_tax_template(osv.osv):
52     _inherit = 'account.tax.template'
53     
54     def get_precision_tax():
55         def change_digit_tax(cr):
56             res = pooler.get_pool(cr.dbname).get('decimal.precision').precision_get(cr, 1, 'Account')
57             return (16, res+2)
58         return change_digit_tax
59     
60     _columns = {
61                 'tax_discount': fields.boolean('Discount this Tax in Prince', help="Mark it for (ICMS, PIS e etc.)."),
62                 'base_reduction': fields.float('Redution', required=True, digits_compute=get_precision_tax(), help="For taxes of type percentage, enter % ratio between 0-1."),
63                 'amount_mva': fields.float('MVA Percent', required=True, digits_compute=get_precision_tax(), help="For taxes of type percentage, enter % ratio between 0-1."),
64                 'type': fields.selection( [('percent','Percentage'), ('fixed','Fixed Amount'), ('none','None'), ('code','Python Code'), ('balance','Balance'), ('quantity','Quantity')], 'Tax Type', required=True,
65                                           help="The computation method for the tax amount."),
66     }
67     _defaults = {
68                 'base_reduction': 0,
69                 'amount_mva': 0,
70     }
71     
72     def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
73         
74         result = {'value': {}}
75             
76         if not tax_code_id:
77             return result
78         
79         obj_tax_code = self.pool.get('account.tax.code.template').browse(cr, uid, tax_code_id)     
80     
81         if obj_tax_code:
82             result['value']['tax_discount'] = obj_tax_code.tax_discount
83             result['value']['domain'] = obj_tax_code.domain
84
85         return result
86
87 account_tax_template()
88
89 class account_tax(osv.osv):
90     _inherit = 'account.tax'
91     
92     def get_precision_tax():
93         def change_digit_tax(cr):
94             res = pooler.get_pool(cr.dbname).get('decimal.precision').precision_get(cr, 1, 'Account')
95             return (16, res+2)
96         return change_digit_tax
97     
98     _columns = {
99                 'tax_discount': fields.boolean('Discount this Tax in Prince', help="Mark it for (ICMS, PIS e etc.)."),
100                 'base_reduction': fields.float('Redution', required=True, digits_compute=get_precision_tax(), help="Um percentual decimal em % entre 0-1."),
101                 'amount_mva': fields.float('MVA Percent', required=True, digits_compute=get_precision_tax(), help="Um percentual decimal em % entre 0-1."),
102                 'type': fields.selection( [('percent','Percentage'), ('fixed','Fixed Amount'), ('none','None'), ('code','Python Code'), ('balance','Balance'), ('quantity','Quantity')], 'Tax Type', required=True,
103                                           help="The computation method for the tax amount."),
104     }
105     _defaults = {
106                  'base_reduction': 0,
107                  'amount_mva': 0,
108     }
109     
110     def onchange_tax_code_id(self, cr, uid, ids, tax_code_id, context=None):
111         
112         result = {'value': {}}
113             
114         if not tax_code_id:
115             return result
116         
117         obj_tax_code = self.pool.get('account.tax.code').browse(cr, uid, tax_code_id)      
118     
119         if obj_tax_code:
120             result['value']['tax_discount'] = obj_tax_code.tax_discount
121             result['value']['domain'] = obj_tax_code.domain
122
123         return result
124
125 account_tax()
126
127 class wizard_multi_charts_accounts(osv.osv_memory):
128
129     _inherit = 'wizard.multi.charts.accounts'
130     
131     def execute(self, cr, uid, ids, context=None):
132         
133         super(wizard_multi_charts_accounts, self).execute(cr, uid, ids, context)
134         
135         obj_multi = self.browse(cr, uid, ids[0])
136         obj_acc_tax = self.pool.get('account.tax')
137         obj_acc_tax_tmp = self.pool.get('account.tax.template')
138         obj_acc_cst = self.pool.get('l10n_br_account.cst')
139         obj_acc_cst_tmp = self.pool.get('l10n_br_account.cst.template')
140         obj_tax_code = self.pool.get('account.tax.code')
141         obj_tax_code_tmp = self.pool.get('account.tax.code.template')
142
143         # Creating Account
144         obj_acc_root = obj_multi.chart_template_id.account_root_id
145         tax_code_root_id = obj_multi.chart_template_id.tax_code_root_id.id
146         company_id = obj_multi.company_id.id
147         
148         children_tax_code_template = self.pool.get('account.tax.code.template').search(cr, uid, [('parent_id','child_of',[tax_code_root_id])], order='id')
149         children_tax_code_template.sort()
150         for tax_code_template in self.pool.get('account.tax.code.template').browse(cr, uid, children_tax_code_template, context=context):
151             tax_code_id = self.pool.get('account.tax.code').search(cr, uid, [('code','=',tax_code_template.code),('company_id','=',company_id)])
152             if tax_code_id:
153                 obj_tax_code.write(cr, uid, tax_code_id, {'domain': tax_code_template.domain,'tax_discount': tax_code_template.tax_discount})
154             
155                 cst_tmp_ids = self.pool.get('l10n_br_account.cst.template').search(cr, uid, [('tax_code_template_id','=',tax_code_template.id)], order='id')
156                 for cst_tmp in self.pool.get('l10n_br_account.cst.template').browse(cr, uid, cst_tmp_ids, context=context):
157                     obj_acc_cst.create(cr, uid, {
158                                                  'code': cst_tmp.code,
159                                                  'name': cst_tmp.name,
160                                                  'tax_code_id': tax_code_id[0],
161                                                  })
162             
163         tax_ids = self.pool.get('account.tax').search(cr, uid, [('company_id','=',company_id)])
164         for tax in self.pool.get('account.tax').browse(cr, uid, tax_ids, context=context):
165             if tax.tax_code_id:
166                 obj_acc_tax.write(cr, uid, tax.id, {'domain': tax.tax_code_id.domain,'tax_discount': tax.tax_code_id.tax_discount})
167         
168 wizard_multi_charts_accounts()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: