Merge branch 'main' into make_payment_with_efective_purchase_#13
This commit is contained in:
commit
9260230d38
@ -0,0 +1,50 @@
|
||||
const quantity_lineRegexSelector = `[id^="${idPrefix}"][id$="${quantitySuffix}"]`;
|
||||
const price_lineRegexSelector = `[id^="${idPrefix}"][id$="${priceSuffix}"]`;
|
||||
|
||||
function insertSubtotalField() {
|
||||
// Selecciona la fila de precio unitario para añadir la fila del subtotal después de ella
|
||||
const unitPriceRow = document.querySelector('input[id="id_saleline_set-0-unit_price"]').closest('tr');
|
||||
|
||||
// Crear una nueva fila para el subtotal
|
||||
const subtotalRow = document.createElement('tr');
|
||||
subtotalRow.innerHTML = `
|
||||
<th><label for="id_saleline_set-0-subtotal">Subtotal:</label></th>
|
||||
<td><input type="number" name="saleline_set-0-subtotal" id="id_saleline_set-0-subtotal" readonly></td>
|
||||
`;
|
||||
|
||||
// Insertar la fila del subtotal después de la fila del precio unitario
|
||||
unitPriceRow.after(subtotalRow);
|
||||
}
|
||||
|
||||
function calculateSubtotal(id) {
|
||||
const quantityElement = document.getElementById(`id_saleline_set-${id}-quantity`);
|
||||
const unitPriceElement = document.getElementById(`id_saleline_set-${id}-unit_price`);
|
||||
const subtotalElement = document.getElementById(`id_saleline_set-${id}-subtotal`);
|
||||
|
||||
const quantity = parseFloat(quantityElement.value) || 0;
|
||||
const unitPrice = parseFloat(unitPriceElement.value) || 0;
|
||||
const subtotal = quantity * unitPrice;
|
||||
|
||||
subtotalElement.value = subtotal.toFixed(2);
|
||||
}
|
||||
|
||||
// Inserta el campo subtotal al cargar la página
|
||||
window.addEventListener('load', () => {
|
||||
insertSubtotalField();
|
||||
|
||||
complete_form.addEventListener('change', function(event){
|
||||
const quantityInputs = document.querySelectorAll(quantity_lineRegexSelector);
|
||||
const ids = Array.prototype.map.call(quantityInputs, function(input) {
|
||||
return input.id.match(/\d+/)[0];
|
||||
});
|
||||
|
||||
ids.forEach(function(id) {
|
||||
if (event.target.matches(quantity_lineRegexSelector)) {
|
||||
calculateSubtotal(id);
|
||||
}
|
||||
if (event.target.matches(price_lineRegexSelector)) {
|
||||
calculateSubtotal(id);
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
@ -18,4 +18,5 @@
|
||||
<br/><button name="form" type="submit" >Comprar</button>
|
||||
<script src="{% static 'js/add_line.js' %}"></script>
|
||||
<script src="{% static 'js/sale_summary.js' %}"></script>
|
||||
<script src="{% static 'js/calculate_subtotal_line.js' %}"></script>
|
||||
</form>
|
||||
|
Loading…
Reference in New Issue
Block a user