Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
10794 มุมมอง

Hello all,

I'm in the model account.invoice, in a new method.

An invoice has an origin sale order.

Trying to get the id of the origin sale_order of an invoice.

Is this the best and cleanest way to get this id?


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

    def get_origin_sale_order_id(self):
sale_order_id = self.env['sale.order'].search([('name', '=', self.origin)])[0].id
        return sale_order_id


Thanks for your comments!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

No, it's not.

You can do like this.

This will work for you.

sale_order_id = self.invoice_line_ids.mapped('sale_line_ids').order_id
อวตาร
ละทิ้ง

It works! However I got expected singleton error if there are multiple sale.order.line lines

Here is my source code to overcome this error:

sale_line_ids = self.invoice_line_ids.mapped('sale_line_ids')

if sale_line_ids:

sale_order = sale_line_ids[0].order_id

If we think straightway, it's not possible that one invoice is created for 2 sale orders.

Related Posts ตอบกลับ มุมมอง กิจกรรม
1
พ.ค. 16
5697
0
ส.ค. 16
9139
1
มี.ค. 16
4481
0
มี.ค. 15
5552
object has no attribute '_origin' แก้ไขแล้ว
2
ส.ค. 20
10816