368 lines
14 KiB
JavaScript
368 lines
14 KiB
JavaScript
import { generateUrl } from "@nextcloud/router";
|
|
import { updateDB } from "../modules/ajaxRequest.mjs";
|
|
import { baseUrl, checkSelectPurJs, LoadDT, showDone } from "../modules/mainFunction.mjs";
|
|
import { NewDevisMentionConstant,FacturedDevisMentionConstant,DefaultDevisMentionConstant,CanceledDevisMentionConstant } from "../constants/invoiceConstant.js";
|
|
export class Devis {
|
|
|
|
/**
|
|
* Devis object
|
|
* @param myresp instantiate devis object
|
|
*/
|
|
|
|
constructor(myresp) {
|
|
this.id = myresp.id;
|
|
this.user_id = myresp.user_id;
|
|
this.id_nextcloud = myresp.id_nextcloud;
|
|
this.date = ((myresp.date == null || myresp.date.length === 0) ? '-' : myresp.date);
|
|
this.num = ((myresp.num == null || myresp.num.length === 0) ? '-' : myresp.num);
|
|
// defunt
|
|
this.id_defunt = ((myresp.id_defunt == null || myresp.id_defunt.length === 0) ? '-' : myresp.id_defunt);
|
|
this.nom_defunt = ((myresp.nom_defunt == null || myresp.nom_defunt.length === 0) ? '-' : myresp.nom_defunt);
|
|
// fin defunt
|
|
this.cid = ((myresp.cid == null || myresp.cid.length === 0) ? '-' : myresp.cid);
|
|
this.lid = ((myresp.lid == null || myresp.lid.length === 0) ? '-' : myresp.lid);
|
|
this.tid = ((myresp.tid == null || myresp.tid.length === 0) ? '-' : myresp.tid);
|
|
this.nom = ((myresp.nom == null || myresp.nom.length === 0) ? '-' : myresp.nom);
|
|
this.prenom = ((myresp.prenom == null || myresp.prenom.length === 0) ? '-' : myresp.prenom);
|
|
this.nomThanato = Devis.getDevisThanatoFullname(myresp);
|
|
this.version = ((myresp.version == null || myresp.version.length === 0) ? '-' : myresp.version);
|
|
this.lieu = ((myresp.lieu == null || myresp.lieu.length === 0) ? '-' : myresp.lieu);
|
|
this.mentions = Devis.getDevisMentionFromApiResponse(myresp);
|
|
this.baseUrl = generateUrl(`/apps/gestion/devis/${this.id}/show`);
|
|
this.devisFullNumber = ((myresp.devis_full_number != null && myresp.mentions.devis_full_number != 0) ? myresp.devis_full_number : '-');
|
|
|
|
this.devisProduits = Devis.getDevisProduitsString(myresp);
|
|
}
|
|
|
|
static getDevisMentionLabelFromMention(mention){
|
|
let labelValue = mention;
|
|
switch (mention) {
|
|
case FacturedDevisMentionConstant:
|
|
labelValue = "Facturé"
|
|
break;
|
|
case CanceledDevisMentionConstant:
|
|
labelValue = "Annulé"
|
|
break;
|
|
default:
|
|
labelValue = mention
|
|
}
|
|
return labelValue;
|
|
}
|
|
|
|
static getDevisMentionFromApiResponse(myresp){
|
|
let mention = "-";
|
|
if(myresp.mentions != null && myresp.mentions.length > 0){
|
|
mention = myresp.mentions;
|
|
}
|
|
return mention;
|
|
}
|
|
|
|
static getDevisThanatoFullname(myresp){
|
|
let thanatoPrenom = '';
|
|
let thanatoNom = '';
|
|
let thanatoFullName = '';
|
|
if(myresp.nom_thanato != null && myresp.nom_thanato.length != 0){
|
|
thanatoNom = myresp.nom_thanato;
|
|
thanatoFullName += thanatoNom;
|
|
}
|
|
if(myresp.prenom_thanato != null && myresp.prenom_thanato.length != 0){
|
|
thanatoPrenom = myresp.prenom_thanato;
|
|
if(thanatoNom.length > 0 && thanatoPrenom.length > 0){
|
|
thanatoFullName += ' '
|
|
}
|
|
thanatoFullName += thanatoPrenom;
|
|
}
|
|
return (thanatoFullName.length === 0) ? '-' : thanatoFullName;
|
|
}
|
|
|
|
static getDevisMentionCssStyle(mention){
|
|
let style = "display:inline; border-radius: 5px; padding: 8px;";
|
|
if(mention === FacturedDevisMentionConstant){
|
|
style += " background-color:green !important; color: white";
|
|
}
|
|
else if(mention === CanceledDevisMentionConstant){
|
|
style += " background-color:red !important; color: white";
|
|
}
|
|
else{
|
|
style += " background-color:yellow !important";
|
|
}
|
|
return style;
|
|
}
|
|
|
|
static getDevisProduitsString(myresp){
|
|
let devisProduitsAsString = '';
|
|
if(myresp.produits != null && myresp.produits.length > 0){
|
|
let devisProduits = myresp.produits;
|
|
let devisProduitsLength = devisProduits.length;
|
|
for (let i = 0; i < devisProduitsLength; i++) {
|
|
devisProduitsAsString += devisProduits[i].produit_reference;
|
|
if(i != devisProduitsLength - 1){
|
|
devisProduitsAsString += '-';
|
|
}
|
|
}
|
|
}
|
|
return devisProduitsAsString.length === 0 ? '-' : devisProduitsAsString;
|
|
}
|
|
|
|
/**undefined
|
|
* Get datatable row for a devis
|
|
*/
|
|
getDTRow() {
|
|
// let signe_prop = (this.id_nextcloud.toLowerCase() == 'emmanuelle')?'propdevis-emmanuelle':(this.id_nextcloud.toLowerCase() == 'delphine')?'propdevis-delphine':'';
|
|
let myrow = [
|
|
// '<div><div class="'+signe_prop+'">' + this.user_id + '</div></div>',
|
|
'<input class="devisToFacture" data-id= '+ this.user_id + ' type="checkbox" name="devisToFacture" value="' + this.id + '"/>',
|
|
'<div>' + this.user_id + '</div>',
|
|
'<div>' + this.devisFullNumber + '</div>',
|
|
'<input style="margin:0;padding:0;" class="inputDate devisOrFactureInputDate" type="date" value=' + this.date + ' data-table="devis" data-column="date" data-id="' + this.id + '"/>',
|
|
// '<div class="editable" data-table="devis" data-column="num" data-id="' + this.id + '" style="display:inline">' + this.num + '</div>',
|
|
'<div class="loadSelect_listdefunt" data-table="devis" data-column="id_defunt" data-id="' + this.id + '" data-current="' + this.id_defunt + '">'+ this.id_defunt + ' (' + this.nom_defunt + ')</div>',
|
|
'<div class="loadSelect_listclient" data-table="devis" data-column="id_client" data-id="' + this.id + '" data-current="' + this.cid + '">'+ this.cid + ' (' + this.prenom + ' ' + this.nom + ')</div>',
|
|
'<div class="loadSelect_listthanato" data-table="devis" data-column="id_thanato" data-id="' + this.id + '" data-current="' + this.tid + '">'+ this.tid + ' (' + this.nomThanato+ ')</div>',
|
|
'<div class="loadSelect_listlieu" data-table="devis" data-column="id_lieu" data-id="' + this.id + '" data-current="' + this.lid + '">' + this.lieu + '</div>',
|
|
'<div>' + this.devisProduits + '</div>',
|
|
'<div data-table="devis" data-column="mentions" data-id="' + this.id +'" style="'+ Devis.getDevisMentionCssStyle(this.mentions) +'">' +Devis.getDevisMentionLabelFromMention(this.mentions) + '</div>',
|
|
'<div style="display:inline-block;margin-right:0px;width:80%;"><a href="' + this.baseUrl + '"><button>' + t('gestion', 'Open') + '</button></a></div><div data-modifier="devis" data-id=' + this.id + ' data-table="devis" style="display:inline-block;margin-right:0px;" class="deleteItem icon-delete"></div>'
|
|
];
|
|
return myrow;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {*} dt
|
|
*/
|
|
static newDevis(dt) {
|
|
var oReq = new XMLHttpRequest();
|
|
oReq.open('POST', baseUrl + '/devis/insert', true);
|
|
oReq.onload = function(e){
|
|
if (this.status == 200) {
|
|
showDone()
|
|
Devis.loadDevisDT(dt);
|
|
}else{
|
|
showError(this.response);
|
|
}
|
|
};
|
|
oReq.send();
|
|
}
|
|
|
|
/**
|
|
* Load devis ajax
|
|
* @param devisDT devis datatable
|
|
*/
|
|
static loadDevisDT(devisDT) {
|
|
var oReq = new XMLHttpRequest();
|
|
oReq.open('PROPFIND', baseUrl + '/getDevisWithProduits', true);
|
|
oReq.setRequestHeader("Content-Type", "application/json");
|
|
oReq.onload = function(e){
|
|
if (this.status == 200) {
|
|
// let devis_temp = (JSON.parse(JSON.parse(this.response))).filter((d)=>(d.mentions != "facturé"));
|
|
// devis_temp = JSON.stringify(JSON.stringify(devis_temp));
|
|
LoadDT(devisDT, JSON.parse(this.response), Devis);
|
|
}else{
|
|
showError(this.response);
|
|
}
|
|
};
|
|
oReq.send();
|
|
}
|
|
|
|
static getDevis(callback){
|
|
var oReq = new XMLHttpRequest();
|
|
oReq.open('PROPFIND', baseUrl + '/getDevis', true);
|
|
oReq.setRequestHeader("Content-Type", "application/json");
|
|
oReq.onload = function(e){
|
|
if (this.status == 200) {
|
|
let devis_temp = (JSON.parse(JSON.parse(this.response))).filter((d)=>(d.mentions != "facturé"));
|
|
devis_temp = JSON.stringify(JSON.stringify(devis_temp));
|
|
callback(JSON.parse(devis_temp));
|
|
}else{
|
|
showError(this.response);
|
|
}
|
|
};
|
|
oReq.send();
|
|
}
|
|
|
|
static getAllDevis(callback,mentionFilters = []){
|
|
var oReq = new XMLHttpRequest();
|
|
let url = baseUrl + '/getDevis';
|
|
if(mentionFilters.length > 0){
|
|
const queryParams = mentionFilters.map(mention => `mentionFilters[]=${encodeURIComponent(mention)}`).join('&');
|
|
url = `${url}?${queryParams}`;
|
|
}
|
|
oReq.open('PROPFIND', url, true);
|
|
oReq.setRequestHeader("Content-Type", "application/json");
|
|
oReq.onload = function(e){
|
|
if (this.status == 200) {
|
|
let devis_temp = (JSON.parse(JSON.parse(this.response)));
|
|
devis_temp = JSON.stringify(JSON.stringify(devis_temp));
|
|
callback(JSON.parse(devis_temp));
|
|
}else{
|
|
showError(this.response);
|
|
}
|
|
};
|
|
oReq.send();
|
|
}
|
|
|
|
static getAllDevisDelphine(e, callback){
|
|
var oReq = new XMLHttpRequest();
|
|
oReq.open('PROPFIND', baseUrl + '/getDevisDelphine/'+e.target.dataset.id, true);
|
|
oReq.setRequestHeader("Content-Type", "application/json");
|
|
oReq.onload = function(e){
|
|
if (this.status == 200) {
|
|
let devis_temp = (JSON.parse(JSON.parse(this.response)));
|
|
devis_temp = JSON.stringify(JSON.stringify(devis_temp));
|
|
callback(JSON.parse(devis_temp));
|
|
}else{
|
|
showError(this.response);
|
|
}
|
|
};
|
|
oReq.send();
|
|
}
|
|
|
|
static loadDevisList_dnum(e){
|
|
Devis.getDevis( response => {
|
|
var selectElement = document.createElement("select");
|
|
selectElement.dataset.current = e.target.dataset.current;
|
|
selectElement.dataset.id = e.target.dataset.id;
|
|
selectElement.dataset.old = e.target.innerHTML;
|
|
|
|
selectElement.addEventListener("change", el=>{
|
|
if(el.target.value != 0){
|
|
updateDB(el.target.parentElement.dataset.table,
|
|
el.target.parentElement.dataset.column,
|
|
el.target.value,
|
|
el.target.parentElement.dataset.id
|
|
);
|
|
|
|
location.reload();
|
|
|
|
var parentElement = el.target.parentElement;
|
|
parentElement.innerHTML = el.target.options[el.target.selectedIndex].text;
|
|
parentElement.dataset.current = el.target.value;
|
|
}else{
|
|
var parentElement = el.target.parentElement
|
|
parentElement.innerHTML = el.target.dataset.old
|
|
}
|
|
});
|
|
|
|
var option = document.createElement("option");
|
|
option.value = 0;
|
|
option.text = t('gestion', 'Cancel');
|
|
selectElement.appendChild(option);
|
|
|
|
JSON.parse(response).forEach(myresp => {
|
|
var nomClient = (myresp.prenom&&myresp.nom) ? ((myresp.prenom?myresp.prenom:'') + ' ' + (myresp.nom?myresp.nom:'')) : '-';
|
|
var nomDefunt = myresp.nom_defunt ? myresp.nom_defunt : '-';
|
|
var txt = document.createElement("textarea");
|
|
txt.innerHTML = nomDefunt + ' (' + nomClient +')';
|
|
var option = document.createElement("option");
|
|
option.value = myresp.id;
|
|
option.text = txt.value;
|
|
selectElement.appendChild(option);
|
|
});
|
|
|
|
checkSelectPurJs(selectElement);
|
|
|
|
e.target.innerHTML = ''
|
|
e.target.appendChild(selectElement);
|
|
});
|
|
}
|
|
|
|
static loadAllDevisList_dnum(e,mentionFilters = []){
|
|
Devis.getAllDevis( response => {
|
|
var selectElement = document.createElement("select");
|
|
selectElement.dataset.current = e.target.dataset.current;
|
|
selectElement.dataset.id = e.target.dataset.id;
|
|
selectElement.dataset.old = e.target.innerHTML;
|
|
|
|
selectElement.addEventListener("change", el=>{
|
|
if(el.target.value != 0){
|
|
updateDB(el.target.parentElement.dataset.table,
|
|
el.target.parentElement.dataset.column,
|
|
el.target.value,
|
|
el.target.parentElement.dataset.id
|
|
);
|
|
|
|
location.reload();
|
|
|
|
var parentElement = el.target.parentElement;
|
|
parentElement.innerHTML = el.target.options[el.target.selectedIndex].text;
|
|
parentElement.dataset.current = el.target.value;
|
|
}else{
|
|
var parentElement = el.target.parentElement
|
|
parentElement.innerHTML = el.target.dataset.old
|
|
}
|
|
});
|
|
|
|
var option = document.createElement("option");
|
|
option.value = 0;
|
|
option.text = t('gestion', 'Cancel');
|
|
selectElement.appendChild(option);
|
|
|
|
JSON.parse(response).forEach(myresp => {
|
|
var nomClient = (myresp.prenom&&myresp.nom) ? ((myresp.prenom?myresp.prenom:'') + ' ' + (myresp.nom?myresp.nom:'')) : '-';
|
|
var nomDefunt = myresp.nom_defunt ? myresp.nom_defunt : '-';
|
|
var txt = document.createElement("textarea");
|
|
txt.innerHTML = nomDefunt + ' (' + nomClient +')';
|
|
var option = document.createElement("option");
|
|
option.value = myresp.id;
|
|
option.text = txt.value;
|
|
selectElement.appendChild(option);
|
|
});
|
|
|
|
checkSelectPurJs(selectElement);
|
|
|
|
e.target.innerHTML = ''
|
|
e.target.appendChild(selectElement);
|
|
},mentionFilters);
|
|
}
|
|
|
|
static loadDelphineDevisList_dnum(e){
|
|
Devis.getAllDevisDelphine( e, response => {
|
|
var selectElement = document.createElement("select");
|
|
selectElement.dataset.current = e.target.dataset.current;
|
|
selectElement.dataset.id = e.target.dataset.id;
|
|
selectElement.dataset.old = e.target.innerHTML;
|
|
|
|
selectElement.addEventListener("change", el=>{
|
|
if(el.target.value != 0){
|
|
updateDB(el.target.parentElement.dataset.table,
|
|
el.target.parentElement.dataset.column,
|
|
el.target.value,
|
|
el.target.parentElement.dataset.id
|
|
);
|
|
|
|
location.reload();
|
|
|
|
var parentElement = el.target.parentElement;
|
|
parentElement.innerHTML = el.target.options[el.target.selectedIndex].text;
|
|
parentElement.dataset.current = el.target.value;
|
|
}else{
|
|
var parentElement = el.target.parentElement
|
|
parentElement.innerHTML = el.target.dataset.old
|
|
}
|
|
});
|
|
|
|
var option = document.createElement("option");
|
|
option.value = 0;
|
|
option.text = t('gestion', 'Cancel');
|
|
selectElement.appendChild(option);
|
|
|
|
JSON.parse(response).forEach(myresp => {
|
|
var nomClient = (myresp.prenom&&myresp.nom) ? ((myresp.prenom?myresp.prenom:'') + ' ' + (myresp.nom?myresp.nom:'')) : '-';
|
|
var nomDefunt = myresp.nom_defunt ? myresp.nom_defunt : '-';
|
|
var txt = document.createElement("textarea");
|
|
txt.innerHTML = nomDefunt + ' (' + nomClient +')';
|
|
var option = document.createElement("option");
|
|
option.value = myresp.id;
|
|
option.text = txt.value;
|
|
selectElement.appendChild(option);
|
|
});
|
|
|
|
checkSelectPurJs(selectElement);
|
|
|
|
e.target.innerHTML = ''
|
|
e.target.appendChild(selectElement);
|
|
});
|
|
}
|
|
}
|