eiru_crm.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding:utf-8 -*-
  2. from openerp import api, fields, models, exceptions, osv
  3. from datetime import datetime
  4. class crm_lead(models.Model):
  5. _inherit="crm.lead"
  6. task_ids = fields.One2many('crm.task','opportunity_id',"Actividades")
  7. @api.multi
  8. def open_wizard(self):
  9. now = datetime.now()
  10. view = self.env.ref('eiru_crm.crm_task_wizard')
  11. return {
  12. 'name': 'Añadir Actividad',
  13. 'res_model': 'crm.task',
  14. 'view_type': 'form',
  15. 'view_mode': 'form',
  16. 'target': 'new',
  17. 'views': [(view.id, 'form')],
  18. 'view_id': view.id,
  19. 'context': {
  20. 'default_date_action_next': now.strftime("%Y-%m-%d %H:%M:00"),
  21. 'default_user_id': self.user_id.id,
  22. 'default_opportunity_id': self.id,
  23. 'default_partner_id': self.partner_id.id,
  24. },
  25. 'type': 'ir.actions.act_window',
  26. }
  27. @api.one
  28. def _exist_task(self):
  29. now = datetime.now()
  30. for task in self:
  31. count1 = self.env['crm.task'].search_count([('opportunity_id', '=', task.id),('state','=','pending'), ('date_action_next','>=',now.strftime("%Y-%m-%d %H:%M:00"))])
  32. count2 = self.env['crm.task'].search_count([('opportunity_id', '=', task.id),('date_action_next','<',now.strftime("%Y-%m-%d %H:%M:00")),('state','=','pending')])
  33. if count1 > 0:
  34. self.exist_task = True
  35. if count2 > 0:
  36. self.expired_date = True
  37. exist_task = fields.Boolean(string="Tarea Pendiente", compute="_exist_task", store=False)
  38. expired_date = fields.Boolean(string="Fecha Vencida", compute="_exist_task", store=True)
  39. def on_change_partner_id(self, cr, uid, ids, partner_id, context=None):
  40. values = {}
  41. if partner_id:
  42. partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
  43. partner_name = (partner.parent_id and partner.parent_id.name) or (partner.is_company and partner.name) or False
  44. values = {
  45. 'partner_name': partner_name,
  46. 'contact_name': (not partner.is_company and partner.name) or False,
  47. 'title': partner.title and partner.title.id or False,
  48. 'street': partner.street,
  49. 'street2': partner.street2,
  50. 'city': partner.city,
  51. 'state_id': partner.state_id and partner.state_id.id or False,
  52. 'country_id': partner.country_id and partner.country_id.id or False,
  53. 'email_from': partner.email,
  54. 'phone': partner.phone,
  55. 'mobile': partner.mobile,
  56. 'fax': partner.fax,
  57. 'zip': partner.zip,
  58. 'function': partner.function,
  59. 'facebook': partner.facebook,
  60. 'instagram': partner.instagram,
  61. }
  62. return {'value': values}
  63. def case_mark_won(self, cr, uid, ids, context=None):
  64. """ Mark the case as won: state=done and probability=100
  65. """
  66. for each in ids:
  67. crm = self.pool.get('crm.lead').browse(cr, uid, each)
  68. self.pool.get('res.partner').write(cr, uid, crm.partner_id.id, {'customer': True}, context=context)
  69. stages_leads = {}
  70. for lead in self.browse(cr, uid, ids, context=context):
  71. stage_id = self.stage_find(cr, uid, [lead], lead.section_id.id or False, [('probability', '=', 100.0), ('on_change', '=', True)], context=context)
  72. if stage_id:
  73. if stages_leads.get(stage_id):
  74. stages_leads[stage_id].append(lead.id)
  75. else:
  76. stages_leads[stage_id] = [lead.id]
  77. else:
  78. raise osv.except_osv(_('Warning!'),
  79. _('To relieve your sales pipe and group all Won opportunities, configure one of your sales stage as follow:\n'
  80. 'probability = 100 % and select "Change Probability Automatically".\n'
  81. 'Create a specific stage or edit an existing one by editing columns of your opportunity pipe.'))
  82. for stage_id, lead_ids in stages_leads.items():
  83. self.write(cr, uid, lead_ids, {'stage_id': stage_id}, context=context)
  84. return True
  85. def create(self, cr, uid, vals, context=None):
  86. context = dict(context or {})
  87. if vals.get('type') and not context.get('default_type'):
  88. context['default_type'] = vals.get('type')
  89. if vals.get('section_id') and not context.get('default_section_id'):
  90. context['default_section_id'] = vals.get('section_id')
  91. if vals.get('user_id'):
  92. vals['date_open'] = fields.datetime.now()
  93. partner_id = vals['partner_id']
  94. values = {
  95. 'email': vals['email_from'],
  96. 'phone': vals['phone'],
  97. 'mobile': vals['mobile'],
  98. 'facebook': vals['facebook'],
  99. 'instagram': vals['instagram']
  100. }
  101. self.pool.get('res.partner').write(cr,uid,partner_id,values, context=context)
  102. # context: no_log, because subtype already handle this
  103. create_context = dict(context, mail_create_nolog=True)
  104. return super(crm_lead, self).create(cr, uid, vals, context=create_context)
  105. def write(self, cr, uid, ids, vals, context=None):
  106. # stage change: update date_last_stage_update
  107. if 'stage_id' in vals:
  108. vals['date_last_stage_update'] = fields.datetime.now()
  109. if vals.get('user_id'):
  110. vals['date_open'] = fields.datetime.now()
  111. # stage change with new stage: update probability and date_closed
  112. if vals.get('stage_id') and not vals.get('probability'):
  113. onchange_stage_values = self.onchange_stage_id(cr, uid, ids, vals.get('stage_id'), context=context)['value']
  114. vals.update(onchange_stage_values)
  115. self.update_partner(cr,uid,ids,vals)
  116. return super(crm_lead, self).write(cr, uid, ids, vals, context=context)
  117. def update_partner(self, cr, uid, ids, vals, context=None):
  118. context = dict(context or {})
  119. crm = self.pool.get('crm.lead').browse(cr,uid, ids)
  120. partner_id = crm.partner_id.id
  121. values = {'email_from','phone','mobile','facebook','instagram'}
  122. vals2 = {}
  123. for each in vals:
  124. if each in values:
  125. if each == 'email_from':
  126. vals2['email'] = vals[each]
  127. else:
  128. vals2[each] = vals[each]
  129. self.pool.get('res.partner').write(cr,uid,partner_id,vals2, context=context)
  130. return True
  131. class CrmStage(models.Model):
  132. _inherit="crm.case.stage"
  133. active = fields.Boolean('Activo', default=True)
  134. type2 = fields.Selection([('opportunity', 'Oportunidad')], string='Tipo', required=True, default='opportunity')
  135. code = fields.Char('Codigo')
  136. @api.multi
  137. def unlink(self):
  138. for record in self:
  139. if record.code == 'LEAD_NO_DELETE':
  140. raise exceptions.Warning(('Aviso'), ('Este estado no puede ser eliminado. Pruebe desactivarlo.'))
  141. return super(CrmStage, self).unlink()