Set sales order fields reference in picking - odoo

I would like to set Sales Team reference in picking directly when sales order confirm and picking is getting created.

But I didn't get enough hint how can I achieve this. Because the method which is called at the time of sales order confirmation is as follow.

def action_button_confirm(self, cr, uid, ids, context=None):
    if not context:
        context = {}
    assert len(ids) == 1, 'This option should only be used for a single id at a time.'
    self.signal_workflow(cr, uid, ids, 'order_confirm')
    if context.get('send_email'):
        self.force_quotation_send(cr, uid, ids, context=context)
    return True

Here there is no any hint how can I pass it to picking ?

Purpose:

My aim is to set sales team reference in picking / shipment.

Upvotes: 0

Views: 1145

Answers (2)

I got that method from where it create picking. So I have just inherited it and added my code. action_ship_create will always get called at the time of shipment creation from the sales order.

 @api.cr_uid_ids_context
    def action_ship_create(self,cr,uid,ids,context={}):
        result=super(sale_order,self).action_ship_create(cr,uid,ids,context=context)
        for order in self.browse(cr,uid,ids,context=context):
            order.picking_ids.write({'section_id':order.section_id.id})
        return result

Upvotes: 0

CZoellner
CZoellner

Reputation: 14768

It's not that easy. Odoo uses procurement.orders for creating stock.moves and for them stock.pickings. Problem: Maybe a picking has more than one sales orders as origin. So there could be more than one sales team referenced.

But try to use a computed field:

section_id = fields.Many2one(
    comodel_name="crm.case.section", string="Sales Team",
    compute="_compute_section_id")

@api.multi
def _compute_section_id(self):
    for picking in self:
        section_ids = set()
        for move in picking.move_lines:
            if move.sale_line_id.order_id.section_id
                section_ids.add(move.sale_line_id.order_id.section_id.id)
        if len(section_ids) == 1:
            picking.section_id = section_ids.pop()

You could also use a related field, but that could have really bad side effects. Because Odoo will take the first move.

section_id = fields.Many2one(
    comodel_name="crm.case.section", string="Sales Team",
    related="move_lines.sale_line_id.order_id.section_id")

Upvotes: 1

Related Questions