Search code examples
odooodoo-8

odoo get several data in text field


I have this py script

@api.one
@api.depends('customer_id')
def _get_unpaid_list(self):
    invoice_pool = self.env['account.invoice']
    for rec in invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', self.customer_id.id)]):
        amount_total = rec.amount_total or ''
        number = rec.number or ''
        name = ustr(amount_total) + "-" + ustr(number)
        self.unpaid_list = name
unpaid_list = fields.Text(string="Customer Debt List", compute="_get_unpaid_list", )

and this xml

<field name="unpaid_list" />

It works with this output

19690000.0-INV/2021/13928

BUT it should show several line of data like this

19690000.0-INV/2021/13928
25590000.0-INV/2021/13929
23450000.0-INV/2021/13930

How to make a list of data like it should? Thank you


Solution

  • The value of unpaid_list is set to the last name in the loop. To fix the issue, you need to set the field value outside the for loop.

    Example:

    @api.depends('customer_id')
    def _get_unpaid_list(self):
        invoice_pool = self.env['account.invoice']
        for record in self:
            name = ''
            for invoice in invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', record.customer_id.id)]):
                amount_total = invoice.amount_total or ''
                number = invoice.number or ''
                name += ustr(amount_total) + "-" + ustr(number) + "\n"
            record.unpaid_list = name  
    

    You can use a computed One2many field to show the list of unpaid invoices and let users open (the invoice form) and view the invoice details.

    Example:

    @api.depends('customer_id')
    def _get_unpaid_invoices(self):
        invoice_pool = self.env['account.invoice']
        for record in self:
            record.unpaid_list_ids = invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', record.customer_id.id)])
    
    unpaid_list_ids = fields.One2many("account.invoice", string="Customer Debt List", compute="_get_unpaid_invoices")