crm_task.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding:utf-8 -*-
  2. from openerp import fields, models, api, osv
  3. from datetime import datetime
  4. class crm_task(models.Model):
  5. _name="crm.task"
  6. name = fields.Char(string="Tarea", size=100)
  7. active = fields.Boolean("Activo", default=True)
  8. date_action_next = fields.Datetime('Fecha Prox. Accion')
  9. date_last_state_update = fields.Datetime('Last State Update')
  10. user_id = fields.Many2one('res.users', 'Responsable')
  11. partner_id = fields.Many2one('res.partner', 'Cliente')
  12. description = fields.Text('Descripción')
  13. state = fields.Selection([
  14. ('pending', 'Pendiente'),
  15. ('done', 'Realizado'),
  16. ('cancel','Cancelado')],
  17. string='Estado', track_visibility='onchange', default='pending')
  18. opportunity_id = fields.Many2one ('crm.lead', string='Oportunidad',ondelete='cascade')
  19. task_type_id = fields.Many2one('crm.task.type', string="Tipo de Tarea")
  20. is_opportunity = fields.Boolean(string="Es oportunidad?")
  21. @api.onchange('opportunity_id')
  22. def onchange_opportunity_id(self):
  23. res = {}
  24. opportunity = self.env['crm.lead'].search([('id','=', self.opportunity_id.id)])
  25. self.partner_id = opportunity.partner_id.id
  26. res = {'value' :{'partner_id': self.partner_id}}
  27. return res
  28. @api.onchange('state')
  29. def onchange_state(self):
  30. res = {}
  31. res = {'value':{'state':self.state}}
  32. return res
  33. @api.multi
  34. def write(self,vals):
  35. ctx = dict(self._context or {})
  36. vals['date_last_state_update'] = fields.datetime.now()
  37. return super(crm_task, self.with_context(ctx)).write(vals)
  38. # @api.onchange('task_type_id')
  39. def onchange_task_type_id(self):
  40. res = {}
  41. if hasattr(self, 'partner_id'):
  42. task = self.env['crm.task.type'].search([('id','=', self.task_type_id.id)])
  43. partner = self.env['res.partner'].search([('id','=', self.partner_id.id)])
  44. partner = partner.name
  45. self.name = "%s %s" % (task.name,partner)
  46. res = {'value':{'name':self.name}}
  47. return res
  48. @api.one
  49. def action_done(self):
  50. if self.state == 'pending':
  51. self.state = 'done'
  52. else:
  53. self.state = 'pending'
  54. @api.one
  55. def action_cancel(self):
  56. self.state = 'cancel'
  57. @api.multi
  58. def unlink(self):
  59. for all_id in self:
  60. res= self.env.cr.execute("DELETE FROM crm_task WHERE id= %s" % (all_id.id))
  61. return res
  62. @api.one
  63. def _current_datetime(self):
  64. now = datetime.now()
  65. if self.date_action_next < now.strftime("%Y-%m-%d %H:%M:00"):
  66. self.expired_date = True
  67. expired_date = fields.Boolean(string="Fecha Vencida", compute="_current_datetime", store=True)
  68. @api.multi
  69. def createOpportunity(self):
  70. vals = {
  71. 'name': self.name,
  72. 'partner_id': self.partner_id.id,
  73. 'user_id': self.user_id.id,
  74. 'type':'opportunity'
  75. }
  76. self.is_opportunity = True
  77. res = self.env['crm.lead'].create(vals)
  78. return res