Skip to content

Commit 76e01a5

Browse files
[ADD] l10n_jp_partner_corporate_number
1 parent 9f06c6c commit 76e01a5

File tree

15 files changed

+875
-0
lines changed

15 files changed

+875
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
===========================
2+
JP Partner Corporate Number
3+
===========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:bed290b070d13a23b9cf15549ee6e845d7ce10bdae34fd7f5c08478ccc0e09a1
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--japan-lightgray.png?logo=github
20+
:target: https://github.com/OCA/l10n-japan/tree/16.0/l10n_jp_partner_corporate_number
21+
:alt: OCA/l10n-japan
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/l10n-japan-16-0/l10n-japan-16-0-l10n_jp_partner_corporate_number
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-japan&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module add a corporate registration number by Japanese government
32+
and get it information through API.
33+
34+
**Table of contents**
35+
36+
.. contents::
37+
:local:
38+
39+
Usage
40+
=====
41+
42+
Install module and set a corporate registration number in a contact.
43+
44+
Access token for gBizINFO
45+
=========================
46+
47+
Get corporate information through gBizINFO API. Prepare an access token
48+
before use the API from following web site.
49+
50+
https://info.gbiz.go.jp/api/index.html
51+
52+
Set access token in a system parameter
53+
--------------------------------------
54+
55+
Create a system parameter as ``gbizinfo_access_token`` in the system
56+
parameter setting. Set a value to the access token.
57+
58+
Known issues / Roadmap
59+
======================
60+
61+
Change the current implementation:
62+
63+
- Initial release
64+
65+
Bug Tracker
66+
===========
67+
68+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/l10n-japan/issues>`_.
69+
In case of trouble, please check there if your issue has already been reported.
70+
If you spotted it first, help us to smash it by providing a detailed and welcomed
71+
`feedback <https://github.com/OCA/l10n-japan/issues/new?body=module:%20l10n_jp_partner_corporate_number%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
72+
73+
Do not contact contributors directly about support or help with technical issues.
74+
75+
Credits
76+
=======
77+
78+
Authors
79+
-------
80+
81+
* Quartile Limited
82+
83+
Maintainers
84+
-----------
85+
86+
This module is maintained by the OCA.
87+
88+
.. image:: https://odoo-community.org/logo.png
89+
:alt: Odoo Community Association
90+
:target: https://odoo-community.org
91+
92+
OCA, or the Odoo Community Association, is a nonprofit organization whose
93+
mission is to support the collaborative development of Odoo features and
94+
promote its widespread use.
95+
96+
This module is part of the `OCA/l10n-japan <https://github.com/OCA/l10n-japan/tree/16.0/l10n_jp_partner_corporate_number>`_ project on GitHub.
97+
98+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2024 Quartile Limited
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
{
4+
"name": "JP Partner Corporate Number",
5+
"version": "16.0.1.0.0",
6+
"license": "AGPL-3",
7+
"author": "Quartile Limited, Odoo Community Association (OCA)",
8+
"website": "https://github.com/OCA/l10n-japan",
9+
"depends": ["base", "base_country_state_translatable"],
10+
"data": [
11+
"views/res_company_views.xml",
12+
"views/res_partner_views.xml",
13+
],
14+
"demo": [],
15+
"installable": True,
16+
"application": False,
17+
"auto_install": False,
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * ax_corp_reg_number
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 16.0-20221116\n"
8+
"Report-Msgid-Bugs-To: \n"
9+
"POT-Creation-Date: 2024-04-10 04:55+0000\n"
10+
"PO-Revision-Date: 2024-04-10 04:55+0000\n"
11+
"Last-Translator: \n"
12+
"Language-Team: \n"
13+
"MIME-Version: 1.0\n"
14+
"Content-Type: text/plain; charset=UTF-8\n"
15+
"Content-Transfer-Encoding: \n"
16+
"Plural-Forms: \n"
17+
18+
#. module: ax_corp_reg_number
19+
#: model:ir.model,name:ax_corp_reg_number.model_res_partner
20+
msgid "Contact"
21+
msgstr "連絡先"
22+
23+
#. module: ax_corp_reg_number
24+
#: model:ir.model.fields,field_description:ax_corp_reg_number.field_res_partner__corporate_number
25+
#: model:ir.model.fields,field_description:ax_corp_reg_number.field_res_users__corporate_number
26+
msgid "Corporate Number"
27+
msgstr "法人番号"
28+
29+
#. module: ax_corp_reg_number
30+
#: model_terms:ir.ui.view,arch_db:ax_corp_reg_number.view_partner_form_corporate_number
31+
msgid "Get corp. info."
32+
msgstr "法人情報取得"
33+
34+
#. module: ax_corp_reg_number
35+
#: model:ir.model.fields,help:ax_corp_reg_number.field_res_partner__corporate_number
36+
#: model:ir.model.fields,help:ax_corp_reg_number.field_res_users__corporate_number
37+
msgid "Japanese Corporate Registration Number"
38+
msgstr "日本の法人番号"
39+
40+
#. module: ax_corp_reg_number
41+
#: model:ir.model.constraint,message:ax_corp_reg_number.constraint_res_partner_corporate_number_unique
42+
msgid "The corporate number must be unique."
43+
msgstr "法人番号が重複しています"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import res_company
2+
from . import res_partner
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2024 Quartile Limited
2+
3+
from odoo import fields, models
4+
5+
6+
class ResCompany(models.Model):
7+
_inherit = "res.company"
8+
9+
duplicate_corp_number_behavior = fields.Selection(
10+
[("error", "Error"), ("warning", "Warning")],
11+
help="Define the behavior when a duplicate corporate number is detected: "
12+
"'Error' will block the operation, "
13+
"'Warning' will allow but notify.",
14+
)
15+
16+
error_message = fields.Text(
17+
string="Error Message for Duplicate Corp Number",
18+
default="A partner with this corporate number already exists.",
19+
help="Custom error message to display when a duplicate corporate number "
20+
"is detected and behavior is set to 'Error'.",
21+
)
22+
23+
warning_message = fields.Text(
24+
string="Warning Message for Duplicate Corp Number",
25+
default="Warning: A partner with this corporate number already exists.",
26+
help="Custom warning message to display when a duplicate corporate number "
27+
"is detected and behavior is set to 'Warning'.",
28+
)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Copyright 2024 Quartile Limited
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
import logging
5+
import re
6+
7+
import requests
8+
9+
from odoo import _, api, fields, models
10+
from odoo.exceptions import ValidationError
11+
12+
_logger = logging.getLogger(__name__)
13+
14+
15+
class ResPartner(models.Model):
16+
_inherit = "res.partner"
17+
_timeout = 10
18+
19+
corporate_number = fields.Char(help="Japanese Corporate Registration Number")
20+
duplicate_corp_number_warning_message = fields.Text(
21+
compute="_compute_duplicate_corp_number_warning_message"
22+
)
23+
24+
@api.depends("corporate_number")
25+
def _compute_duplicate_corp_number_warning_message(self):
26+
for rec in self:
27+
rec.duplicate_corp_number_warning_message = False
28+
if rec.corporate_number:
29+
domain = [("corporate_number", "=", rec.corporate_number)]
30+
if (
31+
rec.id
32+
): # Exclude the current record from the search to avoid self-detection
33+
domain.append(("id", "!=", rec.id))
34+
duplicates = self.env["res.partner"].search(domain, limit=1)
35+
company = self.env.company
36+
if duplicates and company.duplicate_corp_number_behavior == "warning":
37+
rec.duplicate_corp_number_warning_message = company.warning_message
38+
39+
@api.constrains("corporate_number")
40+
def _check_corporate_number(self):
41+
for record in self:
42+
duplicate_rec = self.env["res.partner"].search(
43+
[
44+
("corporate_number", "=", record.corporate_number),
45+
("id", "!=", record.id),
46+
],
47+
limit=1,
48+
)
49+
company = self.env.company
50+
if duplicate_rec and company.duplicate_corp_number_behavior == "error":
51+
raise ValidationError(_(company.error_message))
52+
if record.corporate_number and not re.match(
53+
r"^\d{13}$", record.corporate_number
54+
):
55+
raise ValidationError(_("Corporate Number must be 13 digits."))
56+
57+
def fetch_corporate_info(self):
58+
self.ensure_one()
59+
if self.corporate_number:
60+
data = self._fetch_gbizinfo(self.corporate_number)
61+
hojin_info = data.get("hojin-infos", [{}])[0] # get a first element
62+
address_vals = self._split_address(hojin_info.get("location"))
63+
partner_vals = {
64+
"name": hojin_info.get("name"),
65+
"street": hojin_info.get("location"),
66+
**address_vals,
67+
}
68+
self.write(partner_vals)
69+
70+
def _split_address(self, location):
71+
# in Japan
72+
country_id = self.env.ref("base.jp").id
73+
# remove Japanese ZIP code
74+
address_without_zip = re.sub(r"\d{3}-\d{4}", "", location).strip()
75+
pattern = """(...??[都道府県])((?:旭川|伊達|石狩|盛岡|奥州|田村|南相馬|那須塩原|東村山|武蔵村山|羽村|十日町|上越|
76+
富山|野々市|大町|蒲郡|四日市|姫路|大和郡山|廿日市|下松|岩国|田川|大村|宮古|富良野|別府|佐伯|黒部|小諸|塩尻|玉野|
77+
周南)市|(?:余市|高市|[^市]{2,3}?)郡(?:玉村|大町|.{1,5}?)[町村]|(?:.{1,4}市)?[^町]{1,4}?区|.{1,7}?[市町村])(.+)"""
78+
result = re.match(pattern, address_without_zip)
79+
if result:
80+
state_name = result.group(1)
81+
city = result.group(2)
82+
street = result.group(3)
83+
# Stateの取得(存在しない場合はNone)
84+
# 都道府県を日本語化するモジュール(base_country_state_translatable)依存
85+
# 都道府県名は日本語翻訳済とする
86+
state_id = (
87+
self.env["res.country.state"]
88+
.search(
89+
[("name", "like", state_name), ("country_id", "=", country_id)], limit=1
90+
)
91+
.id
92+
)
93+
return {
94+
"country_id": country_id,
95+
"state_id": state_id,
96+
"city": city,
97+
"street": street,
98+
}
99+
100+
def _fetch_gbizinfo(self, corporate_number):
101+
# gBizINFOのエンドポイントURL + 法人番号
102+
endpoint = f"https://info.gbiz.go.jp/hojin/v1/hojin/{corporate_number}"
103+
access_token = (
104+
self.env["ir.config_parameter"].sudo().get_param("gbizinfo_access_token")
105+
)
106+
headers = {
107+
"Accept": "application/json",
108+
"X-hojinInfo-api-token": access_token,
109+
}
110+
111+
# APIリクエストを実行
112+
try:
113+
response = requests.get(
114+
endpoint, headers=headers, timeout=(self._timeout, self._timeout)
115+
)
116+
if response.status_code == 200:
117+
# 成功した場合、JSONデータを抽出
118+
return response.json()
119+
else:
120+
# エラーがあった場合、問題をログに記録
121+
error_msg = (
122+
f"Failed to fetch data from gBizINFO: {response.status_code}"
123+
)
124+
_logger.error(error_msg)
125+
return None
126+
except requests.Timeout:
127+
_logger.error("Request timed out.")
128+
return None
129+
except requests.RequestException as e:
130+
_logger.error(f"An error occurred: {e}")
131+
return None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module add a corporate registration number by Japanese government and get it information through API.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Change the current implementation:
2+
3+
- Initial release
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Install module and set a corporate registration number in a contact.
2+
3+
# Access token for gBizINFO
4+
5+
Get corporate information through gBizINFO API.
6+
Prepare an access token before use the API from following web site.
7+
8+
https://info.gbiz.go.jp/api/index.html
9+
10+
## Set access token in a system parameter
11+
12+
Create a system parameter as `gbizinfo_access_token` in the system parameter setting.
13+
Set a value to the access token.

0 commit comments

Comments
 (0)