import { updateDB } from "../modules/ajaxRequest.mjs"; import { baseUrl, checkSelectPurJs, LoadDT, removeOptions, showDone } from "../modules/mainFunction.mjs"; import 'select2/dist/css/select2.css'; import 'select2'; export class Client { /** * * @param myresp instantiate client object */ constructor(myresp) { this.id = myresp.id; this.code_comptable = ((myresp.code_comptable && myresp.code_comptable.length > 0) ? myresp.code_comptable : '-'); // AJOUTÉ this.agence = ((myresp.agence && myresp.agence.length > 0) ? myresp.agence : '-'); // AJOUTÉ this.entreprise = ((myresp.entreprise && myresp.entreprise.length > 0) ? myresp.entreprise : '-'); this.prenom = ((myresp.prenom && myresp.prenom.length > 0) ? myresp.prenom : '-'); this.nom = ((myresp.nom && myresp.nom.length > 0) ? myresp.nom : '-'); this.legal_one = ((myresp.legal_one && myresp.legal_one.length > 0) ? myresp.legal_one : '-'); this.telephone = ((myresp.telephone && myresp.telephone.length > 0) ? myresp.telephone : '-'); this.mail = ((myresp.mail && myresp.mail.length > 0) ? myresp.mail : '-'); this.adresse = ((myresp.adresse && myresp.adresse.length > 0) ? myresp.adresse : '-'); this.is_tva = myresp.is_tva !== undefined && myresp.is_tva !== null ? myresp.is_tva : 0; this.clientGroupName = ((myresp.client_group_name && myresp.client_group_name.length > 0) ? myresp.client_group_name : '-'); this.clientGroupFacturationName = ((myresp.client_group_facturation_name && myresp.client_group_facturation_name.length > 0) ? myresp.client_group_facturation_name : '-'); let clientGroupId = 0; if(myresp.fk_client_group_id != null && myresp.fk_client_group_id.length > 0){ clientGroupId = myresp.fk_client_group_id } this.clientGroupId = clientGroupId; let clientGroupFacturationId = 0; if(myresp.fk_client_group_facturation_id != null && myresp.fk_client_group_facturation_id.length > 0){ clientGroupFacturationId = myresp.fk_client_group_facturation_id } this.clientGroupFacturationId = clientGroupFacturationId; } /** * Get datatable row for a client */ getDTRow() { let myrow = [ '', '
' + this.id + '
', '
' + this.code_comptable + '
', '
' + this.agence + '
', '
' + this.prenom + '
', '
' + this.nom + '
', '
' + this.entreprise + '
', '
' + this.legal_one + '
', '
' + this.telephone + '
', '
' + this.mail + '
', '
' + this.adresse + '
', '
', '
' + this.clientGroupName + '
', '
' + this.clientGroupFacturationName + '
', '
' // MODIFIÉ ]; return myrow; } /** * * @param {*} dt */ static newClient(dt) { var oReq = new XMLHttpRequest(); oReq.open('POST', baseUrl + '/client/insert', true); oReq.onload = function(e){ if (this.status == 200) { showDone() Client.loadClientDT(dt); }else{ showError(this.response); } }; oReq.send(); } /** * * @param {*} clientDT */ static loadClientDT(clientDT) { var oReq = new XMLHttpRequest(); oReq.open('PROPFIND', baseUrl + '/getClients', true); oReq.setRequestHeader("Content-Type", "application/json"); oReq.onload = function(e){ if (this.status == 200) { const data = JSON.parse(this.response); LoadDT(clientDT, data, Client); }else{ showError(this.response); } }; oReq.send(); } /** * * @param {*} callback */ static getClients(callback) { var oReq = new XMLHttpRequest(); oReq.open('PROPFIND', baseUrl + '/getClients', true); oReq.setRequestHeader("Content-Type", "application/json"); oReq.onload = function(e){ if (this.status == 200) { const data = JSON.parse(this.response); callback(data); }else{ showError(this.response); } }; oReq.send(); } /** * * @param {*} id */ static getClientByIdDevis(id) { var myData = { id: id, }; $.ajax({ url: baseUrl + '/clientbyiddevis', type: 'POST', contentType: 'application/json', data: JSON.stringify(myData) }).done(function (response, code) { $.each(JSON.parse(response), function (arrayID, myresp) { $("#nomprenom").html(myresp.prenom + ' ' + myresp.nom); $("#nomcli").html(myresp.prenom.toUpperCase() + ' ' +myresp.nom.toUpperCase()); $("#nomprenom").attr('data-id', id); $("#entreprise").html(myresp.entreprise); $("#etp").html(myresp.entreprise); $("#adresse").html(myresp.adresse); $("#mail").html(myresp.mail); $("#idcli").html(myresp.id); $('#dateContext').html(myresp.date); $("#telephone").html(myresp.telephone); $("#legal_one").html(myresp.legal_one); $("#pdf").attr("data-folder", myresp.num); if ($("#factureid").length) { $("#pdf").data('name', myresp.entreprise + "_" + $("#factureid").text() + "_v" + $('#factureversion').text()); } else { $("#pdf").data('name', myresp.entreprise + "_" + myresp.num + "_v" + $('#devisversion').text()); } }); }).fail(function (response, code) { showError(response); }); } /** * * @param {*} cid */ static loadClientList_cid(e){ Client.getClients(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; var option = document.createElement("option"); option.value = 0; option.text = t('gestion', 'Cancel'); selectElement.appendChild(option); JSON.parse(response).forEach(myresp => { var txt = document.createElement("textarea"); txt.innerHTML = myresp.prenom + ' ' + myresp.nom; 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); $(selectElement).select2(); $(selectElement).on("select2:select", function (event) { var el = event.target; if (el.value != 0) { updateDB(el.parentElement.dataset.table, el.parentElement.dataset.column, el.value, el.parentElement.dataset.id ); var parentElement = el.parentElement; parentElement.innerHTML = el.value + " " + el.options[el.selectedIndex].text; parentElement.dataset.current = el.value; } else { var parentElement = el.parentElement; parentElement.innerHTML = el.dataset.old; } }); }); } }