[IMP] update pot 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     _inherit = "account.invoice"
30     _columns = {
31         'price_type': fields.selection([('tax_included','Tax included'),
32                                         ('tax_excluded','Tax excluded')],
33                                         'Price method', required=True, readonly=True,
34                                         states={'draft':[('readonly',False)]}),
35     }
36     _defaults = {
37         'price_type': lambda *a: 'tax_excluded',
38     }
39 account_invoice()
40
41 class account_invoice_line(osv.osv):
42     _inherit = "account.invoice.line"
43     def _amount_line2(self, cr, uid, ids, name, args, context={}):
44         """
45         Return the subtotal excluding taxes with respect to price_type.
46         """
47         res = {}
48         tax_obj = self.pool.get('account.tax')
49         res_init = super(account_invoice_line, self)._amount_line(cr, uid, ids, name, args, context)
50         for line in self.browse(cr, uid, ids):
51             res[line.id] = {
52                 'price_subtotal': 0.0,
53                 'price_subtotal_incl': 0.0,
54                 'data': []
55             }
56             if not line.quantity:
57                 continue
58             if line.invoice_id:
59                 product_taxes = []
60                 if line.product_id:
61                     if line.invoice_id.type in ('out_invoice', 'out_refund'):
62                         product_taxes = filter(lambda x: x.price_include, line.product_id.taxes_id)
63                     else:
64                         product_taxes = filter(lambda x: x.price_include, line.product_id.supplier_taxes_id)
65
66                 if ((set(product_taxes) == set(line.invoice_line_tax_id)) or not product_taxes) and (line.invoice_id.price_type == 'tax_included'):
67                     res[line.id]['price_subtotal_incl'] = res_init[line.id]
68                 else:
69                     res[line.id]['price_subtotal'] = res_init[line.id]
70                     for tax in tax_obj.compute_inv(cr, uid, product_taxes, res_init[line.id]/line.quantity, line.quantity):
71                         res[line.id]['price_subtotal'] = res[line.id]['price_subtotal'] - round(tax['amount'], 2)
72             else:
73                 res[line.id]['price_subtotal'] = res_init[line.id]
74
75             if res[line.id]['price_subtotal']:
76                 res[line.id]['price_subtotal_incl'] = res[line.id]['price_subtotal']
77                 for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, res[line.id]['price_subtotal']/line.quantity, line.quantity):
78                     res[line.id]['price_subtotal_incl'] = res[line.id]['price_subtotal_incl'] + tax['amount']
79                     res[line.id]['data'].append( tax)
80             else:
81                 res[line.id]['price_subtotal'] = res[line.id]['price_subtotal_incl']
82                 for tax in tax_obj.compute_inv(cr, uid, line.invoice_line_tax_id, res[line.id]['price_subtotal_incl']/line.quantity, line.quantity):
83                     res[line.id]['price_subtotal'] = res[line.id]['price_subtotal'] - tax['amount']
84                     res[line.id]['data'].append( tax)
85
86         res[line.id]['price_subtotal']= round(res[line.id]['price_subtotal'], 2)
87         res[line.id]['price_subtotal_incl']= round(res[line.id]['price_subtotal_incl'], 2)
88         return res
89
90     def _price_unit_default(self, cr, uid, context={}):
91         if 'check_total' in context:
92             t = context['check_total']
93             if context.get('price_type', False) == 'tax_included':
94                 for l in context.get('invoice_line', {}):
95                     if len(l) >= 3 and l[2]:
96                         p = l[2].get('price_unit', 0) * (1-l[2].get('discount', 0)/100.0)
97                         t = t - (p * l[2].get('quantity'))
98                 return t
99             return super(account_invoice_line, self)._price_unit_default(cr, uid, context)
100         return 0
101
102     def _get_invoice(self, cr, uid, ids, context):
103         result = {}
104         for inv in self.pool.get('account.invoice').browse(cr, uid, ids, context=context):
105             for line in inv.invoice_line:
106                 result[line.id] = True
107         return result.keys()
108     _columns = {
109         'price_subtotal': fields.function(_amount_line2, method=True, string='Subtotal w/o tax', multi='amount',
110             store={'account.invoice':(_get_invoice,['price_type'],10), 'account.invoice.line': (lambda self,cr,uid,ids,c={}: ids, None,10)}),
111         'price_subtotal_incl': fields.function(_amount_line2, method=True, string='Subtotal', multi='amount',
112             store={'account.invoice':(_get_invoice,['price_type'],10), 'account.invoice.line': (lambda self,cr,uid,ids,c={}: ids, None,10)}),
113     }
114
115     _defaults = {
116         'price_unit': _price_unit_default,
117     }
118
119     def move_line_get_item(self, cr, uid, line, context={}):
120         return {
121                 'type':'src',
122                 'name':line.name,
123                 'price_unit':line.price_subtotal / line.quantity,
124                 'quantity':line.quantity,
125                 'price':line.price_subtotal,
126                 'account_id':line.account_id.id,
127                 'product_id': line.product_id.id,
128                 'uos_id':line.uos_id.id,
129                 'account_analytic_id':line.account_analytic_id.id,
130             }
131
132 # TODO: check why ?
133 #
134 #    def product_id_change_unit_price_inv(self, cr, uid, tax_id, price_unit, qty, address_invoice_id, product, partner_id, context={}):
135 #        if context.get('price_type', False) == 'tax_included':
136 #            return {'price_unit': price_unit,'invoice_line_tax_id': tax_id}
137 #        else:
138 #            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)
139 #
140 #    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={}):
141 #        context.update({'price_type': price_type})
142 #        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)
143 account_invoice_line()
144
145 class account_invoice_tax(osv.osv):
146     _inherit = "account.invoice.tax"
147
148     def compute(self, cr, uid, invoice_id, context={}):
149         inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id)
150         line_ids = map(lambda x: x.id, inv.invoice_line)
151
152
153
154         tax_grouped = {}
155         tax_obj = self.pool.get('account.tax')
156         cur_obj = self.pool.get('res.currency')
157         cur = inv.currency_id
158
159         for line in inv.invoice_line:
160             data = self.pool.get('account.invoice.line')._amount_line2(cr, uid, [line.id], [], [], context)[line.id]
161             for tax in data['data']:
162                 val={}
163                 val['invoice_id'] = inv.id
164                 val['name'] = tax['name']
165                 val['amount'] = cur_obj.round(cr, uid, cur, tax['amount'])
166                 val['manual'] = False
167                 val['sequence'] = tax['sequence']
168                 val['base'] = tax['price_unit'] * line['quantity']
169
170                 if inv.type in ('out_invoice','in_invoice'):
171                     val['base_code_id'] = tax['base_code_id']
172                     val['tax_code_id'] = tax['tax_code_id']
173                     val['base_amount'] = val['base'] * tax['base_sign']
174                     val['tax_amount'] = val['amount'] * tax['tax_sign']
175                     val['account_id'] = tax['account_collected_id'] or line.account_id.id
176                 else:
177                     val['base_code_id'] = tax['ref_base_code_id']
178                     val['tax_code_id'] = tax['ref_tax_code_id']
179                     val['base_amount'] = val['base'] * tax['ref_base_sign']
180                     val['tax_amount'] = val['amount'] * tax['ref_tax_sign']
181                     val['account_id'] = tax['account_paid_id'] or line.account_id.id
182
183                 key = (val['tax_code_id'], val['base_code_id'], val['account_id'])
184                 if not key in tax_grouped:
185                     tax_grouped[key] = val
186                 else:
187                     tax_grouped[key]['amount'] += val['amount']
188                     tax_grouped[key]['base'] += val['base']
189                     tax_grouped[key]['base_amount'] += val['base_amount']
190                     tax_grouped[key]['tax_amount'] += val['tax_amount']
191
192         return tax_grouped
193 account_invoice_tax()
194
195
196 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
197