|
@ -6,6 +6,7 @@ from .ffck_structure_type import SCALES |
|
|
|
|
|
|
|
|
class ResPartner(models.Model): |
|
|
class ResPartner(models.Model): |
|
|
_inherit = "res.partner" |
|
|
_inherit = "res.partner" |
|
|
|
|
|
_rec_names_search = ["display_name", "ref", "partner_code", "email"] |
|
|
|
|
|
|
|
|
# Partner fields |
|
|
# Partner fields |
|
|
ffck_network = fields.Boolean(string="FFCK network") |
|
|
ffck_network = fields.Boolean(string="FFCK network") |
|
@ -22,7 +23,9 @@ class ResPartner(models.Model): |
|
|
string="Structure type", |
|
|
string="Structure type", |
|
|
) |
|
|
) |
|
|
partner_code = fields.Char(string="FFCK code", size=6, index=True) |
|
|
partner_code = fields.Char(string="FFCK code", size=6, index=True) |
|
|
partner_code_editable = fields.Boolean(string="FFCK code editable", default=True) |
|
|
|
|
|
|
|
|
partner_code_editable = fields.Boolean( |
|
|
|
|
|
string="FFCK code editable", default=True, readonly=True |
|
|
|
|
|
) |
|
|
# FFCK |
|
|
# FFCK |
|
|
ffck_partner_id = fields.Many2one( |
|
|
ffck_partner_id = fields.Many2one( |
|
|
comodel_name="res.partner", |
|
|
comodel_name="res.partner", |
|
@ -80,7 +83,7 @@ class ResPartner(models.Model): |
|
|
domain=[("partner_scale", "=", "4")], |
|
|
domain=[("partner_scale", "=", "4")], |
|
|
string="Local structures", |
|
|
string="Local structures", |
|
|
) |
|
|
) |
|
|
# Compute hierarchy for searchpanel |
|
|
|
|
|
|
|
|
# Compute hierarchy for searchpanel TODO : move to standalone module |
|
|
hierarchy_id = fields.Many2one( |
|
|
hierarchy_id = fields.Many2one( |
|
|
comodel_name="res.partner.hierarchy", |
|
|
comodel_name="res.partner.hierarchy", |
|
|
string="Hierarchy element", |
|
|
string="Hierarchy element", |
|
@ -88,66 +91,54 @@ class ResPartner(models.Model): |
|
|
store=True, |
|
|
store=True, |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
@api.depends("ffck_network", "partner_scale", "partner_code") |
|
|
|
|
|
|
|
|
@api.depends( |
|
|
|
|
|
"ffck_network", |
|
|
|
|
|
"partner_scale", |
|
|
|
|
|
"partner_code", |
|
|
|
|
|
"local_partner_id", |
|
|
|
|
|
"cdck_partner_id", |
|
|
|
|
|
"crck_partner_id", |
|
|
|
|
|
"ffck_partner_id", |
|
|
|
|
|
) |
|
|
def _compute_partner_hierarchy(self): |
|
|
def _compute_partner_hierarchy(self): |
|
|
for partner in self: |
|
|
for partner in self: |
|
|
partner.hierarchy_id = ( |
|
|
partner.hierarchy_id = ( |
|
|
partner.id |
|
|
partner.id |
|
|
if partner.ffck_network |
|
|
if partner.ffck_network |
|
|
and int(partner.partner_scale) in range(1, 5) |
|
|
|
|
|
|
|
|
and int(partner.partner_scale) in range(1, 4) |
|
|
and partner.partner_code |
|
|
and partner.partner_code |
|
|
else False |
|
|
|
|
|
|
|
|
else partner.local_partner_id.id |
|
|
|
|
|
or partner.cdck_partner_id.id |
|
|
|
|
|
or partner.crck_partner_id.id |
|
|
|
|
|
or partner.ffck_partner_id.id |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
@api.model |
|
|
|
|
|
def name_search(self, name="", args=None, operator="ilike", limit=100): |
|
|
|
|
|
"""Allow searching by sequence code by default.""" |
|
|
|
|
|
# Do not add any domain when user just clicked on search widget |
|
|
|
|
|
if not (name == "" and operator == "ilike"): |
|
|
|
|
|
# The dangling | is needed to combine with the domain added by super() |
|
|
|
|
|
args = OR( |
|
|
|
|
|
[ |
|
|
|
|
|
[ |
|
|
|
|
|
"&", |
|
|
|
|
|
("ffck_network", "=", True), |
|
|
|
|
|
("partner_code", "=like", name + "%"), |
|
|
|
|
|
], |
|
|
|
|
|
args or [], |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
return super().name_search(name, args, operator, limit) |
|
|
|
|
|
|
|
|
|
|
|
@api.depends("name", "ref", "partner_code") |
|
|
|
|
|
def name_get(self): |
|
|
|
|
|
result = [] |
|
|
|
|
|
ffck_partners = self.filtered("ffck_network") |
|
|
|
|
|
others = self - ffck_partners |
|
|
|
|
|
for record in ffck_partners: |
|
|
|
|
|
code = record.partner_code |
|
|
|
|
|
ref = record.ref |
|
|
|
|
|
result.append( |
|
|
|
|
|
( |
|
|
|
|
|
record.id, |
|
|
|
|
|
"{}{}{}".format( |
|
|
|
|
|
code and code + " - " or "", |
|
|
|
|
|
ref and record.is_company and ref + " - " or "", |
|
|
|
|
|
super(ResPartner, record).name_get()[0][1], |
|
|
|
|
|
), |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
result += super(ResPartner, others).name_get() |
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
# COMPUTES |
|
|
# COMPUTES |
|
|
|
|
|
|
|
|
@api.depends("company_type", "ffck_structure_type_id") |
|
|
|
|
|
|
|
|
@api.depends( |
|
|
|
|
|
"is_company", |
|
|
|
|
|
"ffck_network", |
|
|
|
|
|
"ffck_structure_type_id", |
|
|
|
|
|
"ffck_structure_type_id.scale", |
|
|
|
|
|
) |
|
|
def _get_partner_scale(self): |
|
|
def _get_partner_scale(self): |
|
|
for partner in self: |
|
|
|
|
|
if partner.company_type == "individual": |
|
|
|
|
|
|
|
|
ffck_partners = self.filtered("ffck_network") |
|
|
|
|
|
for partner in ffck_partners: |
|
|
|
|
|
if not partner.is_company: |
|
|
partner.partner_scale = "5" |
|
|
partner.partner_scale = "5" |
|
|
else: |
|
|
else: |
|
|
partner.partner_scale = partner.ffck_structure_type_id.scale |
|
|
|
|
|
|
|
|
partner.partner_scale = ( |
|
|
|
|
|
partner.ffck_structure_type_id.scale |
|
|
|
|
|
if partner.ffck_structure_type_id |
|
|
|
|
|
else False |
|
|
|
|
|
) |
|
|
|
|
|
for partner in self - ffck_partners: |
|
|
|
|
|
partner.partner_scale = False |
|
|
|
|
|
|
|
|
|
|
|
def lock_partner_code(self): |
|
|
|
|
|
self.write({"partner_code_editable": False}) |
|
|
|
|
|
|
|
|
|
|
|
@api.depends("ffck_network") |
|
|
def _get_ffck_partner(self): |
|
|
def _get_ffck_partner(self): |
|
|
ffck = self.env.ref("ffck_commons.res_partner_ffck", raise_if_not_found=False) |
|
|
ffck = self.env.ref("ffck_commons.res_partner_ffck", raise_if_not_found=False) |
|
|
main = self.env.ref("base.main_partner") |
|
|
main = self.env.ref("base.main_partner") |
|
@ -159,29 +150,39 @@ class ResPartner(models.Model): |
|
|
else: |
|
|
else: |
|
|
partner.ffck_partner_id = ffck |
|
|
partner.ffck_partner_id = ffck |
|
|
|
|
|
|
|
|
@api.depends("state_id", "ffck_network", "partner_scale") |
|
|
|
|
|
|
|
|
@api.depends( |
|
|
|
|
|
"state_id", |
|
|
|
|
|
"ffck_network", |
|
|
|
|
|
"partner_scale", |
|
|
|
|
|
"cdck_partner_id", |
|
|
|
|
|
) |
|
|
def _get_crck_partner(self): |
|
|
def _get_crck_partner(self): |
|
|
states = self.mapped("state_id") |
|
|
|
|
|
crck_ok = self.with_context(active_test=False).search( |
|
|
|
|
|
[ |
|
|
|
|
|
("ffck_network", "=", True), |
|
|
|
|
|
("partner_scale", "=", "2"), |
|
|
|
|
|
("state_id", "in", states.ids), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
crck_by_state = {crck.state_id: crck for crck in crck_ok} |
|
|
|
|
|
states_ok = states & crck_ok.mapped("state_id") |
|
|
|
|
|
concerned = self.filtered( |
|
|
|
|
|
lambda rp: rp.ffck_network |
|
|
|
|
|
and int(rp.partner_scale) >= 2 |
|
|
|
|
|
and rp.state_id in states_ok |
|
|
|
|
|
) |
|
|
|
|
|
for partner in concerned: |
|
|
|
|
|
state = partner.state_id |
|
|
|
|
|
partner.crck_partner_id = crck_by_state[state] |
|
|
|
|
|
# treat unconcerned |
|
|
|
|
|
for partner in self - concerned: |
|
|
|
|
|
partner.crck_partner_id = False |
|
|
|
|
|
|
|
|
cdck_ok = self.filtered("cdck_partner_id") |
|
|
|
|
|
for partner in cdck_ok: |
|
|
|
|
|
partner.crck_partner_id = partner.cdck_partner_id.crck_partner_id |
|
|
|
|
|
todo = self - cdck_ok |
|
|
|
|
|
if todo: |
|
|
|
|
|
states = todo.mapped("state_id") |
|
|
|
|
|
crck_ok = self.with_context(active_test=False).search( |
|
|
|
|
|
[ |
|
|
|
|
|
("ffck_network", "=", True), |
|
|
|
|
|
("partner_scale", "=", "2"), |
|
|
|
|
|
("state_id", "in", states.ids), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
states_ok = states & crck_ok.mapped("state_id") |
|
|
|
|
|
crck_by_state = {crck.state_id: crck for crck in crck_ok} |
|
|
|
|
|
concerned = todo.filtered( |
|
|
|
|
|
lambda rp: rp.ffck_network |
|
|
|
|
|
and int(rp.partner_scale) >= 2 |
|
|
|
|
|
and rp.state_id in states_ok |
|
|
|
|
|
) |
|
|
|
|
|
for partner in concerned: |
|
|
|
|
|
state = partner.state_id |
|
|
|
|
|
partner.crck_partner_id = crck_by_state[state] |
|
|
|
|
|
# treat unconcerned |
|
|
|
|
|
for partner in todo - concerned: |
|
|
|
|
|
partner.crck_partner_id = False |
|
|
|
|
|
|
|
|
@api.depends( |
|
|
@api.depends( |
|
|
"is_company", |
|
|
"is_company", |
|
@ -189,56 +190,70 @@ class ResPartner(models.Model): |
|
|
"partner_code", |
|
|
"partner_code", |
|
|
"partner_scale", |
|
|
"partner_scale", |
|
|
"local_partner_id", |
|
|
"local_partner_id", |
|
|
|
|
|
"local_partner_id", |
|
|
"local_partner_id.partner_code", |
|
|
"local_partner_id.partner_code", |
|
|
"country_department_id", |
|
|
"country_department_id", |
|
|
"country_department_id.code", |
|
|
"country_department_id.code", |
|
|
) |
|
|
) |
|
|
def _get_cdck_partner(self): |
|
|
def _get_cdck_partner(self): |
|
|
structures = self.filtered( |
|
|
|
|
|
lambda rp: rp.is_company |
|
|
|
|
|
and rp.ffck_network |
|
|
|
|
|
and rp.partner_code |
|
|
|
|
|
and len(rp.partner_code) >= 2 |
|
|
|
|
|
and int(rp.partner_scale) in (3, 4) |
|
|
|
|
|
) |
|
|
|
|
|
licensees = self.filtered( |
|
|
|
|
|
lambda rp: not rp.is_company |
|
|
|
|
|
and rp.ffck_network |
|
|
|
|
|
and int(rp.partner_scale) == 5 |
|
|
|
|
|
and ( |
|
|
|
|
|
rp.local_partner_id |
|
|
|
|
|
and rp.local_partner_id.cdck_partner_id |
|
|
|
|
|
or rp.country_department_id |
|
|
|
|
|
|
|
|
local_ok = self.filtered("local_partner_id") |
|
|
|
|
|
for partner in local_ok: |
|
|
|
|
|
partner.cdck_partner_id = partner.local_partner_id.cdck_partner_id |
|
|
|
|
|
todo = self - local_ok |
|
|
|
|
|
if todo: |
|
|
|
|
|
structures = todo.filtered( |
|
|
|
|
|
lambda rp: rp.is_company |
|
|
|
|
|
and rp.ffck_network |
|
|
|
|
|
and rp.partner_code |
|
|
|
|
|
and len(rp.partner_code) >= 2 |
|
|
|
|
|
and int(rp.partner_scale) in (3, 4) |
|
|
|
|
|
) |
|
|
|
|
|
licensees = todo.filtered( |
|
|
|
|
|
lambda rp: not rp.is_company |
|
|
|
|
|
and rp.ffck_network |
|
|
|
|
|
and int(rp.partner_scale) == 5 |
|
|
|
|
|
and rp.country_department_id |
|
|
) |
|
|
) |
|
|
) |
|
|
|
|
|
dept_codes = self.mapped( |
|
|
|
|
|
lambda rp: rp.partner_code |
|
|
|
|
|
and len(rp.partner_code) >= 2 |
|
|
|
|
|
and int(rp.partner_scale) in (3, 4) |
|
|
|
|
|
and rp.partner_code[:2] |
|
|
|
|
|
) |
|
|
|
|
|
cdck_ok = self.with_context(active_test=False).search( |
|
|
|
|
|
[ |
|
|
|
|
|
("ffck_network", "=", True), |
|
|
|
|
|
("partner_scale", "=", "3"), |
|
|
|
|
|
("partner_code", "in", ["{}00".format(code) for code in dept_codes]), |
|
|
|
|
|
] |
|
|
|
|
|
) |
|
|
|
|
|
cdck_by_code = {cdck.partner_code[:2]: cdck for cdck in cdck_ok} |
|
|
|
|
|
# Treat structures |
|
|
|
|
|
for partner in structures: |
|
|
|
|
|
partner.cdck_partner_id = cdck_by_code[partner.partner_code[:2]] |
|
|
|
|
|
# Treat licensees |
|
|
|
|
|
for partner in licensees: |
|
|
|
|
|
dept = partner.country_department_id |
|
|
|
|
|
local = partner.local_partner_id |
|
|
|
|
|
local_cd = local.cdck_partner_id |
|
|
|
|
|
partner.cdck_partner_id = ( |
|
|
|
|
|
local_cd if local and local_cd else cdck_by_code[dept.code] |
|
|
|
|
|
|
|
|
dept_codes = todo.mapped( |
|
|
|
|
|
lambda rp: rp.ffck_network |
|
|
|
|
|
and ( |
|
|
|
|
|
rp.is_company |
|
|
|
|
|
and rp.partner_code |
|
|
|
|
|
and len(rp.partner_code) >= 2 |
|
|
|
|
|
and int(rp.partner_scale) in (3, 4) |
|
|
|
|
|
and rp.partner_code[:2] |
|
|
|
|
|
or not rp.is_company |
|
|
|
|
|
and rp.country_department_id |
|
|
|
|
|
and rp.country_department_id.code |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
cdck_ok = self.with_context(active_test=False).search( |
|
|
|
|
|
[ |
|
|
|
|
|
("is_company", "=", True), |
|
|
|
|
|
("ffck_network", "=", True), |
|
|
|
|
|
("partner_scale", "=", "3"), |
|
|
|
|
|
( |
|
|
|
|
|
"partner_code", |
|
|
|
|
|
"in", |
|
|
|
|
|
["{}00".format(code) for code in dept_codes], |
|
|
|
|
|
), |
|
|
|
|
|
] |
|
|
) |
|
|
) |
|
|
# treat others |
|
|
|
|
|
for partner in self - (structures | licensees): |
|
|
|
|
|
partner.cdck_partner_id = False |
|
|
|
|
|
|
|
|
cdck_by_code = {cdck.partner_code[:2]: cdck for cdck in cdck_ok} |
|
|
|
|
|
# Treat structures |
|
|
|
|
|
for partner in structures: |
|
|
|
|
|
partner.cdck_partner_id = cdck_by_code[partner.partner_code[:2]] |
|
|
|
|
|
# Treat licensees |
|
|
|
|
|
for partner in licensees: |
|
|
|
|
|
dept = partner.country_department_id |
|
|
|
|
|
local = partner.local_partner_id |
|
|
|
|
|
local_cd = local.cdck_partner_id |
|
|
|
|
|
partner.cdck_partner_id = ( |
|
|
|
|
|
local_cd if local and local_cd else cdck_by_code[dept.code] |
|
|
|
|
|
) |
|
|
|
|
|
# treat others |
|
|
|
|
|
for partner in todo - (structures | licensees): |
|
|
|
|
|
partner.cdck_partner_id = False |
|
|
|
|
|
|
|
|
@api.depends( |
|
|
@api.depends( |
|
|
"ffck_network", |
|
|
"ffck_network", |
|
@ -263,6 +278,59 @@ class ResPartner(models.Model): |
|
|
else: |
|
|
else: |
|
|
partner.ffck_parent_id = False |
|
|
partner.ffck_parent_id = False |
|
|
|
|
|
|
|
|
|
|
|
# INHERITANCE |
|
|
|
|
|
|
|
|
|
|
|
# @api.model |
|
|
|
|
|
# def name_search(self, name="", args=None, operator="ilike", limit=100): |
|
|
|
|
|
# """Allow searching by sequence code by default.""" |
|
|
|
|
|
# # Do not add any domain when user just clicked on search widget |
|
|
|
|
|
# if not (name == "" and operator == "ilike"): |
|
|
|
|
|
# # The dangling | is needed to combine with the domain added by super() |
|
|
|
|
|
# args = OR( |
|
|
|
|
|
# [ |
|
|
|
|
|
# [ |
|
|
|
|
|
# "&", |
|
|
|
|
|
# ("ffck_network", "=", True), |
|
|
|
|
|
# ("partner_code", "=like", name + "%"), |
|
|
|
|
|
# ], |
|
|
|
|
|
# args or [], |
|
|
|
|
|
# ] |
|
|
|
|
|
# ) |
|
|
|
|
|
# return super().name_search(name, args, operator, limit) |
|
|
|
|
|
|
|
|
|
|
|
@api.depends( |
|
|
|
|
|
"is_company", |
|
|
|
|
|
"name", |
|
|
|
|
|
"parent_id.display_name", |
|
|
|
|
|
"type", |
|
|
|
|
|
"company_name", |
|
|
|
|
|
"commercial_company_name", |
|
|
|
|
|
"ref", |
|
|
|
|
|
"partner_code", |
|
|
|
|
|
) |
|
|
|
|
|
def _compute_display_name(self): |
|
|
|
|
|
return super()._compute_display_name() |
|
|
|
|
|
|
|
|
|
|
|
def name_get(self): |
|
|
|
|
|
res = [] |
|
|
|
|
|
ffck_partners = self.filtered("ffck_network") |
|
|
|
|
|
others = self - ffck_partners |
|
|
|
|
|
for record in ffck_partners: |
|
|
|
|
|
code = record.partner_code |
|
|
|
|
|
ref = record.ref |
|
|
|
|
|
res.append( |
|
|
|
|
|
( |
|
|
|
|
|
record.id, |
|
|
|
|
|
"{}{}{}".format( |
|
|
|
|
|
code and code + " - " or "", |
|
|
|
|
|
ref and record.is_company and ref + " - " or "", |
|
|
|
|
|
super(ResPartner, record).name_get()[0][1], |
|
|
|
|
|
), |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
res += super(ResPartner, others).name_get() |
|
|
|
|
|
return res |
|
|
|
|
|
|
|
|
# ONCHANGES |
|
|
# ONCHANGES |
|
|
|
|
|
|
|
|
@api.onchange("partner_code", "company_type", "ffck_network") |
|
|
@api.onchange("partner_code", "company_type", "ffck_network") |
|
@ -276,8 +344,8 @@ class ResPartner(models.Model): |
|
|
|
|
|
|
|
|
# CRUD |
|
|
# CRUD |
|
|
|
|
|
|
|
|
@api.model_create_multi |
|
|
|
|
|
def create(self, vals_list): |
|
|
|
|
|
for vals in vals_list: |
|
|
|
|
|
vals.update({"partner_code_editable": False}) |
|
|
|
|
|
return super().create(vals_list) |
|
|
|
|
|
|
|
|
# @api.model_create_multi |
|
|
|
|
|
# def create(self, vals_list): |
|
|
|
|
|
# for vals in vals_list: |
|
|
|
|
|
# vals.update({"partner_code_editable": False}) |
|
|
|
|
|
# return super().create(vals_list) |