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 openerp.osv import fields,osv
23 from openerp.osv import orm
25 from openerp.tools.translate import _
27 def _get_answers(cr, uid, ids):
29 @param cr: the current row, from the database cursor,
30 @param uid: the current user’s ID for security checks,
31 @param ids: List of crm profiling’s IDs """
34 select distinct(answer)
35 from profile_question_yes_rel
36 where profile IN %s"""
38 cr.execute(query, (tuple(ids),))
39 ans_yes = [x[0] for x in cr.fetchall()]
42 select distinct(answer)
43 from profile_question_no_rel
44 where profile IN %s"""
46 cr.execute(query, (tuple(ids),))
47 ans_no = [x[0] for x in cr.fetchall()]
49 return [ans_yes, ans_no]
52 def _get_parents(cr, uid, ids):
54 @param cr: the current row, from the database cursor,
55 @param uid: the current user’s ID for security checks,
56 @param ids: List of crm profiling’s IDs
57 @return: Get parents's Id """
61 select distinct(parent_id)
63 where parent_id is not null
64 and id IN %s""",(tuple(ids),))
66 parent_ids = [x[0] for x in cr.fetchall()]
70 if x not in ids_to_check:
71 ids_to_check.append(x)
75 ids_to_check = _get_parents(cr, uid, ids_to_check)
80 def test_prof(cr, uid, seg_id, pid, answers_ids=None):
82 """ return True if the partner pid fetch the segmentation rule seg_id
83 @param cr: the current row, from the database cursor,
84 @param uid: the current user’s ID for security checks,
85 @param seg_id: Segmentaion's ID
86 @param pid: partner's ID
87 @param answers_ids: Answers's IDs
90 ids_to_check = _get_parents(cr, uid, [seg_id])
91 [yes_answers, no_answers] = _get_answers(cr, uid, ids_to_check)
93 for y_ans in yes_answers:
94 if y_ans not in answers_ids:
98 for ans in answers_ids:
107 def _recompute_categ(self, cr, uid, pid, answers_ids):
108 """ Recompute category
109 @param self: The object pointer
110 @param cr: the current row, from the database cursor,
111 @param uid: the current user’s ID for security checks,
112 @param pid: partner's ID
113 @param answers_ids: Answers's IDs
119 from res_partner_res_partner_category_rel r left join crm_segmentation s on (r.category_id = s.categ_id)
120 where r.partner_id = %s and (s.exclusif = false or s.exclusif is null)
122 for x in cr.fetchall():
127 from crm_segmentation
128 where profiling_active = true'''
130 query = query +''' and categ_id not in(%s)'''% ','.join([str(i) for i in ok ])
131 query = query + ''' order by id '''
134 segm_cat_ids = cr.fetchall()
136 for (segm_id, cat_id) in segm_cat_ids:
137 if test_prof(cr, uid, segm_id, pid, answers_ids):
142 class question(osv.osv):
145 _name="crm_profiling.question"
146 _description= "Question"
149 'name': fields.char("Question",size=128, required=True),
150 'answers_ids': fields.one2many("crm_profiling.answer","question_id","Avalaible Answers",),
155 class questionnaire(osv.osv):
156 """ Questionnaire """
158 _name="crm_profiling.questionnaire"
159 _description= "Questionnaire"
162 'name': fields.char("Questionnaire",size=128, required=True),
163 'description':fields.text("Description", required=True),
164 'questions_ids': fields.many2many('crm_profiling.question','profile_questionnaire_quest_rel',\
165 'questionnaire', 'question', "Questions"),
170 class answer(osv.osv):
171 _name="crm_profiling.answer"
172 _description="Answer"
174 "name": fields.char("Answer",size=128, required=True),
175 "question_id": fields.many2one('crm_profiling.question',"Question"),
179 class partner(osv.osv):
180 _inherit="res.partner"
182 "answers_ids": fields.many2many("crm_profiling.answer","partner_question_rel",\
183 "partner","answer","Answers"),
186 def _questionnaire_compute(self, cr, uid, answers, context=None):
188 @param self: The object pointer
189 @param cr: the current row, from the database cursor,
190 @param uid: the current user’s ID for security checks,
191 @param data: Get Data
192 @param context: A standard dictionary for contextual values """
193 partner_id = context.get('active_id')
194 query = "select answer from partner_question_rel where partner=%s"
195 cr.execute(query, (partner_id,))
196 for x in cr.fetchall():
198 self.write(cr, uid, [partner_id], {'answers_ids': [[6, 0, answers]]}, context=context)
202 def write(self, cr, uid, ids, vals, context=None):
204 @param self: The object pointer
205 @param cr: the current row, from the database cursor,
206 @param uid: the current user’s ID for security checks,
207 @param ids: List of crm profiling’s IDs
208 @param context: A standard dictionary for contextual values """
210 if 'answers_ids' in vals:
211 vals['category_id']=[[6, 0, _recompute_categ(self, cr, uid, ids[0], vals['answers_ids'][0][2])]]
212 return super(partner, self).write(cr, uid, ids, vals, context=context)
216 class crm_segmentation(osv.osv):
217 """ CRM Segmentation """
219 _inherit="crm.segmentation"
221 "answer_yes": fields.many2many("crm_profiling.answer","profile_question_yes_rel",\
222 "profile","answer","Included Answers"),
223 "answer_no": fields.many2many("crm_profiling.answer","profile_question_no_rel",\
224 "profile","answer","Excluded Answers"),
225 'parent_id': fields.many2one('crm.segmentation', 'Parent Profile'),
226 'child_ids': fields.one2many('crm.segmentation', 'parent_id', 'Child Profiles'),
227 'profiling_active': fields.boolean('Use The Profiling Rules', help='Check\
228 this box if you want to use this tab as part of the \
229 segmentation rule. If not checked, the criteria beneath will be ignored')
233 (osv.osv._check_recursion, 'Error ! You cannot create recursive profiles.', ['parent_id'])
236 def process_continue(self, cr, uid, ids, start=False):
238 @param self: The object pointer
239 @param cr: the current row, from the database cursor,
240 @param uid: the current user’s ID for security checks,
241 @param ids: List of crm segmentation’s IDs """
243 partner_obj = self.pool.get('res.partner')
244 categs = self.read(cr,uid,ids,['categ_id','exclusif','partner_id', \
245 'sales_purchase_active', 'profiling_active'])
248 if categ['exclusif']:
249 cr.execute('delete from res_partner_res_partner_category_rel where \
250 category_id=%s', (categ['categ_id'][0],))
254 cr.execute('select id from res_partner order by id ')
255 partners = [x[0] for x in cr.fetchall()]
257 if categ['sales_purchase_active']:
259 cr.execute('select id from crm_segmentation_line where segmentation_id=%s', (id,))
260 line_ids = [x[0] for x in cr.fetchall()]
263 if (not self.pool.get('crm.segmentation.line').test(cr, uid, line_ids, pid)):
264 to_remove_list.append(pid)
265 for pid in to_remove_list:
268 if categ['profiling_active']:
272 cr.execute('select distinct(answer) from partner_question_rel where partner=%s',(pid,))
273 answers_ids = [x[0] for x in cr.fetchall()]
275 if (not test_prof(cr, uid, id, pid, answers_ids)):
276 to_remove_list.append(pid)
277 for pid in to_remove_list:
280 for partner in partner_obj.browse(cr, uid, partners):
281 category_ids = [categ_id.id for categ_id in partner.category_id]
282 if categ['categ_id'][0] not in category_ids:
283 cr.execute('insert into res_partner_res_partner_category_rel (category_id,partner_id) values (%s,%s)', (categ['categ_id'][0],partner.id))
285 self.write(cr, uid, [id], {'state':'not running', 'partner_id':0})
289 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: