[IMP] crm: Rewrite merge opportunities wizard.

bzr revid: uco@tinyerp.com-20110203065614-6xq0c6qctl7pem85
This commit is contained in:
Ujjvala Collins (OpenERP)
2011-02-03 12:26:14 +05:30
parent daa1b72973
commit 1837358d31
2 changed files with 80 additions and 84 deletions
+80 -83
View File
@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from crm import crm
from osv import osv, fields
from tools.translate import _
@@ -28,35 +27,9 @@ class crm_merge_opportunity(osv.osv_memory):
_name = 'crm.merge.opportunity'
_description = 'Merge two Opportunities'
def view_init(self, cr, uid, fields, context=None):
"""
This function checks for precondition before wizard executes
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current user’s ID for security checks,
@param fields: List of fields for default value
@param context: A standard dictionary for contextual values
"""
record_id = context and context.get('active_id', False) or False
if record_id:
opp_obj = self.pool.get('crm.lead')
opp = opp_obj.browse(cr, uid, record_id, context=context)
if not opp.partner_id:
raise osv.except_osv(_('Warning!'), _('Opportunity must have Partner assigned before merging with other Opportunity.'))
#Search for Opportunity for the same partner
opp_ids = opp_obj.search(cr, uid, [('partner_id', '=', opp.partner_id.id), ('type', '=', 'opportunity'), ('state', 'in', ('open', 'pending'))])
# Removing current opportunity
if record_id in opp_ids:
opp_ids.remove(record_id)
if not opp_ids:
raise osv.except_osv(_('Warning!'), _("There are no other 'Open' or 'Pending' Opportunities for the partner '%s'.") % (opp.partner_id.name))
pass
def action_merge(self, cr, uid, ids, context=None):
"""
This function merges two opportunities
This function merges opportunities
@param self: The object pointer
@param cr: the current row, from the database cursor,
@param uid: the current user’s ID for security checks,
@@ -65,54 +38,87 @@ class crm_merge_opportunity(osv.osv_memory):
@return : Dictionary value for created Opportunity form
"""
record_id = context and context.get('active_id', False) or False
if record_id:
opp_obj = self.pool.get('crm.lead')
message_obj = self.pool.get('mailgate.message')
current_opp = opp_obj.browse(cr, uid, record_id, context=context)
record_ids = context and context.get('active_ids') or False
if len(record_ids) <= 1:
return {'type': 'ir.actions.act_window_close'}
obj_opportunity = self.browse(cr, uid, ids[0], context=context)
opp_obj = self.pool.get('crm.lead')
first_opp = obj_opportunity.opportunity_ids[0]
op_ids = obj_opportunity.opportunity_ids
first_opp_data = {}
for this in self.browse(cr, uid, ids, context=context):
for opp in this.opportunity_ids:
opp_obj.write(cr, uid, [opp.id], {
'stage_id': opp.stage_id.id or current_opp.stage_id.id or False,
'priority': opp.priority or current_opp.priority,
'email_from': opp.email_from or current_opp.email_from,
'phone': opp.phone or current_opp.phone,
'section_id': opp.section_id.id or current_opp.section_id.id or False,
'categ_id': opp.categ_id.id or current_opp.categ_id.id or False,
'description': (opp.description or '') + '\n' + (current_opp.description or ''),
'partner_name': opp.partner_name or current_opp.partner_name,
'title': opp.title.id or current_opp.title.id or False,
'function': opp.function or current_opp.function,
'street': opp.street or current_opp.street,
'street2': opp.street2 or current_opp.street2,
'zip': opp.zip or current_opp.zip,
'city': opp.city or current_opp.city,
'country_id': opp.country_id.id or current_opp.country_id.id or False,
'state_id': opp.state_id.id or current_opp.state_id.id or False,
'fax': opp.fax or current_opp.fax,
'mobile': opp.mobile or current_opp.mobile,
'email_cc': ','.join(filter(lambda x: x, [opp.email_cc, current_opp.email_cc]))
})
for history in current_opp.message_ids:
if history.history:
new_history = message_obj.copy(cr, uid, history.id, default={'res_id': opp.id})
opp_obj._history(cr, uid, [current_opp], _('Merged into Opportunity: %s') % (opp.id))
for opp in op_ids[1:]:
first_opp_data = {
'partner_id': first_opp.partner_id.id or opp.partner_id.id,
'stage_id': first_opp.stage_id.id or opp.stage_id.id,
'section_id': first_opp.section_id.id or opp.section_id.id,
'categ_id': first_opp.categ_id.id or opp.categ_id.id,
'type_id': first_opp.type_id.id or opp.type_id.id,
'channel_id': first_opp.channel_id.id or opp.channel_id.id,
'user_id': first_opp.user_id.id or opp.user_id.id,
'country_id': first_opp.country_id.id or opp.country_id.id,
'state_id': first_opp.state_id.id or opp.state_id.id,
'partner_address_id': first_opp.partner_address_id.id or opp.partner_address_id.id,
'priority': first_opp.priority or opp.priority,
'title': first_opp.title.id or opp.title.id,
'function': first_opp.function or opp.function,
'email_from': first_opp.email_from or opp.email_from,
'phone': first_opp.phone or opp.phone,
'description': first_opp.description or opp.description,
'partner_name': first_opp.partner_name or opp.partner_name,
'street': first_opp.street or opp.street,
'street2': first_opp.street2 or opp.street2,
'zip': first_opp.zip or opp.zip,
'city': first_opp.city or opp.city,
'fax': first_opp.fax or opp.fax,
'mobile': first_opp.mobile or opp.mobile,
'email_cc': ','.join(filter(lambda x: x, [opp.email_cc, first_opp.email_cc])),
'state': 'open'
}
for history in opp.message_ids:
if history.history:
new_history = message_obj.copy(cr, uid, history.id, default={'res_id': opp.id})
opp_obj._history(cr, uid, [first_opp], _('Merged from Opportunity: %s : Information lost : [Partner: %s, Stage: %s, Section: %s, Salesman: %s]') % (opp.name, opp.partner_id.name or '', opp.stage_id.name or '', opp.section_id.name or '', opp.user_id.name or ''))
if this.state == 'unchanged':
pass
elif this.state == 'done':
opp_obj.case_close(cr, uid, [record_id])
elif this.state == 'draft':
opp_obj.case_reset(cr, uid, [record_id])
elif this.state in ['cancel', 'open', 'pending']:
act = 'case_' + this.state
getattr(opp_obj, act)(cr, uid, [record_id])
return {'type': 'ir.actions.act_window_close'}
opp_obj.write(cr, uid, [first_opp.id], first_opp_data)
unlink_ids = map(lambda x: x.id, op_ids[1:])
opp_obj.unlink(cr, uid, unlink_ids)
models_data = self.pool.get('ir.model.data')
# Get Opportunity views
result = models_data._get_id(
cr, uid, 'crm', 'view_crm_case_opportunities_filter')
opportunity_view_form = models_data._get_id(
cr, uid, 'crm', 'crm_case_form_view_oppor')
opportunity_view_tree = models_data._get_id(
cr, uid, 'crm', 'crm_case_tree_view_oppor')
if opportunity_view_form:
opportunity_view_form = models_data.browse(
cr, uid, opportunity_view_form, context=context).res_id
if opportunity_view_tree:
opportunity_view_tree = models_data.browse(
cr, uid, opportunity_view_tree, context=context).res_id
return {
'name': _('Opportunity'),
'view_type': 'form',
'view_mode': 'tree, form',
'res_model': 'crm.lead',
'domain': [('type', '=', 'opportunity')],
'res_id': int(first_opp.id),
'view_id': False,
'views': [(opportunity_view_form, 'form'),
(opportunity_view_tree, 'tree'),
(False, 'calendar'), (False, 'graph')],
'type': 'ir.actions.act_window',
}
_columns = {
'opportunity_ids' : fields.many2many('crm.lead', 'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
'state': fields.selection(crm.AVAILABLE_STATES + [('unchanged', 'Unchanged')], string='Set State To', required=True),
}
def default_get(self, cr, uid, fields, context=None):
@@ -126,21 +132,12 @@ class crm_merge_opportunity(osv.osv_memory):
@return : default values of fields.
"""
record_id = context and context.get('active_id', False) or False
record_ids = context and context.get('active_ids', False) or False
res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
if record_id:
opp_obj = self.pool.get('crm.lead')
opp = opp_obj.browse(cr, uid, record_id, context=context)
opp_ids = opp_obj.search(cr, uid, [('partner_id', '=', opp.partner_id.id), ('type', '=', 'opportunity'), ('state', 'in', ('open', 'pending'))])
# Removing current opportunity
if record_id in opp_ids:
opp_ids.remove(record_id)
if record_ids:
if 'opportunity_ids' in fields:
res.update({'opportunity_ids': opp_ids})
if 'state' in fields:
res.update({'state': u'cancel'})
res.update({'opportunity_ids': record_ids})
return res
@@ -14,7 +14,6 @@
<field name="opportunity_ids" nolabel="1" colspan="4" width="550" height="300"/>
<separator colspan="4"/>
<group col="4" colspan="4">
<field name="state" />
<button string="_Cancel" icon="gtk-cancel" special="cancel" />
<button name="action_merge" type="object"
string="_Merge" icon="gtk-ok" />