import { showError } from "@nextcloud/dialogs";
import {
baseUrl,
checkSelectPurJs,
LoadDT,
showDone,
} from "../modules/mainFunction.mjs";
import { updateDB } from "../modules/ajaxRequest.mjs";
export class ClientGroupFacturation {
/**
*
* @param myresp instantiate client group facturation object
*/
constructor(myresp) {
this.id = myresp.id;
this.clientGroupFacturationName =
myresp.group_facturation_name.length === 0
? "-"
: myresp.group_facturation_name;
this.phoneNumber =
myresp.phone_number != null && myresp.phone_number.length > 0
? myresp.phone_number
: "-";
this.address =
myresp.address != null && myresp.address.length > 0
? myresp.address
: "-";
this.postalCode =
myresp.postal_code != null && myresp.postal_code.length > 0
? myresp.postal_code
: "-";
this.city =
myresp.city != null && myresp.city.length > 0 ? myresp.city : "-";
this.email =
myresp.email != null && myresp.email.length > 0 ? myresp.email : "-";
this.siretNumber =
myresp.siret_number != null && myresp.siret_number.length > 0
? myresp.siret_number
: "-";
this.tvaIntraCommu =
myresp.tva_intracommu != null && myresp.tva_intracommu.length > 0
? myresp.tva_intracommu
: "-";
this.templateTypeKey =
myresp.fk_template_type_key != null &&
myresp.fk_template_type_key.length > 0
? myresp.fk_template_type_key
: "-";
}
/**
* Get datatable row for a client group facturation
*/
getDTRow() {
let clientGroupFacturationRow = [
"
" + this.id + "
",
'' +
this.clientGroupFacturationName +
"
",
'' +
this.address +
"
",
'' +
this.city +
"
",
'' +
this.postalCode +
"
",
'' +
this.email +
"
",
'' +
this.phoneNumber +
"
",
'' +
this.siretNumber +
"
",
'' +
this.tvaIntraCommu +
"
",
'' +
this.templateTypeKey +
"
",
'',
];
return clientGroupFacturationRow;
}
/**
*
* @param {*} clientGroupFacturationDatatable
*/
static loadClientGroupFacturationDatatable(clientGroupFacturationDatatable) {
var oReq = new XMLHttpRequest();
oReq.open("PROPFIND", baseUrl + "/client/getClientGroupFacturations", true);
oReq.setRequestHeader("Content-Type", "application/json");
oReq.onload = function (e) {
if (this.status == 200) {
LoadDT(
clientGroupFacturationDatatable,
JSON.parse(this.response),
ClientGroupFacturation
);
} else {
showError(this.response);
}
};
oReq.send();
}
static getClientGroupFacturations(callback) {
var oReq = new XMLHttpRequest();
oReq.open("PROPFIND", baseUrl + "/client/getClientGroupFacturations", true);
oReq.setRequestHeader("Content-Type", "application/json");
oReq.onload = function (e) {
if (this.status == 200) {
callback(JSON.parse(this.response));
} else {
showError(this.response);
}
};
oReq.send();
}
/**
*
* @param {*} lid
*/
static loadClientGroupFacturationListToSelect(e) {
ClientGroupFacturation.getClientGroupFacturations((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 txt = document.createElement("textarea");
txt.innerHTML = myresp.group_facturation_name;
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);
});
}
/**
*
* @param {*} dt
*/
static createDefaultClientGroupFacturation(dt) {
var oReq = new XMLHttpRequest();
oReq.open(
"POST",
baseUrl + "/client/createDefaultClientGroupFacturation",
true
);
oReq.onload = function (e) {
if (this.status == 200) {
showDone();
ClientGroupFacturation.loadClientGroupFacturationDatatable(dt);
} else {
showError(this.response);
}
};
oReq.send();
}
static getClientTemplateTypes(callback) {
var oReq = new XMLHttpRequest();
oReq.open("PROPFIND", baseUrl + "/client/getClientTemplateTypes", true);
oReq.setRequestHeader("Content-Type", "application/json");
oReq.onload = function (e) {
if (this.status == 200) {
callback(JSON.parse(this.response));
} else {
showError(this.response);
}
};
oReq.send();
}
static loadClientTemplateTypesIntoSelect(e) {
ClientGroupFacturation.getClientTemplateTypes((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 != "") {
updateDB(
el.target.parentElement.dataset.table,
el.target.parentElement.dataset.column,
el.target.value,
el.target.parentElement.dataset.id
);
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 = "";
option.text = t("gestion", "Cancel");
selectElement.appendChild(option);
JSON.parse(response).forEach((myresp) => {
var txt = document.createElement("textarea");
txt.innerHTML = myresp.template_type_key;
var option = document.createElement("option");
option.value = myresp.template_type_key;
option.text = txt.value;
selectElement.appendChild(option);
});
checkSelectPurJs(selectElement);
e.target.innerHTML = "";
e.target.appendChild(selectElement);
});
}
}