<?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/decomptes.php");
	include("$currDir/decomptes_dml.php");

	// mm: can the current member access this page?
	$perm=getTablePermissions('decomptes');
	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 = "decomptes";

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`decomptes`.`iddecompte`" => "iddecompte",
		"if(`decomptes`.`date_demande_c`,date_format(`decomptes`.`date_demande_c`,'%d/%m/%Y'),'')" => "date_demande_c",
		"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 prenom */" => "nom_prenom",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nni`), CONCAT_WS('',   `dossier_individuel1`.`nni`), '') /* Nni */" => "nni",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`compte`) || CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `dossier_individuel1`.`compte`, '  /  ', `banques1`.`banque`), '') /* N&#176; COMPTE & BANQUE */" => "num_cpte",
		"if(`decomptes`.`date_recrutement`,date_format(`decomptes`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"if(`decomptes`.`date_depart`,date_format(`decomptes`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"`decomptes`.`anciennete`" => "anciennete",
		"REPLACE(FORMAT(`decomptes`.`salaire_m_m`, 0), ',', ' ')" => "salaire_m_m",
		"`decomptes`.`from_details`" => "from_details",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`decomptes`.`iddecompte`',
		2 => '`decomptes`.`date_demande_c`',
		3 => '`dossier_individuel1`.`matricule`',
		4 => '`dossier_individuel1`.`nom_prenom`',
		5 => '`dossier_individuel1`.`nni`',
		6 => 6,
		7 => '`decomptes`.`date_recrutement`',
		8 => '`decomptes`.`date_depart`',
		9 => '`decomptes`.`anciennete`',
		10 => '`decomptes`.`salaire_m_m`',
		11 => 11,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`decomptes`.`iddecompte`" => "iddecompte",
		"if(`decomptes`.`date_demande_c`,date_format(`decomptes`.`date_demande_c`,'%d/%m/%Y'),'')" => "date_demande_c",
		"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 prenom */" => "nom_prenom",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nni`), CONCAT_WS('',   `dossier_individuel1`.`nni`), '') /* Nni */" => "nni",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`compte`) || CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `dossier_individuel1`.`compte`, '  /  ', `banques1`.`banque`), '') /* N&#176; COMPTE & BANQUE */" => "num_cpte",
		"if(`decomptes`.`date_recrutement`,date_format(`decomptes`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"if(`decomptes`.`date_depart`,date_format(`decomptes`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"`decomptes`.`anciennete`" => "anciennete",
		"REPLACE(FORMAT(`decomptes`.`salaire_m_m`, 0), ',', ' ')" => "salaire_m_m",
		"`decomptes`.`from_details`" => "from_details",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`decomptes`.`iddecompte`" => "N&#176;",
		"`decomptes`.`date_demande_c`" => "Date",
		"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 prenom */" => "Nom prenom",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nni`), CONCAT_WS('',   `dossier_individuel1`.`nni`), '') /* Nni */" => "Nni",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`compte`) || CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `dossier_individuel1`.`compte`, '  /  ', `banques1`.`banque`), '') /* N&#176; COMPTE & BANQUE */" => "N&#176; COMPTE & BANQUE",
		"`decomptes`.`date_recrutement`" => "DATE D\'ENGAGEMENT",
		"`decomptes`.`date_depart`" => "Date depart",
		"`decomptes`.`anciennete`" => "Anciennete",
		"`decomptes`.`salaire_m_m`" => "SALAIRE MENSUEL MOYEN",
		"`decomptes`.`from_details`" => "From details",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`decomptes`.`iddecompte`" => "iddecompte",
		"if(`decomptes`.`date_demande_c`,date_format(`decomptes`.`date_demande_c`,'%d/%m/%Y'),'')" => "date_demande_c",
		"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 prenom */" => "nom_prenom",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`nni`), CONCAT_WS('',   `dossier_individuel1`.`nni`), '') /* Nni */" => "nni",
		"IF(    CHAR_LENGTH(`dossier_individuel1`.`compte`) || CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `dossier_individuel1`.`compte`, '  /  ', `banques1`.`banque`), '') /* N&#176; COMPTE & BANQUE */" => "num_cpte",
		"if(`decomptes`.`date_recrutement`,date_format(`decomptes`.`date_recrutement`,'%d/%m/%Y'),'')" => "date_recrutement",
		"if(`decomptes`.`date_depart`,date_format(`decomptes`.`date_depart`,'%d/%m/%Y'),'')" => "date_depart",
		"`decomptes`.`anciennete`" => "anciennete",
		"REPLACE(FORMAT(`decomptes`.`salaire_m_m`, 0), ',', ' ')" => "salaire_m_m",
		"`decomptes`.`from_details`" => "from_details",
	);

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

	$x->QueryFrom = "`decomptes` LEFT JOIN `dossier_individuel` as dossier_individuel1 ON `dossier_individuel1`.`iddindividuel`=`decomptes`.`matricule` LEFT JOIN `banques` as banques1 ON `banques1`.`idbanque`=`dossier_individuel1`.`banque` ";
	$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 = "decomptes_view.php";
	$x->RedirectAfterInsert = "decomptes_view.php?SelectedID=#ID#";
	$x->TableTitle = "DECOMPTES";
	$x->TableIcon = "resources/table_icons/folder_table.png";
	$x->PrimaryKey = "`decomptes`.`iddecompte`";
	$x->DefaultSortField = '`decomptes`.`date_demande_c`';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Date", "Matricule", "Nom prenom", "Nni", "N&#176; COMPTE & BANQUE", "DATE D'ENGAGEMENT", "Date depart", "Anciennete", "SALAIRE MENSUEL MOYEN");
	$x->ColFieldName = array('date_demande_c', 'matricule', 'nom_prenom', 'nni', 'num_cpte', 'date_recrutement', 'date_depart', 'anciennete', 'salaire_m_m');
	$x->ColNumber  = array(2, 3, 4, 5, 6, 7, 8, 9, 10);

	// template paths below are based on the app main directory
	$x->Template = 'templates/decomptes_templateTV.html';
	$x->SelectedTemplate = 'templates/decomptes_templateTVS.html';
	$x->TemplateDV = 'templates/decomptes_templateDV.html';
	$x->TemplateDVP = 'templates/decomptes_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 `decomptes`.`iddecompte`=membership_userrecords.pkValue and membership_userrecords.tableName='decomptes' 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 `decomptes`.`iddecompte`=membership_userrecords.pkValue and membership_userrecords.tableName='decomptes' 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 = '`decomptes`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: decomptes_init
	$render=TRUE;
	if(function_exists('decomptes_init')) {
		$args=array();
		$render=decomptes_init($x, getMemberInfo(), $args);
	}

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

	// hook: decomptes_header
	$headerCode='';
	if(function_exists('decomptes_header')) {
		$args=array();
		$headerCode=decomptes_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: decomptes_footer
	$footerCode='';
	if(function_exists('decomptes_footer')) {
		$args=array();
		$footerCode=decomptes_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);
	}
?>