# -*- coding: utf-8 -*-
from odoo import models, api, fields, _
from odoo.exceptions import UserError, ValidationError

class Lead2OpportunityMassConvert(models.TransientModel):

    _inherit = 'crm.lead2opportunity.partner.mass'

    @api.multi
    def mass_convert(self):
        self.ensure_one()
        res = super().mass_convert()
        salesteam_id = self.team_id.id if self.team_id else False
        # 被勾選的商機的銷售團隊
        lead_selected = self._context.get('active_ids', [])
        for lead_id in lead_selected:
            lead = self.env['crm.lead'].browse(lead_id)
            # 並將active改為有效狀態
            lead.active = True
            lead.user_id = 0
            # 選擇畫面所選的銷售團隊
            lead.team_id = salesteam_id
            # 階段為該所選銷售團隊的第一個階段
            stage = self.env['crm.stage'].search([('team_id', '=', salesteam_id)], order='sequence', limit=1)
            lead.stage_id = stage.id
        return res
