111 lines
6.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Display Uber fee in the total summary -->
<template id="chennora_total_uber_display" inherit_id="website_sale.total" name="Chennora Total Uber Display">
<xpath expr="//tr[@id='order_total_untaxed']" position="after">
<t t-set="uber_line" t-value="website_sale_order.order_line.filtered(lambda l: l.product_id.name == 'Uber Delivery Fee')"/>
<tr t-if="uber_line" id="uber_delivery_row">
<td class="border-0 pb-2 ps-0 pt-0 text-start text-muted" colspan="2">Uber Delivery Fee</td>
<td class="text-end border-0 pb-2 pe-0 pt-0">
<span t-field="uber_line[0].price_subtotal" t-options='{"widget": "monetary", "display_currency": website_sale_order.currency_id}' class="monetary_field"/>
</td>
</tr>
</xpath>
</template>
<!-- Hide Uber fee from cart summary items -->
<template id="chennora_cart_summary_uber_hide" inherit_id="website_sale.checkout_layout" name="Chennora Cart Summary Uber Hide">
<xpath expr="//table[@id='cart_products']//tr" position="attributes">
<attribute name="t-if">line.product_id.name != 'Uber Delivery Fee'</attribute>
</xpath>
</template>
<!-- Simplify Checkout: Show only Billing Address, Hide Shipping Address UI -->
<template id="chennora_checkout_custom" inherit_id="website_sale.checkout" name="Chennora Checkout Custom" priority="99">
<!-- Hide the Shipping column UI to simplify the page (avoids confusion) -->
<xpath expr="//div[h4[contains(., 'Shipping')]]" position="attributes">
<attribute name="style">display: none !important;</attribute>
</xpath>
<!-- Rename Billing to just 'Address' -->
<xpath expr="//h4[contains(., 'Billing')]" position="replace">
<h3 class="mb-4">Delivery Address</h3>
</xpath>
<!-- Script for Uber Quote using the primary partner address -->
<xpath expr="//h3" position="after">
<div id="uber_error" class="alert alert-danger d-none my-3" role="alert"></div>
<script type="text/javascript">
(function() {
function initUberQuote() {
const btn = document.querySelector('a[name="website_sale_main_button"]') ||
document.querySelector('.oe_cart .btn-primary') ||
document.querySelector('.btn-primary[href*="/payment"]');
const errorDiv = document.getElementById('uber_error');
if (!btn) {
setTimeout(initUberQuote, 500);
return;
}
const disableButton = () => {
btn.classList.add('disabled', 'btn-secondary');
btn.classList.remove('btn-primary');
btn.style.pointerEvents = 'none';
};
const enableButton = () => {
btn.classList.remove('disabled', 'btn-secondary');
btn.classList.add('btn-primary');
btn.style.pointerEvents = 'auto';
};
const fulfilmentType = "<t t-esc='order.fulfilment_type'/>";
if (fulfilmentType !== "delivery") {
enableButton();
return;
}
disableButton();
// Use Billing Address (Invoice) for Uber Quote
const addressData = {
street: "<t t-esc='order.partner_invoice_id.street'/>",
city: "<t t-esc='order.partner_invoice_id.city'/>",
zip: "<t t-esc='order.partner_invoice_id.zip'/>",
country: "<t t-esc='order.partner_invoice_id.country_id.name'/>"
};
if (addressData.street &amp;&amp; addressData.zip) {
fetch('/shop/uber/quote', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ params: { address_data: addressData } })
}).then(r => r.json()).then(data => {
if (data.result &amp;&amp; data.result.success) {
enableButton();
if (errorDiv) errorDiv.classList.add('d-none');
const hasFee = "<t t-esc="any(l.product_id.name == 'Uber Delivery Fee' for l in website_sale_order.order_line) and 'true' or 'false'"/>" === "true";
if (!hasFee &amp;&amp; window.location.search.indexOf('quoted=1') === -1) {
window.location.href = window.location.pathname + '?quoted=1';
}
} else {
disableButton();
if (errorDiv) {
errorDiv.innerText = (data.result &amp;&amp; data.result.error) ? data.result.error : "Uber delivery is not available for this address.";
errorDiv.classList.remove('d-none');
}
}
}).catch(err => {
console.error("Uber error", err);
enableButton();
});
}
}
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initUberQuote); } else { initUberQuote(); }
})();
</script>
</xpath>
</template>
</odoo>