Fixed problems with po files:
[odoo/odoo.git] / addons / account_tax_include / invoice_tax_incl.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import time
24 import netsvc
25 from osv import fields, osv
26 import ir
27
28 class account_invoice(osv.osv):
29     def _amount_untaxed(self, cr, uid, ids, name, args, context={}):
30         res = {}
31         for invoice in self.browse(cr,uid,ids):
32             if invoice.price_type == 'tax_included':
33                 res[invoice.id] = reduce( lambda x, y: x+y.price_subtotal, invoice.invoice_line,0)
34             else:
35                 res[invoice.id] = super(account_invoice, self)._amount_untaxed(cr, uid, [invoice.id], name, args, context)[invoice.id]
36         return res
37
38     def _amount_tax(self, cr, uid, ids, name, args, context={}):
39         res = {}
40         for invoice in self.browse(cr,uid,ids):
41             if invoice.price_type == 'tax_included':
42                 res[invoice.id] = reduce( lambda x, y: x+y.amount, invoice.tax_line,0)
43             else:
44                 res[invoice.id] = super(account_invoice, self)._amount_tax(cr, uid, [invoice.id], name, args, context)[invoice.id]
45         return res
46
47     def _amount_total(self, cr, uid, ids, name, args, context={}):
48         res = {}
49         for invoice in self.browse(cr,uid,ids):
50             if invoice.price_type == 'tax_included':
51                 res[invoice.id]= invoice.amount_untaxed + invoice.amount_tax
52             else:
53                 res[invoice.id] = super(account_invoice, self)._amount_total(cr, uid, [invoice.id], name, args, context)[invoice.id]
54         return res
55
56     _inherit = "account.invoice"
57     _columns = {
58         'price_type': fields.selection([('tax_included','Tax included'),
59                                         ('tax_excluded','Tax excluded')],
60                                         'Price method', required=True, readonly=True,
61                                         states={'draft':[('readonly',False)]}),
62         'amount_untaxed': fields.function(_amount_untaxed, digits=(16,2), method=True,string='Untaxed Amount'),
63         'amount_tax': fields.function(_amount_tax, method=True, string='Tax', store=True),
64         'amount_total': fields.function(_amount_total, method=True, string='Total', store=True),
65     }
66     _defaults = {
67         'price_type': lambda *a: 'tax_excluded',
68     }
69 account_invoice()
70
71 class account_invoice_line(osv.osv):
72     _inherit = "account.invoice.line"
73     def _amount_line(self, cr, uid, ids, name, args, context={}):
74         """
75         Return the subtotal excluding taxes with respect to price_type.
76         """
77         res = {}
78         tax_obj = self.pool.get('account.tax')
79         res = super(account_invoice_line, self)._amount_line(cr, uid, ids, name, args, context)
80         res2 = res.copy()
81         for line in self.browse(cr, uid, ids):
82             if not line.quantity:
83                 res[line.id] = 0.0
84                 continue
85             if line.invoice_id and line.invoice_id.price_type == 'tax_included':
86                 product_taxes = None
87                 if line.product_id:
88                     if line.invoice_id.type in ('out_invoice', 'out_refund'):
89                         product_taxes = line.product_id.taxes_id
90                     else:
91                         product_taxes = line.product_id.supplier_taxes_id
92                 if product_taxes:
93                     for tax in tax_obj.compute_inv(cr, uid, product_taxes, res[line.id]/line.quantity, line.quantity):
94                         res[line.id] = res[line.id] - round(tax['amount'], 2)
95                 else:
96                     for tax in tax_obj.compute_inv(cr, uid,line.invoice_line_tax_id, res[line.id]/line.quantity, line.quantity):
97                         res[line.id] = res[line.id] - round(tax['amount'], 2)
98             if name == 'price_subtotal_incl' and line.invoice_id and line.invoice_id.price_type == 'tax_included':
99                 prod_taxe_ids = None
100                 line_taxe_ids = None
101                 if product_taxes:
102                     prod_taxe_ids = [ t.id for t in product_taxes ]
103                     prod_taxe_ids.sort()
104                     line_taxe_ids = [ t.id for t in line.invoice_line_tax_id ]
105                     line_taxe_ids.sort()
106                 if product_taxes and prod_taxe_ids == line_taxe_ids:
107                     res[line.id] = res2[line.id]
108                 elif not line.product_id:
109                     res[line.id] = res2[line.id]
110                 else:
111                     for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, res[line.id]/line.quantity, line.quantity):
112                         res[line.id] = res[line.id] + tax['amount']
113             res[line.id]= round(res[line.id], 2)
114         return res
115
116     def _price_unit_default(self, cr, uid, context={}):
117         if 'check_total' in context:
118             t = context['check_total']
119             if context.get('price_type', False) == 'tax_included':
120                 for l in context.get('invoice_line', {}):
121                     if len(l) >= 3 and l[2]:
122                         p = l[2].get('price_unit', 0) * (1-l[2].get('discount', 0)/100.0)
123                         t = t - (p * l[2].get('quantity'))
124                 return t
125             return super(account_invoice_line, self)._price_unit_default(cr, uid, context)
126         return 0
127
128     _columns = {
129         'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal w/o tax', store=True),
130         'price_subtotal_incl': fields.function(_amount_line, method=True, string='Subtotal'),
131     }
132
133     _defaults = {
134         'price_unit': _price_unit_default,
135     }
136
137     def move_line_get_item(self, cr, uid, line, context={}):
138         return {
139                 'type':'src',
140                 'name':line.name,
141                 'price_unit':line.price_subtotal / line.quantity,
142                 'quantity':line.quantity,
143                 'price':line.price_subtotal,
144                 'account_id':line.account_id.id,
145                 'product_id': line.product_id.id,
146                 'uos_id':line.uos_id.id,
147                 'account_analytic_id':line.account_analytic_id.id,
148             }
149
150     def product_id_change_unit_price_inv(self, cr, uid, tax_id, price_unit, qty, address_invoice_id, product, partner_id, context={}):
151         if context.get('price_type', False) == 'tax_included':
152             return {'price_unit': price_unit,'invoice_line_tax_id': tax_id}
153         else:
154             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)
155
156     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={}):
157         context.update({'price_type': price_type})
158         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)
159 account_invoice_line()
160
161 class account_invoice_tax(osv.osv):
162     _inherit = "account.invoice.tax"
163
164     def compute(self, cr, uid, invoice_id):
165         tax_grouped = {}
166         tax_obj = self.pool.get('account.tax')
167         cur_obj = self.pool.get('res.currency')
168         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id)
169         cur = inv.currency_id
170
171         if inv.price_type=='tax_excluded':
172             return super(account_invoice_tax,self).compute(cr, uid, invoice_id)
173
174         for line in inv.invoice_line:
175             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):
176                 val={}
177                 val['invoice_id'] = inv.id
178                 val['name'] = tax['name']
179                 val['amount'] = cur_obj.round(cr, uid, cur, tax['amount'])
180                 val['manual'] = False
181                 val['sequence'] = tax['sequence']
182                 val['base'] = tax['price_unit'] * line['quantity']
183
184                 if inv.type in ('out_invoice','in_invoice'):
185                     val['base_code_id'] = tax['base_code_id']
186                     val['tax_code_id'] = tax['tax_code_id']
187                     val['base_amount'] = val['base'] * tax['base_sign']
188                     val['tax_amount'] = val['amount'] * tax['tax_sign']
189                     val['account_id'] = tax['account_collected_id'] or line.account_id.id
190                 else:
191                     val['base_code_id'] = tax['ref_base_code_id']
192                     val['tax_code_id'] = tax['ref_tax_code_id']
193                     val['base_amount'] = val['base'] * tax['ref_base_sign']
194                     val['tax_amount'] = val['amount'] * tax['ref_tax_sign']
195                     val['account_id'] = tax['account_paid_id'] or line.account_id.id
196
197                 key = (val['tax_code_id'], val['base_code_id'], val['account_id'])
198                 if not key in tax_grouped:
199                     tax_grouped[key] = val
200                 else:
201                     tax_grouped[key]['amount'] += val['amount']
202                     tax_grouped[key]['base'] += val['base']
203                     tax_grouped[key]['base_amount'] += val['base_amount']
204                     tax_grouped[key]['tax_amount'] += val['tax_amount']
205
206         return tax_grouped
207 account_invoice_tax()
208
209
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
211