1
|
/**
|
2
|
* SPDX-License-Identifier: Apache-2.0
|
3
|
*
|
4
|
* Copyright © 2021 jahoti <jahoti@tilde.team>
|
5
|
*
|
6
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
* you may not use this file except in compliance with the License.
|
8
|
* You may obtain a copy of the License at
|
9
|
*
|
10
|
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
*
|
12
|
* Unless required by applicable law or agreed to in writing, software
|
13
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
* See the License for the specific language governing permissions and
|
16
|
* limitations under the License.
|
17
|
*/
|
18
|
|
19
|
const formId = document.querySelector('input[name="page_id"]').value;
|
20
|
const form = document.querySelector('form[name="specbuilder"]');
|
21
|
const noVat = document.getElementById('running_total_ex');
|
22
|
const incVat = document.getElementById('running_total_inc');
|
23
|
|
24
|
function updatePrice() {
|
25
|
const xhr = new XMLHttpRequest();
|
26
|
|
27
|
var names = [], values = [];
|
28
|
for (var inputElement of form.querySelectorAll('select, input[type="radio"]')) {
|
29
|
if (inputElement.name && (inputElement.checked || inputElement.tagName === 'SELECT')) {
|
30
|
names.push(inputElement.name);
|
31
|
values.push(inputElement.value);
|
32
|
}
|
33
|
}
|
34
|
|
35
|
const url = '/ajax/running_total.php?categories=' + names.join('%2C') +
|
36
|
'%2C&products=' + values.join('%2C') + '%2C&q=' + form.querySelector('input[name="q"]').value + '&form_id=' + formId;
|
37
|
|
38
|
xhr.onreadystatechange = priceUpdated;
|
39
|
xhr.open('GET', url, true);
|
40
|
xhr.send();
|
41
|
}
|
42
|
|
43
|
function priceUpdated() {
|
44
|
if (this.readyState === 4) {
|
45
|
if (this.status === 200) {
|
46
|
const parts = this.responseText.split("'");
|
47
|
noVat.innerText = parts[parts.length - 6];
|
48
|
incVat.innerText = parts[parts.length - 2];
|
49
|
}
|
50
|
else alert('Failed to get data: HTTP status code ' + this.status);
|
51
|
}
|
52
|
}
|
53
|
|
54
|
const button = document.createElement('button');
|
55
|
button.innerText = 'Update Prices';
|
56
|
button.onclick = updatePrice;
|
57
|
document.querySelector('.price-holder.price-finance-holder').append(button);
|