Search code examples
pythonodoo

how to get value from odoo purchase lines order to stock.picking lines?


i'am added analytic account field on stock move model, I need as the stock move lines get quantity form PO lines to get analytic account field form lines when I confirm the order, how can I do that

 class StockMove(models.Model):
_inherit = "stock.move"

 analytic_account_id = fields.Many2one(string='Analytic Account',comodel_name='account.analytic.account',)

any help will be appreciated


Solution

  • Override _prepare_stock_moves method which prepares the stock moves data for one order line and returns a list of dictionary ready to be used in stock.move's create().

    class PurchaseOrderLine(models.Model):
        _inherit = 'purchase.order.line'
    
        @api.multi
        def _prepare_stock_moves(self, picking):
            res = super(PurchaseOrderLine, self)._prepare_stock_moves(picking)
            res[0]['analytic_account_id'] = self.account_analytic_id.id
            return res 
    

    To get field values from purchase order use the inverse_name order_id.

    res[0]['analytic_account_id'] = self.order_id.account_analytic_id.id
    

    Edit:

    To use the same logic on production orders, you can set the account when you mark the order as done:

    class ManufacturingOrder(models.Model):
        _inherit = 'mrp.production'
    
        analytic_account_id = fields.Many2one(string='Analytic Account', comodel_name='account.analytic.account')
    
        @api.multi
        def button_mark_done(self):           
            for order in self:
                for move in order.move_finished_ids:
                    move.analytic_account_id = order.analytic_account_id
            res = super(ManufacturingOrder, self).button_mark_done()
            return res