[IMP] crm, crm_claim, crm_fundraising : search view of claim stages and fundraising...
[odoo/odoo.git] / addons / crm_claim / crm_claim.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 from osv import fields, osv
23 from crm import crm
24 import time
25
26 class crm_claim(crm.crm_case, osv.osv):
27     """
28     Crm claim
29     """
30     _name = "crm.claim"
31     _description = "Claim"
32     _order = "priority,date desc"
33     _inherit = ['mailgate.thread']
34     _columns = {
35         'id': fields.integer('ID', readonly=True), 
36         'name': fields.char('Claim Subject', size=128, required=True), 
37         'action_next': fields.char('Next Action', size=200),
38         'date_action_next': fields.datetime('Next Action Date'),
39         'description': fields.text('Description'), 
40         'resolution': fields.text('Resolution'), 
41         'create_date': fields.datetime('Creation Date' , readonly=True), 
42         'write_date': fields.datetime('Update Date' , readonly=True), 
43         'date_deadline': fields.date('Deadline'), 
44         'date_closed': fields.datetime('Closed', readonly=True), 
45         'date': fields.datetime('Claim Date'), 
46         'ref' : fields.reference('Reference', selection=crm._links_get, size=128), 
47         'categ_id': fields.many2one('crm.case.categ', 'Category', \
48                             domain="[('section_id','=',section_id),\
49                             ('object_id.model', '=', 'crm.claim')]"), 
50         'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'), 
51         'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
52         'user_id': fields.many2one('res.users', 'Responsible'), 
53         'user_fault': fields.char('Trouble Responsible', size=64), 
54         'section_id': fields.many2one('crm.case.section', 'Sales Team', \
55                         select=True, help="Sales team to which Case belongs to."\
56                                 "Define Responsible user and Email account for"\
57                                 " mail gateway."), 
58         'company_id': fields.many2one('res.company', 'Company'), 
59         'partner_id': fields.many2one('res.partner', 'Partner'), 
60         'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
61                                 # domain="[('partner_id','=',partner_id)]"
62                                  ), 
63         '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"), 
64         'email_from': fields.char('Email', size=128, help="These people will receive email."), 
65         'partner_phone': fields.char('Phone', size=32), 
66         'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type','=','claim')]"), 
67         'cause': fields.text('Root Cause'), 
68         'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True, 
69                                   help='The state is set to \'Draft\', when a case is created.\
70                                   \nIf the case is in progress the state is set to \'Open\'.\
71                                   \nWhen the case is over, the state is set to \'Done\'.\
72                                   \nIf the case needs to be reviewed then the state is set to \'Pending\'.'), 
73         'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
74     }
75     
76     def _get_stage_id(self, cr, uid, context=None):
77         """Finds type of stage according to object.
78         @param self: The object pointer
79         @param cr: the current row, from the database cursor,
80         @param uid: the current user’s ID for security checks,
81         @param context: A standard dictionary for contextual values
82         """
83         if context is None:
84             context = {}
85         type = context and context.get('stage_type', '')
86         stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=',type),('sequence','>=',1)])
87         return stage_ids and stage_ids[0] or False
88
89     _defaults = {
90         'user_id': crm.crm_case._get_default_user, 
91         'partner_id': crm.crm_case._get_default_partner, 
92         'partner_address_id': crm.crm_case._get_default_partner_address, 
93         'email_from':crm.crm_case. _get_default_email, 
94         'state': lambda *a: 'draft', 
95         'section_id':crm.crm_case. _get_section, 
96         'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
97         'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c), 
98         'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
99         #'stage_id': _get_stage_id, 
100     }
101     
102     def onchange_partner_id(self, cr, uid, ids, part, email=False):
103         """This function returns value of partner address based on partner
104         @param self: The object pointer
105         @param cr: the current row, from the database cursor,
106         @param uid: the current user’s ID for security checks,
107         @param ids: List of case IDs
108         @param part: Partner's id
109         @email: Partner's email ID
110         """
111         if not part:
112             return {'value': {'partner_address_id': False,
113                             'email_from': False, 
114                             'partner_phone': False,
115                             'partner_mobile': False
116                             }}
117         addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
118         data = {'partner_address_id': addr['contact']}
119         data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
120         return {'value': data}
121
122     def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
123         """This function returns value of partner email based on Partner Address
124         @param self: The object pointer
125         @param cr: the current row, from the database cursor,
126         @param uid: the current user’s ID for security checks,
127         @param ids: List of case IDs
128         @param add: Id of Partner's address
129         @email: Partner's email ID
130         """
131         if not add:
132             return {'value': {'email_from': False}}
133         address = self.pool.get('res.partner.address').browse(cr, uid, add)
134         return {'value': {'email_from': address.email, 'partner_phone': address.phone, 'partner_mobile': address.mobile}}
135         
136     def case_open(self, cr, uid, ids, *args):
137         """
138             Opens Claim
139         """
140         res = super(crm_claim, self).case_open(cr, uid, ids, *args)
141         claims = self.browse(cr, uid, ids)
142         
143         for i in xrange(0, len(ids)):
144             if not claims[i].stage_id :
145                 stage_id = self._find_first_stage(cr, uid, 'claim', claims[i].section_id.id or False)
146                 self.write(cr, uid, [ids[i]], {'stage_id' : stage_id})
147         
148         return res
149
150 crm_claim()
151
152
153 class crm_stage_claim(osv.osv):
154     
155     def _get_type_value(self, cr, user, context):
156         list = super(crm_stage_claim, self)._get_type_value(cr, user, context)
157         list.append(('claim','Claim'))
158         return list
159     
160     _inherit = "crm.case.stage"
161     _columns = {
162             'type': fields.selection(_get_type_value, 'Type'),
163     }
164    
165     
166 crm_stage_claim()
167
168
169 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: