Search code examples
pythonpython-3.xodooodoo-11

Odoo 11 How to override compute_all() method?


I am developping a module to use a custom formula for the amount calculation method in invoices and sales orders. The line amount formula should be : nbJours * price_unit * quantity instead of the default formula : price_unit * quantity

I added a custom field to AccountInvoiceLine class by inheriting it as following :

class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"
# Nombre de jours de location
nombreJours = fields.Integer("Nombre de jours",default=1,required=True)

@api.multi
@api.depends('nombreJours','price_unit', 'discount', 'invoice_line_tax_ids', 'quantity', 'product_id', 'invoice_id.partner_id', 'invoice_id.currency_id', 'invoice_id.company_id', 'invoice_id.date_invoice', 'invoice_id.date')
def _compute_price(self):
    ...
    ...
    if self.invoice_line_tax_ids:
        taxes = self.invoice_line_tax_ids.compute_all(self.nombreJours, price, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)

    # Calcul du sous-total de la ligne
    self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price * self.nombreJours
    self.price_total = taxes['total_included'] if taxes else self.price_subtotal
    ...
    ...

And I also need to set the custom formula in the AccountTax class. I tried to override the compute_all() method :

# Modification du modèle de Taxes
class AccountTax(models.Model):
_inherit = 'account.tax'

@api.multi
def compute_all(self, nbJrs=1, price_unit=1, currency=None, quantity=1.0, product=None, partner=None):
    ...
    ...
    if not base_values:
            odooAmount = price_unit * quantity
            customAmount = nbJrs * odooAmount
            total_excluded = total_included = base = round( customAmount , prec)
        else:
            total_excluded, total_included, base = base_values
    ...
    ...

    return {
        'taxes': sorted(taxes, key=lambda k: k['sequence']),
        'total_excluded': currency.round(total_excluded) if round_total else total_excluded,
        'total_included': currency.round(total_included) if round_total else total_included,
        'base': base,
    }

I've successfully updated my module, but when I try to add a product in a new invoice I get this error :

... ... ... File "/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py", line 618, in _onchange_invoice_line_ids taxes_grouped = self.get_taxes_values()
File "/OdooERP/Odoo 11.0/addons/account/models/account_invoice.py", line 889, in get_taxes_values taxes = line.invoice_line_tax_ids.compute_all(price_unit, self.currency_id, line.quantity, line.product_id, self.partner_id)['taxes']

File "/OdooERP/Instances/xaymalab/addons/sunu_location_event/models/accountinvoice.py",line 82, in compute_all odooAmount = price_unit * quantity

TypeError: unsupported operand type(s) for *: 'res.currency' and 'product.product'

When I remove "nbJrs" parameter in the definition, it works! The values shift one parameter to the right. Can anybody help me out regarding it?


Solution

  • How I solved this : I was trying to override the compute_all() by adding a new parameter. It was the wrong way. I updated the _compute_price() by adding a new variable "rentalprice" and passing it to compute_all():

    def _compute_price(self):
        currency = self.invoice_id and self.invoice_id.currency_id or None
        price = self.price_unit * (1 - (self.discount or 0.0) / 100.0)
        # Prix unitaire d'un article pendant la durée totale de location:
        rentalprice = self.nombrejours * price
        taxes = False
        if self.invoice_line_tax_ids:
            taxes = self.invoice_line_tax_ids.compute_all(rentalprice, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)
    

    I removed the AccountTax inheritance code because I no longer need it, updated my module and now it's working.

    Thanks to @CZoellner for helping me to better understand what I was missing.