2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-03-22 10:40:26 +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-06-16 11:00:21 +00:00
#
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.
2006-12-07 13:41:40 +00:00
#
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.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-22 10:40:26 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv , orm
2006-12-07 13:41:40 +00:00
class crm_segmentation ( osv . osv ) :
2008-07-22 15:11:28 +00:00
'''
A segmentation is a tool to automatically assign categories on partners .
These assignations are based on criterions .
'''
_name = " crm.segmentation "
_description = " Partner Segmentation "
2010-03-22 10:40:26 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True , help = ' The name of the segmentation. ' ) ,
' description ' : fields . text ( ' Description ' ) ,
2010-03-22 10:40:26 +00:00
' categ_id ' : fields . many2one ( ' res.partner.category ' , ' Partner Category ' , \
required = True , help = ' The partner category that will be \
2010-05-12 13:16:01 +00:00
added to partners that match the segmentation criterions after computation . ' ),
' exclusif ' : fields . boolean ( ' Exclusive ' , help = ' Check if the category is limited to partners that match the segmentation criterions. \
\nIf checked , remove the category from partners that doesn \' t match segmentation criterions ' ) ,
2010-03-22 10:40:26 +00:00
' state ' : fields . selection ( [ ( ' not running ' , ' Not Running ' ) , \
( ' running ' , ' Running ' ) ] , ' Execution Status ' , readonly = True ) ,
2008-07-22 15:11:28 +00:00
' partner_id ' : fields . integer ( ' Max Partner ID processed ' ) ,
2010-03-22 10:40:26 +00:00
' segmentation_line ' : fields . one2many ( ' crm.segmentation.line ' , \
' segmentation_id ' , ' Criteria ' , required = True ) ,
2010-05-12 13:16:01 +00:00
' sales_purchase_active ' : fields . boolean ( ' Use The Sales Purchase Rules ' , help = ' Check if you want to use this tab as part of the segmentation rule. If not checked, the criteria beneath will be ignored ' )
2008-07-22 15:11:28 +00:00
}
_defaults = {
' partner_id ' : lambda * a : 0 ,
' state ' : lambda * a : ' not running ' ,
}
def process_continue ( self , cr , uid , ids , start = False ) :
2010-03-22 10:40:26 +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 Process continue ’ s IDs """
2012-11-01 08:39:08 +00:00
partner_obj = self . pool . get ( ' res.partner ' )
2010-03-22 10:40:26 +00:00
categs = self . read ( cr , uid , ids , [ ' categ_id ' , ' exclusif ' , ' partner_id ' , \
' sales_purchase_active ' , ' profiling_active ' ] )
2008-07-22 15:11:28 +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 \
2010-03-22 10:40:26 +00:00
where category_id = % s ' , (categ[ ' categ_id ' ][0],))
2008-07-22 15:11:28 +00:00
id = categ [ ' id ' ]
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-07-22 15:11:28 +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 )
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))
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , [ id ] , { ' state ' : ' not running ' , ' partner_id ' : 0 } )
return True
def process_stop ( self , cr , uid , ids , * args ) :
2010-03-22 10:40:26 +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 Process stop ’ s IDs """
2008-07-22 15:11:28 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' not running ' , ' partner_id ' : 0 } )
def process_start ( self , cr , uid , ids , * args ) :
2010-03-22 10:40:26 +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 Process start ’ s IDs """
2008-07-22 15:11:28 +00:00
self . write ( cr , uid , ids , { ' state ' : ' running ' , ' partner_id ' : 0 } )
return self . process_continue ( cr , uid , ids , start = True )
2006-12-07 13:41:40 +00:00
class crm_segmentation_line ( osv . osv ) :
2010-03-22 10:40:26 +00:00
""" Segmentation line """
2008-07-22 15:11:28 +00:00
_name = " crm.segmentation.line "
_description = " Segmentation line "
2010-03-22 10:40:26 +00:00
2008-07-22 15:11:28 +00:00
_columns = {
' name ' : fields . char ( ' Rule Name ' , size = 64 , required = True ) ,
' segmentation_id ' : fields . many2one ( ' crm.segmentation ' , ' Segmentation ' ) ,
2012-06-04 17:10:42 +00:00
' expr_name ' : fields . selection ( [ ( ' sale ' , ' Sale Amount ' ) ,
2010-03-22 10:40:26 +00:00
( ' purchase ' , ' Purchase Amount ' ) ] , ' Control Variable ' , size = 64 , required = True ) ,
2008-07-22 15:11:28 +00:00
' expr_operator ' : fields . selection ( [ ( ' < ' , ' < ' ) , ( ' = ' , ' = ' ) , ( ' > ' , ' > ' ) ] , ' Operator ' , required = True ) ,
' expr_value ' : fields . float ( ' Value ' , required = True ) ,
2010-03-22 10:40:26 +00:00
' operator ' : fields . selection ( [ ( ' and ' , ' Mandatory Expression ' ) , \
( ' or ' , ' Optional Expression ' ) ] , ' Mandatory / Optional ' , required = True ) ,
2008-07-22 15:11:28 +00:00
}
_defaults = {
' expr_name ' : lambda * a : ' sale ' ,
' expr_operator ' : lambda * a : ' > ' ,
' operator ' : lambda * a : ' and '
}
def test ( self , cr , uid , ids , partner_id ) :
2010-03-22 10:40:26 +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 Test ’ s IDs """
2008-07-22 15:11:28 +00:00
expression = { ' < ' : lambda x , y : x < y , ' = ' : lambda x , y : x == y , ' > ' : lambda x , y : x > y }
ok = False
lst = self . read ( cr , uid , ids )
for l in lst :
cr . execute ( ' select * from ir_module_module where name= %s and state= %s ' , ( ' account ' , ' installed ' ) )
if cr . fetchone ( ) :
2012-06-04 17:10:42 +00:00
if l [ ' expr_name ' ] == ' sale ' :
2008-07-22 15:11:28 +00:00
cr . execute ( ' SELECT SUM(l.price_unit * l.quantity) ' \
' FROM account_invoice_line l, account_invoice i ' \
' WHERE (l.invoice_id = i.id) ' \
2008-12-10 14:29:55 +00:00
' AND i.partner_id = %s ' \
2008-07-22 15:11:28 +00:00
' AND i.type = \' out_invoice \' ' ,
( partner_id , ) )
value = cr . fetchone ( ) [ 0 ] or 0.0
cr . execute ( ' SELECT SUM(l.price_unit * l.quantity) ' \
' FROM account_invoice_line l, account_invoice i ' \
' WHERE (l.invoice_id = i.id) ' \
2008-12-10 14:29:55 +00:00
' AND i.partner_id = %s ' \
2008-07-22 15:11:28 +00:00
' AND i.type = \' out_refund \' ' ,
( partner_id , ) )
value - = cr . fetchone ( ) [ 0 ] or 0.0
elif l [ ' expr_name ' ] == ' purchase ' :
cr . execute ( ' SELECT SUM(l.price_unit * l.quantity) ' \
' FROM account_invoice_line l, account_invoice i ' \
' WHERE (l.invoice_id = i.id) ' \
2008-12-10 14:29:55 +00:00
' AND i.partner_id = %s ' \
2008-07-22 15:11:28 +00:00
' AND i.type = \' in_invoice \' ' ,
( partner_id , ) )
value = cr . fetchone ( ) [ 0 ] or 0.0
cr . execute ( ' SELECT SUM(l.price_unit * l.quantity) ' \
' FROM account_invoice_line l, account_invoice i ' \
' WHERE (l.invoice_id = i.id) ' \
2008-12-10 14:29:55 +00:00
' AND i.partner_id = %s ' \
2008-07-22 15:11:28 +00:00
' AND i.type = \' in_refund \' ' ,
( partner_id , ) )
value - = cr . fetchone ( ) [ 0 ] or 0.0
res = expression [ l [ ' expr_operator ' ] ] ( value , l [ ' expr_value ' ] )
if ( not res ) and ( l [ ' operator ' ] == ' and ' ) :
return False
if res :
return True
return True
2006-12-07 13:41:40 +00:00
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: