# -*- coding:utf-8 -*- from openerp import fields, models, api, osv from datetime import datetime class crm_task(models.Model): _name="crm.task" name = fields.Char(string="Tarea", size=100) active = fields.Boolean("Activo", default=True) date_action_next = fields.Datetime('Fecha Prox. Accion') date_last_state_update = fields.Datetime('Last State Update') user_id = fields.Many2one('res.users', 'Responsable') partner_id = fields.Many2one('res.partner', 'Cliente') description = fields.Text('DescripciĆ³n') state = fields.Selection([ ('pending', 'Pendiente'), ('done', 'Realizado'), ('cancel','Cancelado')], string='Estado', track_visibility='onchange', default='pending') opportunity_id = fields.Many2one ('crm.lead', string='Oportunidad',ondelete='cascade') task_type_id = fields.Many2one('crm.task.type', string="Tipo de Tarea") is_opportunity = fields.Boolean(string="Es oportunidad?") @api.onchange('opportunity_id') def onchange_opportunity_id(self): res = {} opportunity = self.env['crm.lead'].search([('id','=', self.opportunity_id.id)]) self.partner_id = opportunity.partner_id.id res = {'value' :{'partner_id': self.partner_id}} return res @api.onchange('state') def onchange_state(self): res = {} res = {'value':{'state':self.state}} return res @api.multi def write(self,vals): ctx = dict(self._context or {}) vals['date_last_state_update'] = fields.datetime.now() return super(crm_task, self.with_context(ctx)).write(vals) # @api.onchange('task_type_id') def onchange_task_type_id(self): res = {} if hasattr(self, 'partner_id'): task = self.env['crm.task.type'].search([('id','=', self.task_type_id.id)]) partner = self.env['res.partner'].search([('id','=', self.partner_id.id)]) partner = partner.name self.name = "%s %s" % (task.name,partner) res = {'value':{'name':self.name}} return res @api.one def action_done(self): if self.state == 'pending': self.state = 'done' else: self.state = 'pending' @api.one def action_cancel(self): self.state = 'cancel' @api.multi def unlink(self): for all_id in self: res= self.env.cr.execute("DELETE FROM crm_task WHERE id= %s" % (all_id.id)) return res @api.one def _current_datetime(self): now = datetime.now() if self.date_action_next < now.strftime("%Y-%m-%d %H:%M:00"): self.expired_date = True expired_date = fields.Boolean(string="Fecha Vencida", compute="_current_datetime", store=True) @api.multi def createOpportunity(self): vals = { 'name': self.name, 'partner_id': self.partner_id.id, 'user_id': self.user_id.id, 'type':'opportunity' } self.is_opportunity = True res = self.env['crm.lead'].create(vals) return res