2017-08-09 14:55:17 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
""" CRM Action """
|
|
|
|
|
|
|
|
from odoo import models, fields, api, _
|
|
|
|
|
|
|
|
|
|
|
|
class CrmAction(models.Model):
|
|
|
|
""" CRM Action """
|
|
|
|
_name = 'crm.action'
|
|
|
|
_description = 'CRM Action'
|
|
|
|
_rec_name = 'display_name'
|
|
|
|
|
|
|
|
display_name = fields.Char(compute='_compute_display_name', store=True)
|
|
|
|
|
2017-08-09 15:50:25 +02:00
|
|
|
state = fields.Selection([('draft', 'Todo'), ('done', 'Done'),
|
|
|
|
('cancel', 'Canceled')], string='Status',
|
2017-08-09 17:07:49 +02:00
|
|
|
required=True, default='draft')
|
2017-08-09 14:55:17 +02:00
|
|
|
priority = fields.Selection([('0', 'Not evaluated'), ('1', 'Low'),
|
|
|
|
('2', 'Medium'), ('3', 'High'),
|
|
|
|
('4', 'Urgent')],
|
|
|
|
string='Priority', default='0', index=True)
|
2017-08-09 15:50:25 +02:00
|
|
|
details = fields.Text()
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
partner_id = fields.Many2one('res.partner', string='Customer', index=True)
|
|
|
|
date = fields.Date('Date', required=True, default=fields.Date.context_today)
|
|
|
|
user_id = fields.Many2one('res.users', string='User', required=True,
|
|
|
|
default=lambda self: self.env.user)
|
|
|
|
lead_id = fields.Many2one('crm.lead', string='Lead', ondelete='cascade')
|
|
|
|
event_id = fields.Many2one('calendar.event', 'Calendar event', ondelete='cascade')
|
2017-10-29 09:15:02 +01:00
|
|
|
action_type_id = fields.Many2one('crm.action.type', string='Type', required=True,
|
|
|
|
default=lambda self: self.env['crm.action.type'].search([], limit=1))
|
2017-08-09 14:55:17 +02:00
|
|
|
action_type_name = fields.Char(related='action_type_id.name')
|
|
|
|
|
|
|
|
@api.onchange('lead_id')
|
|
|
|
def onchange_lead(self):
|
|
|
|
""" If lead change, adapts partner """
|
|
|
|
for action in self:
|
|
|
|
if action.lead_id and action.lead_id.partner_id:
|
|
|
|
action.partner_id = action.lead_id.partner_id
|
|
|
|
|
|
|
|
@api.depends('action_type_id.name', 'details')
|
|
|
|
def _compute_display_name(self):
|
|
|
|
for action in self:
|
2017-08-09 17:07:49 +02:00
|
|
|
details = u'...'
|
|
|
|
if action.details:
|
|
|
|
details = u' '.join(action.details.split()[:5]) + details
|
2017-08-09 14:55:17 +02:00
|
|
|
action.display_name = u'[{}]{}'.format(action.action_type_name,
|
2017-08-09 17:07:49 +02:00
|
|
|
details)
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def confirm(self):
|
|
|
|
""" Changes state to done """
|
2017-08-09 15:50:25 +02:00
|
|
|
self.write({'state': 'done'})
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def set_to_draft(self):
|
|
|
|
""" Changes state to draft """
|
2017-08-09 15:50:25 +02:00
|
|
|
self.write({'state': 'draft'})
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def cancel(self):
|
|
|
|
""" Cancel action """
|
2017-08-09 15:50:25 +02:00
|
|
|
self.write({'state': 'cancel'})
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def create_linked_event(self):
|
|
|
|
""" Creates a linked event """
|
|
|
|
for action in self:
|
|
|
|
if not action.event_id:
|
|
|
|
attendees = [action.user_id.partner_id.id]
|
|
|
|
if action.partner_id:
|
|
|
|
attendees.append(action.partner_id.id)
|
|
|
|
data = {'start': action.date,
|
|
|
|
'stop': action.date,
|
|
|
|
'duration': 1.0,
|
|
|
|
'allday': False,
|
2017-08-09 15:50:25 +02:00
|
|
|
'name': action.display_name,
|
2017-08-09 14:55:17 +02:00
|
|
|
'description': action.details,
|
|
|
|
'privacy': 'confidential',
|
|
|
|
'partner_ids': [(6, False, attendees)],
|
|
|
|
'partner_location_id': action.partner_id.id or False}
|
|
|
|
action.event_id = self.env['calendar.event'].create(data)
|
|
|
|
return {'type': 'ir.actions.act_window',
|
|
|
|
'name': _('Linked event'),
|
|
|
|
'res_model': 'calendar.event',
|
|
|
|
'res_id': action.event_id.id,
|
|
|
|
'view_mode': 'form',
|
|
|
|
'view_type': 'form',
|
|
|
|
'flags': {'initial_mode': 'edit'}}
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def remove_linked_event(self):
|
|
|
|
""" Removes linked event and deletes it """
|
|
|
|
for action in self:
|
|
|
|
if action.event_id:
|
|
|
|
event = action.event_id
|
|
|
|
action.event_id = False
|
|
|
|
event.unlink()
|
|
|
|
|
|
|
|
|
|
|
|
class CrmActionType(models.Model):
|
|
|
|
""" CRM Action Type """
|
|
|
|
_name = 'crm.action.type'
|
|
|
|
_description = 'CRM Action Type'
|
|
|
|
_order = 'priority'
|
|
|
|
|
|
|
|
name = fields.Char('Name', required=True)
|
|
|
|
priority = fields.Integer('Priority', required=True, default=0)
|
|
|
|
active = fields.Boolean('Active', default=True)
|