1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
6 # $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
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
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.
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.
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.
29 ##############################################################################
33 from osv import fields, osv
36 class account_invoice(osv.osv):
37 def _amount_untaxed(self, cr, uid, ids, name, args, context={}):
39 for invoice in self.browse(cr,uid,ids):
40 if invoice.price_type == 'tax_included':
41 res[invoice.id]= invoice.amount_total - invoice.amount_tax
43 res[invoice.id] = super(account_invoice, self)._amount_untaxed(cr, uid, [invoice.id], name, args, context)[invoice.id]
46 def _amount_tax(self, cr, uid, ids, name, args, context={}):
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)
52 res[invoice.id] = super(account_invoice, self)._amount_tax(cr, uid, [invoice.id], name, args, context)[invoice.id]
55 def _amount_total(self, cr, uid, ids, name, args, context={}):
57 for invoice in self.browse(cr,uid,ids):
58 if invoice.price_type == 'tax_included':
59 res[invoice.id]= reduce( lambda x, y: x+y.price_subtotal_incl, invoice.invoice_line,0)
61 res[invoice.id] = super(account_invoice, self)._amount_total(cr, uid, [invoice.id], name, args, context)[invoice.id]
64 _inherit = "account.invoice"
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'),
72 'amount_total': fields.function(_amount_total, method=True, string='Total'),
75 'price_type': lambda *a: 'tax_excluded',
79 class account_invoice_line(osv.osv):
80 _inherit = "account.invoice.line"
81 def _amount_line(self, cr, uid, ids, name, args, context={}):
83 Return the subtotal excluding taxes with respect to price_type.
86 tax_obj = self.pool.get('account.tax')
87 res = super(account_invoice_line, self)._amount_line(cr, uid, ids, name, args, context)
89 for line in self.browse(cr, uid, ids):
90 if line.invoice_id.price_type == 'tax_included':
92 for tax in tax_obj.compute_inv(cr, uid,line.product_id.taxes_id, res[line.id], line.quantity):
93 res[line.id] = res[line.id] - tax['amount']
95 for tax in tax_obj.compute_inv(cr, uid,line.invoice_line_tax_id, res[line.id], line.quantity):
96 res[line.id] = res[line.id] - tax['amount']
97 if name == 'price_subtotal_incl':
98 if line.product_id and line.invoice_id.price_type == 'tax_included':
99 prod_taxe_ids = [ t.id for t in line.product_id.taxes_id ]
101 line_taxe_ids = [ t.id for t in line.invoice_line_tax_id ]
103 if line.product_id and line.invoice_id.price_type == 'tax_included' and prod_taxe_ids == line_taxe_ids:
104 res[line.id] = res2[line.id]
106 for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, res[line.id], line.quantity):
107 res[line.id] = res[line.id] + tax['amount']
108 res[line.id]= round(res[line.id], 2)
111 def _price_unit_default(self, cr, uid, context={}):
112 if 'check_total' in context:
113 t = context['check_total']
114 if context.get('price_type', False) == 'tax_included':
115 for l in context.get('invoice_line', {}):
116 if len(l) >= 3 and l[2]:
117 p = l[2].get('price_unit', 0) * (1-l[2].get('discount', 0)/100.0)
118 t = t - (p * l[2].get('quantity'))
120 return super(account_invoice_line, self)._price_unit_default(cr, uid, context)
124 'price_subtotal': fields.function(_amount_line, method=True, string='Subtotal w/o tax'),
125 'price_subtotal_incl': fields.function(_amount_line, method=True, string='Subtotal'),
129 'price_unit': _price_unit_default,
133 # Compute a tax amount for each kind of tax :
135 def move_line_get(self, cr, uid, invoice_id, context={}):
136 inv = self.pool.get('account.invoice').browse(cr, uid, invoice_id)
137 if inv.price_type=='tax_excluded':
138 return super(account_invoice_line,self).move_line_get(cr, uid, invoice_id)
142 tax_obj = self.pool.get('account.tax')
143 cur_obj = self.pool.get('res.currency')
144 ait_obj = self.pool.get('account.invoice.tax')
145 cur = inv.currency_id
147 for line in inv.invoice_line:
148 res.append( self.move_line_get_item(cr, uid, line, context))
149 for tax in tax_obj.compute(cr, uid, line.invoice_line_tax_id, (line.price_unit *(1.0-(line['discount'] or 0.0)/100.0)), line.quantity, inv.address_invoice_id.id, line.product_id, inv.partner_id):
150 if inv.type in ('out_invoice', 'in_invoice'):
151 res[-1]['tax_code_id'] = tax['base_code_id']
152 res[-1]['tax_amount'] = tax['price_unit'] * line['quantity'] * tax['base_sign']
154 res[-1]['ta_code_id'] = tax['ref_base_code_id']
155 res[-1]['tax_amount'] = tax['price_unit'] * line['quantity'] * tax['ref_base_sign']
158 def move_line_get_item(self, cr, uid, line, context={}):
162 'price_unit':line.price_unit,
163 'quantity':line.quantity,
164 'price':line.price_subtotal,
165 'account_id':line.account_id.id,
166 'product_id': line.product_id.id,
167 'uos_id':line.uos_id.id,
168 'account_analytic_id':line.account_analytic_id.id,
171 def product_id_change_unit_price_inv(self, cr, uid, tax_id, price_unit, qty, address_invoice_id, product, partner_id, context={}):
172 if context.get('price_type', False) == 'tax_included':
173 return {'price_unit': price_unit,'invoice_line_tax_id': tax_id}
175 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)
177 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={}):
178 context.update({'price_type': price_type})
179 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)
180 account_invoice_line()