Recount-Server/web/static/new_transactions.js

141 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2024-02-09 21:06:09 -08:00
import {
computePosition,
offset,
} from 'https://cdn.jsdelivr.net/npm/@floating-ui/dom@1.5.4/+esm';
const centerOffset = offset(({rects}) => {
return -rects.reference.height / 2 - rects.floating.height / 2;
});
function open_new_transaction_pane() {
let center = document.querySelector("#main-body-content");
2024-02-10 10:52:09 -08:00
let new_tp = document.querySelector("#new-transaction-pane");
2024-02-09 21:06:09 -08:00
new_tp.style.opacity = "1";
let center_width = center.offsetWidth;
center_width = center_width - (center_width / 10);
new_tp.style.width = center_width + "px";
computePosition(center, new_tp, {
placement: "bottom",
middleware: [
centerOffset,
],
}).then(({x, y}) => {
Object.assign(new_tp.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
function close_new_transaction_pane() {
let right_column = document.querySelector("#right-col");
2024-02-10 10:52:09 -08:00
let new_tp = document.querySelector("#new-transaction-pane");
2024-02-09 21:06:09 -08:00
new_tp.style.opacity = "0";
computePosition(right_column, new_tp, {
placement: "right",
middleware: [
offset(40),
],
}).then(({x, y}) => {
Object.assign(new_tp.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
2024-02-27 18:38:00 -08:00
/**
* @param {Event} event
*/
function on_input(event) {
if (event.target.id === "bottom-most-breakdown-input"
2024-02-27 19:00:39 -08:00
|| document.querySelectorAll("#breakdown-tbody input[type='number']:not(#tax-input)").length === 0
2024-02-27 18:38:00 -08:00
) {
add_new_breakdown();
}
2024-02-27 19:00:39 -08:00
let total_input = document.getElementById("amount-entry");
if (event.target.id === "amount-entry") {
let tax_entry = document.getElementById("tax-input");
let tax_rate = document.getElementById("new-transaction-pane").dataset.rcntTaxrate;
if (tax_entry) {
tax_entry.value = (parseFloat(total_input.value) - (parseFloat(total_input.value) / (1 + parseFloat(tax_rate)))).toFixed(2);
}
}
2024-02-27 18:38:00 -08:00
2024-02-27 19:00:39 -08:00
let breakdown_sum = Array.from(document
2024-02-27 18:38:00 -08:00
.querySelectorAll("#breakdown-tbody input[type='number']:not(#bottom-most-breakdown-input)"
2024-02-27 19:00:39 -08:00
)).reduce(function(total, el) {
2024-02-27 18:38:00 -08:00
return total + parseFloat(el.value || 0);
}, 0);
if (total_input.value) {
let value_total = parseFloat(total_input.value);
document.getElementById("bottom-most-breakdown-input").value = (value_total - breakdown_sum).toFixed(2);
} else {
total_input.placeholder = breakdown_sum;
}
}
2024-02-27 17:35:33 -08:00
function add_new_breakdown() {
2024-02-27 18:38:00 -08:00
let input_latest_old = document.getElementById("bottom-most-breakdown-input");
if (input_latest_old) {
input_latest_old.id = "";
}
let source = document.importNode(document.getElementById("breakdown-template-row").content, true);
let input_latest = source.querySelector("input[type='number']");
input_latest.addEventListener("input", on_input);
input_latest.id = "bottom-most-breakdown-input";
2024-02-27 17:35:33 -08:00
document.getElementById("breakdown-tbody").appendChild(source);
}
2024-03-03 20:21:25 -08:00
function insert_tax(event) {
event.target.parentNode.classList.remove("c-green");
event.target.parentNode.classList.add("c-overlay2");
2024-02-27 19:00:39 -08:00
if (document.getElementById("tax-input")) return;
let source = document.importNode(document.getElementById("breakdown-template-row").content, true);
source.querySelector("input[type='text']").value = "Tax";
source.querySelector("input[type='number']").id = "tax-input";
let table_body = document.getElementById("breakdown-tbody");
table_body.insertBefore(source, table_body.firstChild);
2024-03-03 20:21:25 -08:00
// calculate the the tax and final amount
on_input({target: {id: "amount-entry"}});
}
function submit_form(event) {
event.preventDefault(); // Prevent the default form submission
// Your custom submission handling code here
// For example, you might want to send the form data using AJAX
var formData = new FormData(this);
fetch('/submitTransaction', {
method: 'POST',
body: formData
})
.then(_ => {
alert("form submitted");
})
.catch(error => {
console.log("Form submission error:\n" + error);
alert("There was an issue submitting the form");
});
2024-02-27 19:00:39 -08:00
}
2024-02-27 18:38:00 -08:00
function register_breakdown_handlers() {
document.getElementById("amount-entry").addEventListener("input", on_input);
2024-02-27 19:00:39 -08:00
document.getElementById("new-breakdown-btn").addEventListener("click", add_new_breakdown);
document.getElementById("tax-breakdown-btn").addEventListener("click", insert_tax);
2024-03-03 20:21:25 -08:00
document.getElementById("new-transaction-form").addEventListener("submit", submit_form);
2024-02-27 17:35:33 -08:00
}
2024-02-27 19:00:39 -08:00
export {open_new_transaction_pane, close_new_transaction_pane, register_breakdown_handlers}