1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
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.
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.
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/>.
21 ##############################################################################
25 from osv import fields, osv
28 class account_invoice(osv.osv):
29 def _amount_untaxed(self, cr, uid, ids, name, args, context={}):
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)
35 res[invoice.id] = super(account_invoice, self)._amount_untaxed(cr, uid, [invoice.id], name, args, context)[invoice.id]
38 def _amount_tax(self, cr, uid, ids, name, args, context={}):
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)
44 res[invoice.id] = super(account_invoice, self)._amount_tax(cr, uid, [invoice.id], name, args, context)[invoice.id]
47 def _amount_total(self, cr, uid, ids, name, args, context={}):
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
53 res[invoice.id] = super(account_invoice, self)._amount_total(cr, uid, [invoice.id], name, args, context)[invoice.id]
56 _inherit = "account.invoice"
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),
67 'price_type': lambda *a: 'tax_excluded',
71 class account_invoice_line(osv.osv):
72 _inherit = "account.invoice.line"
73 def _amount_line(self, cr, uid, ids, name, args, context={}):
75 Return the subtotal excluding taxes with respect to price_type.
78 tax_obj = self.pool.get('account.tax')
79 res = super(account_invoice_line, self)._amount_line(cr, uid, ids, name, args, context)
81 for line in self.browse(cr, uid, ids):
85 if line.invoice_id and line.invoice_id.price_type == 'tax_included':
88 if line.invoice_id.type in ('out_invoice', 'out_refund'):
89 product_taxes = line.product_id.taxes_id
91 product_taxes = line.product_id.supplier_taxes_id
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)
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':
102 prod_taxe_ids = [ t.id for t in product_taxes ]
104 line_taxe_ids = [ t.id for t in line.invoice_line_tax_id ]
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]
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)
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'))
125 return super(account_invoice_line, self)._price_unit_default(cr, uid, context)
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'),
134 'price_unit': _price_unit_default,
137 def move_line_get_item(self, cr, uid, line, context={}):
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,
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}
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)
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()
161 class account_invoice_tax(osv.osv):
162 _inherit = "account.invoice.tax"
164 def compute(self, cr, uid, invoice_id):
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
171 if inv.price_type=='tax_excluded':
172 return super(account_invoice_tax,self).compute(cr, uid, invoice_id)
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):
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']
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
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
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
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']
207 account_invoice_tax()
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: