ACCOUNT, ACCOUNT_TAX_INCLUDE, PURCHASE, PURCHASE_TAX_INCLUDE, SALE_TAX_INCLUDE: fix...
[odoo/odoo.git] / addons / purchase_tax_include / purchase_tax_incl.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 time
32 import netsvc
33 from osv import fields, osv
34 import ir
35
36 class purchase_order(osv.osv):
37         _inherit = "purchase.order"
38         def _amount_tax(self, cr, uid, ids, field_name, arg, context):
39                 res = {}
40                 cur_obj=self.pool.get('res.currency')
41                 for order in self.browse(cr, uid, ids):
42                         val = 0.0
43                         cur=order.pricelist_id.currency_id
44                         for line in order.order_line:
45                                 if order.price_type=='tax_included':
46                                         ttt = self.pool.get('account.tax').compute_inv(cr, uid, line.taxes_id, line.price_unit, line.product_qty, order.partner_address_id.id, line.product_id, order.partner_id)
47                                 else:
48                                         ttt = self.pool.get('account.tax').compute(cr, uid, line.taxes_id, line.price_unit, line.product_qty, order.partner_address_id.id, line.product_id, order.partner_id)
49                                 for c in ttt:
50                                         val += cur_obj.round(cr, uid, cur, c['amount'])
51                         res[order.id]=cur_obj.round(cr, uid, cur, val)
52                 return res
53         _columns = {
54                 'price_type': fields.selection([
55                         ('tax_included','Tax included'),
56                         ('tax_excluded','Tax excluded')
57                 ], 'Price method', required=True),
58                 'amount_tax': fields.function(_amount_tax, method=True, string='Taxes'),
59         }
60         _defaults = {
61                 'price_type': lambda *a: 'tax_excluded',
62         }
63         def _inv_get(self, cr, uid, order, context={}):
64                 return {
65                         'price_type': order.price_type
66                 }
67 purchase_order()
68
69 class purchase_order_line(osv.osv):
70         _inherit = 'purchase.order.line'
71         def _amount_line(self, cr, uid, ids, name, arg, context):
72                 res = {}
73                 cur_obj=self.pool.get('res.currency')
74                 tax_obj = self.pool.get('account.tax')
75                 res = super(purchase_order_line, self)._amount_line(cr, uid, ids, name, arg, context)
76                 res2 = res.copy()
77                 for line in self.browse(cr, uid, ids):
78                         if line.order_id.price_type == 'tax_included':
79                                 if line.product_id:
80                                         for tax in tax_obj.compute_inv(cr, uid, line.product_id.supplier_taxes_id, res[line.id]/line.product_qty, line.product_qty):
81                                                 res[line.id] = res[line.id] - tax['amount']
82                                 else:
83                                         for tax in tax_obj.compute_inv(cr, uid, line.taxes_id, res[line.id]/line.product_qty, line.product_qty):
84                                                 res[line.id] = res[line.id] - tax['amount']
85                         if name == 'price_subtotal_incl' and line.order_id.price_type == 'tax_included':
86                                 if line.product_id:
87                                         prod_taxe_ids = [ t.id for t in line.product_id.supplier_taxes_id ]
88                                         prod_taxe_ids.sort()
89                                         line_taxe_ids = [ t.id for t in line.taxes_id ]
90                                         line_taxe_ids.sort()
91                                 if line.product_id and prod_taxe_ids == line_taxe_ids:
92                                         res[line.id] = res2[line.id]
93                                 elif not line.product_id:
94                                         res[line.id] = res2[line.id]
95                                 else:
96                                         for tax in tax_obj.compute(cr, uid, line.taxes_id, res[line.id]/line.product_qty, line.product_qty):
97                                                 res[line.id] = res[line.id] + tax['amount']
98                         cur = line.order_id.pricelist_id.currency_id
99                         res[line.id] = cur_obj.round(cr, uid, cur, res[line.id])
100                 return res
101         _columns = {
102                 'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal w/o tax'),
103                 'price_subtotal_incl': fields.function(_amount_line, method=True, string='Subtotal'),
104         }
105 purchase_order_line()