|
@@ -1,6 +1,6 @@
|
|
# -*- coding:utf-8 -*-
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
-from openerp import api, fields, models
|
|
|
|
|
|
+from openerp import api, fields, models, exceptions, osv
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
|
class crm_lead(models.Model):
|
|
class crm_lead(models.Model):
|
|
@@ -70,7 +70,96 @@ class crm_lead(models.Model):
|
|
}
|
|
}
|
|
return {'value': values}
|
|
return {'value': values}
|
|
|
|
|
|
|
|
+ def case_mark_won(self, cr, uid, ids, context=None):
|
|
|
|
+ """ Mark the case as won: state=done and probability=100
|
|
|
|
+ """
|
|
|
|
+ for each in ids:
|
|
|
|
+ crm = self.pool.get('crm.lead').browse(cr, uid, each)
|
|
|
|
+ self.pool.get('res.partner').write(cr, uid, crm.partner_id.id, {'customer': True}, context=context)
|
|
|
|
+
|
|
|
|
+ stages_leads = {}
|
|
|
|
+ for lead in self.browse(cr, uid, ids, context=context):
|
|
|
|
+ stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 100.0), ('on_change', '=', True)], context=context)
|
|
|
|
+ if stage_id:
|
|
|
|
+ if stages_leads.get(stage_id):
|
|
|
|
+ stages_leads[stage_id].append(lead.id)
|
|
|
|
+ else:
|
|
|
|
+ stages_leads[stage_id] = [lead.id]
|
|
|
|
+ else:
|
|
|
|
+ raise osv.except_osv(_('Warning!'),
|
|
|
|
+ _('To relieve your sales pipe and group all Won opportunities, configure one of your sales stage as follow:\n'
|
|
|
|
+ 'probability = 100 % and select "Change Probability Automatically".\n'
|
|
|
|
+ 'Create a specific stage or edit an existing one by editing columns of your opportunity pipe.'))
|
|
|
|
+ for stage_id, lead_ids in stages_leads.items():
|
|
|
|
+ self.write(cr, uid, lead_ids, {'stage_id': stage_id}, context=context)
|
|
|
|
+ return True
|
|
|
|
+
|
|
|
|
+ def create(self, cr, uid, vals, context=None):
|
|
|
|
+
|
|
|
|
+ context = dict(context or {})
|
|
|
|
+ if vals.get('type') and not context.get('default_type'):
|
|
|
|
+ context['default_type'] = vals.get('type')
|
|
|
|
+ if vals.get('section_id') and not context.get('default_section_id'):
|
|
|
|
+ context['default_section_id'] = vals.get('section_id')
|
|
|
|
+ if vals.get('user_id'):
|
|
|
|
+ vals['date_open'] = fields.datetime.now()
|
|
|
|
+
|
|
|
|
+ partner_id = vals['partner_id']
|
|
|
|
+ values = {
|
|
|
|
+ 'email': vals['email_from'],
|
|
|
|
+ 'phone': vals['phone'],
|
|
|
|
+ 'mobile': vals['mobile'],
|
|
|
|
+ 'facebook': vals['facebook'],
|
|
|
|
+ 'instagram': vals['instagram']
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ self.pool.get('res.partner').write(cr,uid,partner_id,values, context=context)
|
|
|
|
+
|
|
|
|
+ # context: no_log, because subtype already handle this
|
|
|
|
+ create_context = dict(context, mail_create_nolog=True)
|
|
|
|
+ return super(crm_lead, self).create(cr, uid, vals, context=create_context)
|
|
|
|
+
|
|
|
|
+ def write(self, cr, uid, ids, vals, context=None):
|
|
|
|
+ # stage change: update date_last_stage_update
|
|
|
|
+ if 'stage_id' in vals:
|
|
|
|
+ vals['date_last_stage_update'] = fields.datetime.now()
|
|
|
|
+ if vals.get('user_id'):
|
|
|
|
+ vals['date_open'] = fields.datetime.now()
|
|
|
|
+ # stage change with new stage: update probability and date_closed
|
|
|
|
+ if vals.get('stage_id') and not vals.get('probability'):
|
|
|
|
+ onchange_stage_values = self.onchange_stage_id(cr, uid, ids, vals.get('stage_id'), context=context)['value']
|
|
|
|
+ vals.update(onchange_stage_values)
|
|
|
|
+ self.update_partner(cr,uid,ids,vals)
|
|
|
|
+ return super(crm_lead, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
+
|
|
|
|
+ def update_partner(self, cr, uid, ids, vals, context=None):
|
|
|
|
+ context = dict(context or {})
|
|
|
|
+
|
|
|
|
+ crm = self.pool.get('crm.lead').browse(cr,uid, ids)
|
|
|
|
+ partner_id = crm.partner_id.id
|
|
|
|
+ values = {'email_from','phone','mobile','facebook','instagram'}
|
|
|
|
+ vals2 = {}
|
|
|
|
+
|
|
|
|
+ for each in vals:
|
|
|
|
+ if each in values:
|
|
|
|
+ if each == 'email_from':
|
|
|
|
+ vals2['email'] = vals[each]
|
|
|
|
+ else:
|
|
|
|
+ vals2[each] = vals[each]
|
|
|
|
+
|
|
|
|
+ self.pool.get('res.partner').write(cr,uid,partner_id,vals2, context=context)
|
|
|
|
+ return True
|
|
|
|
+
|
|
class CrmStage(models.Model):
|
|
class CrmStage(models.Model):
|
|
_inherit="crm.case.stage"
|
|
_inherit="crm.case.stage"
|
|
|
|
|
|
- type = fields.Selection([('opportunity', 'Oportunidad')], string='Tipo', required=True, default='opportunity')
|
|
|
|
|
|
+ active = fields.Boolean('Activo', default=True)
|
|
|
|
+ type2 = fields.Selection([('opportunity', 'Oportunidad')], string='Tipo', required=True, default='opportunity')
|
|
|
|
+ code = fields.Char('Codigo')
|
|
|
|
+
|
|
|
|
+ @api.multi
|
|
|
|
+ def unlink(self):
|
|
|
|
+ for record in self:
|
|
|
|
+ if record.code == 'LEAD_NO_DELETE':
|
|
|
|
+ raise exceptions.Warning(('Aviso'), ('Este estado no puede ser eliminado. Pruebe desactivarlo.'))
|
|
|
|
+ return super(CrmStage, self).unlink()
|