2009-10-13 05:58:37 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2008-11-03 19:18:56 +00:00
|
|
|
|
##############################################################################
|
2009-11-26 13:07:24 +00:00
|
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# GNU Affero General Public License for more details.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-26 13:07:24 +00:00
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-11-03 19:18:56 +00:00
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
2009-10-14 11:15:34 +00:00
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.osv import fields,osv
|
2012-12-17 15:46:28 +00:00
|
|
|
|
from openerp.osv import orm
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.tools.translate import _
|
2008-12-08 17:08:40 +00:00
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
def _get_answers(cr, uid, ids):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"""
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of crm profiling’s IDs """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
query = """
|
|
|
|
|
select distinct(answer)
|
|
|
|
|
from profile_question_yes_rel
|
2010-06-16 11:51:39 +00:00
|
|
|
|
where profile IN %s"""
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
2010-06-18 08:28:36 +00:00
|
|
|
|
cr.execute(query, (tuple(ids),))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
ans_yes = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
query = """
|
|
|
|
|
select distinct(answer)
|
|
|
|
|
from profile_question_no_rel
|
2010-06-16 11:51:39 +00:00
|
|
|
|
where profile IN %s"""
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
2010-06-16 11:51:39 +00:00
|
|
|
|
cr.execute(query, (tuple(ids),))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
ans_no = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
return [ans_yes, ans_no]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_parents(cr, uid, ids):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"""
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of crm profiling’s IDs
|
|
|
|
|
@return: Get parents's Id """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
ids_to_check = ids
|
|
|
|
|
cr.execute("""
|
|
|
|
|
select distinct(parent_id)
|
|
|
|
|
from crm_segmentation
|
|
|
|
|
where parent_id is not null
|
2010-06-16 11:51:39 +00:00
|
|
|
|
and id IN %s""",(tuple(ids),))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
|
|
parent_ids = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
trigger = False
|
|
|
|
|
for x in parent_ids:
|
|
|
|
|
if x not in ids_to_check:
|
|
|
|
|
ids_to_check.append(x)
|
|
|
|
|
trigger = True
|
|
|
|
|
|
|
|
|
|
if trigger:
|
|
|
|
|
ids_to_check = _get_parents(cr, uid, ids_to_check)
|
|
|
|
|
|
|
|
|
|
return ids_to_check
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 18:40:03 +00:00
|
|
|
|
def test_prof(cr, uid, seg_id, pid, answers_ids=None):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
|
|
|
|
|
""" return True if the partner pid fetch the segmentation rule seg_id
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param seg_id: Segmentaion's ID
|
|
|
|
|
@param pid: partner's ID
|
|
|
|
|
@param answers_ids: Answers's IDs
|
|
|
|
|
"""
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
ids_to_check = _get_parents(cr, uid, [seg_id])
|
|
|
|
|
[yes_answers, no_answers] = _get_answers(cr, uid, ids_to_check)
|
|
|
|
|
temp = True
|
|
|
|
|
for y_ans in yes_answers:
|
|
|
|
|
if y_ans not in answers_ids:
|
|
|
|
|
temp = False
|
|
|
|
|
break
|
|
|
|
|
if temp:
|
|
|
|
|
for ans in answers_ids:
|
|
|
|
|
if ans in no_answers:
|
|
|
|
|
temp = False
|
|
|
|
|
break
|
|
|
|
|
if temp:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _recompute_categ(self, cr, uid, pid, answers_ids):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
""" Recompute category
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param pid: partner's ID
|
|
|
|
|
@param answers_ids: Answers's IDs
|
|
|
|
|
"""
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
ok = []
|
|
|
|
|
cr.execute('''
|
2009-11-26 13:07:24 +00:00
|
|
|
|
select r.category_id
|
2012-06-19 15:06:47 +00:00
|
|
|
|
from res_partner_res_partner_category_rel r left join crm_segmentation s on (r.category_id = s.categ_id)
|
2008-12-10 14:29:55 +00:00
|
|
|
|
where r.partner_id = %s and (s.exclusif = false or s.exclusif is null)
|
|
|
|
|
''', (pid,))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
for x in cr.fetchall():
|
|
|
|
|
ok.append(x[0])
|
|
|
|
|
|
|
|
|
|
query = '''
|
2009-11-26 13:07:24 +00:00
|
|
|
|
select id, categ_id
|
|
|
|
|
from crm_segmentation
|
|
|
|
|
where profiling_active = true'''
|
2008-08-24 14:45:43 +00:00
|
|
|
|
if ok != []:
|
|
|
|
|
query = query +''' and categ_id not in(%s)'''% ','.join([str(i) for i in ok ])
|
|
|
|
|
query = query + ''' order by id '''
|
|
|
|
|
|
|
|
|
|
cr.execute(query)
|
|
|
|
|
segm_cat_ids = cr.fetchall()
|
|
|
|
|
|
|
|
|
|
for (segm_id, cat_id) in segm_cat_ids:
|
|
|
|
|
if test_prof(cr, uid, segm_id, pid, answers_ids):
|
|
|
|
|
ok.append(cat_id)
|
|
|
|
|
return ok
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class question(osv.osv):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
""" Question """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
_name="crm_profiling.question"
|
|
|
|
|
_description= "Question"
|
2010-03-26 13:27:16 +00:00
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
_columns={
|
2014-05-21 09:52:05 +00:00
|
|
|
|
'name': fields.char("Question", required=True),
|
2014-07-06 14:44:26 +00:00
|
|
|
|
'answers_ids': fields.one2many("crm_profiling.answer", "question_id", "Available Answers", copy=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class questionnaire(osv.osv):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
""" Questionnaire """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
_name="crm_profiling.questionnaire"
|
|
|
|
|
_description= "Questionnaire"
|
|
|
|
|
|
2008-12-11 23:49:48 +00:00
|
|
|
|
_columns = {
|
2014-05-21 09:52:05 +00:00
|
|
|
|
'name': fields.char("Questionnaire", required=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
|
'description':fields.text("Description", required=True),
|
2010-03-26 13:27:16 +00:00
|
|
|
|
'questions_ids': fields.many2many('crm_profiling.question','profile_questionnaire_quest_rel',\
|
|
|
|
|
'questionnaire', 'question', "Questions"),
|
2008-12-11 23:49:48 +00:00
|
|
|
|
}
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class answer(osv.osv):
|
|
|
|
|
_name="crm_profiling.answer"
|
|
|
|
|
_description="Answer"
|
|
|
|
|
_columns={
|
2014-05-21 09:52:05 +00:00
|
|
|
|
"name": fields.char("Answer", required=True),
|
2008-08-24 14:45:43 +00:00
|
|
|
|
"question_id": fields.many2one('crm_profiling.question',"Question"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class partner(osv.osv):
|
|
|
|
|
_inherit="res.partner"
|
|
|
|
|
_columns={
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"answers_ids": fields.many2many("crm_profiling.answer","partner_question_rel",\
|
|
|
|
|
"partner","answer","Answers"),
|
2008-08-24 14:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 13:30:12 +00:00
|
|
|
|
def _questionnaire_compute(self, cr, uid, answers, context=None):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"""
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param data: Get Data
|
|
|
|
|
@param context: A standard dictionary for contextual values """
|
2011-11-08 13:30:12 +00:00
|
|
|
|
partner_id = context.get('active_id')
|
2008-12-10 14:29:55 +00:00
|
|
|
|
query = "select answer from partner_question_rel where partner=%s"
|
2011-11-08 13:30:12 +00:00
|
|
|
|
cr.execute(query, (partner_id,))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
for x in cr.fetchall():
|
2011-11-08 13:30:12 +00:00
|
|
|
|
answers.append(x[0])
|
|
|
|
|
self.write(cr, uid, [partner_id], {'answers_ids': [[6, 0, answers]]}, context=context)
|
2008-08-24 14:45:43 +00:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"""
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of crm profiling’s IDs
|
|
|
|
|
@param context: A standard dictionary for contextual values """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
if 'answers_ids' in vals:
|
|
|
|
|
vals['category_id']=[[6, 0, _recompute_categ(self, cr, uid, ids[0], vals['answers_ids'][0][2])]]
|
2014-07-06 14:44:26 +00:00
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
return super(partner, self).write(cr, uid, ids, vals, context=context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class crm_segmentation(osv.osv):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
""" CRM Segmentation """
|
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
_inherit="crm.segmentation"
|
|
|
|
|
_columns={
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"answer_yes": fields.many2many("crm_profiling.answer","profile_question_yes_rel",\
|
|
|
|
|
"profile","answer","Included Answers"),
|
|
|
|
|
"answer_no": fields.many2many("crm_profiling.answer","profile_question_no_rel",\
|
|
|
|
|
"profile","answer","Excluded Answers"),
|
2008-08-24 14:45:43 +00:00
|
|
|
|
'parent_id': fields.many2one('crm.segmentation', 'Parent Profile'),
|
2009-01-27 11:15:46 +00:00
|
|
|
|
'child_ids': fields.one2many('crm.segmentation', 'parent_id', 'Child Profiles'),
|
2010-03-26 13:27:16 +00:00
|
|
|
|
'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')
|
2008-08-24 14:45:43 +00:00
|
|
|
|
}
|
2010-03-26 13:27:16 +00:00
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
_constraints = [
|
2012-07-12 05:50:38 +00:00
|
|
|
|
(osv.osv._check_recursion, 'Error ! You cannot create recursive profiles.', ['parent_id'])
|
2008-08-24 14:45:43 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def process_continue(self, cr, uid, ids, start=False):
|
2010-03-26 13:27:16 +00:00
|
|
|
|
"""
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param ids: List of crm segmentation’s IDs """
|
|
|
|
|
|
2012-11-01 08:39:08 +00:00
|
|
|
|
partner_obj = self.pool.get('res.partner')
|
2010-03-26 13:27:16 +00:00
|
|
|
|
categs = self.read(cr,uid,ids,['categ_id','exclusif','partner_id', \
|
|
|
|
|
'sales_purchase_active', 'profiling_active'])
|
2008-08-24 14:45:43 +00:00
|
|
|
|
for categ in categs:
|
|
|
|
|
if start:
|
|
|
|
|
if categ['exclusif']:
|
2012-06-19 15:06:47 +00:00
|
|
|
|
cr.execute('delete from res_partner_res_partner_category_rel where \
|
2010-03-26 13:27:16 +00:00
|
|
|
|
category_id=%s', (categ['categ_id'][0],))
|
2014-07-06 14:44:26 +00:00
|
|
|
|
partner_obj.invalidate_cache(cr, uid, ['category_id'])
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
2009-11-26 13:07:24 +00:00
|
|
|
|
id = categ['id']
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
|
|
cr.execute('select id from res_partner order by id ')
|
|
|
|
|
partners = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
if categ['sales_purchase_active']:
|
|
|
|
|
to_remove_list=[]
|
2008-12-10 14:29:55 +00:00
|
|
|
|
cr.execute('select id from crm_segmentation_line where segmentation_id=%s', (id,))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
line_ids = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
for pid in partners:
|
|
|
|
|
if (not self.pool.get('crm.segmentation.line').test(cr, uid, line_ids, pid)):
|
|
|
|
|
to_remove_list.append(pid)
|
|
|
|
|
for pid in to_remove_list:
|
|
|
|
|
partners.remove(pid)
|
|
|
|
|
|
|
|
|
|
if categ['profiling_active']:
|
2010-03-26 13:27:16 +00:00
|
|
|
|
to_remove_list = []
|
2008-08-24 14:45:43 +00:00
|
|
|
|
for pid in partners:
|
|
|
|
|
|
2008-12-10 14:29:55 +00:00
|
|
|
|
cr.execute('select distinct(answer) from partner_question_rel where partner=%s',(pid,))
|
2008-08-24 14:45:43 +00:00
|
|
|
|
answers_ids = [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
|
|
if (not test_prof(cr, uid, id, pid, answers_ids)):
|
|
|
|
|
to_remove_list.append(pid)
|
|
|
|
|
for pid in to_remove_list:
|
|
|
|
|
partners.remove(pid)
|
|
|
|
|
|
2012-11-01 08:39:08 +00:00
|
|
|
|
for partner in partner_obj.browse(cr, uid, partners):
|
|
|
|
|
category_ids = [categ_id.id for categ_id in partner.category_id]
|
|
|
|
|
if categ['categ_id'][0] not in category_ids:
|
|
|
|
|
cr.execute('insert into res_partner_res_partner_category_rel (category_id,partner_id) values (%s,%s)', (categ['categ_id'][0],partner.id))
|
2014-07-06 14:44:26 +00:00
|
|
|
|
partner_obj.invalidate_cache(cr, uid, ['category_id'], [partner.id])
|
2008-08-24 14:45:43 +00:00
|
|
|
|
|
|
|
|
|
self.write(cr, uid, [id], {'state':'not running', 'partner_id':0})
|
|
|
|
|
return True
|
|
|
|
|
|
2010-03-26 13:27:16 +00:00
|
|
|
|
|
2008-08-24 14:45:43 +00:00
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|
|