Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] base_partner_sequence: Allows to use unique (and distinct) generated references across all partners #1875

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion base_partner_sequence/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Add a sequence on customers' code
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:a4504f35c230e89260203071561597acc90e6e7e278aa336f67b34dc823c9d0e
!! source digest: sha256:6a84e4672fe940b66806ea10c754257fa3476d36a31eed544a24960583ce27fb
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand Down Expand Up @@ -40,11 +40,21 @@ but it can only be modified from the commercial partner.
No references are assigned for contacts such as shipping and
invoice addresses.

That initial behavior can be changed to use unique generated references
across all partners through a configuration parameter.

**Table of contents**

.. contents::
:local:

Configuration
=============

- Go to Settings > General Parameters > Contacts
- If you want to set the partner reference as readonly, check the box 'Partner Reference Readonly'.
- If you want to get a unique generated reference across all partners, check the box 'Partner Generated Reference Unique'.

Bug Tracker
===========

Expand Down
5 changes: 5 additions & 0 deletions base_partner_sequence/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def _needs_ref(self, vals=None):
raise exceptions.UserError(
_("Either field values or an id must be provided.")
)
# If references should be unique across all partners
if self.env.company.partner_generated_reference_unique:
return True
# only assign a 'ref' to commercial partners
fields_for_check = ["is_company", "parent_id"]
# Copy original vals to prevent modifying them
Expand All @@ -86,4 +89,6 @@ def _commercial_fields(self):
Make the partner reference a field that is propagated
to the partner's contacts
"""
if self.env.company.partner_generated_reference_unique:
return super()._commercial_fields()
return super(ResPartner, self)._commercial_fields() + ["ref"]
28 changes: 28 additions & 0 deletions base_partner_sequence/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,31 @@ class ResCompany(models.Model):
string="Partner Reference Readonly",
help="If marked, the Reference in partners will not be editable.",
)
partner_generated_reference_unique = fields.Boolean(
compute="_compute_partner_generated_reference_unique",
inverse="_inverse_partner_generated_reference_unique",
groups="base.group_system",
help="This is a technical field that allows to get this from config parameter",
)

def _compute_partner_generated_reference_unique(self):
"""
Compute the parameter to be accessible on company level
"""
unique = (
self.env["ir.config_parameter"]
.sudo()
.get_param("base_partner_sequence.partner_generated_reference_unique")
)
self.partner_generated_reference_unique = unique

def _inverse_partner_generated_reference_unique(self):
"""
Allows to change the parameter through company field.
"""
unique = bool()
for company in self:
unique = company.partner_generated_reference_unique
self.env["ir.config_parameter"].sudo().set_param(
"base_partner_sequence.partner_generated_reference_unique", unique
)
7 changes: 7 additions & 0 deletions base_partner_sequence/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ class ResConfigSettings(models.TransientModel):
partner_ref_readonly = fields.Boolean(
related="company_id.partner_ref_readonly", readonly=False
)
# This as general parameter as multi-company behavior is quite impossible
# to manage correctly as 'ref' field is not company dependent.
partner_generated_reference_unique = fields.Boolean(
config_parameter="base_partner_sequence.partner_generated_reference_unique",
help="Check this if you want to have unique generated references "
"across all partners (companies, contacts, addresses,...)",
)
3 changes: 3 additions & 0 deletions base_partner_sequence/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Go to Settings > General Parameters > Contacts
- If you want to set the partner reference as readonly, check the box 'Partner Reference Readonly'.
- If you want to get a unique generated reference across all partners, check the box 'Partner Generated Reference Unique'.
3 changes: 3 additions & 0 deletions base_partner_sequence/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ but it can only be modified from the commercial partner.

No references are assigned for contacts such as shipping and
invoice addresses.

That initial behavior can be changed to use unique generated references
across all partners through a configuration parameter.
42 changes: 27 additions & 15 deletions base_partner_sequence/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Add a sequence on customers' code</title>
<style type="text/css">

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

Expand Down Expand Up @@ -366,9 +367,9 @@ <h1 class="title">Add a sequence on customers’ code</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:a4504f35c230e89260203071561597acc90e6e7e278aa336f67b34dc823c9d0e
!! source digest: sha256:6a84e4672fe940b66806ea10c754257fa3476d36a31eed544a24960583ce27fb
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/partner-contact/tree/16.0/base_partner_sequence"><img alt="OCA/partner-contact" src="https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/partner-contact-16-0/partner-contact-16-0-base_partner_sequence"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/partner-contact&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/partner-contact/tree/16.0/base_partner_sequence"><img alt="OCA/partner-contact" src="https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/partner-contact-16-0/partner-contact-16-0-base_partner_sequence"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/builds?repo=OCA/partner-contact&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds the possibility to define a sequence for
the partner’s reference. This reference is then set as default
when you create a new partner, using the defined sequence.</p>
Expand All @@ -378,30 +379,41 @@ <h1 class="title">Add a sequence on customers’ code</h1>
but it can only be modified from the commercial partner.</p>
<p>No references are assigned for contacts such as shipping and
invoice addresses.</p>
<p>That initial behavior can be changed to use unique generated references
across all partners through a configuration parameter.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-1">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-2">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-3">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-4">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-5">Maintainers</a></li>
<li><a class="reference internal" href="#configuration" id="id1">Configuration</a></li>
<li><a class="reference internal" href="#bug-tracker" id="id2">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id3">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id4">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id5">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id6">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="configuration">
<h1><a class="toc-backref" href="#id1">Configuration</a></h1>
<ul class="simple">
<li>Go to Settings &gt; General Parameters &gt; Contacts</li>
<li>If you want to set the partner reference as readonly, check the box ‘Partner Reference Readonly’.</li>
<li>If you want to get a unique generated reference across all partners, check the box ‘Partner Generated Reference Unique’.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/partner-contact/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/partner-contact/issues/new?body=module:%20base_partner_sequence%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
<h1><a class="toc-backref" href="#id3">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<h2><a class="toc-backref" href="#id4">Authors</a></h2>
<ul class="simple">
<li>Tiny/initOS GmbH &amp; Co. KG</li>
<li>ACSONE SA/NV</li>
Expand All @@ -410,7 +422,7 @@ <h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<ul class="simple">
<li>Thomas Rehn &lt;<a class="reference external" href="mailto:thomas.rehn&#64;initos.com">thomas.rehn&#64;initos.com</a>&gt;</li>
<li>Stefan Rijnhart &lt;<a class="reference external" href="mailto:stefan&#64;therp.nl">stefan&#64;therp.nl</a>&gt;</li>
Expand All @@ -430,7 +442,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
<h2><a class="toc-backref" href="#id6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
Expand Down
42 changes: 35 additions & 7 deletions base_partner_sequence/tests/test_base_partner_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Copyright 2016 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import odoo.tests.common as common
from odoo.addons.base.tests.common import BaseCommon


class TestBasePartnerSequence(common.TransactionCase):
def setUp(self):
super(TestBasePartnerSequence, self).setUp()

self.res_partner = self.env["res.partner"]
self.partner = self.res_partner.create(
class TestBasePartnerSequence(BaseCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.res_partner = cls.env["res.partner"]
cls.partner = cls.res_partner.create(
{"name": "test1", "email": "[email protected]"}
)

Expand Down Expand Up @@ -65,3 +65,31 @@ def test_ref_change_convert_child_to_parent(self):
contact.write({"parent_id": False})
self.assertTrue(contact.ref)
self.assertFalse(contact.ref == self.partner.ref)

def test_ref_sequence_on_contact_copy_unique(self):
"""
Activate the functionality to have unique generated references
across partners.
Create a first child partner, reference should be different
Copy the child to a new one, references should be different
"""
self.env.company.partner_generated_reference_unique = True

self.partner_child = self.res_partner.create(
{
"name": "Contact 1",
"parent_id": self.partner.id,
}
)

self.assertNotEqual(self.partner_child.ref, self.partner.ref)

copy = self.partner_child.copy()
self.assertTrue(
copy.ref, "A partner with ref created by copy has a ref by default."
)
self.assertNotEqual(copy.ref, self.partner_child.ref)

self.env.company.partner_generated_reference_unique = False
copy.name = "Contact 2"
self.assertNotEqual(copy.ref, self.partner_child.ref)
15 changes: 15 additions & 0 deletions base_partner_sequence/views/res_config_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
Set Partner Reference field as read-only
</div>
</div>
<div class="o_setting_left_pane">
<field name="partner_generated_reference_unique" />
</div>
<div
class="o_setting_right_pane"
id="partner_reference_unique_settings"
>
<label for="partner_generated_reference_unique" />
<div class="text-muted">
Partner generated reference will be unique.<br />
<span
class="fa fa-warning"
/> Be aware that changing this parameter will affect only new partners.
</div>
</div>
</div>
</xpath>
</field>
Expand Down
Loading