<?php
// This script and data application were generated by AppGini 5.81
// Download AppGini for free from https://bigprof.com/appgini/download/

	$currDir=dirname(__FILE__);
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");
	@include("$currDir/hooks/expenses.php");
	include("$currDir/expenses_dml.php");

	// mm: can the current member access this page?
	$perm=getTablePermissions('expenses');
	if(!$perm[0]) {
		echo error_message($Translation['tableAccessDenied'], false);
		echo '<script>setTimeout("window.location=\'index.php?signOut=1\'", 2000);</script>';
		exit;
	}

	$x = new DataList;
	$x->TableName = "expenses";

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`expenses`.`idexpense`" => "idexpense",
		"`expenses`.`type`" => "type",
		"`expenses`.`nature`" => "nature",
		"if(`expenses`.`date_saisie`,date_format(`expenses`.`date_saisie`,'%d/%m/%Y'),'')" => "date_saisie",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* Nom & Pr&#233;nom */" => "client",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* Date recrutement */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"`expenses`.`amount`" => "amount",
		"if(`expenses`.`date_depart`,date_format(`expenses`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"if(`expenses`.`date_retour`,date_format(`expenses`.`date_retour`,'%d/%m/%Y'),'')" => "date_retour",
		"if(`expenses`.`date_depart_derniere`,date_format(`expenses`.`date_depart_derniere`,'%d/%m/%Y'),'')" => "date_depart_derniere",
		"`expenses`.`adresse_absence`" => "adresse_absence",
		"`expenses`.`departement`" => "departement",
		"`expenses`.`signataire`" => "signataire",
		"`expenses`.`observations`" => "observations",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`expenses`.`idexpense`',
		2 => 2,
		3 => 3,
		4 => '`expenses`.`date_saisie`',
		5 => '`dossier_individuel1`.`matricule`',
		6 => '`dossier_individuel1`.`nom_prenom`',
		7 => 'date_format(`dossier_individuel1`.`date_recrutement`,\'%d/%m/%Y\')',
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => '`expenses`.`amount`',
		13 => '`expenses`.`date_depart`',
		14 => '`expenses`.`date_retour`',
		15 => '`expenses`.`date_depart_derniere`',
		16 => 16,
		17 => 17,
		18 => 18,
		19 => 19,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`expenses`.`idexpense`" => "idexpense",
		"`expenses`.`type`" => "type",
		"`expenses`.`nature`" => "nature",
		"if(`expenses`.`date_saisie`,date_format(`expenses`.`date_saisie`,'%d/%m/%Y'),'')" => "date_saisie",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* Nom & Pr&#233;nom */" => "client",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* Date recrutement */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"`expenses`.`amount`" => "amount",
		"if(`expenses`.`date_depart`,date_format(`expenses`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"if(`expenses`.`date_retour`,date_format(`expenses`.`date_retour`,'%d/%m/%Y'),'')" => "date_retour",
		"if(`expenses`.`date_depart_derniere`,date_format(`expenses`.`date_depart_derniere`,'%d/%m/%Y'),'')" => "date_depart_derniere",
		"`expenses`.`adresse_absence`" => "adresse_absence",
		"`expenses`.`departement`" => "departement",
		"`expenses`.`signataire`" => "signataire",
		"`expenses`.`observations`" => "observations",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`expenses`.`idexpense`" => "N&#176;",
		"`expenses`.`type`" => "Type",
		"`expenses`.`nature`" => "Nature",
		"`expenses`.`date_saisie`" => "Date demande",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* Matricule */" => "Matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* Nom & Pr&#233;nom */" => "Nom & Pr&#233;nom",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* Date recrutement */" => "Date recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "Fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "Division",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "Service",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "Direction",
		"`expenses`.`amount`" => "Nombre de jour &#224; d&#233;duire",
		"`expenses`.`date_depart`" => "Date de d&#233;part",
		"`expenses`.`date_retour`" => "Date de retour",
		"`expenses`.`date_depart_derniere`" => "Date d&#233;part derni&#232;re cong&#233;/permission",
		"`expenses`.`adresse_absence`" => "Adresse pendant votre absence",
		"`expenses`.`departement`" => "D&#233;cision",
		"`expenses`.`signataire`" => "Signataire",
		"`expenses`.`observations`" => "Observations",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`expenses`.`idexpense`" => "idexpense",
		"`expenses`.`type`" => "type",
		"`expenses`.`nature`" => "nature",
		"if(`expenses`.`date_saisie`,date_format(`expenses`.`date_saisie`,'%d/%m/%Y'),'')" => "date_saisie",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`matricule`), CONCAT_WS('',   `dossier_individuel1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nom_prenom`), CONCAT_WS('',   `dossier_individuel1`.`nom_prenom`), '') /* Nom & Pr&#233;nom */" => "client",
		"IF(    CHAR_LENGTH(if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), CONCAT_WS('',   if(`dossier_individuel1`.`date_recrutement`,date_format(`dossier_individuel1`.`date_recrutement`,'%d/%m/%Y'),'')), '') /* Date recrutement */" => "date_recrutement",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`divisions1`.`division`), CONCAT_WS('',   `divisions1`.`division`), '') /* Division */" => "division",
		"IF(    CHAR_LENGTH(`services1`.`service`), CONCAT_WS('',   `services1`.`service`), '') /* Service */" => "service",
		"IF(    CHAR_LENGTH(`directions1`.`direction`), CONCAT_WS('',   `directions1`.`direction`), '') /* Direction */" => "direction",
		"`expenses`.`amount`" => "amount",
		"if(`expenses`.`date_depart`,date_format(`expenses`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"if(`expenses`.`date_retour`,date_format(`expenses`.`date_retour`,'%d/%m/%Y'),'')" => "date_retour",
		"if(`expenses`.`date_depart_derniere`,date_format(`expenses`.`date_depart_derniere`,'%d/%m/%Y'),'')" => "date_depart_derniere",
		"`expenses`.`adresse_absence`" => "adresse_absence",
		"`expenses`.`departement`" => "departement",
		"`expenses`.`signataire`" => "signataire",
		"`expenses`.`observations`" => "observations",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('matricule' => 'Matricule', );

	$x->QueryFrom = "`expenses` LEFT JOIN `dossier_individuel` as dossier_individuel1 ON `dossier_individuel1`.`iddindividuel`=`expenses`.`matricule` LEFT JOIN `fonctions` as fonctions1 ON `fonctions1`.`idfonction`=`dossier_individuel1`.`fonction` LEFT JOIN `divisions` as divisions1 ON `divisions1`.`iddivision`=`dossier_individuel1`.`division` LEFT JOIN `services` as services1 ON `services1`.`idservice`=`dossier_individuel1`.`service` LEFT JOIN `directions` as directions1 ON `directions1`.`iddirection`=`dossier_individuel1`.`direction` ";
	$x->QueryWhere = '';
	$x->QueryOrder = '';

	$x->AllowSelection = 1;
	$x->HideTableView = ($perm[2]==0 ? 1 : 0);
	$x->AllowDelete = $perm[4];
	$x->AllowMassDelete = true;
	$x->AllowInsert = $perm[1];
	$x->AllowUpdate = $perm[3];
	$x->SeparateDV = 1;
	$x->AllowDeleteOfParents = 1;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = 1;
	$x->AllowSorting = 1;
	$x->AllowNavigation = 1;
	$x->AllowPrinting = 1;
	$x->AllowPrintingDV = 1;
	$x->AllowCSV = 1;
	$x->RecordsPerPage = 500;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation["quick search"];
	$x->ScriptFileName = "expenses_view.php";
	$x->RedirectAfterInsert = "expenses_view.php?SelectedID=#ID#";
	$x->TableTitle = "CONGES & PERMISSIONS";
	$x->TableIcon = "resources/table_icons/holiday.png";
	$x->PrimaryKey = "`expenses`.`idexpense`";
	$x->DefaultSortField = '`expenses`.`date_saisie`';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Type", "Nature", "Date demande", "Matricule", "Nom & Pr&#233;nom", "Date recrutement", "Fonction", "Division", "Service", "Direction", "Nombre de jour &#224; d&#233;duire", "Date de d&#233;part", "Date de retour", "Observations");
	$x->ColFieldName = array('type', 'nature', 'date_saisie', 'matricule', 'client', 'date_recrutement', 'fonction', 'division', 'service', 'direction', 'amount', 'date_depart', 'date_retour', 'observations');
	$x->ColNumber  = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 19);

	// template paths below are based on the app main directory
	$x->Template = 'templates/expenses_templateTV.html';
	$x->SelectedTemplate = 'templates/expenses_templateTVS.html';
	$x->TemplateDV = 'templates/expenses_templateDV.html';
	$x->TemplateDVP = 'templates/expenses_templateDVP.html';

	$x->ShowTableHeader = 1;
	$x->TVClasses = "";
	$x->DVClasses = "";
	$x->HighlightColor = '#FFF0C2';
	$x->HasCalculatedFields = false;

	// mm: build the query based on current member's permissions
	$DisplayRecords = $_REQUEST['DisplayRecords'];
	if(!in_array($DisplayRecords, array('user', 'group'))) { $DisplayRecords = 'all'; }
	if($perm[2]==1 || ($perm[2]>1 && $DisplayRecords=='user' && !$_REQUEST['NoFilter_x'])) { // view owner only
		$x->QueryFrom.=', membership_userrecords';
		$x->QueryWhere="where `expenses`.`idexpense`=membership_userrecords.pkValue and membership_userrecords.tableName='expenses' and lcase(membership_userrecords.memberID)='".getLoggedMemberID()."'";
	}elseif($perm[2]==2 || ($perm[2]>2 && $DisplayRecords=='group' && !$_REQUEST['NoFilter_x'])) { // view group only
		$x->QueryFrom.=', membership_userrecords';
		$x->QueryWhere="where `expenses`.`idexpense`=membership_userrecords.pkValue and membership_userrecords.tableName='expenses' and membership_userrecords.groupID='".getLoggedGroupID()."'";
	}elseif($perm[2]==3) { // view all
		// no further action
	}elseif($perm[2]==0) { // view none
		$x->QueryFields = array("Not enough permissions" => "NEP");
		$x->QueryFrom = '`expenses`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: expenses_init
	$render=TRUE;
	if(function_exists('expenses_init')) {
		$args=array();
		$render=expenses_init($x, getMemberInfo(), $args);
	}

	if($render) $x->Render();

	// column sums
	if(strpos($x->HTML, '<!-- tv data below -->')) {
		// if printing multi-selection TV, calculate the sum only for the selected records
		if(isset($_REQUEST['Print_x']) && is_array($_REQUEST['record_selector'])) {
			$QueryWhere = '';
			foreach($_REQUEST['record_selector'] as $id) {   // get selected records
				if($id != '') $QueryWhere .= "'" . makeSafe($id) . "',";
			}
			if($QueryWhere != '') {
				$QueryWhere = 'where `expenses`.`idexpense` in ('.substr($QueryWhere, 0, -1).')';
			}else{ // if no selected records, write the where clause to return an empty result
				$QueryWhere = 'where 1=0';
			}
		}else{
			$QueryWhere = $x->QueryWhere;
		}

		$sumQuery = "select sum(`expenses`.`amount`) from {$x->QueryFrom} {$QueryWhere}";
		$res = sql($sumQuery, $eo);
		if($row = db_fetch_row($res)) {
			$sumRow = '<tr class="success">';
			if(!isset($_REQUEST['Print_x'])) $sumRow .= '<td class="text-center"><strong>&sum;</strong></td>';
			$sumRow .= '<td class="expenses-type"></td>';
			$sumRow .= '<td class="expenses-nature"></td>';
			$sumRow .= '<td class="expenses-date_saisie"></td>';
			$sumRow .= '<td class="expenses-matricule"></td>';
			$sumRow .= '<td class="expenses-client"></td>';
			$sumRow .= '<td class="expenses-date_recrutement"></td>';
			$sumRow .= '<td class="expenses-fonction"></td>';
			$sumRow .= '<td class="expenses-division"></td>';
			$sumRow .= '<td class="expenses-service"></td>';
			$sumRow .= '<td class="expenses-direction"></td>';
			$sumRow .= "<td class=\"expenses-amount text-right\">{$row[0]}</td>";
			$sumRow .= '<td class="expenses-date_depart"></td>';
			$sumRow .= '<td class="expenses-date_retour"></td>';
			$sumRow .= '<td class="expenses-observations"></td>';
			$sumRow .= '</tr>';

			$x->HTML = str_replace('<!-- tv data below -->', '', $x->HTML);
			$x->HTML = str_replace('<!-- tv data above -->', $sumRow, $x->HTML);
		}
	}

	// hook: expenses_header
	$headerCode='';
	if(function_exists('expenses_header')) {
		$args=array();
		$headerCode=expenses_header($x->ContentType, getMemberInfo(), $args);
	}  
	if(!$headerCode) {
		include_once("$currDir/header.php"); 
	}else{
		ob_start(); include_once("$currDir/header.php"); $dHeader=ob_get_contents(); ob_end_clean();
		echo str_replace('<%%HEADER%%>', $dHeader, $headerCode);
	}

	echo $x->HTML;
	// hook: expenses_footer
	$footerCode='';
	if(function_exists('expenses_footer')) {
		$args=array();
		$footerCode=expenses_footer($x->ContentType, getMemberInfo(), $args);
	}  
	if(!$footerCode) {
		include_once("$currDir/footer.php"); 
	}else{
		ob_start(); include_once("$currDir/footer.php"); $dFooter=ob_get_contents(); ob_end_clean();
		echo str_replace('<%%FOOTER%%>', $dFooter, $footerCode);
	}
?>