[MERGE] Merge from trunk-wms-loconopreport-jco
[odoo/odoo.git] / addons / crm_project_issue / project_issue.py
1
2 from openerp.osv import osv, fields
3
4
5 class crm_lead_to_project_issue_wizard(osv.TransientModel):
6     """ wizard to convert a Lead into a Project Issue and move the Mail Thread """
7     _name = "crm.lead2projectissue.wizard"
8     _inherit = 'crm.partner.binding'
9
10     _columns = {
11         "lead_id": fields.many2one("crm.lead", "Lead", domain=[("type", "=", "lead")]),
12         "project_id": fields.many2one("project.project", "Project", domain=[("use_issues", "=", True)])
13     }
14
15     _defaults = {
16         "lead_id": lambda self, cr, uid, context=None: context.get('active_id')
17     }
18
19     def action_lead_to_project_issue(self, cr, uid, ids, context=None):
20         # get the wizards and models
21         wizards = self.browse(cr, uid, ids, context=context)
22         Lead = self.pool["crm.lead"]
23         Issue = self.pool["project.issue"]
24
25         for wizard in wizards:
26             # get the lead to transform
27             lead = wizard.lead_id
28
29             partner = self._find_matching_partner(cr, uid, context=context)
30             if not partner and (lead.partner_name or lead.contact_name):
31                 partner_ids = Lead.handle_partner_assignation(cr, uid, [lead.id], context=context)
32                 partner = partner_ids[lead.id]
33
34             # create new project.issue
35             vals = {
36                 "name": lead.name,
37                 "description": lead.description,
38                 "email_from": lead.email_from,
39                 "project_id": wizard.project_id.id,
40                 "partner_id": partner,
41                 "user_id": None
42             }
43             issue_id = Issue.create(cr, uid, vals, context=None)
44             # move the mail thread
45             Lead.message_change_thread(cr, uid, lead.id, issue_id, "project.issue", context=context)
46             # delete the lead
47             Lead.unlink(cr, uid, [lead.id], context=None)
48         # return the action to go to the form view of the new Issue
49         view_id = self.pool.get('ir.ui.view').search(cr, uid, [('model', '=', 'project.issue'), ('name', '=', 'project_issue_form_view')])
50         return {
51             'name': 'Issue created',
52             'view_type': 'form',
53             'view_mode': 'form',
54             'view_id': view_id,
55             'res_model': 'project.issue',
56             'type': 'ir.actions.act_window',
57             'res_id': issue_id,
58             'context': context
59         }