From 3dbfe0763db405e78b7f6f9dbecd4c3b3b2aa9fb Mon Sep 17 00:00:00 2001 From: Aungkokolin1997 Date: Tue, 16 Apr 2024 03:10:58 +0000 Subject: [PATCH] [ADD] l10n_jp_partner_corporate_number --- l10n_jp_partner_corporate_number/README.rst | 98 ++++ l10n_jp_partner_corporate_number/__init__.py | 1 + .../__manifest__.py | 18 + .../i18n/ja_JP.po | 108 +++++ .../models/__init__.py | 2 + .../models/res_company.py | 28 ++ .../models/res_partner.py | 131 ++++++ .../readme/DESCRIPTION.md | 1 + .../readme/ROADMAP.md | 3 + .../readme/USAGE.md | 13 + .../static/description/index.html | 443 ++++++++++++++++++ .../views/res_company_views.xml | 27 ++ .../views/res_partner_views.xml | 60 +++ .../addons/l10n_jp_partner_corporate_number | 1 + .../l10n_jp_partner_corporate_number/setup.py | 6 + 15 files changed, 940 insertions(+) create mode 100644 l10n_jp_partner_corporate_number/README.rst create mode 100644 l10n_jp_partner_corporate_number/__init__.py create mode 100644 l10n_jp_partner_corporate_number/__manifest__.py create mode 100644 l10n_jp_partner_corporate_number/i18n/ja_JP.po create mode 100644 l10n_jp_partner_corporate_number/models/__init__.py create mode 100644 l10n_jp_partner_corporate_number/models/res_company.py create mode 100644 l10n_jp_partner_corporate_number/models/res_partner.py create mode 100644 l10n_jp_partner_corporate_number/readme/DESCRIPTION.md create mode 100644 l10n_jp_partner_corporate_number/readme/ROADMAP.md create mode 100644 l10n_jp_partner_corporate_number/readme/USAGE.md create mode 100644 l10n_jp_partner_corporate_number/static/description/index.html create mode 100644 l10n_jp_partner_corporate_number/views/res_company_views.xml create mode 100644 l10n_jp_partner_corporate_number/views/res_partner_views.xml create mode 120000 setup/l10n_jp_partner_corporate_number/odoo/addons/l10n_jp_partner_corporate_number create mode 100644 setup/l10n_jp_partner_corporate_number/setup.py diff --git a/l10n_jp_partner_corporate_number/README.rst b/l10n_jp_partner_corporate_number/README.rst new file mode 100644 index 0000000..a22cfda --- /dev/null +++ b/l10n_jp_partner_corporate_number/README.rst @@ -0,0 +1,98 @@ +=========================== +JP Partner Corporate Number +=========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:bed290b070d13a23b9cf15549ee6e845d7ce10bdae34fd7f5c08478ccc0e09a1 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--japan-lightgray.png?logo=github + :target: https://github.com/OCA/l10n-japan/tree/16.0/l10n_jp_partner_corporate_number + :alt: OCA/l10n-japan +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/l10n-japan-16-0/l10n-japan-16-0-l10n_jp_partner_corporate_number + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-japan&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module add a corporate registration number by Japanese government +and get it information through API. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Install module and set a corporate registration number in a contact. + +Access token for gBizINFO +========================= + +Get corporate information through gBizINFO API. Prepare an access token +before use the API from following web site. + +https://info.gbiz.go.jp/api/index.html + +Set access token in a system parameter +-------------------------------------- + +Create a system parameter as ``gbizinfo_access_token`` in the system +parameter setting. Set a value to the access token. + +Known issues / Roadmap +====================== + +Change the current implementation: + +- Initial release + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Quartile Limited + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/l10n-japan `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/l10n_jp_partner_corporate_number/__init__.py b/l10n_jp_partner_corporate_number/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/l10n_jp_partner_corporate_number/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/l10n_jp_partner_corporate_number/__manifest__.py b/l10n_jp_partner_corporate_number/__manifest__.py new file mode 100644 index 0000000..fb96f2c --- /dev/null +++ b/l10n_jp_partner_corporate_number/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "JP Partner Corporate Number", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "Quartile Limited, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/l10n-japan", + "depends": ["base", "base_country_state_translatable"], + "data": [ + "views/res_company_views.xml", + "views/res_partner_views.xml", + ], + "demo": [], + "installable": True, + "application": False, + "auto_install": False, +} diff --git a/l10n_jp_partner_corporate_number/i18n/ja_JP.po b/l10n_jp_partner_corporate_number/i18n/ja_JP.po new file mode 100644 index 0000000..c6ff398 --- /dev/null +++ b/l10n_jp_partner_corporate_number/i18n/ja_JP.po @@ -0,0 +1,108 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * l10n_jp_partner_corporate_number +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-04-16 03:20+0000\n" +"PO-Revision-Date: 2024-04-16 03:20+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model,name:l10n_jp_partner_corporate_number.model_res_company +msgid "Companies" +msgstr "会社" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model,name:l10n_jp_partner_corporate_number.model_res_partner +msgid "Contact" +msgstr "連絡先" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_partner__corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_users__corporate_number +#: model_terms:ir.ui.view,arch_db:l10n_jp_partner_corporate_number.view_res_partner_search_inherit +msgid "Corporate Number" +msgstr "法人番号" + +#. module: l10n_jp_partner_corporate_number +#. odoo-python +#: code:addons/l10n_jp_partner_corporate_number/models/res_partner.py:0 +#, python-format +msgid "Corporate Number must be 13 digits." +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,help:l10n_jp_partner_corporate_number.field_res_company__error_message +msgid "" +"Custom error message to display when a duplicate corporate number is " +"detected and behavior is set to 'Error'." +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,help:l10n_jp_partner_corporate_number.field_res_company__warning_message +msgid "" +"Custom warning message to display when a duplicate corporate number is " +"detected and behavior is set to 'Warning'." +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,help:l10n_jp_partner_corporate_number.field_res_company__duplicate_corp_number_behavior +msgid "" +"Define the behavior when a duplicate corporate number is detected: 'Error' " +"will block the operation, 'Warning' will allow but notify." +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_company__duplicate_corp_number_behavior +msgid "Duplicate Corp Number Behavior" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model_terms:ir.ui.view,arch_db:l10n_jp_partner_corporate_number.view_company_form_inherit +msgid "Duplicate Corp Number Settings" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_partner__duplicate_corp_number_warning_message +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_users__duplicate_corp_number_warning_message +msgid "Duplicate Corp Number Warning Message" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields.selection,name:l10n_jp_partner_corporate_number.selection__res_company__duplicate_corp_number_behavior__error +msgid "Error" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_company__error_message +msgid "Error Message for Duplicate Corp Number" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model_terms:ir.ui.view,arch_db:l10n_jp_partner_corporate_number.view_partner_form_corporate_number +msgid "Get corp. info." +msgstr "法人情報取得" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,help:l10n_jp_partner_corporate_number.field_res_partner__corporate_number +#: model:ir.model.fields,help:l10n_jp_partner_corporate_number.field_res_users__corporate_number +msgid "Japanese Corporate Registration Number" +msgstr "日本の法人番号" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields.selection,name:l10n_jp_partner_corporate_number.selection__res_company__duplicate_corp_number_behavior__warning +msgid "Warning" +msgstr "" + +#. module: l10n_jp_partner_corporate_number +#: model:ir.model.fields,field_description:l10n_jp_partner_corporate_number.field_res_company__warning_message +msgid "Warning Message for Duplicate Corp Number" +msgstr "" diff --git a/l10n_jp_partner_corporate_number/models/__init__.py b/l10n_jp_partner_corporate_number/models/__init__.py new file mode 100644 index 0000000..b067a53 --- /dev/null +++ b/l10n_jp_partner_corporate_number/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_company +from . import res_partner diff --git a/l10n_jp_partner_corporate_number/models/res_company.py b/l10n_jp_partner_corporate_number/models/res_company.py new file mode 100644 index 0000000..6f69229 --- /dev/null +++ b/l10n_jp_partner_corporate_number/models/res_company.py @@ -0,0 +1,28 @@ +# Copyright 2024 Quartile Limited + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + duplicate_corp_number_behavior = fields.Selection( + [("error", "Error"), ("warning", "Warning")], + help="Define the behavior when a duplicate corporate number is detected: " + "'Error' will block the operation, " + "'Warning' will allow but notify.", + ) + + error_message = fields.Text( + string="Error Message for Duplicate Corp Number", + default="A partner with this corporate number already exists.", + help="Custom error message to display when a duplicate corporate number " + "is detected and behavior is set to 'Error'.", + ) + + warning_message = fields.Text( + string="Warning Message for Duplicate Corp Number", + default="Warning: A partner with this corporate number already exists.", + help="Custom warning message to display when a duplicate corporate number " + "is detected and behavior is set to 'Warning'.", + ) diff --git a/l10n_jp_partner_corporate_number/models/res_partner.py b/l10n_jp_partner_corporate_number/models/res_partner.py new file mode 100644 index 0000000..003fcfc --- /dev/null +++ b/l10n_jp_partner_corporate_number/models/res_partner.py @@ -0,0 +1,131 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import logging +import re + +import requests + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + +_logger = logging.getLogger(__name__) + + +class ResPartner(models.Model): + _inherit = "res.partner" + _timeout = 10 + + corporate_number = fields.Char(help="Japanese Corporate Registration Number") + duplicate_corp_number_warning_message = fields.Text( + compute="_compute_duplicate_corp_number_warning_message" + ) + + @api.depends("corporate_number") + def _compute_duplicate_corp_number_warning_message(self): + for rec in self: + rec.duplicate_corp_number_warning_message = False + if rec.corporate_number: + domain = [("corporate_number", "=", rec.corporate_number)] + if ( + rec.id + ): # Exclude the current record from the search to avoid self-detection + domain.append(("id", "!=", rec.id)) + duplicates = self.env["res.partner"].search(domain, limit=1) + company = self.env.company + if duplicates and company.duplicate_corp_number_behavior == "warning": + rec.duplicate_corp_number_warning_message = company.warning_message + + @api.constrains("corporate_number") + def _check_corporate_number(self): + for record in self: + duplicate_rec = self.env["res.partner"].search( + [ + ("corporate_number", "=", record.corporate_number), + ("id", "!=", record.id), + ], + limit=1, + ) + company = self.env.company + if duplicate_rec and company.duplicate_corp_number_behavior == "error": + raise ValidationError(_(company.error_message)) + if record.corporate_number and not re.match( + r"^\d{13}$", record.corporate_number + ): + raise ValidationError(_("Corporate Number must be 13 digits.")) + + def fetch_corporate_info(self): + self.ensure_one() + if self.corporate_number: + data = self._fetch_gbizinfo(self.corporate_number) + hojin_info = data.get("hojin-infos", [{}])[0] # get a first element + address_vals = self._split_address(hojin_info.get("location")) + partner_vals = { + "name": hojin_info.get("name"), + "street": hojin_info.get("location"), + **address_vals, + } + self.write(partner_vals) + + def _split_address(self, location): + # in Japan + country_id = self.env.ref("base.jp").id + # remove Japanese ZIP code + address_without_zip = re.sub(r"\d{3}-\d{4}", "", location).strip() + pattern = """(...??[都道府県])((?:旭川|伊達|石狩|盛岡|奥州|田村|南相馬|那須塩原|東村山|武蔵村山|羽村|十日町|上越| +富山|野々市|大町|蒲郡|四日市|姫路|大和郡山|廿日市|下松|岩国|田川|大村|宮古|富良野|別府|佐伯|黒部|小諸|塩尻|玉野| +周南)市|(?:余市|高市|[^市]{2,3}?)郡(?:玉村|大町|.{1,5}?)[町村]|(?:.{1,4}市)?[^町]{1,4}?区|.{1,7}?[市町村])(.+)""" + result = re.match(pattern, address_without_zip) + if result: + state_name = result.group(1) + city = result.group(2) + street = result.group(3) + # Stateの取得(存在しない場合はNone) + # 都道府県を日本語化するモジュール(base_country_state_translatable)依存 + # 都道府県名は日本語翻訳済とする + state_id = ( + self.env["res.country.state"] + .search( + [("name", "like", state_name), ("country_id", "=", country_id)], limit=1 + ) + .id + ) + return { + "country_id": country_id, + "state_id": state_id, + "city": city, + "street": street, + } + + def _fetch_gbizinfo(self, corporate_number): + # gBizINFOのエンドポイントURL + 法人番号 + endpoint = f"https://info.gbiz.go.jp/hojin/v1/hojin/{corporate_number}" + access_token = ( + self.env["ir.config_parameter"].sudo().get_param("gbizinfo_access_token") + ) + headers = { + "Accept": "application/json", + "X-hojinInfo-api-token": access_token, + } + + # APIリクエストを実行 + try: + response = requests.get( + endpoint, headers=headers, timeout=(self._timeout, self._timeout) + ) + if response.status_code == 200: + # 成功した場合、JSONデータを抽出 + return response.json() + else: + # エラーがあった場合、問題をログに記録 + error_msg = ( + f"Failed to fetch data from gBizINFO: {response.status_code}" + ) + _logger.error(error_msg) + return None + except requests.Timeout: + _logger.error("Request timed out.") + return None + except requests.RequestException as e: + _logger.error(f"An error occurred: {e}") + return None diff --git a/l10n_jp_partner_corporate_number/readme/DESCRIPTION.md b/l10n_jp_partner_corporate_number/readme/DESCRIPTION.md new file mode 100644 index 0000000..4dbd4d5 --- /dev/null +++ b/l10n_jp_partner_corporate_number/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module add a corporate registration number by Japanese government and get it information through API. \ No newline at end of file diff --git a/l10n_jp_partner_corporate_number/readme/ROADMAP.md b/l10n_jp_partner_corporate_number/readme/ROADMAP.md new file mode 100644 index 0000000..3ddd27c --- /dev/null +++ b/l10n_jp_partner_corporate_number/readme/ROADMAP.md @@ -0,0 +1,3 @@ +Change the current implementation: + +- Initial release diff --git a/l10n_jp_partner_corporate_number/readme/USAGE.md b/l10n_jp_partner_corporate_number/readme/USAGE.md new file mode 100644 index 0000000..893ca35 --- /dev/null +++ b/l10n_jp_partner_corporate_number/readme/USAGE.md @@ -0,0 +1,13 @@ +Install module and set a corporate registration number in a contact. + +# Access token for gBizINFO + +Get corporate information through gBizINFO API. +Prepare an access token before use the API from following web site. + +https://info.gbiz.go.jp/api/index.html + +## Set access token in a system parameter + +Create a system parameter as `gbizinfo_access_token` in the system parameter setting. +Set a value to the access token. \ No newline at end of file diff --git a/l10n_jp_partner_corporate_number/static/description/index.html b/l10n_jp_partner_corporate_number/static/description/index.html new file mode 100644 index 0000000..4649a46 --- /dev/null +++ b/l10n_jp_partner_corporate_number/static/description/index.html @@ -0,0 +1,443 @@ + + + + + + +JP Partner Corporate Number + + + +
+

JP Partner Corporate Number

+ + +

Beta License: AGPL-3 OCA/l10n-japan Translate me on Weblate Try me on Runboat

+

This module add a corporate registration number by Japanese government +and get it information through API.

+

Table of contents

+ +
+

Usage

+

Install module and set a corporate registration number in a contact.

+
+
+

Access token for gBizINFO

+

Get corporate information through gBizINFO API. Prepare an access token +before use the API from following web site.

+

https://info.gbiz.go.jp/api/index.html

+
+

Set access token in a system parameter

+

Create a system parameter as gbizinfo_access_token in the system +parameter setting. Set a value to the access token.

+
+
+
+

Known issues / Roadmap

+

Change the current implementation:

+
    +
  • Initial release
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +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 +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/l10n-japan project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/l10n_jp_partner_corporate_number/views/res_company_views.xml b/l10n_jp_partner_corporate_number/views/res_company_views.xml new file mode 100644 index 0000000..cdc64fd --- /dev/null +++ b/l10n_jp_partner_corporate_number/views/res_company_views.xml @@ -0,0 +1,27 @@ + + + res.company.form.inherit + res.company + + + + + + + + + + + + + + diff --git a/l10n_jp_partner_corporate_number/views/res_partner_views.xml b/l10n_jp_partner_corporate_number/views/res_partner_views.xml new file mode 100644 index 0000000..deb88d0 --- /dev/null +++ b/l10n_jp_partner_corporate_number/views/res_partner_views.xml @@ -0,0 +1,60 @@ + + + res.partner.search.corpnum.inherit + res.partner + + + + + + + + + + res.partner.list.corpnum.inherit + res.partner + + + + + + + + + res.partner.form.corporate.number + res.partner + + + + + + +