reindent all xml files
[odoo/odoo.git] / addons / account_tax_include / invoice_tax_incl.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
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 account_invoice(osv.osv):
37         def _amount_untaxed(self, cr, uid, ids, name, args, context={}):
38                 res = {}
39                 for invoice in self.browse(cr,uid,ids):
40                         if invoice.price_type == 'tax_included':
41                                 res[invoice.id] = reduce( lambda x, y: x+y.price_subtotal, invoice.invoice_line,0)
42                         else:
43                                 res[invoice.id] = super(account_invoice, self)._amount_untaxed(cr, uid, [invoice.id], name, args, context)[invoice.id]
44                 return res
45
46         def _amount_tax(self, cr, uid, ids, name, args, context={}):
47                 res = {}
48                 for invoice in self.browse(cr,uid,ids):
49                         if invoice.price_type == 'tax_included':
50                                 res[invoice.id] = reduce( lambda x, y: x+y.amount, invoice.tax_line,0)
51                         else:
52                                 res[invoice.id] = super(account_invoice, self)._amount_tax(cr, uid, [invoice.id], name, args, context)[invoice.id]
53                 return res
54
55         def _amount_total(self, cr, uid, ids, name, args, context={}):
56                 res = {}
57                 for invoice in self.browse(cr,uid,ids):
58                         if invoice.price_type == 'tax_included':
59                                 res[invoice.id]= invoice.amount_untaxed + invoice.amount_tax
60                         else:
61                                 res[invoice.id] = super(account_invoice, self)._amount_total(cr, uid, [invoice.id], name, args, context)[invoice.id]
62                 return res
63
64         _inherit = "account.invoice"
65         _columns = {
66                 'price_type': fields.selection([('tax_included','Tax included'),
67                                                                                 ('tax_excluded','Tax excluded')],
68                                                                                 'Price method', required=True, readonly=True,
69                                                                                 states={'draft':[('readonly',False)]}),
70                 'amount_untaxed': fields.function(_amount_untaxed, digits=(16,2), method=True,string='Untaxed Amount'),
71                 'amount_tax': fields.function(_amount_tax, method=True, string='Tax', store=True),
72                 'amount_total': fields.function(_amount_total, method=True, string='Total', store=True),
73         }
74         _defaults = {
75                 'price_type': lambda *a: 'tax_excluded',
76         }
77 account_invoice()
78
79 class account_invoice_line(osv.osv):
80         _inherit = "account.invoice.line"
81         def _amount_line(self, cr, uid, ids, name, args, context={}):
82                 """
83                 Return the subtotal excluding taxes with respect to price_type.
84                 """
85                 res = {}
86                 tax_obj = self.pool.get('account.tax')
87                 res = super(account_invoice_line, self)._amount_line(cr, uid, ids, name, args, context)
88                 res2 = res.copy()
89                 for line in self.browse(cr, uid, ids):
90                         if not line.quantity:
91                                 res[line.id] = 0.0
92                                 continue
93                         if line.invoice_id and line.invoice_id.price_type == 'tax_included':
94                                 product_taxes = None
95                                 if line.product_id:
96                                         if line.invoice_id.type in ('out_invoice', 'out_refund'):
97                                                 product_taxes = line.product_id.taxes_id
98                                         else:
99                                                 product_taxes = line.product_id.supplier_taxes_id
100                                 if product_taxes:
101                                         for tax in tax_obj.compute_inv(cr, uid, product_taxes, res[line.id]/line.quantity, line.quantity):
102                                                 res[line.id] = res[line.id] - round(tax['amount'], 2)
103                                 else:
104                                         for tax in tax_obj.compute_inv(cr, uid,line.invoice_line_tax_id, res[line.id]/line.quantity, line.quantity):
105                                                 res[line.id] = res[line.id] - round(tax['amount'], 2)
106                         if name == 'price_subtotal_incl' and line.invoice_id and line.invoice_id.price_type == 'tax_included':
107                                 prod_taxe_ids = None
108                                 line_taxe_ids = None
109                                 if product_taxes:
110                                         prod_taxe_ids = [ t.id for t in product_taxes ]
111                                         prod_taxe_ids.sort()
112                                         line_taxe_ids = [ t.id for t in line.invoice_line_tax_id ]
113                                         line_taxe_ids.sort()
114                                 if product_taxes and prod_taxe_ids == line_taxe_ids:
115                                         res[line.id] = res2[line.id]
116                                 elif not line.product_id:
117                                         res[line.id] = res2[line.id]
118                                 else:
119                                         for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, res[line.id]/line.quantity, line.quantity):
120                                                 res[line.id] = res[line.id] + tax['amount']
121                         res[line.id]= round(res[line.id], 2)
122                 return res
123
124         def _price_unit_default(self, cr, uid, context={}):
125                 if 'check_total' in context:
126                         t = context['check_total']
127                         if context.get('price_type', False) == 'tax_included':
128                                 for l in context.get('invoice_line', {}):
129                                         if len(l) >= 3 and l[2]:
130                                                 p = l[2].get('price_unit', 0) * (1-l[2].get('discount', 0)/100.0)
131                                                 t = t - (p * l[2].get('quantity'))
132                                 return t
133                         return super(account_invoice_line, self)._price_unit_default(cr, uid, context)
134                 return 0
135
136         _columns = {
137                 'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal w/o tax', store=True),
138                 'price_subtotal_incl': fields.function(_amount_line, method=True, string='Subtotal'),
139         }
140
141         _defaults = {
142                 'price_unit': _price_unit_default,
143         }
144
145         def move_line_get_item(self, cr, uid, line, context={}):
146                 return {
147                                 'type':'src',
148                                 'name':line.name,
149                                 'price_unit':line.price_subtotal / line.quantity,
150                                 'quantity':line.quantity,
151                                 'price':line.price_subtotal,
152                                 'account_id':line.account_id.id,
153                                 'product_id': line.product_id.id,
154                                 'uos_id':line.uos_id.id,
155                                 'account_analytic_id':line.account_analytic_id.id,
156                         }
157
158         def product_id_change_unit_price_inv(self, cr, uid, tax_id, price_unit, qty, address_invoice_id, product, partner_id, context={}):
159                 if context.get('price_type', False) == 'tax_included':
160                         return {'price_unit': price_unit,'invoice_line_tax_id': tax_id}
161                 else:
162                         return super(account_invoice_line, self).product_id_change_unit_price_inv(cr, uid, tax_id, price_unit, qty, address_invoice_id, product, partner_id, context=context)
163
164         def product_id_change(self, cr, uid, ids, product, uom, qty=0, name='', type='out_invoice', partner_id=False, price_unit=False, address_invoice_id=False, price_type='tax_excluded', context={}):
165                 context.update({'price_type': price_type})
166                 return super(account_invoice_line, self).product_id_change(cr, uid, ids, product, uom, qty, name, type, partner_id, price_unit, address_invoice_id, context=context)
167 account_invoice_line()
168
169 class account_invoice_tax(osv.osv):
170         _inherit = "account.invoice.tax"
171
172         def compute(self, cr, uid, invoice_id):
173                 tax_grouped = {}
174                 tax_obj = self.pool.get('account.tax')
175                 cur_obj = self.pool.get('res.currency')
176                 inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id)
177                 cur = inv.currency_id
178
179                 if inv.price_type=='tax_excluded':
180                         return super(account_invoice_tax,self).compute(cr, uid, invoice_id)
181
182                 for line in inv.invoice_line:
183                         for tax in tax_obj.compute_inv(cr, uid, line.invoice_line_tax_id, (line.price_unit * (1-(line.discount or 0.0)/100.0)), line.quantity, inv.address_invoice_id.id, line.product_id, inv.partner_id):
184                                 val={}
185                                 val['invoice_id'] = inv.id
186                                 val['name'] = tax['name']
187                                 val['amount'] = cur_obj.round(cr, uid, cur, tax['amount'])
188                                 val['manual'] = False
189                                 val['sequence'] = tax['sequence']
190                                 val['base'] = tax['price_unit'] * line['quantity']
191
192                                 if inv.type in ('out_invoice','in_invoice'):
193                                         val['base_code_id'] = tax['base_code_id']
194                                         val['tax_code_id'] = tax['tax_code_id']
195                                         val['base_amount'] = val['base'] * tax['base_sign']
196                                         val['tax_amount'] = val['amount'] * tax['tax_sign']
197                                         val['account_id'] = tax['account_collected_id'] or line.account_id.id
198                                 else:
199                                         val['base_code_id'] = tax['ref_base_code_id']
200                                         val['tax_code_id'] = tax['ref_tax_code_id']
201                                         val['base_amount'] = val['base'] * tax['ref_base_sign']
202                                         val['tax_amount'] = val['amount'] * tax['ref_tax_sign']
203                                         val['account_id'] = tax['account_paid_id'] or line.account_id.id
204
205                                 key = (val['tax_code_id'], val['base_code_id'], val['account_id'])
206                                 if not key in tax_grouped:
207                                         tax_grouped[key] = val
208                                 else:
209                                         tax_grouped[key]['amount'] += val['amount']
210                                         tax_grouped[key]['base'] += val['base']
211                                         tax_grouped[key]['base_amount'] += val['base_amount']
212                                         tax_grouped[key]['tax_amount'] += val['tax_amount']
213
214                 return tax_grouped
215 account_invoice_tax()
216