diff --git a/partner_usps_address_validation/README.rst b/partner_usps_address_validation/README.rst new file mode 100644 index 00000000..b382e4d9 --- /dev/null +++ b/partner_usps_address_validation/README.rst @@ -0,0 +1,103 @@ +======================= +USPS Address Validation +======================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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--usa-lightgray.png?logo=github + :target: https://github.com/OCA/l10n-usa/tree/16.0/partner_usps_address_validation + :alt: OCA/l10n-usa +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/l10n-usa-16-0/l10n-usa-16-0-partner_usps_address_validation + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/l10n-usa&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds a tool to the Contacts page which validates the contact's address. +Simply click the 'Validate' button, and the address of the contact will be compared to the USPS address database. +The result will be a cleaned address, including the full 9 digit zipcode, in the exact format recognized by USPS. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +In the *General Settings* menu, enter your USPS API credentials under USPS Address Validation Settings + + +Usage +===== + +On a contact, enter as much of the address as possible to ensure an accurate match. Required are address, and either city/state or zipcode. Click the Validate button to bring up the wizard. User entered text appears on the left as the original address, and the right displays the USPS cleansed address, which may be edited as needed. Either accept or cancel the changes to return to the contact page. + + +Known issues / Roadmap +====================== + +* There are no knows issues at this time +* USPS will always return the address in all caps. CamelCase support is not planned for future releases. + + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Open Source Integrators + +Contributors +~~~~~~~~~~~~ + +* Craig Kolobow +* Patrick Wilson +* Urvisha Desai + +Other credits +~~~~~~~~~~~~~ + +**Financial support** + +* Open Source Integrators + +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-usa `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_usps_address_validation/__init__.py b/partner_usps_address_validation/__init__.py new file mode 100644 index 00000000..4cb4d0b1 --- /dev/null +++ b/partner_usps_address_validation/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2022 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models +from . import wizard diff --git a/partner_usps_address_validation/__manifest__.py b/partner_usps_address_validation/__manifest__.py new file mode 100644 index 00000000..ecca6159 --- /dev/null +++ b/partner_usps_address_validation/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2022 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "USPS Address Validation", + "category": "Contact", + "version": "16.0.1.0.0", + "summary": """Utilize the USPS open API for address validation""", + "depends": ["contacts"], + "external_dependencies": {"python": ["xmltodict", "requests"]}, + "data": [ + "security/ir.model.access.csv", + "wizard/usps_address_validation.xml", + "views/res_partner.xml", + "views/config.xml", + ], + "author": "Open Source Integrators, Odoo Community Association (OCA)", + "maintainer": ["ckolobow"], + "website": "https://github.com/OCA/l10n-usa", + "demo": [], + "installable": True, + "application": True, + "auto_install": False, + "license": "AGPL-3", +} diff --git a/partner_usps_address_validation/models/__init__.py b/partner_usps_address_validation/models/__init__.py new file mode 100644 index 00000000..fd5a36d9 --- /dev/null +++ b/partner_usps_address_validation/models/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2022 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import res_partner +from . import usps_credential_configuration diff --git a/partner_usps_address_validation/models/res_partner.py b/partner_usps_address_validation/models/res_partner.py new file mode 100644 index 00000000..6640aa9d --- /dev/null +++ b/partner_usps_address_validation/models/res_partner.py @@ -0,0 +1,141 @@ +# Copyright 2022 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging + +import requests +import xmltodict + +from odoo import _, fields, models +from odoo.exceptions import ValidationError + +_logger = logging.getLogger(__name__) + + +class USPSAddressPartner(models.Model): + _inherit = "res.partner" + + usps_date_validation = fields.Date( + "Last Validation Date", + readonly=True, + copy=False, + help="The date the address was last validated by USPS and accepted", + ) + + def button_usps_address_validation(self): + view_ref = self.env.ref( + "partner_usps_address_validation.usps_address_validation_view_form" + ) + ctx = self.env.context.copy() + ctx.update({"active_ids": self.ids, "active_id": self.id}) + return { + "type": "ir.actions.act_window", + "name": "USPS Address Validation", + "binding_view_types": "form", + "view_mode": "form", + "view_id": view_ref.id, + "res_model": "usps.address.validation", + "nodestroy": True, + "res_id": False, + "target": "new", + "context": ctx, + } + + def cleanse_address(self, response_data): + response_data = response_data.get("AddressValidateResponse").get("Address") + if response_data.get("Address1") != "FALSE": + a1 = response_data.get("Address1") + else: + a1 = " " + if response_data.get("Address2") != "FALSE": + a2 = response_data.get("Address2") + else: + a2 = " " + if response_data.get("City") != "FALSE": + city = response_data.get("City") + else: + city = " " + if response_data.get("State") != "FALSE": + state = response_data.get("State") + else: + state = " " + if response_data.get("Zip5") != "FALSE": + z5 = response_data.get("Zip5") + else: + z5 = " " + if response_data.get("Zip4") != "FALSE": + z4 = response_data.get("Zip4") + else: + z4 = " " + if z4 != " " and z5 != " " and z4 and z5: + z = z5 + " - " + z4 + ok = "true" + elif z5: + z = z5 + ok = "false" + else: + if response_data.get("Error"): + return {"Error": response_data.get("Error").get("Description")} + if a2 != " " or a1 != " ": + am = "true" + else: + am = "false" + cleanse_address_res = { + "Address2": a1, + "Address1": a2, + "City": city, + "State": state, + "ZIPCode": z, + "AddressMatch": am, + "CityStateZipOK": ok, + } + return cleanse_address_res + + def usps_xml_request(self): + """ + prepare xml data for address validation api + """ + # Default address as of 6/22: "https://secure.shippingapis.com/ShippingAPI.dll" + try: + web = self.env["ir.config_parameter"].sudo().get_param("usps_api_url") + user_id = self.env["ir.config_parameter"].sudo().get_param("usps_username") + except Exception: + _logger.exception( + _( + "Your credentials are not configured,\ + please ensure you have a username, password,\ + and API URL set under Contacts/Configuration/USPS\ + Credential Configuration" + ) + ) + address1 = str(self.street).replace("&", "%26amp;").replace("#", "%23") + address2 = str(self.street2).replace("&", "%26amp;").replace("#", "%23") + city = str(self.city).replace("&", "%26amp;").replace("#", "%23") + state = str(self.state_id.code).replace("&", "%26amp;").replace("#", "%23") + zipcode = str(self.zip).replace("&", "%26amp;").replace("#", "%23") + request = ( + '%s+?API=Verify&XML=\ +
%s%s\ + %s%s\ + %s
' + % (web, user_id, address1, address2, city, state, zipcode) + ) + response_data = {} + try: + response = requests.post(request, timeout=10) + response_data = xmltodict.parse(response.text) + except Exception as error: + _logger.exception(error) + if response_data.get("Error"): + raise ValidationError(response_data.get("Error").get("Description")) + try: + if response_data: + cleanse_address_res = self.env["res.partner"].cleanse_address( + response_data + ) + if cleanse_address_res: + return cleanse_address_res + else: + raise ValidationError(_(response_data)) + except Exception as error: + _logger.exception(_(error)) diff --git a/partner_usps_address_validation/models/usps_credential_configuration.py b/partner_usps_address_validation/models/usps_credential_configuration.py new file mode 100644 index 00000000..82bccd58 --- /dev/null +++ b/partner_usps_address_validation/models/usps_credential_configuration.py @@ -0,0 +1,42 @@ +# Copyright 2022 Open Source Integrators +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + usps_api_url = fields.Char( + string="API URL", + default="https://secure.shippingapis.com/ShippingAPI.dll", + config_parameter="usps_api_url", + ) + usps_username = fields.Char(string="Username", config_parameter="usps_username") + usps_password = fields.Char(string="Password", config_parameter="usps_password") + + def get_values(self): + res = super(ResConfigSettings, self).get_values() + res.update( + usps_api_url=self.env["ir.config_parameter"] + .sudo() + .get_param("usps_api_url"), + usps_username=self.env["ir.config_parameter"] + .sudo() + .get_param("usps_username"), + usps_password=self.env["ir.config_parameter"] + .sudo() + .get_param("usps_password"), + ) + return res + + def set_values(self): + res = super(ResConfigSettings, self).set_values() + param = self.env["ir.config_parameter"].sudo() + usps_api_url = self.usps_api_url + usps_username = self.usps_username + usps_password = self.usps_password + param.set_param("usps_api_url", usps_api_url) + param.set_param("usps_username", usps_username) + param.set_param("usps_password", usps_password) + return res diff --git a/partner_usps_address_validation/readme/CONFIGURATION.rst b/partner_usps_address_validation/readme/CONFIGURATION.rst new file mode 100644 index 00000000..8c830fd8 --- /dev/null +++ b/partner_usps_address_validation/readme/CONFIGURATION.rst @@ -0,0 +1,2 @@ +- Create a USPS account at https://www.usps.com/business/web-tools-apis/ and get API credentials. +- In the *General Settings* menu, enter your USPS API credentials under USPS Address Validation Settings diff --git a/partner_usps_address_validation/readme/CONTRIBUTORS.rst b/partner_usps_address_validation/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..dfc52e15 --- /dev/null +++ b/partner_usps_address_validation/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* Craig Kolobow +* Patrick Wilson +* Urvisha Desai diff --git a/partner_usps_address_validation/readme/CREDITS.rst b/partner_usps_address_validation/readme/CREDITS.rst new file mode 100644 index 00000000..fa99fe6f --- /dev/null +++ b/partner_usps_address_validation/readme/CREDITS.rst @@ -0,0 +1,3 @@ +**Financial support** + +* Open Source Integrators diff --git a/partner_usps_address_validation/readme/DESCRIPTION.rst b/partner_usps_address_validation/readme/DESCRIPTION.rst new file mode 100644 index 00000000..4b8177e9 --- /dev/null +++ b/partner_usps_address_validation/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module adds a tool to the Contacts page which validates the contact's address. +Simply click the 'Validate' button, and the address of the contact will be compared to the USPS address database. +The result will be a cleaned address, including the full 9 digit zipcode, in the exact format recognized by USPS. diff --git a/partner_usps_address_validation/requirements.txt b/partner_usps_address_validation/requirements.txt new file mode 100644 index 00000000..a0d4bb76 --- /dev/null +++ b/partner_usps_address_validation/requirements.txt @@ -0,0 +1 @@ +xmltodict diff --git a/partner_usps_address_validation/security/ir.model.access.csv b/partner_usps_address_validation/security/ir.model.access.csv new file mode 100644 index 00000000..3d9b657d --- /dev/null +++ b/partner_usps_address_validation/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_usps_address_validation,usps_address_validation,model_usps_address_validation,base.group_user,1,1,1,0 +access_usps_address_validation_manager,manager_usps_address_validation,model_usps_address_validation,base.group_user,1,1,1,1 diff --git a/partner_usps_address_validation/static/description/icon.png b/partner_usps_address_validation/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/partner_usps_address_validation/static/description/icon.png differ diff --git a/partner_usps_address_validation/static/description/index.html b/partner_usps_address_validation/static/description/index.html new file mode 100644 index 00000000..640ea1b4 --- /dev/null +++ b/partner_usps_address_validation/static/description/index.html @@ -0,0 +1,431 @@ + + + + + + +USPS Address Validation + + + +
+

USPS Address Validation

+ + +

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

+

This module adds a tool to the Contacts page which validates the contact’s address. +Simply click the ‘Validate’ button, and the address of the contact will be compared to the USPS address database. +The result will be a cleaned address, including the full 9 digit zipcode, in the exact format recognized by USPS.

+

Table of contents

+ +
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Open Source Integrators
  • +
+
+ +
+

Other credits

+

Financial support

+
    +
  • Open Source Integrators
  • +
+
+
+

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-usa project on GitHub.

+

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

+
+
+
+ + diff --git a/partner_usps_address_validation/views/config.xml b/partner_usps_address_validation/views/config.xml new file mode 100644 index 00000000..c600947a --- /dev/null +++ b/partner_usps_address_validation/views/config.xml @@ -0,0 +1,45 @@ + + + + res.config.settings.view.form.inherit.base.setup + res.config.settings + + + + +
+
USPS Address Validation Settings
+
+
+ + + + + + +
+
+
+
+
+
diff --git a/partner_usps_address_validation/views/res_partner.xml b/partner_usps_address_validation/views/res_partner.xml new file mode 100644 index 00000000..ff070496 --- /dev/null +++ b/partner_usps_address_validation/views/res_partner.xml @@ -0,0 +1,29 @@ + + + + USPS Address Validation + res.partner + + + +