From f90cfa95995015774a52077857d59673f76088d8 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Thu, 15 Apr 2021 13:28:22 +0100 Subject: [PATCH 01/19] [ADD] website_sale_resource_booking: eCommerce for resource bookings This module extends the functionality of ``sale_resource_booking`` to support the eCommerce use case and to allow your visitors to buy products that produce a resource booking, and pre-book them before buying. You can also set a timeout for those pre-bookings to expire if unpaid. @Tecnativa TT28202 --- website_sale_resource_booking/README.rst | 118 +++++ website_sale_resource_booking/__init__.py | 2 + website_sale_resource_booking/__manifest__.py | 27 ++ .../controllers/__init__.py | 1 + .../controllers/main.py | 112 +++++ .../data/ir_cron_data.xml | 19 + website_sale_resource_booking/demo/assets.xml | 14 + website_sale_resource_booking/i18n/es.po | 147 ++++++ .../i18n/website_sale_resource_booking.pot | 129 +++++ .../models/__init__.py | 4 + .../models/product_template.py | 32 ++ .../models/resource_booking.py | 126 +++++ .../models/sale_order.py | 30 ++ .../models/sale_order_line.py | 54 +++ .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 5 + .../readme/INSTALL.rst | 4 + .../readme/USAGE.rst | 20 + .../static/description/icon.png | Bin 0 -> 9455 bytes .../static/description/index.html | 455 ++++++++++++++++++ .../css/website_sale_resource_booking.scss | 15 + .../static/src/js/tour_checkout.js | 244 ++++++++++ .../templates/assets.xml | 13 + .../templates/website_sale.xml | 107 ++++ .../tests/__init__.py | 1 + .../tests/test_ui.py | 115 +++++ .../views/product_template_view.xml | 19 + 27 files changed, 1815 insertions(+) create mode 100644 website_sale_resource_booking/README.rst create mode 100644 website_sale_resource_booking/__init__.py create mode 100644 website_sale_resource_booking/__manifest__.py create mode 100644 website_sale_resource_booking/controllers/__init__.py create mode 100644 website_sale_resource_booking/controllers/main.py create mode 100644 website_sale_resource_booking/data/ir_cron_data.xml create mode 100644 website_sale_resource_booking/demo/assets.xml create mode 100644 website_sale_resource_booking/i18n/es.po create mode 100644 website_sale_resource_booking/i18n/website_sale_resource_booking.pot create mode 100644 website_sale_resource_booking/models/__init__.py create mode 100644 website_sale_resource_booking/models/product_template.py create mode 100644 website_sale_resource_booking/models/resource_booking.py create mode 100644 website_sale_resource_booking/models/sale_order.py create mode 100644 website_sale_resource_booking/models/sale_order_line.py create mode 100644 website_sale_resource_booking/readme/CONTRIBUTORS.rst create mode 100644 website_sale_resource_booking/readme/DESCRIPTION.rst create mode 100644 website_sale_resource_booking/readme/INSTALL.rst create mode 100644 website_sale_resource_booking/readme/USAGE.rst create mode 100644 website_sale_resource_booking/static/description/icon.png create mode 100644 website_sale_resource_booking/static/description/index.html create mode 100644 website_sale_resource_booking/static/src/css/website_sale_resource_booking.scss create mode 100755 website_sale_resource_booking/static/src/js/tour_checkout.js create mode 100644 website_sale_resource_booking/templates/assets.xml create mode 100644 website_sale_resource_booking/templates/website_sale.xml create mode 100644 website_sale_resource_booking/tests/__init__.py create mode 100644 website_sale_resource_booking/tests/test_ui.py create mode 100644 website_sale_resource_booking/views/product_template_view.xml diff --git a/website_sale_resource_booking/README.rst b/website_sale_resource_booking/README.rst new file mode 100644 index 0000000000..e6047fe4ca --- /dev/null +++ b/website_sale_resource_booking/README.rst @@ -0,0 +1,118 @@ +================================================ +Sell resource booking products in your eCommerce +================================================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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%2Fe--commerce-lightgray.png?logo=github + :target: https://github.com/OCA/e-commerce/tree/12.0/website_sale_resource_booking + :alt: OCA/e-commerce +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/e-commerce-12-0/e-commerce-12-0-website_sale_resource_booking + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/113/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the functionality of ``sale_resource_booking`` to support +the eCommerce use case and to allow your visitors to buy products that produce +a resource booking, and pre-book them before buying. + +You can also set a timeout for those pre-bookings to expire if unpaid. + +**Table of contents** + +.. contents:: + :local: + +Installation +============ + +To install this module, you need these dependencies: + +* ``resource_booking`` from https://github.com/OCA/calendar +* ``sale_resource_booking`` from https://github.com/OCA/sale-workflow + +Usage +===== + +To use this module, you need to know how to use ``sale_resource_booking`` and +``resource_booking``. This document doesn't explain the details for those +related modules. + +All products that you link to a resource booking type will allow pre-bookings +if sold from your eCommerce. To configure those pre-bookings timeout: + +#. Go to the product form in the backend. +#. Use the *Resource booking timeout* field, in the *Sales* tab. + +When you go to that product's eCommerce page, you'll see a little message above +the *Add to cart* button, telling the user that they will be able to pre-book it +before buying. + +When you add to your cart one (or more) bookable products, you will see in the +eCommerce checkout wizard a new step that you will have to follow to be able to +buy. This step will display a calendar with bookable slots for you to choose. + +When you are redirected to payment, make sure to pay before your pre-bookings +expire! + +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 +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `__: + * Jairo Llopis + +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. + +.. |maintainer-Yajo| image:: https://github.com/Yajo.png?size=40px + :target: https://github.com/Yajo + :alt: Yajo + +Current `maintainer `__: + +|maintainer-Yajo| + +This module is part of the `OCA/e-commerce `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_sale_resource_booking/__init__.py b/website_sale_resource_booking/__init__.py new file mode 100644 index 0000000000..f7209b1710 --- /dev/null +++ b/website_sale_resource_booking/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import controllers diff --git a/website_sale_resource_booking/__manifest__.py b/website_sale_resource_booking/__manifest__.py new file mode 100644 index 0000000000..f8a01da60d --- /dev/null +++ b/website_sale_resource_booking/__manifest__.py @@ -0,0 +1,27 @@ +# Copyright 2021 Tecnativa - Jairo Llopis +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Sell resource booking products in your eCommerce", + "summary": "Let customers book resources temporarily before buying", + "version": "12.0.1.0.0", + "development_status": "Production/Stable", + "category": "Website", + "website": "https://github.com/OCA/e-commerce", + "author": "Tecnativa, Odoo Community Association (OCA)", + "maintainers": ["Yajo"], + "license": "AGPL-3", + "external_dependencies": { + "python": [], + "bin": [], + }, + "depends": ["sale_resource_booking", "website_sale"], + "data": [ + "data/ir_cron_data.xml", + "templates/assets.xml", + "templates/website_sale.xml", + "views/product_template_view.xml", + ], + "demo": [ + "demo/assets.xml", + ], +} diff --git a/website_sale_resource_booking/controllers/__init__.py b/website_sale_resource_booking/controllers/__init__.py new file mode 100644 index 0000000000..12a7e529b6 --- /dev/null +++ b/website_sale_resource_booking/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/website_sale_resource_booking/controllers/main.py b/website_sale_resource_booking/controllers/main.py new file mode 100644 index 0000000000..0f1beff1a1 --- /dev/null +++ b/website_sale_resource_booking/controllers/main.py @@ -0,0 +1,112 @@ +# Copyright 2021 Tecnativa - Jairo Llopis +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from datetime import datetime +from dateutil.parser import isoparse +from urllib.parse import quote_plus + +from ...website_sale.controllers import main + +from odoo import _ +from odoo.http import route, request +from odoo.tests.common import Form +from odoo.exceptions import ValidationError + + +class WebsiteSale(main.WebsiteSale): + def _get_bookings(self): + """Obtain bookings from current cart.""" + order = request.website.sale_get_order() + return order.mapped("order_line.resource_booking_ids") + + def _get_indexed_booking(self, index): + """Get indexed booking from current cart. + + :param int index: 1 is the 1st element. + """ + bookings = self._get_bookings().sorted("id") + if index > len(bookings): + raise IndexError() + return bookings[index - 1] + + def checkout_redirection(self, order): + """Redirect to scheduling bookings if still not done.""" + order.order_line._sync_resource_bookings() + bookings = order.mapped("order_line.resource_booking_ids") + for booking in bookings: + if booking.state == "pending": + return request.redirect("/shop/booking/1/schedule") + return super().checkout_redirection(order) + + @route( + [ + "/shop/booking//schedule", + "/shop/booking//schedule//", + ], + type="http", + auth="public", + website=True, + sitemap=False, + ) + def booking_schedule(self, index, year=None, month=None, error=None, **post): + """Schedule pending bookings.""" + # Proceed to checkout if there are no bookings in this cart + bookings = self._get_bookings().with_context(checkout_booking_index=index) + if not bookings: + return request.redirect("/shop/checkout") + # Proceed to checkout if we passed the last booking + try: + booking = self._get_indexed_booking(index).with_context( + checkout_booking_index=index + ) + except IndexError: + return request.redirect("/shop/checkout") + count = len(bookings) + values = booking._get_calendar_context(year, month) + values.update( + { + "booking_index": index, + "bookings_count": count, + "error": error, + "website_sale_order": request.website.sale_get_order(), + "wizard_title": _("Pre-schedule your booking (%(index)d of %(total)d)") + % {"index": index, "total": count}, + } + ) + return request.render("website_sale_resource_booking.scheduling", values) + + @route( + ["/shop/booking//confirm"], + type="http", + auth="public", + website=True, + sitemap=False, + ) + def booking_confirm(self, index, partner_name, partner_email, when, **post): + """Pre-reserve resource booking.""" + booking_sudo = ( + self._get_indexed_booking(index) + .sudo() + .with_context( + # Avoid calendar notifications now, SO is still draft + dont_notify=True, + no_mail_to_attendees=True, + ) + ) + when_tz_aware = isoparse(when) + when_naive = datetime.utcfromtimestamp(when_tz_aware.timestamp()) + try: + with Form(booking_sudo) as booking_form: + booking_form.start = when_naive + except ValidationError as error: + url = "/shop/booking/{}/schedule?error={}".format( + index, quote_plus(error.name) + ) + return request.redirect(url) + # Store partner info to autocreate and autoconfirm later + booking_sudo.write( + { + "prereserved_name": partner_name, + "prereserved_email": partner_email, + } + ) + return request.redirect("/shop/booking/{}/schedule".format(index + 1)) diff --git a/website_sale_resource_booking/data/ir_cron_data.xml b/website_sale_resource_booking/data/ir_cron_data.xml new file mode 100644 index 0000000000..cc967bcf6a --- /dev/null +++ b/website_sale_resource_booking/data/ir_cron_data.xml @@ -0,0 +1,19 @@ + + + + + + + Auto-cancel expired resource bookings + + code + model._cron_cancel_expired() + + 1 + minutes + -1 + + + + diff --git a/website_sale_resource_booking/demo/assets.xml b/website_sale_resource_booking/demo/assets.xml new file mode 100644 index 0000000000..e0a48728ef --- /dev/null +++ b/website_sale_resource_booking/demo/assets.xml @@ -0,0 +1,14 @@ + + + + + +