facture.html 6.81 KB
Newer Older
1
{% extends "cotisations/sidebar.html" %}
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
{% comment %}
Re2o est un logiciel d'administration développé initiallement au rezometz. Il
se veut agnostique au réseau considéré, de manière à être installable en
quelques clics.

Copyright © 2017  Gabriel Détraz
Copyright © 2017  Goulven Kermarec
Copyright © 2017  Augustin Lemesle

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
{% endcomment %}

26
{% load bootstrap3 %}
27
{% load staticfiles%}
28
{% load i18n %}
29

30
{% block title %}{% trans "Creation and editing of invoices" %}{% endblock %}
31 32

{% block content %}
33

34
{% if title %}
35
<h3>{{ title }}</h3>
36
{% else %}
37
<h3>{% trans "Buy" %}</h3>
38
{% endif %}
39
{% if max_balance %}
40
<h4>{% blocktrans %}Maximum allowed balance: {{ max_balance }} €{% endblocktrans %}</h4>
41
{% endif %}
42 43
{% if balance is not None %}
<p>
44
{% blocktrans %}Current balance: {{ balance }} €{% endblocktrans %}
45
</p>
46
{% endif %}
Gabriel Detraz's avatar
Gabriel Detraz committed
47
{% if factureform %}
48
{% bootstrap_form_errors factureform %}
Gabriel Detraz's avatar
Gabriel Detraz committed
49 50
{% endif %}
{% if discount_form %}
51
{% bootstrap_form_errors discount_form %}
Gabriel Detraz's avatar
Gabriel Detraz committed
52
{% endif %}
53

54 55
<form class="form" method="post">
    {% csrf_token %}
56 57 58 59 60
    {% bootstrap_form factureform %}
    {% if payment_method %}
    {% bootstrap_form payment_method %}
    <div id="paymentMethod"></div>
    {% endif %}
61
    {% if articlesformset %}
62
    <h3>{% trans "Articles" %}</h3>
63 64 65 66 67 68 69 70 71 72 73 74 75
    <div id="form_set" class="form-group">
        {{ articlesformset.management_form }}
        {% for articlesform in articlesformset.forms %}
        <div class='product_to_sell form-inline'>
            {% trans "Article" %} : &nbsp;
            {% bootstrap_form articlesform label_class='sr-only' %}
            &nbsp;
            <button class="btn btn-danger btn-sm" id="id_form-0-article-remove" type="button">
                <span class="fa fa-times"></span>
            </button>
        </div>
        {% endfor %}
    </div>
76
    <input class="btn btn-primary btn-block" role="button" value="{% trans "Add an extra article"%}" id="add_one">
77
    {% if discount_form %}
78
    <h3>{% trans "Discount" %}</h3>
79 80
    {% bootstrap_form discount_form %}
    {% endif %}
81
    <p>
82
    {% blocktrans %}Total price: <span id="total_price">0,00</span> €{% endblocktrans %}
83
    </p>
84
    {% endif %}
85
    {% bootstrap_button action_name button_type='submit' icon='ok' button_class='btn-success' %}
86
</form>
87

88
{% if articlesformset or payment_method%}
89
<script type="text/javascript">
90
{% if articlesformset %}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
var prices = {};
{% for article in articlelist %}
prices[{{ article.id|escapejs }}] = {{ article.prix }};
{% endfor %}

var template = `Article : &nbsp;
{% bootstrap_form articlesformset.empty_form label_class='sr-only' %}
&nbsp;
<button class="btn btn-danger btn-sm"
id="id_form-__prefix__-article-remove" type="button">
<span class="fa fa-times"></span>
</button>`

function add_article(){
    // Index start at 0 => new_index = number of items
    var new_index =
        document.getElementsByClassName('product_to_sell').length;
    document.getElementById('id_form-TOTAL_FORMS').value ++;
    var new_article = document.createElement('div');
    new_article.className = 'product_to_sell form-inline';
    new_article.innerHTML = template.replace(/__prefix__/g, new_index);
    document.getElementById('form_set').appendChild(new_article);
    add_listenner_for_id(new_index);
}

function update_price(){
    var price = 0;
    var product_count =
        document.getElementsByClassName('product_to_sell').length;
    var article, article_price, quantity;
    for (i = 0; i < product_count; ++i){
        article = document.getElementById(
123
                'id_form-' + i.toString() + '-article').value;
124 125
        if (article == '') {
            continue;
126
        }
127 128 129 130
        article_price = prices[article];
        quantity = document.getElementById(
                'id_form-' + i.toString() + '-quantity').value;
        price += article_price * quantity;
131
    }
132 133 134 135 136
    {% if discount_form %}
    var relative_discount = document.getElementById('id_is_relative').checked;
    var discount = document.getElementById('id_discount').value;
    if(relative_discount) {
        discount = discount/100 * price;
137
    }
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    price -= discount;
    {% endif %}
    document.getElementById('total_price').innerHTML =
        price.toFixed(2).toString().replace('.', ',');
}

function add_listenner_for_id(i){
    document.getElementById('id_form-' + i.toString() + '-article')
        .addEventListener("change", update_price, true);
    document.getElementById('id_form-' + i.toString() + '-article')
        .addEventListener("onkeypress", update_price, true);
    document.getElementById('id_form-' + i.toString() + '-quantity')
        .addEventListener("change", update_price, true);
    document.getElementById('id_form-' + i.toString() + '-article-remove')
        .addEventListener("click", function(event) {
            var article = event.target.parentNode;
            article.parentNode.removeChild(article);
            document.getElementById('id_form-TOTAL_FORMS').value --;
            update_price();
        })
}

// Add events manager when DOM is fully loaded
document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("add_one")
        .addEventListener("click", add_article, true);
    var product_count =
        document.getElementsByClassName('product_to_sell').length;
    for (i = 0; i < product_count; ++i){
        add_listenner_for_id(i);
    }
    document.getElementById('id_discount')
        .addEventListener('change', update_price, true);
    document.getElementById('id_is_relative')
        .addEventListener('click', update_price, true);
    update_price();
});
175 176
{% endif %}
{% if payment_method.templates %}
177 178 179 180 181 182 183 184 185 186 187 188 189 190
var TEMPLATES = [
    "",
{% for t in payment_method.templates %}
{% if t %}
`{% bootstrap_form t %}`,
{% else %}
"",
{% endif %}
{% endfor %}
];
function update_payment_method_form(){
    var method = document.getElementById('paymentMethodSelect').value;
    if(method==""){
        method=0;
191
    }
192 193 194 195 196 197 198 199 200 201
    else{
        method = Number(method);
        method += 1;
    }
    console.log(method);
    var html = TEMPLATES[method];

    document.getElementById('paymentMethod').innerHTML = html;
}
document.getElementById("paymentMethodSelect").addEventListener("change", update_payment_method_form);
202
{% endif %}
203
</script>
204
{% endif %}
205

206
{% endblock %}
207