This question has been flagged

Hello guy,

This post should be a little more complex than usual.

We have created a new field for an account.invoice.line : pack_operation. With this field, we can print serial/lot number for each line on the PDF invoice (this part works well).

Many hours passed trying to write the domain to select the EXACT and ONLY stock pack operation for each invoice line.

In the code below, we used the domain [('id','=', 31)] to make our tests printing the PDF.

Ho to write this domain to be sure at 100 % that we will get the right stock pack operation for each invoice line?

I really need your help here... Too complex for my brain.


Our code :

class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"
 
    pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id')

    def compute_stock_pack_operation_id(self):
         stock_operation_obj = self.env['stock.pack.operation']
         stock_operation = stock_operation_obj.search( [('id','=', 31)] )
         self.pack_operation = stock_operation[0]



Avatar
Discard
Author

Totally lost! What a zoo with all this tables : stock.move, procurement.order, stock.pack.operation, etc... OUf!

Author

I will pay a beer by Paypal to the one who gives me the right domain. I really need it!

Author Best Answer

Here is my first solution. I'm pretty sure that you won't like it. I give all comments/improvements with great pleasure.

I'm very excited to read what you will have to say about this code...

Thanks

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

    pack_operation = fields.Many2one(comodel_name='stock.pack.operation', compute='compute_stock_pack_operation_id')
   
    @api.one
    def compute_stock_pack_operation_id(self):
        procurement_order_obj = self.env['procurement.order']
        stock_operation_obj = self.env['stock.pack.operation']
        all_picking_ids_for_this_invoice_line = []
       
        for saleorderline in self.sale_line_ids:
            for procurement in saleorderline.procurement_ids:
                for stockmove in procurement.move_ids:
                    if stockmove.picking_id.id not in all_picking_ids_for_this_invoice_line:
                        all_picking_ids_for_this_invoice_line.append(stockmove.picking_id.id)
                                                                                                           
 
        stock_operation = stock_operation_obj.search(
            [ '&',
                                ('picking_id','in',all_picking_ids_for_this_invoice_line),
                ('product_id','=',self.product_id.id)                                ]            )
                                     
        self.pack_operation = stock_operation[0]



Avatar
Discard