Skip to content

Commit

Permalink
[FIX] l10n_fr_department
Browse files Browse the repository at this point in the history
handle Corsica.

closes #198
  • Loading branch information
gurneyalex authored and Alexis de Lattre committed Oct 15, 2020
1 parent af84f64 commit 5774a1a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
27 changes: 22 additions & 5 deletions l10n_fr_department/model/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ def _compute_department(self):
('code', 'in', ('FR', 'GP', 'MQ', 'GF', 'RE', 'YT'))]).ids
for partner in self:
dpt_id = False
zip = partner.zip
zipcode = partner.zip
if (
partner.country_id and
partner.country_id.id in fr_country_ids and
zip and
len(zip) == 5):
code = zip[0:2]
zipcode and
len(zipcode) == 5):
zipcode = partner.zip.strip().replace(' ', '').rjust(5, '0')
code = zipcode[0:2]
if code == '97':
code = zip[0:3]
code = zipcode[0:3]
if code == '20':
code = self._compute_department_corsica(zipcode)
dpts = rcdo.search([
('code', '=', code),
('country_id', 'in', fr_country_ids),
Expand All @@ -36,3 +39,17 @@ def _compute_department(self):
department_id = fields.Many2one(
'res.country.department', compute='_compute_department',
string='Department', readonly=True, store=True)

def _compute_department_corsica(self, zipcode):
try:
zipcode = int(zipcode)
except ValueError:
return '20'
if 20000 <= zipcode < 20200:
# Corse du Sud / 2A
code = '2A'
elif 20200 <= zipcode <= 20620:
code = '2B'
else:
code = '20'
return code
27 changes: 27 additions & 0 deletions l10n_fr_department/tests/test_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,30 @@ def test_fr_department(self):
self.assertEqual(
partner2.department_id,
self.env.ref('l10n_fr_department.res_country_department_vaucluse'))

def test_corse(self):
rpo = self.env['res.partner']
corse2A = self.env.ref(
'l10n_fr_department.res_country_department_corsedusud'
)
corse2B = self.env.ref(
'l10n_fr_department.res_country_department_hautecorse'
)
partner = rpo.create({
'name': 'name',
'street': 'street',
'zip': '20000',
'city': 'Ajaccio',
'country_id': self.env.ref('base.fr').id,
})
self.assertEqual(partner.department_id, corse2A)

partner.write({'zip': '20200', 'city': 'Bastia'})
self.assertEqual(partner.department_id, corse2B)

partner.write({'zip': '20190', 'city': 'Zigliara'})
self.assertEqual(partner.department_id, corse2A)
partner.write({'zip': '20620', 'city': 'Biguglia'})
self.assertEqual(partner.department_id, corse2B)
partner.write({'zip': '20999', 'city': 'Unknown'})
self.assertFalse(partner.department_id)

0 comments on commit 5774a1a

Please sign in to comment.