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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`envois_colis`.`idenvois_coli`" => "idenvois_coli",
		"IF(    CHAR_LENGTH(`colis1`.`link_envoi_colis`), CONCAT_WS('',   `colis1`.`link_envoi_colis`), '') /* Link colis */" => "link_colis",
		"`envois_colis`.`description`" => "description",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Destination */" => "destination",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`villes1`.`ville`), CONCAT_WS('',   `villes1`.`ville`), '') /* Ville */" => "ville",
		"`envois_colis`.`prix_unitaire`" => "prix_unitaire",
		"`envois_colis`.`qts`" => "qts",
		"`envois_colis`.`prix_total`" => "prix_total",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* B&#233;n&#233;ficiaire */" => "beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`telephone`), CONCAT_WS('',   `clients1`.`telephone`), '') /* T&#233;l&#233;phone b&#233;n&#233;ficiaire */" => "telephone_beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`nni`), CONCAT_WS('',   `clients1`.`nni`), '') /* NNI b&#233;n&#233;ficiaire */" => "nni_beneficiaire",
		"if(`envois_colis`.`date_reception`,date_format(`envois_colis`.`date_reception`,'%d/%m/%Y'),'')" => "date_reception",
		"`envois_colis`.`statut`" => "statut",
		"`envois_colis`.`observation`" => "observation",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`envois_colis`.`idenvois_coli`',
		2 => '`colis1`.`link_envoi_colis`',
		3 => 3,
		4 => '`pays1`.`pays`',
		5 => '`agences1`.`agence`',
		6 => '`villes1`.`ville`',
		7 => '`envois_colis`.`prix_unitaire`',
		8 => '`envois_colis`.`qts`',
		9 => '`envois_colis`.`prix_total`',
		10 => '`clients1`.`nom_prenom`',
		11 => '`clients1`.`telephone`',
		12 => '`clients1`.`nni`',
		13 => '`envois_colis`.`date_reception`',
		14 => 14,
		15 => 15,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`envois_colis`.`idenvois_coli`" => "idenvois_coli",
		"IF(    CHAR_LENGTH(`colis1`.`link_envoi_colis`), CONCAT_WS('',   `colis1`.`link_envoi_colis`), '') /* Link colis */" => "link_colis",
		"`envois_colis`.`description`" => "description",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Destination */" => "destination",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`villes1`.`ville`), CONCAT_WS('',   `villes1`.`ville`), '') /* Ville */" => "ville",
		"`envois_colis`.`prix_unitaire`" => "prix_unitaire",
		"`envois_colis`.`qts`" => "qts",
		"`envois_colis`.`prix_total`" => "prix_total",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* B&#233;n&#233;ficiaire */" => "beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`telephone`), CONCAT_WS('',   `clients1`.`telephone`), '') /* T&#233;l&#233;phone b&#233;n&#233;ficiaire */" => "telephone_beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`nni`), CONCAT_WS('',   `clients1`.`nni`), '') /* NNI b&#233;n&#233;ficiaire */" => "nni_beneficiaire",
		"if(`envois_colis`.`date_reception`,date_format(`envois_colis`.`date_reception`,'%d/%m/%Y'),'')" => "date_reception",
		"`envois_colis`.`statut`" => "statut",
		"`envois_colis`.`observation`" => "observation",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`envois_colis`.`idenvois_coli`" => "ID",
		"IF(    CHAR_LENGTH(`colis1`.`link_envoi_colis`), CONCAT_WS('',   `colis1`.`link_envoi_colis`), '') /* Link colis */" => "Link colis",
		"`envois_colis`.`description`" => "Description",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Destination */" => "Destination",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "Agence",
		"IF(    CHAR_LENGTH(`villes1`.`ville`), CONCAT_WS('',   `villes1`.`ville`), '') /* Ville */" => "Ville",
		"`envois_colis`.`prix_unitaire`" => "Prix unitaire",
		"`envois_colis`.`qts`" => "Quantit&#233;",
		"`envois_colis`.`prix_total`" => "Net &#224; payer",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* B&#233;n&#233;ficiaire */" => "B&#233;n&#233;ficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`telephone`), CONCAT_WS('',   `clients1`.`telephone`), '') /* T&#233;l&#233;phone b&#233;n&#233;ficiaire */" => "T&#233;l&#233;phone b&#233;n&#233;ficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`nni`), CONCAT_WS('',   `clients1`.`nni`), '') /* NNI b&#233;n&#233;ficiaire */" => "NNI b&#233;n&#233;ficiaire",
		"`envois_colis`.`date_reception`" => "Date de reception",
		"`envois_colis`.`statut`" => "Statut",
		"`envois_colis`.`observation`" => "Observation",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`envois_colis`.`idenvois_coli`" => "idenvois_coli",
		"IF(    CHAR_LENGTH(`colis1`.`link_envoi_colis`), CONCAT_WS('',   `colis1`.`link_envoi_colis`), '') /* Link colis */" => "link_colis",
		"`envois_colis`.`description`" => "description",
		"IF(    CHAR_LENGTH(`pays1`.`pays`), CONCAT_WS('',   `pays1`.`pays`), '') /* Destination */" => "destination",
		"IF(    CHAR_LENGTH(`agences1`.`agence`), CONCAT_WS('',   `agences1`.`agence`), '') /* Agence */" => "agence",
		"IF(    CHAR_LENGTH(`villes1`.`ville`), CONCAT_WS('',   `villes1`.`ville`), '') /* Ville */" => "ville",
		"`envois_colis`.`prix_unitaire`" => "prix_unitaire",
		"`envois_colis`.`qts`" => "qts",
		"`envois_colis`.`prix_total`" => "prix_total",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* B&#233;n&#233;ficiaire */" => "beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`telephone`), CONCAT_WS('',   `clients1`.`telephone`), '') /* T&#233;l&#233;phone b&#233;n&#233;ficiaire */" => "telephone_beneficiaire",
		"IF(    CHAR_LENGTH(`clients1`.`nni`), CONCAT_WS('',   `clients1`.`nni`), '') /* NNI b&#233;n&#233;ficiaire */" => "nni_beneficiaire",
		"if(`envois_colis`.`date_reception`,date_format(`envois_colis`.`date_reception`,'%d/%m/%Y'),'')" => "date_reception",
		"`envois_colis`.`statut`" => "statut",
		"`envois_colis`.`observation`" => "observation",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('link_colis' => 'Link colis', 'destination' => 'Destination', 'agence' => 'Agence', 'ville' => 'Ville', 'beneficiaire' => 'B&#233;n&#233;ficiaire', );

	$x->QueryFrom = "`envois_colis` LEFT JOIN `colis` as colis1 ON `colis1`.`idcoli`=`envois_colis`.`link_colis` LEFT JOIN `pays` as pays1 ON `pays1`.`idpays`=`envois_colis`.`destination` LEFT JOIN `agences` as agences1 ON `agences1`.`idagence`=`envois_colis`.`agence` LEFT JOIN `villes` as villes1 ON `villes1`.`idville`=`envois_colis`.`ville` LEFT JOIN `clients` as clients1 ON `clients1`.`matricule_client`=`envois_colis`.`beneficiaire` ";
	$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 = "envois_colis_view.php";
	$x->RedirectAfterInsert = "envois_colis_view.php?SelectedID=#ID#";
	$x->TableTitle = "LISTE ENVOIS COLI";
	$x->TableIcon = "resources/table_icons/lorry.png";
	$x->PrimaryKey = "`envois_colis`.`idenvois_coli`";

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Description", "Destination", "Agence", "Ville", "Prix unitaire", "Quantit&#233;", "Net &#224; payer", "B&#233;n&#233;ficiaire", "T&#233;l&#233;phone b&#233;n&#233;ficiaire", "NNI b&#233;n&#233;ficiaire", "Date de reception", "Statut");
	$x->ColFieldName = array('description', 'destination', 'agence', 'ville', 'prix_unitaire', 'qts', 'prix_total', 'beneficiaire', 'telephone_beneficiaire', 'nni_beneficiaire', 'date_reception', 'statut');
	$x->ColNumber  = array(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);

	// template paths below are based on the app main directory
	$x->Template = 'templates/envois_colis_templateTV.html';
	$x->SelectedTemplate = 'templates/envois_colis_templateTVS.html';
	$x->TemplateDV = 'templates/envois_colis_templateDV.html';
	$x->TemplateDVP = 'templates/envois_colis_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 `envois_colis`.`idenvois_coli`=membership_userrecords.pkValue and membership_userrecords.tableName='envois_colis' 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 `envois_colis`.`idenvois_coli`=membership_userrecords.pkValue and membership_userrecords.tableName='envois_colis' 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 = '`envois_colis`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: envois_colis_init
	$render=TRUE;
	if(function_exists('envois_colis_init')) {
		$args=array();
		$render=envois_colis_init($x, getMemberInfo(), $args);
	}

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

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