Files
CRM/NoteDeFraisAjax.php
BACHIR SOULDI 2794e62571 first commit
2025-09-28 08:49:37 +01:00

567 lines
23 KiB
PHP

<?php
require_once 'include/utils/utils.php';
require_once 'includes/Loader.php';
vimport ('includes.runtime.EntryPoint');
require_once 'SUtiles.php';
@session_start();
$current_user = Users_Record_Model::getCurrentUserModel();
$roleid = $current_user->get('roleid');
$userId = $current_user->get('id');
global $adb;
function Existe_note_de_frais(array $attr){
global $adb;
$sql="SELECT * FROM `note_de_frais` WHERE `assigned_user_id` =? and `mois`=? and `annee`=? ";
$role_result = $adb->pquery($sql, array($attr['assigned_user_id'], $attr['mois'] , $attr['annee']));
$numrow = $adb->num_rows($role_result);
return $numrow;
}
/*
function getResponsableId($roleid){
global $adb;
$parentRoles=getParentRole($roleid);
$roleid = end($parentRoles);
$query = "select vtiger_users.id,vtiger_users.first_name ,vtiger_users.last_name
from vtiger_user2role
inner join vtiger_users on vtiger_users.id=vtiger_user2role.userid
where roleid='{$roleid}' and status = 'Active' ";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result[0]['id'];
}
*/
function getSuperieurHierachique($userId){
global $adb;
$query = "select vtiger_users.reports_to_id as id_responsable
from vtiger_users
where id='{$userId}' ";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
if($result[0]['id_responsable'] == '' || $result[0]['id_responsable'] == null){
return null;
}
return $result[0]['id_responsable'];
}
/*
function getRole($userId){
global $adb;
$query = "select vtiger_user2role.roleid from vtiger_user2role
inner join vtiger_users on vtiger_users.id=vtiger_user2role.userid
where userid='{$userId}' ";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result[0]['roleid'];
}
*/
function Parametrages(){
global $adb;
$query = "SELECT * FROM `note_de_frais_parametres`";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result[0];
}
function countLignes($id ,$type){
global $adb;
if($type == "Autre"){
$query = "SELECT count(id) as lignes FROM lignes_note_de_frais WHERE id_note_de_frais = {$id} and type <> '".$type."'";
}else{
$query = "SELECT count(id) as lignes FROM lignes_note_de_frais WHERE id_note_de_frais = {$id} and type = '".$type."'";
}
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result[0]['lignes'];
}
function getNdf($id_note_de_frais){
global $adb;
$query5 = "SELECT * FROM note_de_frais where id_note_de_frais = {$id_note_de_frais} ";
$result5 =$adb->query($query5);
$res=array();
while ($recordinfo = $adb->fetch_array($result5)) {
$res[] = $recordinfo;
}
return $res[0];
}
function EtatPdf($mois ,$annee){
global $adb;
$query = "SELECT CONCAT ( vtiger_users.last_name ,' ' ,vtiger_users.first_name) as fullname ,note_de_frais.total_final ,
vtiger_users.phone_fax as matricule
FROM vtiger_users
LEFT JOIN note_de_frais ON vtiger_users.id = note_de_frais.assigned_user_id
WHERE note_de_frais.mois = '".$mois."' and note_de_frais.annee ='".$annee."' AND etat=2";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result;
}
function change_Etat($numbre , $id){
global $adb;
$sql = "UPDATE note_de_frais SET etat= ? WHERE id_note_de_frais = ? ";
$adb->pquery($sql,array($numbre , $id));
return null;
}
function getIds($mois,$annee){
global $adb;
$query = "SELECT id_note_de_frais FROM `note_de_frais` where mois = '".$mois."' and annee = '".$annee."'";
$sql_get_result = $adb->query($query);
$result = array();
while ($recordinfo = $adb->fetch_array($sql_get_result)) {
$result[] = $recordinfo;
}
return $result;
}
function update_calcul($id_note_de_frais , array $parm , $nbr_jour){
global $adb;
//Frais HR et SR and Vehicule et Indemnite
// 1-Prise en charge S.R
$nbrSR = countLignes($id_note_de_frais,'Prise en charge S.R');
$sql3="UPDATE note_de_frais_types SET
nombre =".$nbrSR.",
indemnite = ".$parm['sr'].",
total =".$nbrSR."*".$parm['sr']."
WHERE id_note_de_frais = ? and ordre=1 ";
$adb->pquery($sql3,array($id_note_de_frais));
// 2-Prise en charge H.R
$nbrHR = countLignes($id_note_de_frais,'Prise en charge H.R');
$sql4="UPDATE note_de_frais_types SET
nombre =".$nbrHR.",
indemnite = ".$parm['hr'].",
total =".$nbrHR."*".$parm['hr']."
WHERE id_note_de_frais = ? and ordre=2 ";
$adb->pquery($sql4,array($id_note_de_frais));
$nbrSRandHR = countLignes($id_note_de_frais,'Autre');
// 3-Parking
$sql5="UPDATE note_de_frais_types SET
indemnite = ".$parm['parking'].",
total = (".$nbrSRandHR."*".$parm['parking'].")/{$nbr_jour}
WHERE id_note_de_frais = ? and ordre=3 ";
$adb->pquery($sql5,array($id_note_de_frais));
// 3-Forfait Carburant
$sql6="UPDATE note_de_frais_types SET
indemnite = ".$parm['forfait_carburant'].",
total =(".$nbrSRandHR."*".$parm['forfait_carburant'].")/{$nbr_jour}
WHERE id_note_de_frais = ? and ordre=4 ";
$adb->pquery($sql6,array($id_note_de_frais));
// 3-Forfait entretien
$sql7="UPDATE note_de_frais_types SET
indemnite = ".$parm['forfait_entretien'].",
total =(".$nbrSRandHR."*".$parm['forfait_entretien'].")/{$nbr_jour}
WHERE id_note_de_frais = ? and ordre=5 ";
$adb->pquery($sql7,array($id_note_de_frais));
// -Indemnite kilometrique
$sql8="UPDATE note_de_frais_types SET
nombre =(SELECT sum(kilometrage) FROM lignes_note_de_frais WHERE id_note_de_frais = '".$id_note_de_frais."'),
total = ((SELECT sum(kilometrage) FROM lignes_note_de_frais WHERE id_note_de_frais = '".$id_note_de_frais."')*".$parm['indemnite_kilometrique']."),
indemnite = ".$parm['indemnite_kilometrique']."
WHERE id_note_de_frais = ? and ordre=6 ";
$adb->pquery($sql8,array($id_note_de_frais));
// totals
$sql9="UPDATE `note_de_frais` SET
total_1=(SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$id_note_de_frais} and ordre in(1,2)),
total_2=(SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$id_note_de_frais} and ordre in(3,4,5,6)),
total_3=(SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$id_note_de_frais} and ordre in(7,8,9,10,11)),
total_final= (SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$id_note_de_frais})
WHERE id_note_de_frais= ? ";
$adb->pquery($sql9,array($id_note_de_frais));
}
function processus_validation(array $resNDF , array $attr){
global $adb;
//validations
$id_ndf = $attr['id_note_de_frais'];
if(isset($attr['valid0']) && $attr['valid0'] == 1 && $resNDF['is_valid_0'] == null){
if($resNDF['id_validateur_1'] <> null){
$sql0 = "UPDATE `note_de_frais` SET is_valid_0= 1 , avancement = 1
WHERE id_note_de_frais = ? ";
}else{
$sql0 = "UPDATE `note_de_frais` SET is_valid_0= 1 , avancement = 2
WHERE id_note_de_frais = ? ";
}
$perm0 = array($id_ndf);
$adb->pquery($sql0,$perm0);
change_Etat(1 , $id_ndf);
}
if(isset($attr['valid1']) && $attr['valid1'] == 1 && $resNDF['is_valid_1'] == null){
$sql0 = "UPDATE `note_de_frais` SET is_valid_1= 1 , avancement = 2
WHERE id_note_de_frais = ? ";
$perm0 = array($id_ndf);
$adb->pquery($sql0,$perm0);
}
if(isset($attr['valid2']) && $attr['valid2'] == 1 && $resNDF['is_valid_2'] == null){
$sql0 = "UPDATE `note_de_frais` SET is_valid_2= 1 , avancement = 3
WHERE id_note_de_frais = ? ";
$perm0 = array($id_ndf);
$adb->pquery($sql0,$perm0);
}
if(isset($attr['valid3']) && $attr['valid3'] == 1 && $resNDF['is_valid_3'] == null){
$sql0 = "UPDATE `note_de_frais` SET is_valid_3= 1 , avancement = 4
WHERE id_note_de_frais = ? ";
$perm0 = array($id_ndf);
$adb->pquery($sql0,$perm0);
}
if(isset($attr['valid4']) && $attr['valid4'] == 1 && $resNDF['is_valid_4'] == null){
$sql0 = "UPDATE `note_de_frais` SET is_valid_4= 1 , avancement = 5
WHERE id_note_de_frais = ? ";
$perm0 = array($id_ndf);
$adb->pquery($sql0,$perm0);
change_Etat(2 , $id_ndf);
}
}
$Parametrages = Parametrages();
$nbr_jour = $Parametrages['jour'];
switch ($_GET['action']) {
case "addnotedefrais":
$numrow = Existe_note_de_frais($_POST);
//$idr = getResponsableId(getRole($_POST['assigned_user_id']));
$idr = getSuperieurHierachique($_POST['assigned_user_id']);
if($numrow == 0){
$sql = "INSERT INTO `note_de_frais`(`assigned_user_id`, `mois`, `annee`, `region`, `cree_par`, `date_creation` ,
`id_validateur_0`, `id_validateur_1` , `id_validateur_2` , `id_validateur_3`,
`id_validateur_4` ,`etat`)
values (?,?,?,?,?,NOW(), ? ,? ,?,?,?,0)";
$adb->pquery($sql,array($_POST['assigned_user_id'], $_POST['mois'] , $_POST['annee'] , $_POST['region'] , $userId ,
$_POST['assigned_user_id'], $idr ,$Parametrages['id_validateur_2'] , $Parametrages['id_validateur_3'] ,$Parametrages['id_validateur_4']));
$last_id = $adb->getLastInsertID();
if(!empty($last_id)){
for ($x = 0; $x < count($_POST['localite']) ; $x++) {
$sql2 = "INSERT INTO lignes_note_de_frais(id_note_de_frais, type, jour, localite, kilometrage)
VALUES (?,?,?,?,?)";
$adb->pquery($sql2,array($last_id , $_POST["type"][$x], $_POST["jour"][$x] , $_POST["localite"][$x] , $_POST["kilometrage"][$x]));
}
}
//Frais HR et SR and Vehicule et Indemnite
// 1-Prise en charge S.R
$nbrSR = countLignes($last_id,'Prise en charge S.R');
$sql3 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Prise en charge S.R',".$nbrSR.",".$Parametrages['sr'].",".$nbrSR."*".$Parametrages['sr'].",1)";
$adb->pquery($sql3,array($last_id));
// 2-Prise en charge H.R
$nbrHR = countLignes($last_id,'Prise en charge H.R');
$sql4 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Prise en charge H.R',".$nbrHR.",".$Parametrages['hr'].",".$nbrHR."*".$Parametrages['hr'].",2)";
$adb->pquery($sql4,array($last_id));
// 3-Parking
$nbrSRandHR = countLignes($last_id,'Autre');
$sql5 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Parking', 1,".$Parametrages['parking'].",(".$nbrSRandHR."*".$Parametrages['parking'].")/{$nbr_jour}, 3)";
$adb->pquery($sql5,array($last_id));
// 4-Forfait Carburant
$sql6 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Forfait Carburant', 1, ".$Parametrages['forfait_carburant'].",(".$nbrSRandHR."*".$Parametrages['forfait_carburant'].")/{$nbr_jour}, 4)";
$adb->pquery($sql6,array($last_id));
// 4-Forfait entretien
$sql7 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Forfait entretien', 1, ".$Parametrages['forfait_entretien'].",(".$nbrSRandHR."*".$Parametrages['forfait_entretien'].")/{$nbr_jour}, 5)";
$adb->pquery($sql7,array($last_id));
// -Indemnite kilometrique
$sql8 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `nombre`, `indemnite`, `total`, `ordre`)
VALUES (?,'Indemnite kilometrique', (SELECT sum(kilometrage) FROM lignes_note_de_frais WHERE id_note_de_frais = {$last_id}),".$Parametrages['indemnite_kilometrique'].",
((SELECT sum(kilometrage) FROM lignes_note_de_frais WHERE id_note_de_frais = {$last_id})*".$Parametrages['indemnite_kilometrique']."),
6)";
$adb->pquery($sql8,array($last_id));
// -Bureau, P et T
$sql10 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `ordre`) VALUES (?,'bureau', 7)";
$adb->pquery($sql10,array($last_id));
// -Internet
$sql11 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `ordre`) VALUES (?,'internet', 8)";
$adb->pquery($sql11,array($last_id));
// -Divers
$sql12 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `ordre`) VALUES (?,'divers', 9)";
$adb->pquery($sql12,array($last_id));
// -Taxi
$sql13 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `ordre`) VALUES (?,'taxi', 10)";
$adb->pquery($sql13,array($last_id));
// -Facture Hotel
$sql14 ="INSERT INTO `note_de_frais_types`(`id_note_de_frais`, `type`, `ordre`) VALUES (?,'facture', 11)";
$adb->pquery($sql14,array($last_id));
// totals
$sql9="UPDATE `note_de_frais` SET
total_1=(SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$last_id} and ordre in(1,2)),
total_2=(SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$last_id} and ordre in(3,4,5,6)),
total_final= (SELECT sum(total) FROM note_de_frais_types WHERE id_note_de_frais = {$last_id})
WHERE id_note_de_frais= ? ";
$adb->pquery($sql9,array($last_id));
echo "0";
}else if($numrow > 0){
echo "1";
}
break;
case "updatenotedefrais":
$idsToDelete = explode(",", $_GET['idsToDelete']);
$commentaire_0 = !empty($_POST['commentaire_0']) ? $_POST['commentaire_0'] : null;
$commentaire_1 = !empty($_POST['commentaire_1']) ? $_POST['commentaire_1'] : null;
$commentaire_2 = !empty($_POST['commentaire_2']) ? $_POST['commentaire_2'] : null;
$commentaire_3 = !empty($_POST['commentaire_3']) ? $_POST['commentaire_3'] : null;
$commentaire_4 = !empty($_POST['commentaire_4']) ? $_POST['commentaire_4'] : null;
$sql = "UPDATE `note_de_frais` SET region= ? ,modifier_par= ? ,date_modification= NOW() ,commentaire_0 = ? ,commentaire_1 = ? ,
commentaire_2 = ? , commentaire_3 = ? , commentaire_4 = ?
WHERE id_note_de_frais = ? ";
$perm = array( $_POST['region'] ,
$userId ,
$commentaire_0 ,
$commentaire_1 ,
$commentaire_2 ,
$commentaire_3 ,
$commentaire_4 ,
$_POST['id_note_de_frais']
);
$adb->pquery($sql,$perm);
for ($y = 0; $y < count($_POST["localite"]); $y++) {
//Insert
if($_POST["ids"][$y] == 0){
$sql2 = "INSERT INTO lignes_note_de_frais(id_note_de_frais, type, jour, localite, kilometrage)
VALUES (?,?,?,?,?)";
$adb->pquery($sql2,array($_POST['id_note_de_frais'] , $_POST["type"][$y], $_POST["jour"][$y] ,
$_POST["localite"][$y] , $_POST["kilometrage"][$y]));
//Update
}elseif($_POST["ids"][$y] != 0){
$sql3 = "UPDATE `lignes_note_de_frais` SET `type`= ?,`jour`= ?,`localite`= ?,`kilometrage`= ? WHERE id = ?";
$adb->pquery($sql3,array($_POST['type'][$y], $_POST['jour'][$y] , $_POST['localite'][$y] ,
$_POST['kilometrage'][$y] , $_POST['ids'][$y]));
}
}
//Deleted
for ($c = 0; $c< count($idsToDelete); $c++) {
$query = "DELETE FROM lignes_note_de_frais WHERE id = '".$idsToDelete[$c]."'";
$adb->pquery($query);
}
// -Bureau, P et T
$bureau_c = !empty($_POST['bureau_c']) ? $_POST['bureau_c'] : null;
$bureau_t = !empty($_POST['bureau_t']) ? $_POST['bureau_t'] : null;
$sql10="UPDATE note_de_frais_types SET
commentaire = ?, total = ? WHERE id_note_de_frais = ? and ordre=7 ";
$adb->pquery($sql10,array($bureau_c , $bureau_t , $_POST['id_note_de_frais']));
// -internet
$internet_c = !empty($_POST['internet_c']) ? $_POST['internet_c'] : null;
$internet_t = !empty($_POST['internet_t']) ? $_POST['internet_t'] : null;
$sql11="UPDATE note_de_frais_types SET
commentaire = ?, total = ? WHERE id_note_de_frais = ? and ordre=8 ";
$adb->pquery($sql11,array($internet_c , $internet_t , $_POST['id_note_de_frais']));
// -divers
$divers_c = !empty($_POST['divers_c']) ? $_POST['divers_c'] : null;
$divers_t = !empty($_POST['divers_t']) ? $_POST['divers_t'] : null;
$sql12="UPDATE note_de_frais_types SET
commentaire = ?, total = ? WHERE id_note_de_frais = ? and ordre=9 ";
$adb->pquery($sql12,array($divers_c , $divers_t , $_POST['id_note_de_frais']));
// -taxi
$taxi_c = !empty($_POST['taxi_c']) ? $_POST['taxi_c'] : null;
$taxi_t = !empty($_POST['taxi_t']) ? $_POST['taxi_t'] : null;
$sql13="UPDATE note_de_frais_types SET
commentaire = ?, total = ? WHERE id_note_de_frais = ? and ordre=10 ";
$adb->pquery($sql13,array($taxi_c , $taxi_t , $_POST['id_note_de_frais']));
// -facture Hotel
$facture_c = !empty($_POST['facture_c']) ? $_POST['facture_c'] : null;
$facture_t = !empty($_POST['facture_t']) ? $_POST['facture_t'] : null;
$sql14="UPDATE note_de_frais_types SET
commentaire = ?, total = ? WHERE id_note_de_frais = ? and ordre=11 ";
$adb->pquery($sql14,array($facture_c , $facture_t , $_POST['id_note_de_frais']));
update_calcul($_POST['id_note_de_frais'] , $Parametrages , $nbr_jour);
//validations
$resNDF = getNdf($_POST['id_note_de_frais']);
processus_validation($resNDF , $_POST);
break;
case "updateparametrage":
//mettre a jour les parametres
$sql9="UPDATE `note_de_frais_parametres`
SET `jour`= ?,`sr`=?,`hr`=?,`parking`=?,`forfait_carburant`=?,
`forfait_entretien`=?,`indemnite_kilometrique`=? ";
$adb->pquery($sql9,array($_POST['jour'] , $_POST['sr'],$_POST['hr'] ,$_POST['parking'],
$_POST['forfait_carburant'] ,$_POST['forfait_entretien'],$_POST['indemnite_kilometrique']));
$Parametrages = Parametrages();
$nbr_jour = $Parametrages['jour'];
//relancer le calculement
$ids = getIds($_POST['mois1'],$_POST['annee1']);
$num_ids = count($ids);
if($num_ids > 0){
for($k=0 ;$k < $num_ids ; $k++){
update_calcul($ids[$k]['id_note_de_frais'] , $Parametrages , $nbr_jour);
}
}
break;
case "exportpdf":
$users = EtatPdf($_POST['mois'] ,$_POST['annee']);
echo'<table id="etat" >
<tr>
<th>Matricule</th>
<th>Nom & Prénom</th>
<th>Montant</th>
</tr>';
for ($i = 0 ; $i < count($users) ; $i++) {
echo " <tr>
<td>".$users[$i]['matricule']."</td>
<td>".$users[$i][0]."</td>
<td>".$users[$i][1]."</td>
</tr>";
}
echo'</table>';
break;
case "updateworkflow": //flux de validation
$sql9="UPDATE note_de_frais_parametres SET id_validateur_2 = ?,id_validateur_3 = ?,id_validateur_4= ? ";
$adb->pquery($sql9,array($_POST['id_validateur_2'] , $_POST['id_validateur_3'],$_POST['id_validateur_4']));
break;
case "updateavancement": //update Avancement
//supprimer les validations
if($_GET['new_avancement'] < $_GET['old_avancement']){
for($i = $_GET['new_avancement'] ; $i < $_GET['old_avancement']; $i++){
$sql="UPDATE note_de_frais SET is_valid_".$i." = null where id_note_de_frais = ? ";
$adb->pquery($sql,array( $_GET['id'] ));
}
}
if($_GET['new_avancement'] == 0){
change_Etat(0 , $_GET['id']);
$etat = 0;
}elseif($_GET['new_avancement'] == 1 or $_GET['new_avancement'] == 2 or
$_GET['new_avancement'] == 3 or $_GET['new_avancement'] == 4){
change_Etat(1 , $_GET['id']);
$etat = 1;
}elseif($_GET['new_avancement'] == 5){
change_Etat(2 , $_GET['id']);
$etat = 2;
}
$sql9="UPDATE note_de_frais SET avancement = ? where id_note_de_frais = ? ";
$adb->pquery($sql9,array($_GET['new_avancement'] , $_GET['id'] ));
header('Content-type: application/json');
echo json_encode($etat);
break;
}
?>