Skip to content

Commit

Permalink
[IMP] partner_company_type: allow to set up countries and states, dis…
Browse files Browse the repository at this point in the history
…play shortcut in name
  • Loading branch information
OriolMForgeFlow committed Nov 17, 2023
1 parent b944b0d commit 5350466
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 11 deletions.
3 changes: 2 additions & 1 deletion partner_company_type/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Partner Company Type
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:fa1211014d9e73379ebfffe2725e096a7a7a85e59920a677fd13393fd66ccb43
!! source digest: sha256:4104bf3343650a9334425baaa43c49ec1e449445415e9f3f87bf4bd30dff0224
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -62,6 +62,7 @@ Contributors
* Denis Roussel <[email protected]> (https://acsone.eu)
* Gilles Meyomesse <[email protected]> (https://acsone.eu)
* Kitti U. <[email protected]> (http://ecosoft.co.th)
* Oriol Miranda <[email protected]> (https://forgeflow.com)

Other credits
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion partner_company_type/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Partner Company Type",
"summary": "Adds a company type to partner that are companies",
"version": "16.0.1.0.0",
"version": "16.0.1.0.1",
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/partner-contact",
Expand Down
11 changes: 11 additions & 0 deletions partner_company_type/migrations/16.0.1.0.1/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2023 ForgeFlow S.L. <http://www.forgeflow.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tools import drop_constraint


def migrate(cr, version):

drop_constraint(
cr, "res_partner_company_type", "res_partner_company_type_name_uniq"
)
33 changes: 32 additions & 1 deletion partner_company_type/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2017-2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


class ResPartner(models.Model):
Expand All @@ -10,3 +11,33 @@ class ResPartner(models.Model):
partner_company_type_id = fields.Many2one(
comodel_name="res.partner.company.type", string="Legal Form"
)

@api.constrains(
"country_id",
"state_id",
"partner_company_type_id",
)
def _check_partner_company_type_country_state(self):
for rec in self:
if not rec.partner_company_type_id:
return
if (
rec.partner_company_type_id.country_ids
and rec.country_id not in rec.partner_company_type_id.country_ids
):
raise UserError(
_(
"You must select a Legal Form from the country "
"of the partner address."
)
)
if (
rec.partner_company_type_id.state_ids
and rec.state_id not in rec.partner_company_type_id.state_ids
):
raise UserError(
_(
"You must select a Legal Form from the state "
"of the partner address."
)
)
40 changes: 36 additions & 4 deletions partner_company_type/models/res_partner_company_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright 2017-2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class ResPartnerCompanyType(models.Model):
Expand All @@ -12,6 +13,37 @@ class ResPartnerCompanyType(models.Model):
name = fields.Char(string="Title", required=True, translate=True)
shortcut = fields.Char(string="Abbreviation", translate=True)

_sql_constraints = [
("name_uniq", "unique (name)", "Partner Company Type already exists!")
]
country_ids = fields.Many2many("res.country", string="Countries")
state_ids = fields.Many2many(
"res.country.state",
string="States",
domain="[('country_id', 'in', country_ids)]",
)

def name_get(self):
res = []

Check warning on line 24 in partner_company_type/models/res_partner_company_type.py

View check run for this annotation

Codecov / codecov/patch

partner_company_type/models/res_partner_company_type.py#L24

Added line #L24 was not covered by tests
for record in self:
name = record.name

Check warning on line 26 in partner_company_type/models/res_partner_company_type.py

View check run for this annotation

Codecov / codecov/patch

partner_company_type/models/res_partner_company_type.py#L26

Added line #L26 was not covered by tests
if record.shortcut:
name = name + " - " + record.shortcut
res.append((record.id, name))
return res

Check warning on line 30 in partner_company_type/models/res_partner_company_type.py

View check run for this annotation

Codecov / codecov/patch

partner_company_type/models/res_partner_company_type.py#L28-L30

Added lines #L28 - L30 were not covered by tests

@api.constrains("name", "shortcut")
def _check_unique_name_shortcut(self):
for rec in self:
if (
self.env["res.partner.company.type"].search_count(
[
("name", "=", rec.name),
("shortcut", "=", rec.shortcut),
]
)
> 1
):
if rec.shortcut:
raise ValidationError(
f"Name ({rec.name}) already exists with this shortcut ({rec.shortcut})!"
)
else:
raise ValidationError(f"Name ({rec.name}) already exists!")

Check warning on line 49 in partner_company_type/models/res_partner_company_type.py

View check run for this annotation

Codecov / codecov/patch

partner_company_type/models/res_partner_company_type.py#L49

Added line #L49 was not covered by tests
1 change: 1 addition & 0 deletions partner_company_type/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Denis Roussel <[email protected]> (https://acsone.eu)
* Gilles Meyomesse <[email protected]> (https://acsone.eu)
* Kitti U. <[email protected]> (http://ecosoft.co.th)
* Oriol Miranda <[email protected]> (https://forgeflow.com)
3 changes: 2 additions & 1 deletion partner_company_type/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Partner Company Type</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:fa1211014d9e73379ebfffe2725e096a7a7a85e59920a677fd13393fd66ccb43
!! source digest: sha256:4104bf3343650a9334425baaa43c49ec1e449445415e9f3f87bf4bd30dff0224
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.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/partner_company_type"><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-partner_company_type"><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>Adds a title to partner that are companies.
Expand Down Expand Up @@ -408,6 +408,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<li>Denis Roussel &lt;<a class="reference external" href="mailto:denis.roussel&#64;acsone.eu">denis.roussel&#64;acsone.eu</a>&gt; (<a class="reference external" href="https://acsone.eu">https://acsone.eu</a>)</li>
<li>Gilles Meyomesse &lt;<a class="reference external" href="mailto:gilles.meyomesse&#64;acsone.eu">gilles.meyomesse&#64;acsone.eu</a>&gt; (<a class="reference external" href="https://acsone.eu">https://acsone.eu</a>)</li>
<li>Kitti U. &lt;<a class="reference external" href="mailto:kittiu&#64;ecosoft.co.th">kittiu&#64;ecosoft.co.th</a>&gt; (<a class="reference external" href="http://ecosoft.co.th">http://ecosoft.co.th</a>)</li>
<li>Oriol Miranda &lt;<a class="reference external" href="mailto:oriol.miranda&#64;forgeflow.com">oriol.miranda&#64;forgeflow.com</a>&gt; (<a class="reference external" href="https://forgeflow.com">https://forgeflow.com</a>)</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand Down
91 changes: 88 additions & 3 deletions partner_company_type/tests/test_res_partner_company_type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2017-2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from psycopg2 import IntegrityError

from odoo import tools
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import TransactionCase


Expand All @@ -15,8 +15,93 @@ def setUpClass(cls):
"partner_company_type.res_partner_company_type_sa"
)

cls.company_type_model = cls.env["res.partner.company.type"]

cls.country_es = cls.env.ref("base.es")
cls.country_us = cls.env.ref("base.us")
cls.state_california = cls.env.ref("base.state_us_5")
cls.state_colorado = cls.env.ref("base.state_us_6")

cls.partner_id = cls.env.ref("base.res_partner_2")

cls.company_type_es = cls.company_type_model.create(
{
"name": "Company Type Spain",
"shortcut": "ESP",
"country_ids": cls.country_es.ids,
}
)

cls.company_type_us = cls.company_type_model.create(
{
"name": "Company Type EEUU",
"shortcut": "USA",
"country_ids": cls.country_us.ids,
}
)

cls.company_type_us_ca = cls.company_type_model.create(
{
"name": "Company Type EEUU California",
"shortcut": "CA",
"country_ids": cls.country_us.ids,
"state_ids": cls.state_california.ids,
}
)
cls.company_type_us_co = cls.company_type_model.create(
{
"name": "Company Type EEUU Colorado",
"shortcut": "CO",
"country_ids": cls.country_us.ids,
"state_ids": cls.state_colorado.ids,
}
)

def test_00_duplicate(self):
# Test Duplicate Company type

with self.assertRaises(IntegrityError), tools.mute_logger("odoo.sql_db"):
self.company_type.create(dict(name=self.company_type.name))
with self.assertRaises(ValidationError), tools.mute_logger("odoo.sql_db"):
self.company_type.create(
dict(name=self.company_type.name, shortcut=self.company_type.shortcut)
)

def test_01_partner_company_type_country(self):
"""Test to set up a partner_company_type_id according to the country of the partner_id,
check that raises an error if they don't match."""

self.partner_id.write(
{
"country_id": self.country_es.id,
"partner_company_type_id": self.company_type_es.id,
}
)
self.assertEqual(self.partner_id.partner_company_type_id, self.company_type_es)

with self.assertRaises(UserError):
self.partner_id.write(
{
"partner_company_type_id": self.company_type_us.id,
}
)

def test_02_partner_company_type_state(self):
"""Test to set up an entity_legal_form_id according to the state of the partner_id,
check that raises an error if they don't match."""

self.partner_id.write(
{
"country_id": self.country_us.id,
"state_id": self.state_california.id,
"partner_company_type_id": self.company_type_us_ca.id,
}
)
self.assertEqual(
self.partner_id.partner_company_type_id, self.company_type_us_ca
)

with self.assertRaises(UserError):
self.partner_id.write(
{
"partner_company_type_id": self.company_type_us_co.id,
}
)
1 change: 1 addition & 0 deletions partner_company_type/views/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
name="partner_company_type_id"
options='{"no_open": True}'
attrs="{'invisible': [('is_company', '=', False)]}"
domain="[ '|', ('country_ids', '=', False), ('country_ids', 'in', [country_id]), '|', ('state_ids', '=', False), ('state_ids', 'in', [state_id])]"
/>
</field>
</field>
Expand Down
4 changes: 4 additions & 0 deletions partner_company_type/views/res_partner_company_type.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<group col="4">
<field name="name" />
<field name="shortcut" />
<field name="country_ids" />
<field name="state_ids" />
</group>
</form>
</field>
Expand All @@ -36,6 +38,8 @@
<tree>
<field name="name" />
<field name="shortcut" />
<field name="country_ids" widget="many2many_tags" />
<field name="state_ids" widget="many2many_tags" />
</tree>
</field>
</record>
Expand Down

0 comments on commit 5350466

Please sign in to comment.