~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to crm_profiling/wizard/open_partner.py

  • Committer: qdp
  • Date: 2007-11-29 09:14:43 UTC
  • Revision ID: qdp-06c49df709b1b84bb442fa5eec5113d58f78f3e6
- merging of the elder segmentation module with the crm_segmentation module because of functionalities overlapping
- adding of the questionaire concept
- automatic updates of partner categories when modification of its answers 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import pooler
2
 
import wizard
3
 
 
4
 
def _open_partner(self, cr, uid, data, context):
5
 
 
6
 
        def _get_answers(self, cr, uid, ids):
7
 
                query = """
8
 
                select distinct(answer)
9
 
                from profile_question_yes_rel
10
 
                where profile in (%s)"""% ','.join([str(i) for i in ids ])
11
 
 
12
 
                cr.execute(query)
13
 
                ans_yes = [x[0] for x in cr.fetchall()]
14
 
 
15
 
                query = """
16
 
                select distinct(answer)
17
 
                from profile_question_no_rel
18
 
                where profile in (%s)"""% ','.join([str(i) for i in ids ])
19
 
 
20
 
                cr.execute(query)
21
 
                ans_no = [x[0] for x in cr.fetchall()]
22
 
 
23
 
                return [ans_yes, ans_no]
24
 
 
25
 
        def _get_parents(self, cr, uid, ids):
26
 
                ids_to_check = ids
27
 
                cr.execute("""
28
 
                 select distinct(parent_id)
29
 
                 from crm_profiling_profile
30
 
                 where parent_id is not null
31
 
                 and id in (%s)""" % ','.join([str(i) for i in ids ]))
32
 
 
33
 
                parent_ids = [x[0] for x in cr.fetchall()]
34
 
 
35
 
                trigger = False
36
 
                for x in parent_ids:
37
 
                        if x not in ids_to_check:
38
 
                                ids_to_check.append(x)
39
 
                                trigger = True
40
 
 
41
 
                if trigger:
42
 
                        ids_to_check = _get_parents(self, cr, uid, ids_to_check)
43
 
 
44
 
                return ids_to_check
45
 
 
46
 
        if (data['id'] in data['ids'])|(data['ids'] == []):
47
 
                ids_to_check = _get_parents(self, cr, uid, data['ids']) 
48
 
        else:           
49
 
                ids_to_check = _get_parents(self, cr, uid, [data['id']])
50
 
 
51
 
        [yes_answers, no_answers] = _get_answers(self, cr, uid, ids_to_check)
52
 
 
53
 
        query = "select partner from partner_question_rel "
54
 
        query_end =     "group by partner"
55
 
 
56
 
        if yes_answers != []:
57
 
                query = query + """
58
 
                where answer in (%s) """        % (','.join([str(a) for a in yes_answers]))     
59
 
 
60
 
                #rebuild the end of the query
61
 
                query_end =     """
62
 
                group by partner
63
 
                having count(*) >= %s """ % len(yes_answers)
64
 
 
65
 
        if no_answers != []:
66
 
                if yes_answers != []:
67
 
                        query = query + "and "
68
 
                else:
69
 
                        query = query + "where "
70
 
 
71
 
                query = query + """
72
 
                partner not in (
73
 
                        select partner from partner_question_rel 
74
 
                        where answer in (%s)) """ % ( ','.join([str(a) for a in no_answers]))
75
 
 
76
 
        query = query + query_end
77
 
        cr.execute(query)
78
 
        
79
 
        action= {
80
 
                'name': 'Matching partners',
81
 
                'view_type': 'form',
82
 
                'view_mode': 'tree,form',
83
 
                'res_model': 'res.partner',
84
 
                'type': 'ir.actions.act_window'
85
 
                }
86
 
 
87
 
        (res,) =cr.fetchall(),
88
 
        if len(res)==1:
89
 
                action['domain']= "[('id','=',(%s))]" % str(res[0][0])
90
 
        else:
91
 
                action['domain']= "[('id','in',(%s))]" % ','.join([str(x[0]) for x in res])
92
 
                
93
 
        return action
94
 
 
95
 
class open_partner(wizard.interface):
96
 
        states = {
97
 
                'init': {
98
 
                        'actions': [],
99
 
                        'result': {'type': 'action', 'action': _open_partner, 'state':'end'}
100
 
                }
101
 
        }
102
 
 
103
 
open_partner('open_partner')