|
@ -1,5 +1,5 @@ |
|
|
from odoo import models, fields, api |
|
|
from odoo import models, fields, api |
|
|
from odoo.exceptions import UserError, ValidationError |
|
|
|
|
|
|
|
|
from odoo.exceptions import UserError, ValidationError,Warning |
|
|
from psycopg2 import sql, DatabaseError |
|
|
from psycopg2 import sql, DatabaseError |
|
|
|
|
|
|
|
|
from werkzeug import utils |
|
|
from werkzeug import utils |
|
@ -32,7 +32,7 @@ class opendons_operation(models.Model): |
|
|
#code=fields.Char(string='Code',required=True, translate=True,track_visibility='always') |
|
|
#code=fields.Char(string='Code',required=True, translate=True,track_visibility='always') |
|
|
name=fields.Char(string='Name',required=True, translate=True,track_visibility='always') |
|
|
name=fields.Char(string='Name',required=True, translate=True,track_visibility='always') |
|
|
partner_count = fields.Integer(string="count",readonly=True) |
|
|
partner_count = fields.Integer(string="count",readonly=True) |
|
|
#begin_date=fields.Date(string='Date begin',required=True, translate=True,track_visibility='always') |
|
|
|
|
|
|
|
|
exported_date=fields.Date(string='Exported Date',track_visibility='always') |
|
|
#end_date=fields.Date(string='Date end',required=True, translate=True,track_visibility='always') |
|
|
#end_date=fields.Date(string='Date end',required=True, translate=True,track_visibility='always') |
|
|
partner_ids = fields.Many2many('res.partner', 'partner_operation_rel', 'partner_id', 'operation_id', string='partners') |
|
|
partner_ids = fields.Many2many('res.partner', 'partner_operation_rel', 'partner_id', 'operation_id', string='partners') |
|
|
#contacts exclus de l'opération |
|
|
#contacts exclus de l'opération |
|
@ -99,13 +99,7 @@ class opendons_operation(models.Model): |
|
|
csv_export = fields.Binary('csv export', filters='.csv', readonly=True) |
|
|
csv_export = fields.Binary('csv export', filters='.csv', readonly=True) |
|
|
document_fname=fields.Char() |
|
|
document_fname=fields.Char() |
|
|
|
|
|
|
|
|
# def _compute_segment_count(self): |
|
|
|
|
|
# # The current user may not have access rights for donations |
|
|
|
|
|
# for operation in self: |
|
|
|
|
|
# try: |
|
|
|
|
|
# operation.segment_count = len(operation.segment_ids) |
|
|
|
|
|
# except Exception: |
|
|
|
|
|
# operation.segment_count = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate(self): |
|
|
def validate(self): |
|
|
for operation in self: |
|
|
for operation in self: |
|
@ -138,8 +132,10 @@ class opendons_operation(models.Model): |
|
|
with open(csv_filename, mode='w') as file: |
|
|
with open(csv_filename, mode='w') as file: |
|
|
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) |
|
|
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writer.writerow(['id','key','name', 'email','operation','segment']) |
|
|
|
|
|
|
|
|
if self.chanel=='email': |
|
|
|
|
|
writer.writerow(['id','key','name','firstname','email','operation','segment']) |
|
|
|
|
|
if self.chanel=='mail': |
|
|
|
|
|
writer.writerow(['id','key','name','firstname','title','street','street2','city','cp','country_name','operation','segment']) |
|
|
|
|
|
|
|
|
#sélections des contacts |
|
|
#sélections des contacts |
|
|
res=self.select_partner_operation() |
|
|
res=self.select_partner_operation() |
|
@ -151,13 +147,23 @@ class opendons_operation(models.Model): |
|
|
if seg.partner_ids: |
|
|
if seg.partner_ids: |
|
|
for p in seg.partner_ids: |
|
|
for p in seg.partner_ids: |
|
|
#raise UserError(p) |
|
|
#raise UserError(p) |
|
|
|
|
|
|
|
|
id=p.id |
|
|
id=p.id |
|
|
key=p.key |
|
|
key=p.key |
|
|
name=p.name |
|
|
name=p.name |
|
|
email=p.email |
|
|
|
|
|
|
|
|
firstname=p.firstname if p.firstname else '' |
|
|
|
|
|
street=p.street if p.street else '' |
|
|
|
|
|
street2=p.street2 if p.street2 else '' |
|
|
|
|
|
city=p.city if p.city else '' |
|
|
|
|
|
cp=p.zip if p.zip else '' |
|
|
|
|
|
country_name=p.country_id.name if p.country_id else '' |
|
|
|
|
|
title=p.title.name if p.title else '' |
|
|
operation=int(self.id) |
|
|
operation=int(self.id) |
|
|
segment=int(seg.id) |
|
|
segment=int(seg.id) |
|
|
writer.writerow([id,key,name,email,operation,segment]) |
|
|
|
|
|
|
|
|
if self.chanel=='email': |
|
|
|
|
|
writer.writerow([id,key,name,firstname,email,operation,segment]) |
|
|
|
|
|
if self.chanel=='mail': |
|
|
|
|
|
writer.writerow([id,key,name,firstname,title,street,street2,city,cp,country_name,operation,segment]) |
|
|
|
|
|
|
|
|
with open(csv_filename, 'r', encoding="utf-8") as f2: |
|
|
with open(csv_filename, 'r', encoding="utf-8") as f2: |
|
|
# file encode and store in a variable ‘data’ |
|
|
# file encode and store in a variable ‘data’ |
|
@ -166,7 +172,10 @@ class opendons_operation(models.Model): |
|
|
|
|
|
|
|
|
self.csv_export=base64.encodestring(data) |
|
|
self.csv_export=base64.encodestring(data) |
|
|
self.document_fname='operation'+str(self.id)+'.csv' |
|
|
self.document_fname='operation'+str(self.id)+'.csv' |
|
|
|
|
|
self.exported_date=fields.Date.context_today(self) |
|
|
os.unlink(csv_filename) |
|
|
os.unlink(csv_filename) |
|
|
|
|
|
#raise Warning('Traitement terminé') |
|
|
|
|
|
|
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
def select_partner_operation(self): |
|
|
def select_partner_operation(self): |
|
@ -185,19 +194,11 @@ class opendons_operation(models.Model): |
|
|
|
|
|
|
|
|
#[["id","not in",[1,3,7,8,10]]] |
|
|
#[["id","not in",[1,3,7,8,10]]] |
|
|
#["&",["id","not in",[22]],["id","not in",[1,3,7,8,10]]] |
|
|
#["&",["id","not in",[22]],["id","not in",[1,3,7,8,10]]] |
|
|
# partner_excl=self.env['res.partner'].search(eval(segexcl.mailing_domain)) |
|
|
|
|
|
# raise UserError(str(partner_excl)) |
|
|
|
|
|
# str_excl="[" |
|
|
|
|
|
# i=1 |
|
|
|
|
|
# for p in partner_excl: |
|
|
|
|
|
# if i==1: str_excl=str_excl+str(p.id) |
|
|
|
|
|
# if i>1 : str_excl=str_excl+','+str(p.id) |
|
|
|
|
|
# str_excl=str_excl+']' |
|
|
|
|
|
# excl_domain='["id","not in"',+str_excl +']' |
|
|
|
|
|
|
|
|
|
|
|
#List of Domain operators: ! (Not), | (Or), & (And) |
|
|
#List of Domain operators: ! (Not), | (Or), & (And) |
|
|
#List of Term operators: '=', '!=', '<=', '<', '>', '>=', '=?', '=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of' |
|
|
#List of Term operators: '=', '!=', '<=', '<', '>', '>=', '=?', '=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike', 'in', 'not in', 'child_of' |
|
|
|
|
|
|
|
|
#excl_domain='[("operation_excl_ids","!=","'+self.id+"')]" |
|
|
|
|
|
|
|
|
|
|
|
i=1 |
|
|
i=1 |
|
|
|
|
|
|
|
|
for sg in segments: |
|
|
for sg in segments: |
|
@ -245,50 +246,11 @@ class opendons_operation(models.Model): |
|
|
sg_1=sg |
|
|
sg_1=sg |
|
|
|
|
|
|
|
|
i=i+1 |
|
|
i=i+1 |
|
|
|
|
|
|
|
|
|
|
|
self.exported_date=fields.Date.context_today(self) |
|
|
|
|
|
|
|
|
return True |
|
|
return True |
|
|
|
|
|
|
|
|
def select_partner_operation_old(self): |
|
|
|
|
|
|
|
|
|
|
|
#le principe est de selectionner les contacts associés aux segments avec les règles suivantes : |
|
|
|
|
|
#RG1: la priorité des segments est l'ordre de présentation dans la liste des segments |
|
|
|
|
|
#RG2: au final, un contact sélectionné est rattaché à l'opération avec le segment d'appartenance |
|
|
|
|
|
#RG1: si un contact est présent dans un segment d'exclusion, alors on le retire de la sélection operateur "!"" |
|
|
|
|
|
|
|
|
|
|
|
#suppression des contacts déjà présents pour l'opération |
|
|
|
|
|
self.env["opendons.operation_partner"].search([('operation_id','=',self.id)]).unlink() |
|
|
|
|
|
|
|
|
|
|
|
segments=self.env['opendons.segment'].search(['&',('operation_id','=',self.id),('exclusion','=','False')],order='sequence asc') |
|
|
|
|
|
i=1 |
|
|
|
|
|
for sg in segments: |
|
|
|
|
|
if i==1: |
|
|
|
|
|
partners=self.env['res.partner'].search(eval(sg.mailing_domain)) |
|
|
|
|
|
for p in partners: |
|
|
|
|
|
vals={} |
|
|
|
|
|
vals['operation_id']=self.id |
|
|
|
|
|
vals['segment_id']=sg.id |
|
|
|
|
|
vals['partner_id']=p.id |
|
|
|
|
|
|
|
|
|
|
|
res2=self.env['opendons.operation_partner'].create(vals) |
|
|
|
|
|
|
|
|
|
|
|
if i>1: |
|
|
|
|
|
partners=self.env['res.partner'].search(eval(sg.mailing_domain)) |
|
|
|
|
|
for p in partners: |
|
|
|
|
|
pp=self.env["opendons.operation_partner"].search_count( |
|
|
|
|
|
['&',('operation_id','=',int(self.id)), |
|
|
|
|
|
('partner_id','=',int(p.id))]) |
|
|
|
|
|
if pp==0: |
|
|
|
|
|
vals={} |
|
|
|
|
|
vals['operation_id']=self.id |
|
|
|
|
|
vals['segment_id']=sg.id |
|
|
|
|
|
vals['partner_id']=p.id |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res2=self.env['opendons.operation_partner'].create(vals) |
|
|
|
|
|
i=i+1 |
|
|
|
|
|
|
|
|
|
|
|
return self.env["opendons.operation_partner"].search([('operation_id','=',self.id)]) |
|
|
|
|
|
|
|
|
|
|
|
@api.model |
|
|
@api.model |
|
|
def create(self,vals): |
|
|
def create(self,vals): |
|
@ -330,28 +292,6 @@ class opendons_operation(models.Model): |
|
|
self.document_fname='operation'+seld.id+'.csv' |
|
|
self.document_fname='operation'+seld.id+'.csv' |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
class opendons_operation_partner(models.Model): |
|
|
|
|
|
_name = 'opendons.operation_partner' |
|
|
|
|
|
_description = 'opération marketing : lignes des contacts sélectionnés et exportés' |
|
|
|
|
|
|
|
|
|
|
|
operation_id = fields.Many2one( |
|
|
|
|
|
'opendons.operation', |
|
|
|
|
|
String='Operation', |
|
|
|
|
|
index=True, |
|
|
|
|
|
readonly=True) |
|
|
|
|
|
|
|
|
|
|
|
segment_id = fields.Many2one( |
|
|
|
|
|
'opendons.segment', |
|
|
|
|
|
String='Segemnt', |
|
|
|
|
|
index=True, |
|
|
|
|
|
readonly=True) |
|
|
|
|
|
|
|
|
|
|
|
partner_id = fields.Many2one( |
|
|
|
|
|
'res.partner', |
|
|
|
|
|
String='Partner', |
|
|
|
|
|
index=True, |
|
|
|
|
|
readonly=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class opendons_segment(models.Model): |
|
|
class opendons_segment(models.Model): |
|
@ -368,7 +308,7 @@ class opendons_segment(models.Model): |
|
|
currency_field='currency_id') |
|
|
currency_field='currency_id') |
|
|
partner_ids = fields.Many2many('res.partner', 'partner_segment_rel', 'partner_id', 'segment_id', string='partners') |
|
|
partner_ids = fields.Many2many('res.partner', 'partner_segment_rel', 'partner_id', 'segment_id', string='partners') |
|
|
partner_count = fields.Integer(string="partners count", compute='_count_partner_segment',readonly=True) |
|
|
partner_count = fields.Integer(string="partners count", compute='_count_partner_segment',readonly=True) |
|
|
mailing_domain = fields.Char(string='partners selection', readonly=True) |
|
|
|
|
|
|
|
|
mailing_domain = fields.Char(string='partners selection') |
|
|
|
|
|
|
|
|
@api.model |
|
|
@api.model |
|
|
def _default_currency(self): |
|
|
def _default_currency(self): |
|
|