# -*- coding:utf-8 -*- from openerp import api, fields, models, exceptions, osv from datetime import datetime class crm_lead(models.Model): _inherit="crm.lead" task_ids = fields.One2many('crm.task','opportunity_id',"Actividades") @api.multi def open_wizard(self): now = datetime.now() view = self.env.ref('eiru_crm.crm_task_wizard') return { 'name': 'AƱadir Actividad', 'res_model': 'crm.task', 'view_type': 'form', 'view_mode': 'form', 'target': 'new', 'views': [(view.id, 'form')], 'view_id': view.id, 'context': { 'default_date_action_next': now.strftime("%Y-%m-%d %H:%M:00"), 'default_user_id': self.user_id.id, 'default_opportunity_id': self.id, 'default_partner_id': self.partner_id.id, }, 'type': 'ir.actions.act_window', } @api.one def _exist_task(self): now = datetime.now() for task in self: count1 = self.env['crm.task'].search_count([('opportunity_id', '=', task.id),('state','=','pending'), ('date_action_next','>=',now.strftime("%Y-%m-%d %H:%M:00"))]) count2 = self.env['crm.task'].search_count([('opportunity_id', '=', task.id),('date_action_next','<',now.strftime("%Y-%m-%d %H:%M:00")),('state','=','pending')]) if count1 > 0: self.exist_task = True if count2 > 0: self.expired_date = True exist_task = fields.Boolean(string="Tarea Pendiente", compute="_exist_task", store=False) expired_date = fields.Boolean(string="Fecha Vencida", compute="_exist_task", store=True) def on_change_partner_id(self, cr, uid, ids, partner_id, context=None): values = {} if partner_id: partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context) partner_name = (partner.parent_id and partner.parent_id.name) or (partner.is_company and partner.name) or False values = { 'partner_name': partner_name, 'contact_name': (not partner.is_company and partner.name) or False, 'title': partner.title and partner.title.id or False, 'street': partner.street, 'street2': partner.street2, 'city': partner.city, 'state_id': partner.state_id and partner.state_id.id or False, 'country_id': partner.country_id and partner.country_id.id or False, 'email_from': partner.email, 'phone': partner.phone, 'mobile': partner.mobile, 'fax': partner.fax, 'zip': partner.zip, 'function': partner.function, 'facebook': partner.facebook, 'instagram': partner.instagram, } 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): _inherit="crm.case.stage" 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()