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
36 class crm_claim(crm.crm_case, osv.osv):
41 _description = "Claim"
42 _order = "priority,date desc"
43 _inherit = ['mailgate.thread']
45 'id': fields.integer('ID', readonly=True),
46 'name': fields.char('Claim Subject', size=128, required=True),
47 'action_next': fields.char('Next Action', size=200),
48 'date_action_next': fields.datetime('Next Action Date'),
49 'description': fields.text('Description'),
50 'resolution': fields.text('Resolution'),
51 'create_date': fields.datetime('Creation Date' , readonly=True),
52 'write_date': fields.datetime('Update Date' , readonly=True),
53 'date_deadline': fields.date('Deadline'),
54 'date_closed': fields.datetime('Closed', readonly=True),
55 'date': fields.datetime('Claim Date'),
56 'ref' : fields.reference('Reference', selection=crm._links_get, size=128),
57 'categ_id': fields.many2one('crm.case.categ', 'Category', \
58 domain="[('section_id','=',section_id),\
59 ('object_id.model', '=', 'crm.claim')]"),
60 'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
61 'type_action': fields.selection([('correction','Corrective Action'),('prevention','Preventive Action')], 'Action Type'),
62 'user_id': fields.many2one('res.users', 'Responsible'),
63 'user_fault': fields.char('Trouble Responsible', size=64),
64 'section_id': fields.many2one('crm.case.section', 'Sales Team', \
65 select=True, help="Sales team to which Case belongs to."\
66 "Define Responsible user and Email account for"\
68 'company_id': fields.many2one('res.company', 'Company'),
69 'partner_id': fields.many2one('res.partner', 'Partner'),
70 'partner_address_id': fields.many2one('res.partner.address', 'Partner Contact', \
71 # domain="[('partner_id','=',partner_id)]"
73 '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"),
74 'email_from': fields.char('Email', size=128, help="These people will receive email."),
75 'partner_phone': fields.char('Phone', size=32),
76 'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('section_ids','=',section_id)]"),
77 'cause': fields.text('Root Cause'),
78 'state': fields.selection(crm.AVAILABLE_STATES, 'State', size=16, readonly=True,
79 help='The state is set to \'Draft\', when a case is created.\
80 \nIf the case is in progress the state is set to \'Open\'.\
81 \nWhen the case is over, the state is set to \'Done\'.\
82 \nIf the case needs to be reviewed then the state is set to \'Pending\'.'),
83 'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
87 'user_id': crm.crm_case._get_default_user,
88 'partner_id': crm.crm_case._get_default_partner,
89 'partner_address_id': crm.crm_case._get_default_partner_address,
90 'email_from':crm.crm_case. _get_default_email,
91 'state': lambda *a: 'draft',
92 'section_id': crm.crm_case._get_section,
93 'date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
94 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
95 'priority': lambda *a: crm.AVAILABLE_PRIORITIES[2][0],
98 def onchange_partner_id(self, cr, uid, ids, part, email=False):
99 """This function returns value of partner address based on partner
100 @param part: Partner's id
101 @email: Partner's email ID
104 return {'value': {'partner_address_id': False,
106 'partner_phone': False,
107 'partner_mobile': False
109 addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
110 data = {'partner_address_id': addr['contact']}
111 data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
112 return {'value': data}
114 def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
115 """This function returns value of partner email based on Partner Address
116 @param add: Id of Partner's address
117 @email: Partner's email ID
120 return {'value': {'email_from': False}}
121 address = self.pool.get('res.partner.address').browse(cr, uid, add)
122 return {'value': {'email_from': address.email, 'partner_phone': address.phone, 'partner_mobile': address.mobile}}
124 def case_open(self, cr, uid, ids, *args):
125 for l in self.browse(cr, uid, ids):
126 # When coming from draft override date and stage otherwise just set state
127 if l.state == 'draft':
128 message = _("The claim '%s' has been opened.") % l.name
129 self.log(cr, uid, l.id, message)
130 value = {'date_open': time.strftime('%Y-%m-%d %H:%M:%S')}
131 self.write(cr, uid, [l.id], value)
132 stage_id = self.stage_find(cr, uid, l.section_id.id or False, [('sequence','>',0)])
134 self.stage_set(cr, uid, [l.id], stage_id)
135 res = super(crm_claim, self).case_open(cr, uid, ids, *args)
138 def message_new(self, cr, uid, msg, context=None):
139 """ Automatically calls when new email message arrives
141 mailgate_pool = self.pool.get('email.server.tools')
143 subject = msg.get('subject') or _("No Subject")
144 body = msg.get('body')
145 msg_from = msg.get('from')
146 priority = msg.get('priority')
150 'email_from': msg_from,
151 'email_cc': msg.get('cc'),
155 if msg.get('priority', False):
156 vals['priority'] = priority
158 res = mailgate_pool.get_partner(cr, uid, msg.get('from') or msg.get_unixfrom())
162 res = self.create(cr, uid, vals, context)
163 attachents = msg.get('attachments', [])
164 for attactment in attachents or []:
167 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
168 'datas_fname': attactment,
169 'description': 'Mail attachment',
170 'res_model': self._name,
173 self.pool.get('ir.attachment').create(cr, uid, data_attach)
177 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
179 @param ids: List of update mail’s IDs
181 if isinstance(ids, (str, int, long)):
184 if msg.get('priority') in dict(crm.AVAILABLE_PRIORITIES):
185 vals['priority'] = msg.get('priority')
188 'cost':'planned_cost',
189 'revenue': 'planned_revenue',
190 'probability':'probability'
193 for line in msg['body'].split('\n'):
195 res = tools.misc.command_re.match(line)
196 if res and maps.get(res.group(1).lower()):
197 key = maps.get(res.group(1).lower())
198 vls[key] = res.group(2).lower()
201 # Unfortunately the API is based on lists
202 # but we want to update the state based on the
203 # previous state, so we have to loop:
204 for case in self.browse(cr, uid, ids, context=context):
206 if case.state in CRM_CLAIM_PENDING_STATES:
207 values.update(state=crm.AVAILABLE_STATES[1][0]) #re-open
208 res = self.write(cr, uid, [case.id], values, context=context)
211 def msg_send(self, cr, uid, id, *args, **argv):
213 @param ids: List of email’s IDs
219 class res_partner(osv.osv):
220 _inherit = 'res.partner'
222 'claims_ids': fields.one2many('crm.claim', 'partner_id', 'Claims'),
226 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: