Skip to content

Commit b0ed365

Browse files
committed
[ADD] website_sale_product_contract_gift: add date for gift
1 parent 96eeae1 commit b0ed365

File tree

7 files changed

+196
-1
lines changed

7 files changed

+196
-1
lines changed

website_sale_product_contract_gift/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
#
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44
from . import models
5+
from . import controllers

website_sale_product_contract_gift/__manifest__.py

+6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
"excludes": [],
2121
"data": [
2222
"views/product_views.xml",
23+
"views/templates.xml",
2324
],
25+
"assets": {
26+
"web.assets_frontend": [
27+
"website_sale_product_contract_gift/static/src/js/website_sale.js",
28+
],
29+
},
2430
"demo": [],
2531
"qweb": [],
2632
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
2+
#
3+
# SPDX-License-Identifier: AGPL-3.0-or-later
4+
from . import main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
2+
#
3+
# SPDX-License-Identifier: AGPL-3.0-or-later
4+
5+
from datetime import date
6+
7+
from odoo import _, http
8+
from odoo.http import request
9+
10+
from odoo.addons.website_sale.controllers.main import WebsiteSale
11+
12+
13+
class WebsiteSaleIsGift(WebsiteSale):
14+
@http.route(
15+
["/shop/checkout"], type="http", auth="public", website=True, sitemap=False
16+
)
17+
def checkout(self, **post):
18+
"""Prevent express checkout if order is a gift and process
19+
formular"""
20+
order = request.website.sale_get_order()
21+
errors = {}
22+
if order.is_gift:
23+
# disable express checkout
24+
if "express" in post:
25+
del post["express"]
26+
27+
# if formular is sent
28+
if request.httprequest.method == "POST":
29+
errors = self.validate_gift_form(post)
30+
if not errors:
31+
return request.redirect("/shop/confirm_order")
32+
result = super().checkout(**post)
33+
result.qcontext["errors"] = errors
34+
return result
35+
36+
def validate_gift_form(self, data):
37+
"""Validate the gift formular"""
38+
errors = {}
39+
date_for_gift = data.get("date_for_gift")
40+
if date_for_gift:
41+
try:
42+
date_for_gift = date.fromisoformat(data.get("date_for_gift", ""))
43+
except ValueError:
44+
errors["date_for_gift"] = _("Date is not valid.")
45+
else:
46+
errors["date_for_gift"] = _("Date is required.")
47+
return errors

website_sale_product_contract_gift/models/sale_order.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
# SPDX-License-Identifier: AGPL-3.0-or-later
44

55

6-
from odoo import _, api, models
6+
from odoo import _, api, fields, models
77
from odoo.exceptions import ValidationError
88

99

1010
class SaleOrder(models.Model):
1111
_inherit = "sale.order"
1212

13+
is_gift = fields.Boolean(compute="_compute_is_gift")
14+
gift_date = fields.Date()
15+
16+
@api.depends("order_line")
17+
def _compute_is_gift(self):
18+
"""Tell if an order is a gift or not"""
19+
# TODO: make a real computation
20+
for order in self:
21+
order.is_gift = True
22+
1323
@api.constrains("order_line")
1424
def _check_gift_alone(self):
1525
"""Ensure gift are not mixed in a sale order."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
odoo.define("website_sale_product_contract_gift.website_sale", function (require) {
2+
"use strict";
3+
4+
var publicWidget = require("web.public.widget");
5+
6+
publicWidget.registry.websiteSaleCartGift = publicWidget.Widget.extend({
7+
selector: ".oe_website_sale .oe_cart",
8+
events: {
9+
"click #gift_form_confirm_button": "_onClickConfirm",
10+
},
11+
12+
// --------------------------------------------------------------------------
13+
// Handlers
14+
// --------------------------------------------------------------------------
15+
16+
/**
17+
* @private
18+
* @param {Event} ev
19+
*/
20+
_onClickConfirm: function () {
21+
var $form = $("form[name='date_for_gift']");
22+
$form.submit();
23+
},
24+
});
25+
26+
return {
27+
websiteSaleCartGift: publicWidget.registry.websiteSaleCart,
28+
};
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
4+
<template id="payment" inherit_id="website_sale.payment">
5+
<xpath expr="//div[@id='payment_method']" position="before">
6+
<div t-if="website_sale_order.is_gift" id="order_is_gift" class="mt-3">
7+
<h3>This order is a gift</h3>
8+
<p>
9+
<b>Date of the gift:</b> <span
10+
t-out="website_sale_order.gift_date"
11+
/>
12+
</p>
13+
</div>
14+
</xpath>
15+
</template>
16+
17+
<template id="checkout" inherit_id="website_sale.checkout">
18+
<!-- Add date for gift formular -->
19+
<xpath
20+
expr="//div[hasclass('oe_cart')]//div[hasclass('row')]"
21+
position="before"
22+
>
23+
<div class="row" id="date_for_gift" t-if="website_sale_order.is_gift">
24+
<div class="col-lg-12">
25+
<h3
26+
class="o_page_header mt8"
27+
>When should the gift be delivered ?</h3>
28+
</div>
29+
<div class="col-lg-12">
30+
<form
31+
action="/shop/checkout"
32+
method="post"
33+
name="date_for_gift"
34+
t-att-class="'was-validated' if errors else None"
35+
>
36+
<input
37+
type="hidden"
38+
name="csrf_token"
39+
t-att-value="request.csrf_token()"
40+
t-nocache="The csrf token must always be up to date."
41+
/>
42+
<div class="form-group">
43+
<label class="form-label" for="date_for_gift">
44+
Date for the gift
45+
</label>
46+
<input
47+
type="date"
48+
t-att-min="datetime.date.today()"
49+
required="required"
50+
class="form-control"
51+
id="date_for_gift"
52+
name="date_for_gift"
53+
/>
54+
<div
55+
t-if="errors.get('date_for_gift')"
56+
t-out="errors.get('date_for_gift')"
57+
class="invalid-feedback"
58+
/>
59+
</div>
60+
</form>
61+
</div>
62+
</div>
63+
</xpath>
64+
65+
<!-- Replace confirm button -->
66+
<xpath expr="//a[@href='/shop/confirm_order']" position="attributes">
67+
<attribute name="style">display: none;</attribute>
68+
</xpath>
69+
70+
<xpath expr="//a[@href='/shop/confirm_order']" position="after">
71+
<button id="gift_form_confirm_button" class="btn btn-primary mb32">
72+
Confirm
73+
<i class="fa fa-chevron-right" />
74+
</button>
75+
</xpath>
76+
77+
<!-- Change section title accordingly -->
78+
<xpath
79+
expr="//div[hasclass('oe_cart')]//h3[@class='o_page_header mt16 mb4']"
80+
position="after"
81+
>
82+
<h3
83+
id="title_gift_for"
84+
class="o_page_header mt16 mb4"
85+
t-if="website_sale_order.is_gift"
86+
>Gift for</h3>
87+
</xpath>
88+
89+
<xpath
90+
expr="//div[hasclass('oe_cart')]//h3[@class='o_page_header mt16 mb4']"
91+
position="attributes"
92+
>
93+
<attribute name="id">title_shipping_address</attribute>
94+
<attribute name="t-if">not website_sale_order.is_gift</attribute>
95+
</xpath>
96+
</template>
97+
98+
</odoo>

0 commit comments

Comments
 (0)