81768

Odoo 8 override _amount_line

Question:

I'm trying to override the _amount_line function in sale.order.line model in order to add custum logic.

Here is my code:

<pre class="lang-py prettyprint-override">class SaleOrderLine(models.Model): _inherit = 'sale.order.line' def _amount_line(self): tax_obj = self.env['account.tax'] cur_obj = self.env['res.currency'] res = {} for line in self: print line.tax_id price = self._calc_line_base_price(line) qty = self._calc_line_quantity(line) print" price:{} & quantity: {}".format(price,qty) taxes = tax_obj.compute_all(line.tax_id, price, qty, line.product_id, line.order_id.partner_id) cur = line.order_id.pricelist_id.currency_id res[line.id] = cur_obj.round(cur, taxes['total']) return res remise_palier = fields.Float('Remise palier (%)') remise_total = fields.Float('Remise totale (%)') price_subtotal = fields.Float(compute='_amount_line', string='Subtotal')

When i run Odoo, i get that error:

<a href="https://i.stack.imgur.com/bagel.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/bagel.png" data-original="https://i.stack.imgur.com/bagel.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

You have made mistake in compute_all method calling.

This method should be like this,

@api.multi def _amount_line(self): tax_obj = self.env['account.tax'] cur_obj = self.env['res.currency'] for line in self: print line.tax_id price = self._calc_line_base_price(line) qty = self._calc_line_quantity(line) print" price:{} & quantity: {}".format(price,qty) taxes = line.tax_id.compute_all(price, qty, line.product_id, line.order_id.partner_id) cur = line.order_id.pricelist_id.currency_id line.price_subtotal = cur.round(taxes['total'])

Answer2:

In Odoo V8+ the compute_all call should be on the taxes itself. You don't need to call it as "class method". Following snippet should work for you:

<pre class="lang-py prettyprint-override">@api.multi # @api.depends() use it for recomputation triggers def _amount_line(self): for line in self: print line.tax_id price = self._calc_line_base_price(line) qty = self._calc_line_quantity(line) print" price:{} & quantity: {}".format(price,qty) taxes = line.tax_id.compute_all( price, qty, line.product_id, line.order_id.partner_id) cur = line.order_id.pricelist_id.currency_id line.price_subtotal = cur.round(taxes['total'])

<strong>Edit</strong>: You don't need to return something on new API computed field functions. Just assign the values to the computed field(s).

<strong>Edit2</strong>: There is a new API style method for currency round(), too. I've changed my snippet.

Recommend

  • Split values in jquery
  • Having trouble initiating an array in a structure
  • WebMethod returning generic list
  • Generating and downloading an excel file generates a ERR_INVALID_RESPONSE
  • Extending Boostrap to include extra small inline form
  • Pass by Value and Pass by Reference
  • magento success page variables
  • WooCommerce creating Product Custom Attributes fields programatically?
  • jQuery Slick Slider showing some empty slides
  • Single virtual attribute definition for multiple fields
  • Show records ordered with maximum price first in PHP & MySQL
  • How to resolve permission denied maybe missing internet permission?
  • pickle.PicklingError: args[0] from __newobj__ args has the wrong class with hadoop python
  • Scanner nextInt() and hasNextInt() problems
  • Find JSON nested nodes using multiple string values
  • Is it possible to run clang with llc flags
  • Hector: how to query parts of a Composite Type
  • Cannot get text from text area
  • Getting short path in python
  • Getting different value with placeholder over CPU/GPU
  • there is no graph with tensorboard
  • Group list of tuples by item
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Get specific string
  • How to remove a SwiftyJSON element?
  • converting text file into xml using php?
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Set matplotlib plot axis to be the dataframe column name
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • Appending Character to Character Array In C
  • Do I need to reset a Perl hash index?
  • Django rest serializer Breaks when data exists
  • Is it possible to access block's scope in method?
  • Checking free space on FTP server
  • Can I check if a recipient has an automatic reply before I send an email?
  • align graphs with different xlab
  • Return words with double consecutive letters
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases