-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcheckout.js
95 lines (87 loc) · 3.82 KB
/
checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { rpc } from "@web/core/network/rpc";
import publicWidget from '@web/legacy/js/public/public_widget';
publicWidget.registry.WebsiteSaleCheckout.include({
/**
* @override
*/
events: Object.assign({}, publicWidget.registry.WebsiteSaleCheckout.prototype.events, {
'change #want_tax_credit': '_toggleTaxCreditRow',
}),
async start() {
await this._super.apply(this, arguments);
this.want_tax_credit_toggle = document.querySelector('#want_tax_credit');
this.taxCreditContainer = this.el.querySelector('#tax_credit_container');
this.$vatInput = this.$("#o_vat");
this.$companyNameInput = this.$("#o_company_name");
if (this.$vatInput.length) {
this.$vatInput.on("change", this._fetchCompanyDetails.bind(this));
}
},
async _fetchCompanyDetails() {
const vat = this.$vatInput.val().trim();
if (!vat) {
return;
}
try {
const result = await rpc("/shop/get_data", { vat: vat });
if (result && result.name) {
console.log(result)
this.$companyNameInput.val(result.name);
await this.updateInvoiceAddress('billing', vat);
} else {
console.warn("Could not find any company registered to this VAT : ", vat);
}
} catch (error) {
console.error("Failed to fetch company details:", error);
}
},
async _changeAddress(ev) {
const newAddress = ev.currentTarget;
if (newAddress.classList.contains('bg-primary')) {
return;
}
const addressType = newAddress.dataset.addressType;
// Remove the highlighting from the previously selected address card.
const previousAddress = this._getSelectedAddress(addressType);
this._tuneDownAddressCard(previousAddress);
// Highlight the newly selected address card.
this._highlightAddressCard(newAddress);
const selectedPartnerId = newAddress.dataset.partnerId;
await this.updateAddress(addressType, selectedPartnerId);
// A delivery address is changed.
if (addressType === 'delivery' || this.taxCreditContainer.dataset.deliveryAddressDisabled) {
if (this.taxCreditContainer.dataset.deliveryAddressDisabled) {
// If a delivery address is disabled in the settings, use a billing address as
// a delivery one.
await this.updateAddress('delivery', selectedPartnerId);
}
if (!this.want_tax_credit_toggle?.checked) {
await this._selectMatchingBillingAddress(selectedPartnerId);
}
// Update the available delivery methods.
document.getElementById('o_delivery_form').innerHTML = await rpc(
'/shop/delivery_methods'
);
await this._prepareDeliveryMethods();
}
this._enableMainButton(); // Try to enable the main button.
},
async _toggleTaxCreditRow(ev) {
const useTaxCredit = ev.target.checked;
if (!useTaxCredit) {
this.taxCreditContainer.classList.add('d-none'); // Hide the Tax Credit row.
const selectedDeliveryAddress = this._getSelectedAddress('delivery');
await this._selectMatchingBillingAddress(selectedDeliveryAddress.dataset.partnerId);
} else {
this._disableMainButton();
this.taxCreditContainer.classList.remove('d-none'); // Show the Tax Credit row.
}
this._enableMainButton(); // Try to enable the main button.
},
async updateInvoiceAddress(addressType, vat) {
await rpc('/shop/update_invoice_address', { address_type: addressType, vat: vat });
},
_isBillingAddressSelected() {
return this.want_tax_credit_toggle?.checked;
},
});