[ADD] change the link between lead and issue. Now : transfrom a lead into a issue and modify the mail thread

bzr revid: jem@openerp.com-20140217135731-538vluw7i1gverjf
This commit is contained in:
Jérôme 2014-02-17 14:57:31 +01:00
parent 7d656e961b
commit 0ff108c00c
5 changed files with 103 additions and 25 deletions

View File

@ -1,3 +1,2 @@
import project_issue
import crm_lead
import project_issue

View File

@ -13,8 +13,7 @@ CRM Project Issues
Link module to map leads and issues
""",
'data': [
'project_issue_view.xml',
'crm_lead_view.xml',
'project_issue_view.xml'
],
'depends' : ['crm', 'project_issue'],
'installable': True,

View File

@ -1,14 +1,36 @@
from openerp.osv import osv, fields
class crm_lead_to_project_issue_wizard(osv.TransientModel):
""" wizard to convert a Lead into a Project Issue and move the Mail Thread """
class project_issue(osv.Model):
_inherit = 'project.issue'
def action_lead_to_project_issue(self, cr, uid, ids, context=None):
# get the wizards
wizards = self.browse(cr, uid, ids, context=context)
lead_model = self.pool.get("crm.lead")
issue_model = self.pool.get("project.issue")
for wizard in wizards:
# get the lead to transform
lead = lead_model.browse(cr, uid, wizard.lead_id.id, context=context)
# create new project.issue
vals = {}
vals["name"] = lead.name
vals["description"] = lead.description
vals["email_from"] = lead.email_from
vals["partner_id"] = lead.partner_id.id
vals["project_id"] = wizard.project_id.id
issue_id = issue_model.create(cr, uid, vals, context=None)
# move the mail thread
lead_model.transform_model_messages(cr, uid, wizard.lead_id.id, issue_id, "project.issue", context=context)
# delete the lead
lead_model.unlink(cr, uid, [wizard.lead_id.id], context=None)
return False
_name = "crm.lead2projectissue.wizard"
_columns = {
'lead_id': fields.many2one('crm.lead', ondelete='set null', string="Related lead"),
}
_defaults = {
"lead_id" : fields.many2one("crm.lead","Lead", domain=[("type","=","lead")]),
"project_id" : fields.many2one("project.project", "Project", domain=[("use_issues","=",True)])
}

View File

@ -1,17 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<openerp>
<data>
<record model="ir.ui.view" id="crm_project_issue_form_view">
<field name="name">project.issue.form</field>
<field name="model">project.issue</field>
<field name="inherit_id" ref="project_issue.project_issue_form_view" />
<field name="arch" type="xml">
<field name="priority" position="after">
<field name="lead_id" groups="base.group_user" context="{'default_name': name, 'default_partner_id' : partner_id, 'default_email_from' : email_from, 'default_description' : description}"/>
</field>
</field>
</record>
<data>
</data>
<!-- inherit view of Lead Form : adding the "convert to issue" button -->
<record model="ir.ui.view" id="crm_case_form_view_leads_project_issue">
<field name="name">CRM - Leads Form</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_leads" />
<field name="arch" type="xml">
<xpath expr="//field[@name='stage_id']" position="before">
<button name="%(convert_lead2projectissue_wizard_action)d" string="Convert to Issue" type="action" help="Convert to Issue" context="{'default_lead_id' : id}" />
</xpath>
</field>
</record>
<!-- action of converting, via wizard -->
<record model="ir.actions.act_window" id="convert_lead2projectissue_wizard_action" >
<field name="name">Convert to Issue</field>
<field name="res_model">crm.lead2projectissue.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<!-- view of the Wizard Form -->
<record model="ir.ui.view" id="view_crm_lead2projectissue_wizard" >
<field name="name">crm.lead2projectissue.wizard.form</field>
<field name="model">crm.lead2projectissue.wizard</field>
<field name="arch" type="xml">
<form string="Convert to Issue" version="7.0">
<field name="lead_id" invisible="True" />
<label for="project_id" />
<field name="project_id" />
<footer>
<button type="special" special="cancel" string="Cancel" />
<button type="object" name="action_lead_to_project_issue" string="Convert" />
</footer>
</form>
</field>
</record>
</data>
</openerp>

View File

@ -1796,3 +1796,34 @@ class mail_thread(osv.AbstractModel):
}
threads.append(data)
return sorted(threads, key=lambda x: (x['popularity'], x['id']), reverse=True)[:3]
def transform_model_messages(self, cr, uid, id, new_res_id, new_model, context=None):
"""
Transfert the list of the mail thread messages from an model to another
:param id : the old res_id of the mail.message
:param new_res_id : the new res_id of the mail.message
:param new_model : the name of the new model of the mail.message
Example : self.pool.get("crm.lead").transform_model_messages(self, cr, uid, 2, 4, "project.issue", context)
will transfert thread of the lead (id=2) to the issue (id=4)
"""
# get the message ids belonging to the thread to migrate
message_obj = self.pool.get('mail.message')
msg_ids = message_obj.search(cr, uid, [
('model', '=', self._name),
('res_id', '=', id)], context=context)
subtype_res_id = self.pool.get('ir.model.data').xmlid_to_res_id(cr, uid, 'mail.mt_comment', raise_if_not_found=True)
# update the messages
for message in message_obj.browse(cr, uid, msg_ids, context=context):
if subtype_res_id == message.subtype_id.id:
# no change the subtype
message_obj.write(cr, uid, message.id, {"res_id" : new_res_id, "model" : new_model}, context=context)
else:
# modify the subtype
message_obj.write(cr, uid, message.id, {"res_id" : new_res_id, "model" : new_model, "subtype_id" : None}, context=context)
return True