1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-04 22:26:52 +00:00
nk20/templates/treasury/invoice_form.html

90 lines
3.7 KiB
HTML
Raw Normal View History

{% extends "base.html" %}
{% load static %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
2020-03-22 17:27:22 +00:00
<p><a class="btn btn-default" href="{% url 'treasury:invoice_list' %}">{% trans "Invoices list" %}</a></p>
<form method="post" action="">
2020-03-21 15:49:18 +00:00
{% csrf_token %}
2020-03-24 19:22:15 +00:00
{# Render the invoice form #}
2020-03-21 15:49:18 +00:00
{% crispy form %}
2020-03-24 19:22:15 +00:00
{# The next part concerns the product formset #}
{# Generate some hidden fields that manage the number of products, and make easier the parsing #}
2020-03-21 15:49:18 +00:00
{{ formset.management_form }}
<table class="table table-condensed table-striped">
2020-03-24 19:22:15 +00:00
{# Fill initial data #}
{% for form in formset %}
{% if forloop.first %}
<thead>
<tr>
<th>{{ form.designation.label }}<span class="asteriskField">*</span></th>
<th>{{ form.quantity.label }}<span class="asteriskField">*</span></th>
<th>{{ form.amount.label }}<span class="asteriskField">*</span></th>
</tr>
</thead>
<tbody id="form_body">
{% endif %}
<tr class="row-formset">
2020-03-21 15:49:18 +00:00
<td>{{ form.designation }}</td>
<td>{{ form.quantity }}</td>
<td>{{ form.amount }}</td>
2020-03-24 19:22:15 +00:00
{# These fields are hidden but handled by the formset to link the id and the invoice id #}
2020-03-22 00:22:27 +00:00
{{ form.invoice }}
2020-03-21 15:49:18 +00:00
{{ form.id }}
2020-03-24 19:22:15 +00:00
</tr>
{% endfor %}
</tbody>
2020-03-21 15:49:18 +00:00
</table>
2020-03-24 19:22:15 +00:00
{# Display buttons to add and remove products #}
2020-03-21 15:49:18 +00:00
<div class="btn-group btn-block" role="group">
<button type="button" id="add_more" class="btn btn-primary">{% trans "Add product" %}</button>
<button type="button" id="remove_one" class="btn btn-danger">{% trans "Remove product" %}</button>
</div>
<div class="btn-block">
<button type="submit" class="btn btn-block btn-primary">{% trans "Submit" %}</button>
</div>
</form>
<div id="empty_form" style="display: none;">
2020-03-24 19:22:15 +00:00
{# Hidden div that store an empty product form, to be copied into new forms #}
2020-03-21 15:49:18 +00:00
<table class='no_error'>
<tbody id="for_real">
2020-03-24 19:22:15 +00:00
<tr class="row-formset">
<td>{{ formset.empty_form.designation }}</td>
<td>{{ formset.empty_form.quantity }} </td>
<td>{{ formset.empty_form.amount }}</td>
2020-03-24 19:22:15 +00:00
{{ formset.empty_form.invoice }}
{{ formset.empty_form.id }}
</tr>
2020-03-21 15:49:18 +00:00
</tbody>
</table>
</div>
{% endblock %}
{% block extrajavascript %}
<script>
2020-03-24 19:22:15 +00:00
{# Script that handles add and remove lines #}
2020-03-21 15:49:18 +00:00
IDS = {};
$("#id_product_set-TOTAL_FORMS").val($(".row-formset").length - 1);
2020-03-24 19:22:15 +00:00
$('#add_more').click(function () {
2020-03-21 15:49:18 +00:00
var form_idx = $('#id_product_set-TOTAL_FORMS').val();
$('#form_body').append($('#for_real').html().replace(/__prefix__/g, form_idx));
$('#id_product_set-TOTAL_FORMS').val(parseInt(form_idx) + 1);
$('#id_product_set-' + parseInt(form_idx) + '-id').val(IDS[parseInt(form_idx)]);
});
2020-03-24 19:22:15 +00:00
$('#remove_one').click(function () {
2020-03-21 15:49:18 +00:00
let form_idx = $('#id_product_set-TOTAL_FORMS').val();
if (form_idx > 0) {
IDS[parseInt(form_idx) - 1] = $('#id_product_set-' + (parseInt(form_idx) - 1) + '-id').val();
$('#form_body tr:last-child').remove();
$('#id_product_set-TOTAL_FORMS').val(parseInt(form_idx) - 1);
}
});
</script>
{% endblock %}