78b2d630111f5e24a3c85b64e2e3401788185955
[odoo/odoo.git] / addons / account_tax_include / account.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 import netsvc
32 from osv import fields, osv
33
34 class account_tax(osv.osv):
35         _inherit = 'account.tax'
36         _description = 'Tax'
37         _columns = {
38                 'python_compute_inv':fields.text('Python Code (VAT Incl)'),
39         }
40         _defaults = {
41                 'python_compute_inv': lambda *a: '''# price_unit\n# address : res.partner.address object or False\n# product : product.product object or False\n\nresult = price_unit * 0.10''',
42         }
43         def _unit_compute_inv(self, cr, uid, taxes, price_unit, address_id=None, product=None):
44                 taxes = self._applicable(cr, uid, taxes, price_unit, address_id)
45
46                 res = []
47                 taxes.reverse()
48                 cur_price_unit=price_unit
49                 for tax in taxes:
50                         # we compute the amount for the current tax object and append it to the result
51                         if tax.type=='percent':
52                                 amount = cur_price_unit - (cur_price_unit / (1 + tax.amount))
53                                 res.append({'id':tax.id, 'name':tax.name, 'amount':amount, 'account_collected_id':tax.account_collected_id.id, 'account_paid_id':tax.account_paid_id.id, 'base_code_id': tax.base_code_id.id, 'ref_base_code_id': tax.ref_base_code_id.id, 'sequence': tax.sequence, 'base_sign': tax.base_sign, 'ref_base_sign': tax.ref_base_sign, 'price_unit': cur_price_unit - amount,})
54                         elif tax.type=='fixed':
55                                 res.append({'id':tax.id, 'name':tax.name, 'amount':tax.amount, 'account_collected_id':tax.account_collected_id.id, 'account_paid_id':tax.account_paid_id.id, 'base_code_id': tax.base_code_id.id, 'ref_base_code_id': tax.ref_base_code_id.id, 'sequence': tax.sequence, 'base_sign': tax.base_sign, 'ref_base_sign': tax.ref_base_sign, 'price_unit': 1,})
56                         elif tax.type=='code':
57                                 address = address_id and self.pool.get('res.partner.address').browse(cr, uid, address_id) or None
58                                 localdict = {'price_unit':cur_price_unit, 'address':address, 'product':product,}
59                                 exec tax.python_compute_inv in localdict
60                                 amount = localdict['result']
61                                 res.append({
62                                         'id': tax.id,
63                                         'name': tax.name,
64                                         'amount': amount,
65                                         'account_collected_id': tax.account_collected_id.id,
66                                         'account_paid_id': tax.account_paid_id.id,
67                                         'base_code_id': tax.base_code_id.id,
68                                         'ref_base_code_id': tax.ref_base_code_id.id,
69                                         'sequence': tax.sequence,
70                                         'base_sign': tax.base_sign,
71                                         'ref_base_sign': tax.ref_base_sign,
72                                         'price_unit': cur_price_unit - amount,
73                                 })
74                         amount2 = res[-1]['amount']
75                         if len(tax.child_ids):
76                                 if tax.child_depend:
77                                         del res[-1]
78                                         amount = amount2
79                                 else:
80                                         amount = amount2
81                         for t in tax.child_ids:
82                                 parent_tax = self._unit_compute_inv(cr, uid, [t], amount, address_id)
83                                 res.extend(parent_tax)
84                         if tax.include_base_amount:
85                                 cur_price_unit-=amount
86                 taxes.reverse()
87                 return res
88
89         def compute_inv(self, cr, uid, taxes, price_unit, quantity, address_id=None, product=None):
90                 """
91                 Compute tax values for given PRICE_UNIT, QUANTITY and a buyer/seller ADDRESS_ID.
92                 Price Unit is a VAT included price
93
94                 RETURN:
95                         [ tax ]
96                         tax = {'name':'', 'amount':0.0, 'account_collected_id':1, 'account_paid_id':2}
97                         one tax for each tax id in IDS and their childs
98                 """
99                 res = self._unit_compute_inv(cr, uid, taxes, price_unit, address_id, product)
100                 for r in res:
101                         r['amount'] *= quantity
102                 return res
103 account_tax()
104