[Python] How to link stock.picking to account.move (invoice) in Odoo 17

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on an Odoo customization where I want to automatically create a stock.picking record when an account.move (invoice) is posted. After creating the stock.picking, I need to link it to the account.move so that the smart button for the delivery appears. [​IMG]

    I know that this is a default feature in Odoo, but for some reason, they are not linked, and I can't find the field that links them. Even when I try to create the field manually, the problem persists, and the smart button still doesn't appear.

    Here’s my code:

    from odoo import fields, models, api, _

    class AccountMove(models.Model):
    _inherit = 'account.move'

    def action_post(self):
    res = super(AccountMove, self).action_post()

    if not self.invoice_line_ids.sale_line_ids and self.move_type == 'out_invoice':
    picking_type = self.env.ref('stock.picking_type_out')
    stock_picking = self.env['stock.picking'].create({
    'picking_type_id': picking_type.id,
    'location_id': picking_type.default_location_src_id.id,
    'location_dest_id': self.env.ref('stock.stock_location_customers').id,
    'partner_id': self.partner_id.id,
    'origin': self.name,
    'move_type': 'direct',
    'move_ids': [(0, 0, {
    'name': line.name,
    'product_id': line.product_id.id,
    'product_uom_qty': line.quantity,
    'product_uom': line.product_uom_id.id,
    'location_id': picking_type.default_location_src_id.id,
    'location_dest_id': self.env.ref('stock.stock_location_customers').id,
    }) for line in self.invoice_line_ids if line.product_id.type == 'product']
    })

    stock_picking.action_confirm()
    for move in stock_picking.move_ids:
    self.env['stock.move.line'].create({
    'move_id': move.id,
    'product_id': move.product_id.id,
    'product_uom_id': move.product_uom.id,
    'location_id': move.location_id.id,
    'location_dest_id': self.env.ref('stock.stock_location_customers').id,
    'state': 'done',
    'picking_id': move.picking_id.id,
    })
    # Link the invoice to the picking
    #self.write({
    # 'move_ids': [(4, stock_picking.id)]
    #})
    stock_picking.button_validate()

    return res

    Continue reading...

Compartilhe esta Página