1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
27 from tools.translate import _
29 CRM_CLAIM_PENDING_STATES = (
30 crm.AVAILABLE_STATES[2][0], # Cancelled
31 crm.AVAILABLE_STATES[3][0], # Done
32 crm.AVAILABLE_STATES[4][0], # Pending
35 class crm_claim(crm.crm_case, osv.osv):
40 _description = "Claim"
41 _order = "priority,date desc"
42 _inherit = ['mailgate.thread']
44 'id': fields.integer('ID', readonly=True),
45 'name': fields.char('Claim Subject', size=128, required=True),
46 'action_next': fields.char('Next Action', size=200),
47 'date_action_next': fields.datetime('Next Action Date'),
48 'description': fields.text('Description'),
49 'resolution': fields.text('Resolution'),
50 'create_date': fields.datetime('Creation Date' , readonly=True),
51 'write_date': fields.datetime('Update Date' , readonly=True),
52 'date_deadline': fields.date('Deadline'),
53 'date_closed': fields.datetime('Closed', readonly=True),
54 'date': fields.datetime('Claim Date'),
55 'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
56 'categ_id': fields.many2one('crm.case.categ', 'Category', \
57 domain="[('section_id','=',section_id),\
58 ('object_id.model', '=', 'crm.claim')]"),
59 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
60 'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
61 'user_id': fields.many2one('res.users', 'Responsible'),
62 'user_fault': fields.char('Trouble Responsible', size=64),
63 'section_id': fields.many2one('crm.case.section', 'Sales Team', \
64 select=True, help="Sales team to which Case belongs to."\
65 "Define Responsible user and Email account for"\
67 'company_id': fields.many2one('res.company', 'Company'),
68 'partner_id': fields.many2one('res.partner', 'Partner'),
69 'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
70 # domain="[('partner_id','=',partner_id)]"
72 '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"),
73 'email_from': fields.char('Email', size=128, help="These people will receive email."),
74 'partner_phone': fields.char('Phone', size=32),
75 'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('type','=','claim')]"),
76 'cause': fields.text('Root Cause'),
77 'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
78 help='The state is set to \'Draft\', when a case is created.\
79 \nIf the case is in progress the state is set to \'Open\'.\
80 \nWhen the case is over, the state is set to \'Done\'.\
81 \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
82 'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
85 def write(self, cr, uid, ids, vals, context=None):
89 if 'stage_id' in vals and vals['stage_id']:
90 type = context and context.get('stage_type', '')
91 stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, vals['stage_id'], context=context)
92 self.history(cr, uid, ids, _("Changed Stage to: ") + stage_obj.name, details=_("Changed Stage to: ") + stage_obj.name)
94 for case in self.browse(cr, uid, ids, context=context):
95 message = _("The stage of claim '%s' has been changed to '%s'.") % (case.name, stage_obj.name)
96 self.log(cr, uid, case.id, message)
97 return super(crm_claim,self).write(cr, uid, ids, vals, context)
99 def stage_historize(self, cr, uid, ids, stage, context=None):
100 stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage, context=context)
101 self.history(cr, uid, ids, _('Stage'), details=stage_obj.name)
102 for case in self.browse(cr, uid, ids, context=context):
103 message = _("The stage of claim '%s' has been changed to '%s'.") % (case.name, stage_obj.name)
104 self.log(cr, uid, case.id, message)
107 def stage_next(self, cr, uid, ids, context=None):
108 stage = super(crm_claim, self).stage_next(cr, uid, ids, context=context)
110 stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage, context=context)
111 if stage_obj.on_change:
112 self.write(cr, uid, ids)
115 def stage_previous(self, cr, uid, ids, context=None):
116 stage = super(crm_claim, self).stage_previous(cr, uid, ids, context=context)
118 stage_obj = self.pool.get('crm.case.stage').browse(cr, uid, stage, context=context)
119 if stage_obj.on_change:
120 self.write(cr, uid, ids)
123 def _get_stage_id(self, cr, uid, context=None):
124 """Finds type of stage according to object.
125 @param self: The object pointer
126 @param cr: the current row, from the database cursor,
127 @param uid: the current user’s ID for security checks,
128 @param context: A standard dictionary for contextual values
132 type = context and context.get('stage_type', '')
133 stage_ids = self.pool.get('crm.case.stage').search(cr, uid, [('type','=',type),('sequence','>=',1)])
134 return stage_ids and stage_ids[0] or False
137 'user_id': crm.crm_case._get_default_user,
138 'partner_id': crm.crm_case._get_default_partner,
139 'partner_address_id': crm.crm_case._get_default_partner_address,
140 'email_from':crm.crm_case. _get_default_email,
141 'state': lambda *a: 'draft',
142 'section_id':crm.crm_case. _get_section,
143 'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
144 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
145 'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
146 #'stage_id': _get_stage_id,
149 def onchange_partner_id(self, cr, uid, ids, part, email=False):
150 """This function returns value of partner address based on partner
151 @param self: The object pointer
152 @param cr: the current row, from the database cursor,
153 @param uid: the current user’s ID for security checks,
154 @param ids: List of case IDs
155 @param part: Partner's id
156 @email: Partner's email ID
159 return {'value': {'partner_address_id': False,
161 'partner_phone': False,
162 'partner_mobile': False
164 addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
165 data = {'partner_address_id': addr['contact']}
166 data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
167 return {'value': data}
169 def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
170 """This function returns value of partner email based on Partner Address
171 @param self: The object pointer
172 @param cr: the current row, from the database cursor,
173 @param uid: the current user’s ID for security checks,
174 @param ids: List of case IDs
175 @param add: Id of Partner's address
176 @email: Partner's email ID
179 return {'value': {'email_from': False}}
180 address = self.pool.get('res.partner.address').browse(cr, uid, add)
181 return {'value': {'email_from': address.email, 'partner_phone': address.phone, 'partner_mobile': address.mobile}}
183 def case_open(self, cr, uid, ids, *args):
187 res = super(crm_claim, self).case_open(cr, uid, ids, *args)
188 claims = self.browse(cr, uid, ids)
190 for i in xrange(0, len(ids)):
191 if not claims[i].stage_id :
192 stage_id = self._find_first_stage(cr, uid, 'claim', claims[i].section_id.id or False)
193 self.write(cr, uid, [ids[i]], {'stage_id' : stage_id})
197 def message_new(self, cr, uid, msg, context=None):
199 Automatically calls when new email message arrives
201 @param self: The object pointer
202 @param cr: the current row, from the database cursor,
203 @param uid: the current user’s ID for security checks
205 mailgate_pool = self.pool.get('email.server.tools')
207 subject = msg.get('subject')
208 body = msg.get('body')
209 msg_from = msg.get('from')
210 priority = msg.get('priority')
214 'email_from': msg_from,
215 'email_cc': msg.get('cc'),
219 if msg.get('priority', False):
220 vals['priority'] = priority
222 res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
226 res = self.create(cr, uid, vals, context)
227 attachents = msg.get('attachments', [])
228 for attactment in attachents or []:
231 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
232 'datas_fname': attactment,
233 'description': 'Mail attachment',
234 'res_model': self._name,
237 self.pool.get('ir.attachment').create(cr, uid, data_attach)
241 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
243 @param self: The object pointer
244 @param cr: the current row, from the database cursor,
245 @param uid: the current user’s ID for security checks,
246 @param ids: List of update mail’s IDs
248 if isinstance(ids, (str, int, long)):
251 if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
252 vals['priority'] = msg.get('priority')
255 'cost':'planned_cost',
256 'revenue': 'planned_revenue',
257 'probability':'probability'
260 for line in msg['body'].split('\n'):
262 res = tools.misc.command_re.match(line)
263 if res and maps.get(res.group(1).lower()):
264 key = maps.get(res.group(1).lower())
265 vls[key] = res.group(2).lower()
268 # Unfortunately the API is based on lists
269 # but we want to update the state based on the
270 # previous state, so we have to loop:
271 for case in self.browse(cr, uid, ids, context=context):
273 if case.state in CRM_CLAIM_PENDING_STATES:
274 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
275 res = self.write(cr, uid, [case.id], values, context=context)
278 def msg_send(self, cr, uid, id, *args, **argv):
281 @param self: The object pointer
282 @param cr: the current row, from the database cursor,
283 @param uid: the current user’s ID for security checks,
284 @param ids: List of email’s IDs
285 @param *args: Return Tuple Value
286 @param **args: Return Dictionary of Keyword Value
293 class crm_stage_claim(osv.osv):
295 def _get_type_value(self, cr, user, context):
296 list = super(crm_stage_claim, self)._get_type_value(cr, user, context)
297 list.append(('claim','Claim'))
300 _inherit = "crm.case.stage"
302 'type': fields.selection(_get_type_value, 'Type'),
309 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: