Skip to content

Commit

Permalink
[ADD] website_payment_acquirer_bank_account
Browse files Browse the repository at this point in the history
  • Loading branch information
unaiberis authored and anajuaristi committed Nov 7, 2024
1 parent 1432422 commit 8b9f6ae
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/website_payment_acquirer_bank_account/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
73 changes: 73 additions & 0 deletions website_payment_acquirer_bank_account/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg
:target: https://opensource.org/licenses/LGPL-3.0
:alt: License: LGPL-3

=====================================
Website Payment Acquirer Bank Account
=====================================

Overview
========

The **Account Payment Mode and Acquirer** module integrates payment modes with bank accounts in sales. It allows users to select a bank account for payments directly from the sales order, enhancing the payment processing workflow.

Features
========

- **Bank Account Selection**: Users can select a bank account for payments directly from the sales order.

- **Automatic Payment Mode Assignment**: When confirming an order, the payment mode is automatically set based on the selected bank account.

- **Bank Account Management**: Users can create new bank accounts linked to partners seamlessly.

Usage
=====

1. **Install the Module**:

- Install the module via Odoo's Apps interface.

2. **Using Bank Accounts**:

- When creating or editing a sales order, select a bank account from the dropdown.
- Confirm the order, and the payment mode will automatically be assigned based on the selected bank account.

Configuration
=============

No additional configuration is required. The module is ready to use once installed.

Testing
=======

Test the following scenarios:

- **Bank Account Selection**:

- Create or edit a sales order and select a bank account.
- Confirm the order and verify that the payment mode is set correctly.

- **Bank Account Creation**:

- Create a new bank account from the sales order and ensure it links correctly to the partner.

Bug Tracker
===========

If you encounter any issues, please report them on the GitHub repository at `GitHub Issues <https://github.com/avanzosc/odoo-addons/issues>`_.

Credits
=======

Contributors
------------

* Unai Beristain <[email protected]>
* Ana Juaristi <[email protected]>

For module-specific questions, please contact the contributors directly. Support requests should be made through the official channels.

License
=======

This project is licensed under the LGPL-3 License. For more details, please refer to the LICENSE file or visit <https://opensource.org/licenses/LGPL-3.0>.
1 change: 1 addition & 0 deletions website_payment_acquirer_bank_account/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import controllers
17 changes: 17 additions & 0 deletions website_payment_acquirer_bank_account/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Website Payment Acquirer Bank Account",
"version": "14.0.1.0.0",
"author": "Avanzosc",
"summary": "Integrates payment modes with bank accounts in sales.",
"website": "https://github.com/avanzosc/odoo-addons",
"license": "LGPL-3",
"depends": [
"web",
"sale",
"account",
"payment_acquirer_payment_mode", # trey
],
"data": ["views/payment_view.xml"],
"installable": True,
"application": False,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
211 changes: 211 additions & 0 deletions website_payment_acquirer_bank_account/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import logging

from odoo import http

_logger = logging.getLogger(__name__)


class PaymentController(http.Controller):
@http.route(
"/create_new_bank_account",
type="http",
auth="user",
methods=["POST"],
csrf=False,
website=True,
)
def create_new_bank_account(self, **kwargs):
new_bank_account = kwargs.get("new_bank_account")
payment_mode_id = kwargs.get("payment_mode_id")

if not self._validate_payment_mode_id(payment_mode_id):
return self._redirect_with_message("Payment mode ID is missing", 400)

if not self._validate_bank_account(new_bank_account):
return self._redirect_with_message(
"The bank account number must start with 'ES' followed by 22 digits.",
400,
)

if self._check_existing_bank_account(new_bank_account):
return self._redirect_with_message(
"The bank account already exists",
400,
existing_bank_id=self.existing_bank_id,
)

self._deactivate_existing_bank_accounts()
self._create_new_bank_account(new_bank_account)

sale_order_id = self._get_sale_order_id(kwargs)
if not sale_order_id:
return self._redirect_with_message("Sale order ID missing", 400)

sale_order = self._get_sale_order(sale_order_id)
if not sale_order:
return self._redirect_with_message("Sale order not found", 400)

if payment_mode_id:
self._assign_payment_mode(sale_order, payment_mode_id)

_logger.info(
"New bank account created and old ones removed for partner ID: %s.\
Assigned to order ID: %s",
http.request.env.user.partner_id.id,
sale_order_id,
)

return self._redirect_with_message("Bank account saved successfully", 200)

def _validate_payment_mode_id(self, payment_mode_id):
if not payment_mode_id:
_logger.warning("Payment mode ID was not provided.")
return False
return True

def _validate_bank_account(self, bank_account):
bank_account = bank_account.upper() if bank_account else ""
is_valid = (
bank_account.startswith("ES")
and len(bank_account) == 24
and bank_account[2:].isdigit()
)
if not is_valid:
_logger.warning(
"The bank account must start with 'ES' followed by 22 digits."
)
return is_valid

def _check_existing_bank_account(self, bank_account):
self.existing_bank_id = None
existing_bank = (
http.request.env["res.partner.bank"]
.sudo()
.search([("acc_number", "=", bank_account)], limit=1)
)
if existing_bank:
_logger.warning("The bank account already exists: %s", bank_account)
self.existing_bank_id = existing_bank.id
return True
return False

def _deactivate_existing_bank_accounts(self):
partner_id = http.request.env.user.partner_id.id
http.request.env["res.partner.bank"].sudo().search(
[("partner_id", "=", partner_id)]
).write({"active": False})

def _create_new_bank_account(self, bank_account):
partner_id = http.request.env.user.partner_id.id
http.request.env["res.partner.bank"].sudo().create(
{
"acc_number": bank_account,
"partner_id": partner_id,
}
)

def _get_sale_order_id(self, kwargs):
return (
kwargs.get("sale_order_id")
or http.request.session.get("sale_order_id")
or http.request.env["website"].sudo().sale_get_order()
)

def _get_sale_order(self, sale_order_id):
sale_order = http.request.env["sale.order"].sudo().browse(sale_order_id)
if not sale_order.exists():
_logger.warning("Sale order not found: %s", sale_order_id)
return None
return sale_order

def _assign_payment_mode(self, sale_order, payment_mode_id):
sale_order.write({"payment_mode_id": int(payment_mode_id)})

def _redirect_with_message(self, message, status, existing_bank_id=None):
redirect_url = f"/shop/payment?message={message}&status={status}"
if existing_bank_id:
redirect_url += f"&existing_bank_id={existing_bank_id}"
return http.request.redirect(redirect_url)

@http.route(
"/choose_bank_account",
type="json",
auth="user",
methods=["POST"],
csrf=False,
website=True,
)
def choose_bank_account(self, bank_id=None, **kwargs):
if not bank_id:
return self._json_error("Bank account ID was not provided.")

try:
bank_id = int(bank_id)
except ValueError:
return self._json_error("The bank account ID is not a valid number.")

sale_order_id = http.request.session.get("sale_order_id")
if not sale_order_id:
return self._json_error("No sale order ID found in the session.")

sale_order = http.request.env["sale.order"].sudo().browse(sale_order_id)
if not sale_order.exists():
return self._json_error("No sale order found with the provided ID.")

_logger.info(
"Bank account selected and successfully assigned to order ID: %s",
sale_order_id,
)
return self._json_success(
"Bank account selected and successfully assigned to the order."
)

def _json_error(self, message):
_logger.warning(message)
return {"status": "error", "message": message}

def _json_success(self, message):
_logger.info(message)
return {"status": "success", "message": message}

@http.route(
"/send_email_to_log_for_existing_account",
type="json",
auth="public",
methods=["POST"],
csrf=False,
)
def send_email_to_log_for_existing_account(self, **kwargs):
existing_bank_id = kwargs.get("existing_bank_id")

if not existing_bank_id:
return self._json_error("No existing bank ID provided.")

bank_record = self._get_bank_record(existing_bank_id)
if not bank_record:
return self._json_error("Bank account not found.")

partner = bank_record.partner_id
if not partner:
return self._json_error("Associated partner not found.")

user = partner.user_id
if not user:
return self._json_error("User not found for the associated partner.")

try:
user.action_reset_password()
return self._json_success("Password reset email sent successfully.")
except Exception as e:
_logger.error("Error sending password reset email: %s", str(e))
return {"error": str(e)}

def _get_bank_record(self, bank_id):
bank_record = (
http.request.env["res.partner.bank"]
.sudo()
.search([("id", "=", bank_id)], limit=1)
)
if not bank_record:
_logger.warning("Bank account not found for ID: %s", bank_id)
return bank_record
69 changes: 69 additions & 0 deletions website_payment_acquirer_bank_account/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * website_payment_acquirer_bank_account
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-25 09:46+0000\n"
"PO-Revision-Date: 2024-10-25 09:46+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: website_payment_acquirer_bank_account
#: code:addons/temp-addons/website_payment_acquirer_bank_account/controllers/main.py:0
#: code:addons/website_payment_acquirer_bank_account/controllers/main.py:0
#, python-format
msgid "Bank account saved successfully"
msgstr "Cuenta bancaria guardada con éxito"

#. module: website_payment_acquirer_bank_account
#: model_terms:ir.ui.view,arch_db:website_payment_acquirer_bank_account.payment_tokens_list
msgid "Enter a new bank account (IBAN) (24 characters)"
msgstr "Ingrese una nueva cuenta bancaria (IBAN) (24 caracteres)"

#. module: website_payment_acquirer_bank_account
#: model_terms:ir.ui.view,arch_db:website_payment_acquirer_bank_account.payment_tokens_list
msgid "Must start with ES followed by 22 digits. No spaces allowed."
msgstr "Debe comenzar con ES seguido de 22 dígitos. No se permiten espacios."

#. module: website_payment_acquirer_bank_account
#: model_terms:ir.ui.view,arch_db:website_payment_acquirer_bank_account.payment_tokens_list
msgid "Payment Method:"
msgstr "Método de Pago:"

#. module: website_payment_acquirer_bank_account
#: code:addons/temp-addons/website_payment_acquirer_bank_account/controllers/main.py:0
#: code:addons/website_payment_acquirer_bank_account/controllers/main.py:0
#, python-format
msgid "Payment mode ID is missing"
msgstr "El ID del modo de pago falta"

#. module: website_payment_acquirer_bank_account
#: model_terms:ir.ui.view,arch_db:website_payment_acquirer_bank_account.payment_tokens_list
msgid "Save"
msgstr "Guardar"

#. module: website_payment_acquirer_bank_account
#: model_terms:ir.ui.view,arch_db:website_payment_acquirer_bank_account.payment_tokens_list
msgid "Select an existing bank account:"
msgstr "Seleccione una cuenta bancaria existente:"

#. module: website_payment_acquirer_bank_account
#: code:addons/temp-addons/website_payment_acquirer_bank_account/controllers/main.py:0
#: code:addons/website_payment_acquirer_bank_account/controllers/main.py:0
#, python-format
msgid "The bank account already exists"
msgstr "La cuenta bancaria ya existe"

#. module: website_payment_acquirer_bank_account
#: code:addons/temp-addons/website_payment_acquirer_bank_account/controllers/main.py:0
#: code:addons/website_payment_acquirer_bank_account/controllers/main.py:0
#, python-format
msgid "The bank account number must start with 'ES' followed by 22 digits."
msgstr "El número de cuenta bancaria debe comenzar con 'ES' seguido de 22 dígitos."
Loading

0 comments on commit 8b9f6ae

Please sign in to comment.