import { showMessage, showSuccess, showError } from "@nextcloud/dialogs"; import { translate as t, translatePlural as n } from '@nextcloud/l10n' import { baseUrl, cur, getGlobal, insertCell, insertRow, insertRowWeekendRow, modifyCell } from "./mainFunction.mjs"; /** * Update data * @param table * @param column * @param data * @param id */ export function updateDB(table, column, data, id) { var myData = { table: table, column: column, data: data, id: id, }; $.ajax({ url: baseUrl + '/update', type: 'POST', async: false, contentType: 'application/json', data: JSON.stringify(myData) }).done(function (response, code) { showSuccess(t('gestion', 'Modification saved')); }).fail(function (response, code) { showError(t('gestion', 'There is an error with the format, please check the documentation')); }); } /** * Delete data * @param table * @param id */ export function deleteDB(table, id) { var myData = { table: table, id: id, }; if(window.confirm(t('gestion','Are you sure you want to delete?'))){ $.ajax({ url: baseUrl + '/delete', type: 'DELETE', async: false, contentType: 'application/json', data: JSON.stringify(myData) }).done(function (response, code) { getStats(); showSuccess(t('gestion', 'Modification saved')); }).fail(function (response, code) { showError(response); }); }else{ showMessage(t('gestion', 'Nothing changed')) } } /** * */ export function getStats() { $.ajax({ url: baseUrl + '/getStats', type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { var res = JSON.parse(response); $("#statsclient").text(res.client); $("#statsthanato").text(res.thanato); $("#statsdefunts").text(res.defunt); $("#statsdevis").text(res.devis); $("#statstrajet").text(res.trajet); $("#statslieu").text(res.lieu); $("#statsfacture").text(res.facture); $("#statsproduit").text(res.produit); $("#statsarticles").text(res.article); $("#statsbibliotheque").text(res.bibliotheque); $("#clientGroupStat").text(res.clientGroup); $("#clientGroupDiscountStat").text(res.clientGroupDiscount); }).fail(function (response, code) { showError(response); }); } /** * * @param {*} f1 */ export function configuration(f1) { $.ajax({ url: baseUrl + '/getConfiguration', type: 'PROPFIND', contentType: 'application/json', async: false, }).done(function (response) { f1(response); }).fail(function (response, code) { showError(response); }); } /** * * @param {*} callback */ export function getBibliotheques(callback) { $.ajax({ url: baseUrl + '/getBibliotheques', type: 'PROPFIND', contentType: 'application/json', async: false, }).done(function (response) { callback(response); }).fail(function (response, code) { showError(response); }); } /** * */ export function isconfig() { $.ajax({ url: baseUrl + '/isconfig', type: 'GET', contentType: 'application/json' }).done(function (response) { if (!response) { var modal = document.getElementById("modalConfig"); modal.style.display = "block"; } }) } /** * * @param {*} cur */ export function getAnnualTurnoverPerMonthNoVat(cur) { $.ajax({ url: baseUrl + '/getAnnualTurnoverPerMonthNoVat', type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { var res = JSON.parse(response); var curY = ""; var curRow; var total=0; res.forEach(function(item){ if(curY !== item.y){ if(curY !== ""){ insertCell(curRow, -1, cur.format(total)); total=0; } curY = item.y; curRow = insertRow("Statistical", -1, 0, item.y); modifyCell(curRow, (item.m), cur.format(Math.round(item.total))); total+= Math.round(item.total); }else{ modifyCell(curRow, (item.m), cur.format(Math.round(item.total))); total+= Math.round(item.total); } }); // At the end insertCell(curRow, -1, cur.format(total)); }).fail(function (response, code) { showError(response); }); } export function getStatArticleAnnuel(annee) { $.ajax({ url: baseUrl + '/getStatArticleAnnuel/'+annee, type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { // refresh table rows var table = document.getElementById("Articles"); // Start from the last row to avoid issues with dynamic indexing for (var i = table.rows.length - 1; i > 0; i--) { table.deleteRow(i); } var res = JSON.parse(response); var curRow; var total = 0; res.forEach(function(item, index){ curRow = insertRow("Articles", -1, 0, item.reference); modifyCell(curRow, 1, item.janvier); modifyCell(curRow, 2, item.fevrier); modifyCell(curRow, 3, item.mars); modifyCell(curRow, 4, item.avril); modifyCell(curRow, 5, item.mai); modifyCell(curRow, 6, item.juin); modifyCell(curRow, 7, item.juillet); modifyCell(curRow, 8, item.aout); modifyCell(curRow, 9, item.septembre); modifyCell(curRow, 10, item.octobre); modifyCell(curRow, 11, item.novembre); modifyCell(curRow, 12, item.decembre); total=item.janvier+item.fevrier+item.mars+item.avril+item.mai+item.juin+item.juillet+item.aout+item.septembre+item.octobre+item.novembre+item.decembre; insertCell(curRow, -1, total); }) }).fail(function (response, code) { showError(response); }); } export function getStatSoinsThanatoAnnuel(annee) { $.ajax({ url: baseUrl + '/getStatSoinsThanatoAnnuel/'+annee, type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { // refresh table rows var table = document.getElementById("Soins"); // Start from the last row to avoid issues with dynamic indexing for (var i = table.rows.length - 1; i > 0; i--) { table.deleteRow(i); } var res = JSON.parse(response); var curRow; var total = 0; res.forEach(function(item, index){ curRow = insertRow("Soins", -1, 0, `${item.nom_thanato} ${item.prenom_thanato}`); modifyCell(curRow, 1, item.janvier); modifyCell(curRow, 2, item.fevrier); modifyCell(curRow, 3, item.mars); modifyCell(curRow, 4, item.avril); modifyCell(curRow, 5, item.mai); modifyCell(curRow, 6, item.juin); modifyCell(curRow, 7, item.juillet); modifyCell(curRow, 8, item.aout); modifyCell(curRow, 9, item.septembre); modifyCell(curRow, 10, item.octobre); modifyCell(curRow, 11, item.novembre); modifyCell(curRow, 12, item.decembre); total=Number(item.janvier)+Number(item.fevrier)+Number(item.mars)+Number(item.avril)+Number(item.mai)+Number(item.juin)+Number(item.juillet)+Number(item.aout)+Number(item.septembre)+Number(item.octobre)+Number(item.novembre)+Number(item.decembre); insertCell(curRow, -1, total); }) }).fail(function (response, code) { showError(response); }); } export function getStatSoinsThanatoWeekend(annee, mois) { $.ajax({ url: baseUrl + `/getStatSoinsThanatoWeekend/${annee}/${mois}`, type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { var table = document.getElementById("SoinsWeekend"); for (var i = table.rows.length - 1; i > 0; i--) { table.deleteRow(i); } var res = JSON.parse(response); var curRow; res.forEach(function(item, index){ curRow = insertRowWeekendRow("SoinsWeekend", -1, 0, `${item.nom_thanato} ${item.prenom_thanato}`); modifyCell(curRow, 1, item.weekends_travailles); }) }).fail(function (response, code) { showError(response); }); } /** * * @param {*} myCase */ export function updateEditable(myCase) { updateDB(myCase.dataset.table, myCase.dataset.column, myCase.innerText, myCase.dataset.id); if(myCase.dataset.table == "lieu" && (myCase.dataset.column == "depart" || myCase.dataset.column == "arrivee")) { location.reload(); } if (myCase.dataset.modifier === "getProduitsById") {getProduitsById();} if (myCase.dataset.modifier === "getArticlesById") {getArticlesById();} myCase.removeAttribute('contenteditable'); } /** * * @param {*} lp * @param {*} id * @param {*} produitid */ export function listProduit(lp, id, produitid) { $.ajax({ url: baseUrl + '/getProduits', type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { lp.append(''); $.each(JSON.parse(response), function (arrayID, myresp) { var selected = ""; if (produitid == myresp.id) { selected = "selected"; } lp.append(''); }); }).fail(function (response, code) { showError(response); }); } /** * * @param {*} la * @param {*} id * @param {*} articleid */ export function listArticle(la, id, articleid) { $.ajax({ url: baseUrl + '/getArticles', type: 'PROPFIND', contentType: 'application/json' }).done(function (response) { la.append(''); $.each(JSON.parse(response), function (arrayID, myresp) { var selected = ""; if (articleid == myresp.id) { selected = "selected"; } la.append(''); }); }).fail(function (response, code) { showError(response); }); } /** * Get list of product by id_devis */ export function getProduitsByIdDevis(id_devis) { var my_data = { numdevis: id_devis }; return $.ajax({ url: baseUrl + '/getProduitsById', type: 'POST', async: false, contentType: 'application/json', data: JSON.stringify(my_data) }) } /** * Get a product in database using id */ export function getProduitsById() { var devis_id = $('#devisid').data('id'); var myData = { numdevis: devis_id, }; $.ajax({ url: baseUrl + '/getProduitsById', type: 'POST', async: false, contentType: 'application/json', data: JSON.stringify(myData) }).done(function (response, code) { $('#produits tbody').empty(); var total = 0; var deleteDisable = ""; if ($('#produits').data("type") === "facture") { deleteDisable = "d-none"; } $.each(JSON.parse(response), function (arrayID, myresp) { $('#produits tbody').append('