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
25 from 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 = []):
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_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",),
156 class questionnaire(osv.osv):
157 """ Questionnaire """
159 _name="crm_profiling.questionnaire"
160 _description= "Questionnaire"
162 def build_form(self, cr, uid, data, context):
164 @param self: The object pointer
165 @param cr: the current row, from the database cursor,
166 @param uid: the current user’s ID for security checks,
167 @param data: Get Data
168 @param context: A standard dictionary for contextual values """
172 from crm_profiling_question
173 where id in ( select question from profile_questionnaire_quest_rel where questionnaire = %s)"""
174 res = cr.execute(query, (data['form']['questionnaire_name'],))
175 result = cr.fetchall()
177 quest_form='''<?xml version="1.0"?>
178 <form string="%s">''' % _('Questionnaire')
179 for name, oid in result:
180 quest_form = quest_form + '<field name="quest_form%d"/><newline/>' % (oid,)
181 quest_fields['quest_form%d' % (oid,)] = {'string': name, 'type': 'many2one', \
182 'relation': 'crm_profiling.answer', 'domain': [('question_id','=',oid)] }
183 quest_form = quest_form + '''</form>'''
184 return quest_form, quest_fields
187 'name': fields.char("Questionnaire",size=128, required=True),
188 'description':fields.text("Description", required=True),
189 'questions_ids': fields.many2many('crm_profiling.question','profile_questionnaire_quest_rel',\
190 'questionnaire', 'question', "Questions"),
196 class answer(osv.osv):
197 _name="crm_profiling.answer"
198 _description="Answer"
200 "name": fields.char("Answer",size=128, required=True),
201 "question_id": fields.many2one('crm_profiling.question',"Question"),
206 class partner(osv.osv):
207 _inherit="res.partner"
209 "answers_ids": fields.many2many("crm_profiling.answer","partner_question_rel",\
210 "partner","answer","Answers"),
213 def _questionnaire_compute(self, cr, uid, data, context):
215 @param self: The object pointer
216 @param cr: the current row, from the database cursor,
217 @param uid: the current user’s ID for security checks,
218 @param data: Get Data
219 @param context: A standard dictionary for contextual values """
222 for x in data['form']:
223 if x.startswith("quest_form") and data['form'][x] != 0 :
224 temp.append(data['form'][x])
226 query = "select answer from partner_question_rel where partner=%s"
227 cr.execute(query, (data['id'],))
228 for x in cr.fetchall():
231 self.write(cr, uid, [data['id']], {'answers_ids':[[6, 0, temp]]}, context)
235 def write(self, cr, uid, ids, vals, context=None):
237 @param self: The object pointer
238 @param cr: the current row, from the database cursor,
239 @param uid: the current user’s ID for security checks,
240 @param ids: List of crm profiling’s IDs
241 @param context: A standard dictionary for contextual values """
245 if 'answers_ids' in vals:
246 vals['category_id']=[[6, 0, _recompute_categ(self, cr, uid, ids[0], vals['answers_ids'][0][2])]]
247 return super(partner, self).write(cr, uid, ids, vals, context=context)
252 class crm_segmentation(osv.osv):
253 """ CRM Segmentation """
255 _inherit="crm.segmentation"
257 "answer_yes": fields.many2many("crm_profiling.answer","profile_question_yes_rel",\
258 "profile","answer","Included Answers"),
259 "answer_no": fields.many2many("crm_profiling.answer","profile_question_no_rel",\
260 "profile","answer","Excluded Answers"),
261 'parent_id': fields.many2one('crm.segmentation', 'Parent Profile'),
262 'child_ids': fields.one2many('crm.segmentation', 'parent_id', 'Child Profiles'),
263 'profiling_active': fields.boolean('Use The Profiling Rules', help='Check\
264 this box if you want to use this tab as part of the \
265 segmentation rule. If not checked, the criteria beneath will be ignored')
269 (orm.orm.check_recursion, 'Error ! You can not create recursive profiles.', ['parent_id'])
272 def process_continue(self, cr, uid, ids, start=False):
274 @param self: The object pointer
275 @param cr: the current row, from the database cursor,
276 @param uid: the current user’s ID for security checks,
277 @param ids: List of crm segmentation’s IDs """
279 categs = self.read(cr,uid,ids,['categ_id','exclusif','partner_id', \
280 'sales_purchase_active', 'profiling_active'])
283 if categ['exclusif']:
284 cr.execute('delete from res_partner_category_rel where \
285 category_id=%s', (categ['categ_id'][0],))
289 cr.execute('select id from res_partner order by id ')
290 partners = [x[0] for x in cr.fetchall()]
292 if categ['sales_purchase_active']:
294 cr.execute('select id from crm_segmentation_line where segmentation_id=%s', (id,))
295 line_ids = [x[0] for x in cr.fetchall()]
298 if (not self.pool.get('crm.segmentation.line').test(cr, uid, line_ids, pid)):
299 to_remove_list.append(pid)
300 for pid in to_remove_list:
303 if categ['profiling_active']:
307 cr.execute('select distinct(answer) from partner_question_rel where partner=%s',(pid,))
308 answers_ids = [x[0] for x in cr.fetchall()]
310 if (not test_prof(cr, uid, id, pid, answers_ids)):
311 to_remove_list.append(pid)
312 for pid in to_remove_list:
315 for partner_id in partners:
316 cr.execute('insert into res_partner_category_rel (category_id,partner_id) values (%s,%s)', (categ['categ_id'][0],partner_id))
318 self.write(cr, uid, [id], {'state':'not running', 'partner_id':0})
323 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: