M.E.
M.E.

Reputation: 5505

Odoo 10 - How to retrieve associated stock_picking name (if such stock_picking exists) from account_invoice_line

I would like to include information about the specific delivery slip in which an invoice line was delivered or is going to be delivered.

Is it possible to retrieve "name" field from the "stock.picking" associated to a given "account_invoice_line"?

Which would be the easiest way to achieve that?

This code provides a new field for sale_order_line_ids in account_invoice_line:

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

    sale_order_line_ids = fields.Many2many('sale.order.line', 'sale_order_line_invoice_rel', 'invoice_line_id', 'order_line_id', string='Sale Order Lines', readonly=True);

I would like to be able to get the stock_picking name for that specific account_invoice_line:

I see the following fields in tables:

Table procurement_order
    Field: sale_line_id
    Field: purchase_line_id

Also:

Table stock_move
    Field: picking_id
    Field: procurement_id
    Field: purchase_line_id

So I need to go backwards but it seems feasible and not complex to get the picking_id.

How could this field be implemented in account_invoice_line model?

Upvotes: 0

Views: 182

Answers (1)

Travis Waelbroeck
Travis Waelbroeck

Reputation: 2135

It's a little bit of a stab in the dark, but you ought to be able to use this or something very similar:

# Assuming `invoice_line_id` is an individual `account.invoice.line` record
invoice_line_id.sale_order_line_ids.mapped('procurement_id').mapped('move_ids').mapped('picking_id')

You can see in the ORM Documentation that mapped is used to get data for a record or recordset. It's extremely useful when you might have many lines and you want to gather data for all of them.

In this case, the above line would return a browse recordset of all stock.picking records linked to any of the Sales Order Lines belonging to that Invoice Line.

Note: It's possible you may get more data than you want from this, so you'll have to do the testing on your own to confirm it's only returning what you expect.

Upvotes: 2

Related Questions