208 lines
8.6 KiB
JavaScript
208 lines
8.6 KiB
JavaScript
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 = [
|
|
'<input class="clientToExport" data-id= '+ this.id + ' type="checkbox" name="clientToExport" value="' + this.id + '"/>',
|
|
'<div>' + this.id + '</div>',
|
|
'<div class="editable" data-table="client" data-column="code_comptable" data-id="' + this.id + '">' + this.code_comptable + '</div>',
|
|
'<div class="editable" data-table="client" data-column="agence" data-id="' + this.id + '">' + this.agence + '</div>',
|
|
'<div class="editable" data-table="client" data-column="prenom" data-id="' + this.id + '">' + this.prenom + '</div>',
|
|
'<div class="editable" data-table="client" data-column="nom" data-id="' + this.id + '">' + this.nom + '</div>',
|
|
'<div class="editable" data-table="client" data-column="entreprise" data-id="' + this.id + '">' + this.entreprise + '</div>',
|
|
'<div class="editable" data-table="client" data-column="legal_one" data-id="' + this.id + '">' + this.legal_one + '</div>',
|
|
'<div class="editable" data-table="client" data-column="telephone" data-id="' + this.id + '">' + this.telephone + '</div>',
|
|
'<div class="editable" data-table="client" data-column="mail" data-id="' + this.id + '">' + this.mail + '</div>',
|
|
'<div class="editable" data-table="client" data-column="adresse" data-id="' + this.id + '">' + this.adresse + '</div>',
|
|
'<div><input type="checkbox" class="gestion-checkbox" data-table="client" data-column="is_tva" data-id="' + this.id + '" ' + (this.is_tva == 1 || this.is_tva == true ? 'checked' : '') + ' /></div>',
|
|
'<div class="selectClientGroupList" data-table="client" data-column="fk_client_group_id" data-id="' + this.id + '" data-current="' + this.clientGroupId + '">' + this.clientGroupName + '</div>',
|
|
'<div class="selectClientGroupFacturationList" data-table="client" data-column="fk_client_group_facturation_id" data-id="' + this.id + '" data-current="' + this.clientGroupFacturationId + '">' + this.clientGroupFacturationName + '</div>',
|
|
'<div><center><div data-modifier="client" data-id=' + this.id + ' data-table="client" style="display:inline-block;margin-right:0px;" class="deleteItem icon-delete"></div></center></div>' // 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;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
} |