~asteam/asdaily/dhinesh

« back to all changes in this revision

Viewing changes to openerp/addons/crm_claim/crm_claim.py

  • Committer: dhinesh
  • Date: 2014-09-09 07:49:53 UTC
  • Revision ID: p.dhinesh1990escet@gmail.com-20140909074953-mjkzeki2uxryewng
by dhinesh

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
import openerp
 
23
from openerp.addons.crm import crm
 
24
from openerp.osv import fields, osv
 
25
from openerp import tools
 
26
from openerp.tools.translate import _
 
27
from openerp.tools import html2plaintext
 
28
 
 
29
 
 
30
class crm_claim_stage(osv.osv):
 
31
    """ Model for claim stages. This models the main stages of a claim
 
32
        management flow. Main CRM objects (leads, opportunities, project
 
33
        issues, ...) will now use only stages, instead of state and stages.
 
34
        Stages are for example used to display the kanban view of records.
 
35
    """
 
36
    _name = "crm.claim.stage"
 
37
    _description = "Claim stages"
 
38
    _rec_name = 'name'
 
39
    _order = "sequence"
 
40
 
 
41
    _columns = {
 
42
        'name': fields.char('Stage Name', required=True, translate=True),
 
43
        'sequence': fields.integer('Sequence', help="Used to order stages. Lower is better."),
 
44
        'section_ids':fields.many2many('crm.case.section', 'section_claim_stage_rel', 'stage_id', 'section_id', string='Sections',
 
45
                        help="Link between stages and sales teams. When set, this limitate the current stage to the selected sales teams."),
 
46
        'case_default': fields.boolean('Common to All Teams',
 
47
                        help="If you check this field, this stage will be proposed by default on each sales team. It will not assign this stage to existing teams."),
 
48
    }
 
49
 
 
50
    _defaults = {
 
51
        'sequence': lambda *args: 1,
 
52
    }
 
53
 
 
54
class crm_claim(osv.osv):
 
55
    """ Crm claim
 
56
    """
 
57
    _name = "crm.claim"
 
58
    _description = "Claim"
 
59
    _order = "priority,date desc"
 
60
    _inherit = ['mail.thread']
 
61
 
 
62
    def _get_default_section_id(self, cr, uid, context=None):
 
63
        """ Gives default section by checking if present in the context """
 
64
        return self.pool.get('crm.lead')._resolve_section_id_from_context(cr, uid, context=context) or False
 
65
 
 
66
    def _get_default_stage_id(self, cr, uid, context=None):
 
67
        """ Gives default stage_id """
 
68
        section_id = self._get_default_section_id(cr, uid, context=context)
 
69
        return self.stage_find(cr, uid, [], section_id, [('sequence', '=', '1')], context=context)
 
70
 
 
71
    _columns = {
 
72
        'id': fields.integer('ID', readonly=True),
 
73
        'name': fields.char('Claim Subject', required=True),
 
74
        'active': fields.boolean('Active'),
 
75
        'action_next': fields.char('Next Action'),
 
76
        'date_action_next': fields.datetime('Next Action Date'),
 
77
        'description': fields.text('Description'),
 
78
        'resolution': fields.text('Resolution'),
 
79
        'create_date': fields.datetime('Creation Date' , readonly=True),
 
80
        'write_date': fields.datetime('Update Date' , readonly=True),
 
81
        'date_deadline': fields.date('Deadline'),
 
82
        'date_closed': fields.datetime('Closed', readonly=True),
 
83
        'date': fields.datetime('Claim Date', select=True),
 
84
        'ref': fields.reference('Reference', selection=openerp.addons.base.res.res_request.referencable_models),
 
85
        'categ_id': fields.many2one('crm.case.categ', 'Category', \
 
86
                            domain="[('section_id','=',section_id),\
 
87
                            ('object_id.model', '=', 'crm.claim')]"),
 
88
        'priority': fields.selection([('0','Low'), ('1','Normal'), ('2','High')], 'Priority'),
 
89
        'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
 
90
        'user_id': fields.many2one('res.users', 'Responsible', track_visibility='always'),
 
91
        'user_fault': fields.char('Trouble Responsible'),
 
92
        'section_id': fields.many2one('crm.case.section', 'Sales Team', \
 
93
                        select=True, help="Responsible sales team."\
 
94
                                " Define Responsible user and Email account for"\
 
95
                                " mail gateway."),
 
96
        'company_id': fields.many2one('res.company', 'Company'),
 
97
        'partner_id': fields.many2one('res.partner', 'Partner'),
 
98
        'email_cc': fields.text('Watchers Emails', size=252, help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
 
99
        'email_from': fields.char('Email', size=128, help="Destination email for email gateway."),
 
100
        'partner_phone': fields.char('Phone'),
 
101
        'stage_id': fields.many2one ('crm.claim.stage', 'Stage', track_visibility='onchange',
 
102
                domain="['|', ('section_ids', '=', section_id), ('case_default', '=', True)]"),
 
103
        'cause': fields.text('Root Cause'),
 
104
    }
 
105
 
 
106
    _defaults = {
 
107
        'user_id': lambda s, cr, uid, c: uid,
 
108
        'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, c),
 
109
        'date': fields.datetime.now,
 
110
        'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
 
111
        'priority': '1',
 
112
        'active': lambda *a: 1,
 
113
        'stage_id': lambda s, cr, uid, c: s._get_default_stage_id(cr, uid, c)
 
114
    }
 
115
 
 
116
    def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None):
 
117
        """ Override of the base.stage method
 
118
            Parameter of the stage search taken from the lead:
 
119
            - section_id: if set, stages must belong to this section or
 
120
              be a default case
 
121
        """
 
122
        if isinstance(cases, (int, long)):
 
123
            cases = self.browse(cr, uid, cases, context=context)
 
124
        # collect all section_ids
 
125
        section_ids = []
 
126
        if section_id:
 
127
            section_ids.append(section_id)
 
128
        for claim in cases:
 
129
            if claim.section_id:
 
130
                section_ids.append(claim.section_id.id)
 
131
        # OR all section_ids and OR with case_default
 
132
        search_domain = []
 
133
        if section_ids:
 
134
            search_domain += [('|')] * len(section_ids)
 
135
            for section_id in section_ids:
 
136
                search_domain.append(('section_ids', '=', section_id))
 
137
        search_domain.append(('case_default', '=', True))
 
138
        # AND with the domain in parameter
 
139
        search_domain += list(domain)
 
140
        # perform search, return the first found
 
141
        stage_ids = self.pool.get('crm.claim.stage').search(cr, uid, search_domain, order=order, context=context)
 
142
        if stage_ids:
 
143
            return stage_ids[0]
 
144
        return False
 
145
 
 
146
    def onchange_partner_id(self, cr, uid, ids, partner_id, email=False, context=None):
 
147
        """This function returns value of partner address based on partner
 
148
           :param email: ignored
 
149
        """
 
150
        if not partner_id:
 
151
            return {'value': {'email_from': False, 'partner_phone': False}}
 
152
        address = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
 
153
        return {'value': {'email_from': address.email, 'partner_phone': address.phone}}
 
154
 
 
155
    def create(self, cr, uid, vals, context=None):
 
156
        context = dict(context or {})
 
157
        if vals.get('section_id') and not context.get('default_section_id'):
 
158
            context['default_section_id'] = vals.get('section_id')
 
159
 
 
160
        # context: no_log, because subtype already handle this
 
161
        return super(crm_claim, self).create(cr, uid, vals, context=context)
 
162
 
 
163
    def copy(self, cr, uid, id, default=None, context=None):
 
164
        claim = self.browse(cr, uid, id, context=context)
 
165
        default = dict(default or {},
 
166
            stage_id = self._get_default_stage_id(cr, uid, context=context),
 
167
            name = _('%s (copy)') % claim.name)
 
168
        return super(crm_claim, self).copy(cr, uid, id, default, context=context)
 
169
 
 
170
    # -------------------------------------------------------
 
171
    # Mail gateway
 
172
    # -------------------------------------------------------
 
173
 
 
174
    def message_new(self, cr, uid, msg, custom_values=None, context=None):
 
175
        """ Overrides mail_thread message_new that is called by the mailgateway
 
176
            through message_process.
 
177
            This override updates the document according to the email.
 
178
        """
 
179
        if custom_values is None:
 
180
            custom_values = {}
 
181
        desc = html2plaintext(msg.get('body')) if msg.get('body') else ''
 
182
        defaults = {
 
183
            'name': msg.get('subject') or _("No Subject"),
 
184
            'description': desc,
 
185
            'email_from': msg.get('from'),
 
186
            'email_cc': msg.get('cc'),
 
187
            'partner_id': msg.get('author_id', False),
 
188
        }
 
189
        if msg.get('priority'):
 
190
            defaults['priority'] = msg.get('priority')
 
191
        defaults.update(custom_values)
 
192
        return super(crm_claim, self).message_new(cr, uid, msg, custom_values=defaults, context=context)
 
193
 
 
194
class res_partner(osv.osv):
 
195
    _inherit = 'res.partner'
 
196
    def _claim_count(self, cr, uid, ids, field_name, arg, context=None):
 
197
        Claim = self.pool['crm.claim']
 
198
        return {
 
199
            partner_id: Claim.search_count(cr,uid, [('partner_id', '=', partner_id)], context=context)  
 
200
            for partner_id in ids
 
201
        }
 
202
 
 
203
    _columns = {
 
204
        'claim_count': fields.function(_claim_count, string='# Claims', type='integer'),
 
205
    }
 
206
 
 
207
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: