2
votes

I want to display all the taxes that are applied in a particular invoice and their amounts[in the tree view of a model account.invoice] This is the output:

enter image description here

The column Tax Lines shows the ids of the taxes that are present in table account.invoice.tax (whereas I want to show their names and corresponding amount)

The model account.invoice has a field called tax_line_ids[Tax Lines] that contains the record of all the taxes on a invoice which is stored in a separate table account.invoice.tax, which in its own tree view looks like this:

enter image description here

I want to extract the tax name and its corresponding amount for it to reflect in account.invoice's tree view

Here's my python code which does'nt seem to work:

@api.one
def taxz(self):
    tax_pool = self.pool.get("account.tax")
    found_taxes = tax_pool.read(cr, uid, [tax_id,], ["tax_line_ids"], context)
    found_tax = found_taxes[0] if found_taxes else None
    tax_line_ids = found_tax["tax_line_ids"]
    _logger.critical("context type: " + type(context))
    _logger.critical("context content: " + str(context))
    _logger.critical(tax_line_ids)

xml code for the view:

<field name="tax_line_ids" widget="many2many_tags" />
2

2 Answers

0
votes

Try this:

    class Invoice(models.Model):
        _inherit = 'account.invoice'

        tax_line_ids = fields.Many2many('account.invoice.tax',
                                        'invoice_taxes', 
                                        'invoice_id', 
                                        'taxt_id',
                                        'List of taxes',
                                    compute='get_tax_list', store=True)



        @api.depends('tax_line', 'tax_line.amount')
        def get_tax_list(self):
            for rec in self:
                if rec.taxe_line:
                    rec.tax_line_ids = [(6,0,rec.tax_line.ids)]
                else:
                    rec.tax_line_ids = [(5,0,0)]

But doing this will just show the list of taxes without amount in your tree view if you want to show the amount you need to override the name_get method in account.invoice.tax but this will affect all the x2many fields.

class AcountInvoiceTax(models.Model):

    _inherit = 'account.invoice.tax'

    @api.multi
    def name_get(self):
        res = []
        for rec in self:
            res.append((rec.id, rec.name +': '+ str(rec.amount)))
        return res

If you don't want this then you need to change the type to Char and recompute the field or create another model to save taxes and define name_get for that model.

As you can see this worked for me now if you still getting the keyErro you must be doing some thing wrong check your code for indentation inherit value... :

enter image description here

0
votes

You can do add a char field just to use it in display in treeview :

class Invoice(models.Model):
        _inherit = 'account.invoice'

        tax_line_tree_view = fields.Char(compute='get_tax_list')

        @api.multi
        def get_tax_list(self):
             tax_disp = ""
             for rec in self:
                  if rec.taxe_line:
                       for tax in taxe_line:
                            tax_disp = tax_disp + "["+tax.name+"], " 
                       rec.tax_line_tree_view = tax_disp[:-2] # [:-2] just to remove the last ', ' characters.
                  else:
                       rec.tax_line_tree_view  = tax_disp

In your XML file where tree_view is located, add this field:

<field name="tax_line_tree_view " />

instead of tax_line_ids.

I hope that answer your question.