1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
22 from osv import fields,osv
25 from tools.translate import _
27 def _get_answers(cr, uid, ids):
29 select distinct(answer)
30 from profile_question_yes_rel
31 where profile in (%s)"""% ','.join([str(i) for i in ids ])
34 ans_yes = [x[0] for x in cr.fetchall()]
37 select distinct(answer)
38 from profile_question_no_rel
39 where profile in (%s)"""% ','.join([str(i) for i in ids ])
42 ans_no = [x[0] for x in cr.fetchall()]
44 return [ans_yes, ans_no]
47 def _get_parents(cr, uid, ids):
50 select distinct(parent_id)
52 where parent_id is not null
53 and id in (%s)""" % ','.join([str(i) for i in ids ]))
55 parent_ids = [x[0] for x in cr.fetchall()]
59 if x not in ids_to_check:
60 ids_to_check.append(x)
64 ids_to_check = _get_parents(cr, uid, ids_to_check)
69 def test_prof(cr, uid, seg_id, pid, answers_ids = []):
70 #return True if the partner pid fetch the segmentation rule seg_id
71 ids_to_check = _get_parents(cr, uid, [seg_id])
72 [yes_answers, no_answers] = _get_answers(cr, uid, ids_to_check)
74 for y_ans in yes_answers:
75 if y_ans not in answers_ids:
79 for ans in answers_ids:
88 def _recompute_categ(self, cr, uid, pid, answers_ids):
92 from res_partner_category_rel r left join crm_segmentation s on (r.category_id = s.categ_id)
93 where r.partner_id = %s and (s.exclusif = false or s.exclusif is null)
95 for x in cr.fetchall():
100 from crm_segmentation
101 where profiling_active = true'''
103 query = query +''' and categ_id not in(%s)'''% ','.join([str(i) for i in ok ])
104 query = query + ''' order by id '''
107 segm_cat_ids = cr.fetchall()
109 for (segm_id, cat_id) in segm_cat_ids:
110 if test_prof(cr, uid, segm_id, pid, answers_ids):
115 class question(osv.osv):
116 _name="crm_profiling.question"
117 _description= "Question"
119 'name': fields.char("Question",size=128, required=True),
120 'answers_ids': fields.one2many("crm_profiling.answer","question_id","Avalaible answers",),
126 class questionnaire(osv.osv):
127 _name="crm_profiling.questionnaire"
128 _description= "Questionnaire"
130 def build_form(self, cr, uid, data, context):
133 from crm_profiling_question
134 where id in ( select question from profile_questionnaire_quest_rel where questionnaire = %s)"""
135 res = cr.execute(query, (data['form']['questionnaire_name'],))
136 result = cr.fetchall()
138 quest_form='''<?xml version="1.0"?>
139 <form string="%s">''' % _('Questionnaire')
140 for name, oid in result:
141 quest_form = quest_form + '<field name="quest_form%d"/><newline/>' % (oid,)
142 quest_fields['quest_form%d' % (oid,)] = {'string': name, 'type': 'many2one', 'relation': 'crm_profiling.answer', 'domain': [('question_id','=',oid)] }
143 quest_form = quest_form + '''</form>'''
144 return quest_form, quest_fields
147 'name': fields.char("Questionnaire",size=128, required=True),
148 'description':fields.text("Description", required=True),
149 'questions_ids': fields.many2many('crm_profiling.question','profile_questionnaire_quest_rel','questionnaire', 'question', "Questions"),
155 class answer(osv.osv):
156 _name="crm_profiling.answer"
157 _description="Answer"
159 "name": fields.char("Answer",size=128, required=True),
160 "question_id": fields.many2one('crm_profiling.question',"Question"),
165 class partner(osv.osv):
166 _inherit="res.partner"
168 "answers_ids": fields.many2many("crm_profiling.answer","partner_question_rel","partner","answer","Answers"),
171 def _questionnaire_compute(self, cr, uid, data, context):
173 for x in data['form']:
174 if x.startswith("quest_form") and data['form'][x] != 0 :
175 temp.append(data['form'][x])
177 query = "select answer from partner_question_rel where partner=%s"
178 cr.execute(query, (data['id'],))
179 for x in cr.fetchall():
182 self.write(cr, uid, [data['id']],{'answers_ids':[[6,0,temp]]}, context)
186 def write(self, cr, uid, ids, vals, context=None):
189 if 'answers_ids' in vals:
190 vals['category_id']=[[6, 0, _recompute_categ(self, cr, uid, ids[0], vals['answers_ids'][0][2])]]
191 return super(partner, self).write(cr, uid, ids, vals, context=context)
196 class crm_segmentation(osv.osv):
197 _inherit="crm.segmentation"
199 "answer_yes": fields.many2many("crm_profiling.answer","profile_question_yes_rel","profile","answer","Included Answers"),
200 "answer_no": fields.many2many("crm_profiling.answer","profile_question_no_rel","profile","answer","Excluded Answers"),
201 'parent_id': fields.many2one('crm.segmentation', 'Parent Profile'),
202 'child_ids': fields.one2many('crm.segmentation', 'parent_id', 'Child Profiles'),
203 'profiling_active': fields.boolean('Use The Profiling Rules', help='Check this box if you want to use this tab as part of the segmentation rule. If not checked, the criteria beneath will be ignored')
206 (orm.orm.check_recursion, 'Error ! You can not create recursive profiles.', ['parent_id'])
209 def process_continue(self, cr, uid, ids, start=False):
210 categs = self.read(cr,uid,ids,['categ_id','exclusif','partner_id', 'sales_purchase_active', 'profiling_active'])
213 if categ['exclusif']:
214 cr.execute('delete from res_partner_category_rel where category_id=%s', (categ['categ_id'][0],))
218 cr.execute('select id from res_partner order by id ')
219 partners = [x[0] for x in cr.fetchall()]
221 if categ['sales_purchase_active']:
223 cr.execute('select id from crm_segmentation_line where segmentation_id=%s', (id,))
224 line_ids = [x[0] for x in cr.fetchall()]
227 if (not self.pool.get('crm.segmentation.line').test(cr, uid, line_ids, pid)):
228 to_remove_list.append(pid)
229 for pid in to_remove_list:
232 if categ['profiling_active']:
236 cr.execute('select distinct(answer) from partner_question_rel where partner=%s',(pid,))
237 answers_ids = [x[0] for x in cr.fetchall()]
239 if (not test_prof(cr, uid, id, pid, answers_ids)):
240 to_remove_list.append(pid)
241 for pid in to_remove_list:
244 for partner_id in partners:
245 cr.execute('insert into res_partner_category_rel (category_id,partner_id) values (%s,%s)', (categ['categ_id'][0],partner_id))
248 self.write(cr, uid, [id], {'state':'not running', 'partner_id':0})
253 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: