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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`clients`.`idclient`" => "idclient",
		"`clients`.`matricule`" => "matricule",
		"`clients`.`agent`" => "agent",
		"if(`clients`.`date_en`,date_format(`clients`.`date_en`,'%d/%m/%Y %H:%i'),'')" => "date_en",
		"IF(    CHAR_LENGTH(`type_client1`.`type_client`), CONCAT_WS('',   `type_client1`.`type_client`), '') /* Type de client */" => "type_client",
		"`clients`.`raison_sociale`" => "raison_sociale",
		"`clients`.`personne_contact`" => "personne_contact",
		"`clients`.`email_pc`" => "email_pc",
		"`clients`.`phone_pc`" => "phone_pc",
		"`clients`.`ninea`" => "ninea",
		"`clients`.`rc`" => "rc",
		"`clients`.`genre`" => "genre",
		"`clients`.`prenom`" => "prenom",
		"`clients`.`nom`" => "nom",
		"`clients`.`cin`" => "cin",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Produits & Services */" => "categorie",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`activites1`.`activite`), CONCAT_WS('',   `activites1`.`activite`), '') /* Activit&#233; */" => "activite",
		"if(`clients`.`date_naissance`,date_format(`clients`.`date_naissance`,'%d/%m/%Y %H:%i'),'')" => "date_naissance",
		"`clients`.`age`" => "age",
		"`clients`.`passport`" => "passport",
		"if(`clients`.`delivre_le`,date_format(`clients`.`delivre_le`,'%d/%m/%Y %H:%i'),'')" => "delivre_le",
		"if(`clients`.`expire_le`,date_format(`clients`.`expire_le`,'%d/%m/%Y %H:%i'),'')" => "expire_le",
		"IF(    CHAR_LENGTH(`pays2`.`pays`), CONCAT_WS('',   `pays2`.`pays`), '') /* Lieu */" => "lieu",
		"`clients`.`validite`" => "validite",
		"`clients`.`adresse`" => "adresse",
		"`clients`.`telephone`" => "telephone",
		"`clients`.`whatsapp`" => "whatsapp",
		"`clients`.`urgence`" => "urgence",
		"`clients`.`email`" => "email",
		"`clients`.`compte`" => "compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`fidelite1`.`carte_fidelite`), CONCAT_WS('',   `fidelite1`.`carte_fidelite`), '') /* Carte fidelit&#233; */" => "carte_fidelite",
		"`clients`.`num_carte`" => "num_carte",
		"`clients`.`signature`" => "signature",
		"`clients`.`photo`" => "photo",
		"`clients`.`dossier`" => "dossier",
		"`clients`.`observations`" => "observations",
		"`clients`.`groupe`" => "groupe",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`clients`.`idclient`',
		2 => 2,
		3 => 3,
		4 => '`clients`.`date_en`',
		5 => '`type_client1`.`type_client`',
		6 => 6,
		7 => 7,
		8 => 8,
		9 => 9,
		10 => 10,
		11 => 11,
		12 => 12,
		13 => 13,
		14 => 14,
		15 => 15,
		16 => '`agences1`.`agence`',
		17 => '`pays1`.`pays`',
		18 => '`produits_services1`.`produit_service`',
		19 => '`fonctions1`.`fonction`',
		20 => '`activites1`.`activite`',
		21 => '`clients`.`date_naissance`',
		22 => '`clients`.`age`',
		23 => 23,
		24 => '`clients`.`delivre_le`',
		25 => '`clients`.`expire_le`',
		26 => '`pays2`.`pays`',
		27 => 27,
		28 => 28,
		29 => 29,
		30 => 30,
		31 => 31,
		32 => 32,
		33 => 33,
		34 => '`banques1`.`banque`',
		35 => '`fidelite1`.`carte_fidelite`',
		36 => '`clients`.`num_carte`',
		37 => 37,
		38 => 38,
		39 => 39,
		40 => 40,
		41 => 41,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`clients`.`idclient`" => "idclient",
		"`clients`.`matricule`" => "matricule",
		"`clients`.`agent`" => "agent",
		"if(`clients`.`date_en`,date_format(`clients`.`date_en`,'%d/%m/%Y %H:%i'),'')" => "date_en",
		"IF(    CHAR_LENGTH(`type_client1`.`type_client`), CONCAT_WS('',   `type_client1`.`type_client`), '') /* Type de client */" => "type_client",
		"`clients`.`raison_sociale`" => "raison_sociale",
		"`clients`.`personne_contact`" => "personne_contact",
		"`clients`.`email_pc`" => "email_pc",
		"`clients`.`phone_pc`" => "phone_pc",
		"`clients`.`ninea`" => "ninea",
		"`clients`.`rc`" => "rc",
		"`clients`.`genre`" => "genre",
		"`clients`.`prenom`" => "prenom",
		"`clients`.`nom`" => "nom",
		"`clients`.`cin`" => "cin",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Produits & Services */" => "categorie",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`activites1`.`activite`), CONCAT_WS('',   `activites1`.`activite`), '') /* Activit&#233; */" => "activite",
		"if(`clients`.`date_naissance`,date_format(`clients`.`date_naissance`,'%d/%m/%Y %H:%i'),'')" => "date_naissance",
		"`clients`.`age`" => "age",
		"`clients`.`passport`" => "passport",
		"if(`clients`.`delivre_le`,date_format(`clients`.`delivre_le`,'%d/%m/%Y %H:%i'),'')" => "delivre_le",
		"if(`clients`.`expire_le`,date_format(`clients`.`expire_le`,'%d/%m/%Y %H:%i'),'')" => "expire_le",
		"IF(    CHAR_LENGTH(`pays2`.`pays`), CONCAT_WS('',   `pays2`.`pays`), '') /* Lieu */" => "lieu",
		"`clients`.`validite`" => "validite",
		"`clients`.`adresse`" => "adresse",
		"`clients`.`telephone`" => "telephone",
		"`clients`.`whatsapp`" => "whatsapp",
		"`clients`.`urgence`" => "urgence",
		"`clients`.`email`" => "email",
		"`clients`.`compte`" => "compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`fidelite1`.`carte_fidelite`), CONCAT_WS('',   `fidelite1`.`carte_fidelite`), '') /* Carte fidelit&#233; */" => "carte_fidelite",
		"`clients`.`num_carte`" => "num_carte",
		"`clients`.`signature`" => "signature",
		"`clients`.`photo`" => "photo",
		"`clients`.`dossier`" => "dossier",
		"`clients`.`observations`" => "observations",
		"`clients`.`groupe`" => "groupe",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`clients`.`idclient`" => "ID",
		"`clients`.`matricule`" => "Matricule",
		"`clients`.`agent`" => "Agent",
		"`clients`.`date_en`" => "Date enregistrement",
		"IF(    CHAR_LENGTH(`type_client1`.`type_client`), CONCAT_WS('',   `type_client1`.`type_client`), '') /* Type de client */" => "Type de client",
		"`clients`.`raison_sociale`" => "Raison sociale",
		"`clients`.`personne_contact`" => "Personne contact",
		"`clients`.`email_pc`" => "Email ",
		"`clients`.`phone_pc`" => "T&#233;l&#233;phone ",
		"`clients`.`ninea`" => "NINEA",
		"`clients`.`rc`" => "RC",
		"`clients`.`genre`" => "Genre",
		"`clients`.`prenom`" => "Pr&#233;nom",
		"`clients`.`nom`" => "Nom",
		"`clients`.`cin`" => "N&#176; C.I.N",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "Agence",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "Nationalit&#233;",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Produits & Services */" => "Produits & Services",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "Fonction",
		"IF(    CHAR_LENGTH(`activites1`.`activite`), CONCAT_WS('',   `activites1`.`activite`), '') /* Activit&#233; */" => "Activit&#233;",
		"`clients`.`date_naissance`" => "Date de naissance",
		"`clients`.`age`" => "Age",
		"`clients`.`passport`" => "Passeport",
		"`clients`.`delivre_le`" => "Delivr&#233; le",
		"`clients`.`expire_le`" => "Expire le",
		"IF(    CHAR_LENGTH(`pays2`.`pays`), CONCAT_WS('',   `pays2`.`pays`), '') /* Lieu */" => "Lieu",
		"`clients`.`validite`" => "Validit&#233;",
		"`clients`.`adresse`" => "Adresse",
		"`clients`.`telephone`" => "T&#233;l&#233;phone",
		"`clients`.`whatsapp`" => "Whatsapp",
		"`clients`.`urgence`" => "Personne &#224; contacter en cas d\'urgence",
		"`clients`.`email`" => "Email",
		"`clients`.`compte`" => "N&#176; Compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "Banque",
		"IF(    CHAR_LENGTH(`fidelite1`.`carte_fidelite`), CONCAT_WS('',   `fidelite1`.`carte_fidelite`), '') /* Carte fidelit&#233; */" => "Carte fidelit&#233;",
		"`clients`.`num_carte`" => "N&#176; de la carte",
		"`clients`.`dossier`" => "Dossier",
		"`clients`.`observations`" => "Observations",
		"`clients`.`groupe`" => "Groupe",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`clients`.`idclient`" => "idclient",
		"`clients`.`matricule`" => "matricule",
		"`clients`.`agent`" => "agent",
		"if(`clients`.`date_en`,date_format(`clients`.`date_en`,'%d/%m/%Y %H:%i'),'')" => "date_en",
		"IF(    CHAR_LENGTH(`type_client1`.`type_client`), CONCAT_WS('',   `type_client1`.`type_client`), '') /* Type de client */" => "type_client",
		"`clients`.`raison_sociale`" => "raison_sociale",
		"`clients`.`personne_contact`" => "personne_contact",
		"`clients`.`email_pc`" => "email_pc",
		"`clients`.`phone_pc`" => "phone_pc",
		"`clients`.`ninea`" => "ninea",
		"`clients`.`rc`" => "rc",
		"`clients`.`genre`" => "genre",
		"`clients`.`prenom`" => "prenom",
		"`clients`.`nom`" => "nom",
		"`clients`.`cin`" => "cin",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Nationalit&#233; */" => "nationalite",
		"IF(    CHAR_LENGTH(`produits_services1`.`produit_service`), CONCAT_WS('',   `produits_services1`.`produit_service`), '') /* Produits & Services */" => "categorie",
		"IF(    CHAR_LENGTH(`fonctions1`.`fonction`), CONCAT_WS('',   `fonctions1`.`fonction`), '') /* Fonction */" => "fonction",
		"IF(    CHAR_LENGTH(`activites1`.`activite`), CONCAT_WS('',   `activites1`.`activite`), '') /* Activit&#233; */" => "activite",
		"if(`clients`.`date_naissance`,date_format(`clients`.`date_naissance`,'%d/%m/%Y %H:%i'),'')" => "date_naissance",
		"`clients`.`age`" => "age",
		"`clients`.`passport`" => "passport",
		"if(`clients`.`delivre_le`,date_format(`clients`.`delivre_le`,'%d/%m/%Y %H:%i'),'')" => "delivre_le",
		"if(`clients`.`expire_le`,date_format(`clients`.`expire_le`,'%d/%m/%Y %H:%i'),'')" => "expire_le",
		"IF(    CHAR_LENGTH(`pays2`.`pays`), CONCAT_WS('',   `pays2`.`pays`), '') /* Lieu */" => "lieu",
		"`clients`.`validite`" => "validite",
		"`clients`.`adresse`" => "adresse",
		"`clients`.`telephone`" => "telephone",
		"`clients`.`whatsapp`" => "whatsapp",
		"`clients`.`urgence`" => "urgence",
		"`clients`.`email`" => "email",
		"`clients`.`compte`" => "compte",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`fidelite1`.`carte_fidelite`), CONCAT_WS('',   `fidelite1`.`carte_fidelite`), '') /* Carte fidelit&#233; */" => "carte_fidelite",
		"`clients`.`num_carte`" => "num_carte",
		"`clients`.`dossier`" => "dossier",
		"`clients`.`observations`" => "observations",
		"`clients`.`groupe`" => "groupe",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('type_client' => 'Type de client', 'agence' => 'Agence', 'nationalite' => 'Nationalit&#233;', 'categorie' => 'Produits & Services', 'fonction' => 'Fonction', 'activite' => 'Activit&#233;', 'lieu' => 'Lieu', 'banque' => 'Banque', 'carte_fidelite' => 'Carte fidelit&#233;', );

	$x->QueryFrom = "`clients` LEFT JOIN `type_client` as type_client1 ON `type_client1`.`idtype_client`=`clients`.`type_client` LEFT JOIN `agences` as agences1 ON `agences1`.`idagence`=`clients`.`agence` LEFT JOIN `pays` as pays1 ON `pays1`.`idpays`=`clients`.`nationalite` LEFT JOIN `produits_services` as produits_services1 ON `produits_services1`.`idps`=`clients`.`categorie` LEFT JOIN `fonctions` as fonctions1 ON `fonctions1`.`idfonction`=`clients`.`fonction` LEFT JOIN `activites` as activites1 ON `activites1`.`idactivite`=`clients`.`activite` LEFT JOIN `pays` as pays2 ON `pays2`.`idpays`=`clients`.`lieu` LEFT JOIN `banques` as banques1 ON `banques1`.`idbanque`=`clients`.`banque` LEFT JOIN `fidelite` as fidelite1 ON `fidelite1`.`idfidelite`=`clients`.`carte_fidelite` ";
	$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 = 0;
	$x->AllowFilters = 1;
	$x->AllowSavingFilters = 1;
	$x->AllowSorting = 1;
	$x->AllowNavigation = 1;
	$x->AllowPrinting = 1;
	$x->AllowPrintingDV = 1;
	$x->AllowCSV = 1;
	$x->RecordsPerPage = 10;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation["quick search"];
	$x->ScriptFileName = "clients_view.php";
	$x->RedirectAfterInsert = "clients_view.php?SelectedID=#ID#";
	$x->TableTitle = "GESTION DES CLIENTS";
	$x->TableIcon = "resources/table_icons/clients.png";
	$x->PrimaryKey = "`clients`.`idclient`";
	$x->DefaultSortField = '`clients`.`date_en`';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Type de client", "Raison sociale", "Email ", "T&#233;l&#233;phone ", "NINEA", "RC", "Genre", "Pr&#233;nom", "Nom", "N&#176; C.I.N", "Agence", "Nationalit&#233;", "Fonction", "Activit&#233;", "Age", "Passeport", "Delivr&#233; le", "Expire le", "Lieu", "Validit&#233;", "Adresse", "T&#233;l&#233;phone", "Whatsapp", "Email", "Banque", "Photo", "Groupe");
	$x->ColFieldName = array('type_client', 'raison_sociale', 'email_pc', 'phone_pc', 'ninea', 'rc', 'genre', 'prenom', 'nom', 'cin', 'agence', 'nationalite', 'fonction', 'activite', 'age', 'passport', 'delivre_le', 'expire_le', 'lieu', 'validite', 'adresse', 'telephone', 'whatsapp', 'email', 'banque', 'photo', 'groupe');
	$x->ColNumber  = array(5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 34, 38, 41);

	// template paths below are based on the app main directory
	$x->Template = 'templates/clients_templateTV.html';
	$x->SelectedTemplate = 'templates/clients_templateTVS.html';
	$x->TemplateDV = 'templates/clients_templateDV.html';
	$x->TemplateDVP = 'templates/clients_templateDVP.html';

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

	// 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 `clients`.`idclient`=membership_userrecords.pkValue and membership_userrecords.tableName='clients' 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 `clients`.`idclient`=membership_userrecords.pkValue and membership_userrecords.tableName='clients' 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 = '`clients`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: clients_init
	$render=TRUE;
	if(function_exists('clients_init')) {
		$args=array();
		$render=clients_init($x, getMemberInfo(), $args);
	}

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

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