|
|
@ -0,0 +1,141 @@ |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
from odoo import models, fields, api |
|
|
|
from odoo.exceptions import UserError, ValidationError,Warning |
|
|
|
from psycopg2 import sql, DatabaseError |
|
|
|
from odoo.tools.safe_eval import safe_eval, datetime |
|
|
|
|
|
|
|
from werkzeug import utils |
|
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class opendons_duplicate_partner(models.Model): |
|
|
|
_name = 'opendons.duplicate_partner' |
|
|
|
_description = 'duplicate partner' |
|
|
|
|
|
|
|
partner_id = fields.Many2one( |
|
|
|
'res.partner', |
|
|
|
string='partner', |
|
|
|
required=True, |
|
|
|
index=True, |
|
|
|
readonly=True, |
|
|
|
track_visibility='onchange', |
|
|
|
ondelete='restrict' |
|
|
|
) |
|
|
|
|
|
|
|
def find_duplicate_partner(self): |
|
|
|
self.env['opendons.duplicate_partner'].search([]).unlink() |
|
|
|
rules=self.env['opendons.duplicate_rule'].search([]) |
|
|
|
dbl_partner_ids=[] |
|
|
|
groupby_fields='' |
|
|
|
list_field=[] |
|
|
|
i=1 |
|
|
|
for rule in rules: |
|
|
|
for f in rule.field_ids: |
|
|
|
if i==1:groupby_fields+="'"+f.field+"'" |
|
|
|
else:groupby_fields+=",'"+f.field+"'" |
|
|
|
list_field.append(f.field) |
|
|
|
i=i+1 |
|
|
|
selected_fields='' |
|
|
|
j=1 |
|
|
|
i=i-1 |
|
|
|
for f in list_field: |
|
|
|
if j==1:selected_fields+=""+f+"" |
|
|
|
if j>1 and j<i:selected_fields+="'"+f+"" |
|
|
|
if j==i and j!=1:selected_fields+=","+f+"" |
|
|
|
j=j+1 |
|
|
|
j=j-1 |
|
|
|
last_field=list_field[j-1] |
|
|
|
|
|
|
|
sql="select count(id) as __count, "+ selected_fields +" from res_partner group by "+ selected_fields |
|
|
|
self.env.cr.execute(sql) |
|
|
|
|
|
|
|
p=self.env.cr.dictfetchall() |
|
|
|
|
|
|
|
#raise Warning(json.dumps(p)) |
|
|
|
for pp in p: |
|
|
|
|
|
|
|
if int(pp['__count'])>1: |
|
|
|
|
|
|
|
domain='' |
|
|
|
|
|
|
|
# if len(list_field)==1: |
|
|
|
# domain= "('"+list_field[0]+"','=','"+list_field[0]+"')" |
|
|
|
# else: |
|
|
|
if len(list_field)>1:domain="['&'," |
|
|
|
else:domain="[" |
|
|
|
|
|
|
|
i=1 |
|
|
|
for f in list_field: |
|
|
|
if pp[f]: |
|
|
|
if i==1:domain+="('"+f+"','=','"+pp[f]+"')" |
|
|
|
if i>1:domain+=",('"+f+"','=','"+pp[f]+"')" |
|
|
|
i=i+1 |
|
|
|
domain+=']' |
|
|
|
if domain!='[]': |
|
|
|
|
|
|
|
partner_dbl=self.env['res.partner'].search(eval(domain)) |
|
|
|
|
|
|
|
for partner in partner_dbl: |
|
|
|
vals={} |
|
|
|
vals['partner_id']=partner.id |
|
|
|
self.env['opendons.duplicate_partner'].create(vals) |
|
|
|
dbl_partner_ids.append(partner.id) |
|
|
|
|
|
|
|
action = self.env.ref("contacts.action_contacts").sudo().read([])[0] |
|
|
|
|
|
|
|
action.update( |
|
|
|
{ |
|
|
|
"domain": [("id", "in", dbl_partner_ids)], |
|
|
|
"limit": 500, |
|
|
|
} |
|
|
|
) |
|
|
|
return action |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class opendons_duplicate_rule(models.Model): |
|
|
|
_name = 'opendons.duplicate_rule' |
|
|
|
_description = 'duplicate partner rule' |
|
|
|
field_ids=fields.One2many( |
|
|
|
'opendons.duplicate_rule_line', |
|
|
|
'rule_id', |
|
|
|
string='fields', |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class opendons_duplicate_rule_line(models.Model): |
|
|
|
_name = 'opendons.duplicate_rule_line' |
|
|
|
_description = 'duplicate partner rule line' |
|
|
|
field=fields.Selection('field_selection', string='fields') |
|
|
|
rule_id=fields.Many2one( |
|
|
|
'opendons.duplicate_rule', |
|
|
|
string='rule', |
|
|
|
required=True, |
|
|
|
index=True, |
|
|
|
readonly=True, |
|
|
|
track_visibility='onchange', |
|
|
|
ondelete='restrict' |
|
|
|
) |
|
|
|
@api.model |
|
|
|
def field_selection(self): |
|
|
|
field_list=self.env['res.partner'].fields_get() |
|
|
|
#raise Warning(json.dumps(field_list)) |
|
|
|
result=[] |
|
|
|
for key in field_list: |
|
|
|
result.append((key,key)) |
|
|
|
result.sort() |
|
|
|
return result |
|
|
|
|
|
|
|
def field_selection2(self): |
|
|
|
|
|
|
|
field_list=self.env['res.partner'].fields_get() |
|
|
|
#raise Warning(json.dumps(field_list)) |
|
|
|
result=[] |
|
|
|
for key in field_list: |
|
|
|
result.append((key,key)) |
|
|
|
raise Warning(result) |
|
|
|
return result |