Browse Source

[IMP] Check that the zip code matches the one of the zip_id

Also:
* Reorder checks by geo level: country, state, city, zip
14.0
Ivàn Todorovich 3 years ago
parent
commit
a27cede623
  1. 20
      base_location/models/res_partner.py
  2. 4
      base_location/tests/test_base_location.py

20
base_location/models/res_partner.py

@ -90,28 +90,30 @@ class ResPartner(models.Model):
if state and record.state_id != state:
record.state_id = record.zip_id.city_id.state_id
@api.constrains("zip_id", "country_id", "city_id", "state_id")
@api.constrains("zip_id", "country_id", "city_id", "state_id", "zip")
def _check_zip(self):
if self.env.context.get("skip_check_zip"):
return
for rec in self:
if not rec.zip_id:
continue
if rec.zip_id.city_id.state_id != rec.state_id:
if rec.zip_id.city_id.country_id != rec.country_id:
raise ValidationError(
_("The state of the partner %s differs from that in " "location %s")
_("The country of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.city_id.country_id != rec.country_id:
if rec.zip_id.city_id.state_id != rec.state_id:
raise ValidationError(
_(
"The country of the partner %s differs from that in "
"location %s"
)
_("The state of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.city_id != rec.city_id:
raise ValidationError(
_("The city of partner %s differs from that in " "location %s")
_("The city of partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.name != rec.zip:
raise ValidationError(
_("The zip of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)

4
base_location/tests/test_base_location.py

@ -122,6 +122,10 @@ class TestBaseLocation(common.SavepointCase):
self.partner_obj.create(
{"name": "P1", "zip_id": self.barcelona.id, "city_id": False}
)
with self.assertRaises(ValidationError):
self.partner_obj.create(
{"name": "P1", "zip_id": self.barcelona.id, "zip": False}
)
def test_writing_company(self):
self.company.zip_id = self.barcelona

Loading…
Cancel
Save