Commit cea1f3f0 authored by Floréal Cabanettes's avatar Floréal Cabanettes

Make send transaction working + Fix id to class + fix amount precision check

parent d598d6db
......@@ -43,10 +43,10 @@
<div id="transactions">
<div class="transaction">
<hr width="50%">
<label for="amount" class="first">Montant</label>
<input class="money" id="amount" type="number" step="0.01"/>
<label for="category">Catégorie</label>
<select id="category" class="select2">
<label class="first">Montant</label>
<input class="money amount" type="number" step="0.01"/>
<label>Catégorie</label>
<select class="category select2">
<option></option>
</select>
</div>
......
......@@ -15,6 +15,18 @@ trackmybank.init = function() {
$("#add-subtr").on("click touch", trackmybank.add_transaction);
$("#del-subtr").on("click touch", trackmybank.del_transaction);
$("#reset").on("click touch", trackmybank.cancel);
$("#send").on("click touch", trackmybank.send);
$(document).on("keypress", ".money", function() {
let value = $(this).val();
let comma = value.indexOf(".");
let len = value.length;
if (comma > -1) {
let len_after_comma = len - comma;
if (len_after_comma > 2) {
$(this).val(value.substr(0, len - (len_after_comma - 2)));
}
}
})
};
trackmybank.init_special_fields = function () {
......@@ -51,7 +63,7 @@ trackmybank.login = function() {
$("#logged").show();
if ("categories" in data) {
$.each(data["categories"], function(c, cat) {
$("#category").append(new Option(cat["name"], cat["id"]));
$(".category").append(new Option(cat["name"], cat["id"]));
});
}
trackmybank.init_special_fields();
......@@ -62,6 +74,55 @@ trackmybank.login = function() {
});
};
trackmybank.send = function() {
trackmybank.hide_notify();
let transactions = [];
let transation_date = $("#date_t").val();
let valid = true;
$.each($("#transactions").find(".transaction"), function(t, transaction) {
let tr = $(transaction)
let montant = tr.find(".amount").val();
let category = tr.find(".category").val();
if (montant === "" || category === "") {
trackmybank.notify("error", "Erreur : tous les champs sont requis !");
valid = false;
return false;
}
transactions.push({
"amount": parseFloat(montant),
"category": parseInt(category)
})
});
if (valid) {
trackmybank.post(credentials.url + "/api/transactions/", {
transation_date: transation_date,
transactions: transactions
}, function(data, success) {
try {
if ("success" in data && data["success"] === true) {
trackmybank.cancel();
let message = "Transaction ajoutée !";
let nb_subtr = transactions.length;
if (nb_subtr > 1) {
message += " (avec " + nb_subtr.toString() + " sous-transactions)";
}
trackmybank.notify("success", message)
}
else {
if ("message" in data) {
trackmybank.notify("error", data.message);
}
else {
trackmybank.notify("error", "Une erreur inconnue s'est produite.");
}
}
} catch (e) {
trackmybank.notify("error", "Une erreur inconnue s'est produite");
}
})
}
};
trackmybank.add_transaction = function () {
$("#transactions").append(trackmybank.transaction_html.clone());
$("#del-subtr").show();
......@@ -110,30 +171,36 @@ trackmybank.notify = async function(level, message) {
};
trackmybank.ajax = function (url, data, success, error, method = "POST", async = true) {
$.ajax(url,
{
method: method,
data: data,
success: success,
error: error || function (res) {
if (res.status === 0) {
trackmybank.notify("error", "Vérifiez votre connexion internet.");
return false;
} else {
try {
data = JSON.parse(res.responseText);
if ("message" in data) {
trackmybank.notify("error", data.message);
return true;
}
} catch (e) {
// do nothing
let options = {
method: method,
data: data,
success: success,
error: error || function (res) {
if (res.status === 0) {
trackmybank.notify("error", "Vérifiez votre connexion internet.");
return false;
} else {
try {
data = JSON.parse(res.responseText);
if ("message" in data) {
trackmybank.notify("error", data.message);
return true;
}
trackmybank.notify("error", "Une erreur est survenue. Veuillez contacter le support.");
} catch (e) {
// do nothing
}
},
async: async,
trackmybank.notify("error", "Une erreur est survenue. Veuillez contacter le support.");
}
},
async: async,
};
if (credentials.token !== undefined) {
options["beforeSend"] = function (xhr) {
xhr.setRequestHeader("Authorization", "Token " + credentials.token);
}
}
$.ajax(url,
options
);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment