Skip to Content
Menu
This question has been flagged

Dear All,

Using Community V10.

I need an urgent help. It may be silly but still new to Odoo so any help is appreciated

I have added new field to Invoice line named "Fuel Surcharge". This is calculated as percent and entered value is properly shown in Untaxed amount..


Here is the code.

I have added custom module and here is the module account_invoice.py related code..

----------------------

class CustomAccountInvoiceLine(models.Model):
    _inherit = "account.invoice.line"

    @api.one
    @api.depends('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','fuel_surcharge')


    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) + self.fuel_surcharge /100 )
        taxes = False
        if self.invoice_line_tax_ids:
            taxes = self.invoice_line_tax_ids.compute_all(price, currency, self.quantity, product=self.product_id, partner=self.invoice_id.partner_id)
        self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price
        if self.invoice_id.currency_id and self.invoice_id.company_id and self.invoice_id.currency_id != self.invoice_id.company_id.currency_id:
            price_subtotal_signed = self.invoice_id.currency_id.with_context(date=self.invoice_id.date_invoice).compute(price_subtotal_signed, self.invoice_id.company_id.currency_id)
        sign = self.invoice_id.type in ['in_refund', 'out_refund'] and -1 or 1
        self.price_subtotal_signed = price_subtotal_signed * sign


    invoice_bdate = fields.Date(string="Booking Date", store=True)
    invoice_docket = fields.Char(string="Docket No.", store=True)
    invoice_destination = fields.Char(string="Destination", store=True)
    invoice_weight = fields.Float(string="Weight", store=True)
    fuel_surcharge = fields.Float(string="Fuel Surcharge(%)", store=True)
    # amount_before_surcharge = fields.Float(string="Amount Before", compute="_compute_price", store=True)


----------------

Attaching the screenshot of the Invoice for reference.

The Untaxed amount is correct, line subtotal is correct.. But the tax still is calculated on just the product price * Amount. :(

Sample calculation.

Product Price = 100
Surcharge = 10%.
Effective product price = 110 after surcharge
Untaxed price = 110
Tax @ 12.36 %

Now, the tax should be calculated on subtotal 110 .
It still calculates on Product Price.

Pleaseeeeeeee help friends.



Avatar
Discard
Author Best Answer

Thanks a lot  Mr Amal.

That helped...:)

@api.multi
    def get_taxes_values(self):
        tax_grouped = {}
        for line in self.invoice_line_ids:
            price_unit = line.price_unit * (1 - (line.discount or 0.0) / 100.0 + line.fuel_surcharge /100)
            taxes = line.invoice_line_tax_ids.compute_all(price_unit, self.currency_id, line.quantity, line.product_id, self.partner_id)['taxes']
            for tax in taxes:
                val = self._prepare_tax_line_vals(line, tax)
                key = self.env['account.tax'].browse(tax['id']).get_grouping_key(val)

                if key not in tax_grouped:
                    tax_grouped[key] = val
                else:
                    tax_grouped[key]['amount'] += val['amount']
                    tax_grouped[key]['base'] += val['base']
        return tax_groupedere is the chunk of code where updated the price on computation of taxes ...

Avatar
Discard

I've updated my answer

any ideas to perform this same functionality in odoo16.

Best Answer

Hi

I believe problem is with this line

self.price_subtotal = price_subtotal_signed = taxes['total_excluded'] if taxes else self.quantity * price

You can either change value assigned for variable total_excluded(should be in account.tax) or change that line to calculate according to tax rule


Update :

if your untaxed amount is correct and taxed is wrong

change this line in method _compute_amount in  account.invoice

self.amount_tax = sum(line.amount for line in self.tax_line_ids)

for this simple way is to change whole line according to invoice line. 

Or you can edit tax to save amount value from your field also(in account.invoice), then this line will work fine

Avatar
Discard
Related Posts Replies Views Activity
0
Jan 23
819
2
Oct 21
2117
0
Feb 20
2496
0
Mar 15
2532
1
Mar 15
3188