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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`gestion_banques`.`idgestion_banque`" => "idgestion_banque",
		"if(`gestion_banques`.`date`,date_format(`gestion_banques`.`date`,'%d/%m/%Y'),'')" => "date",
		"`gestion_banques`.`mouvement`" => "mouvement",
		"`gestion_banques`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`nom_compte`), CONCAT_WS('',   `gestion_comptes_11`.`nom_compte`), '') /* Compte TIERS */" => "compte_emetteur",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`num_compte`), CONCAT_WS('',   `gestion_comptes_11`.`num_compte`), '') /* Numero */" => "numero",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`nom_compte`), '') /* Compte MV */" => "compte_beneficiaire",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`), '') /* Numero  */" => "numero_cb",
		"IF(    CHAR_LENGTH(`banques2`.`banque`), CONCAT_WS('',   `banques2`.`banque`), '') /* Compte  */" => "banque_cb",
		"IF(    CHAR_LENGTH(`cheques1`.`num`), CONCAT_WS('',   `cheques1`.`num`), '') /* N&#176;Cheque / Virement MV */" => "cheque_mv",
		"`gestion_banques`.`cheque_ce`" => "cheque_ce",
		"REPLACE(FORMAT(`gestion_banques`.`depense`, 0), ',', ' ')" => "depense",
		"REPLACE(FORMAT(`gestion_banques`.`recette`, 0), ',', ' ')" => "recette",
		"REPLACE(FORMAT(`gestion_banques`.`solde`, 0), ',', ' ')" => "solde",
		"`gestion_banques`.`cheque`" => "cheque",
		"`gestion_banques`.`observations`" => "observations",
		"`gestion_banques`.`groupe`" => "groupe",
		"`gestion_banques`.`agent`" => "agent",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`gestion_banques`.`idgestion_banque`',
		2 => '`gestion_banques`.`date`',
		3 => 3,
		4 => 4,
		5 => '`gestion_comptes_11`.`nom_compte`',
		6 => '`gestion_comptes_11`.`num_compte`',
		7 => 7,
		8 => '`gestion_comptes1`.`nom_compte`',
		9 => '`gestion_comptes1`.`num_compte`',
		10 => 10,
		11 => '`cheques1`.`num`',
		12 => 12,
		13 => '`gestion_banques`.`depense`',
		14 => '`gestion_banques`.`recette`',
		15 => '`gestion_banques`.`solde`',
		16 => 16,
		17 => 17,
		18 => 18,
		19 => 19,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`gestion_banques`.`idgestion_banque`" => "idgestion_banque",
		"if(`gestion_banques`.`date`,date_format(`gestion_banques`.`date`,'%d/%m/%Y'),'')" => "date",
		"`gestion_banques`.`mouvement`" => "mouvement",
		"`gestion_banques`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`nom_compte`), CONCAT_WS('',   `gestion_comptes_11`.`nom_compte`), '') /* Compte TIERS */" => "compte_emetteur",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`num_compte`), CONCAT_WS('',   `gestion_comptes_11`.`num_compte`), '') /* Numero */" => "numero",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`nom_compte`), '') /* Compte MV */" => "compte_beneficiaire",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`), '') /* Numero  */" => "numero_cb",
		"IF(    CHAR_LENGTH(`banques2`.`banque`), CONCAT_WS('',   `banques2`.`banque`), '') /* Compte  */" => "banque_cb",
		"IF(    CHAR_LENGTH(`cheques1`.`num`), CONCAT_WS('',   `cheques1`.`num`), '') /* N&#176;Cheque / Virement MV */" => "cheque_mv",
		"`gestion_banques`.`cheque_ce`" => "cheque_ce",
		"REPLACE(FORMAT(`gestion_banques`.`depense`, 0), ',', ' ')" => "depense",
		"REPLACE(FORMAT(`gestion_banques`.`recette`, 0), ',', ' ')" => "recette",
		"REPLACE(FORMAT(`gestion_banques`.`solde`, 0), ',', ' ')" => "solde",
		"`gestion_banques`.`cheque`" => "cheque",
		"`gestion_banques`.`observations`" => "observations",
		"`gestion_banques`.`groupe`" => "groupe",
		"`gestion_banques`.`agent`" => "agent",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`gestion_banques`.`idgestion_banque`" => "ID",
		"`gestion_banques`.`date`" => "Date",
		"`gestion_banques`.`mouvement`" => "Mouvement",
		"`gestion_banques`.`libelle`" => "Libell&#233;",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`nom_compte`), CONCAT_WS('',   `gestion_comptes_11`.`nom_compte`), '') /* Compte TIERS */" => "Compte TIERS",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`num_compte`), CONCAT_WS('',   `gestion_comptes_11`.`num_compte`), '') /* Numero */" => "Numero",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "Banque",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`nom_compte`), '') /* Compte MV */" => "Compte MV",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`), '') /* Numero  */" => "Numero ",
		"IF(    CHAR_LENGTH(`banques2`.`banque`), CONCAT_WS('',   `banques2`.`banque`), '') /* Compte  */" => "Compte ",
		"IF(    CHAR_LENGTH(`cheques1`.`num`), CONCAT_WS('',   `cheques1`.`num`), '') /* N&#176;Cheque / Virement MV */" => "N&#176;Cheque / Virement MV",
		"`gestion_banques`.`cheque_ce`" => "N&#176;Cheque/Virement Tiers",
		"`gestion_banques`.`depense`" => "Montant emis",
		"`gestion_banques`.`recette`" => "Montant re&#231;u",
		"`gestion_banques`.`solde`" => "Solde",
		"`gestion_banques`.`cheque`" => "Cheque",
		"`gestion_banques`.`observations`" => "Observations",
		"`gestion_banques`.`groupe`" => "Groupe",
		"`gestion_banques`.`agent`" => "Agent",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`gestion_banques`.`idgestion_banque`" => "idgestion_banque",
		"if(`gestion_banques`.`date`,date_format(`gestion_banques`.`date`,'%d/%m/%Y'),'')" => "date",
		"`gestion_banques`.`mouvement`" => "mouvement",
		"`gestion_banques`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`nom_compte`), CONCAT_WS('',   `gestion_comptes_11`.`nom_compte`), '') /* Compte TIERS */" => "compte_emetteur",
		"IF(    CHAR_LENGTH(`gestion_comptes_11`.`num_compte`), CONCAT_WS('',   `gestion_comptes_11`.`num_compte`), '') /* Numero */" => "numero",
		"IF(    CHAR_LENGTH(`banques1`.`banque`), CONCAT_WS('',   `banques1`.`banque`), '') /* Banque */" => "banque",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`nom_compte`), '') /* Compte MV */" => "compte_beneficiaire",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`), '') /* Numero  */" => "numero_cb",
		"IF(    CHAR_LENGTH(`banques2`.`banque`), CONCAT_WS('',   `banques2`.`banque`), '') /* Compte  */" => "banque_cb",
		"IF(    CHAR_LENGTH(`cheques1`.`num`), CONCAT_WS('',   `cheques1`.`num`), '') /* N&#176;Cheque / Virement MV */" => "cheque_mv",
		"`gestion_banques`.`cheque_ce`" => "cheque_ce",
		"REPLACE(FORMAT(`gestion_banques`.`depense`, 0), ',', ' ')" => "depense",
		"REPLACE(FORMAT(`gestion_banques`.`recette`, 0), ',', ' ')" => "recette",
		"REPLACE(FORMAT(`gestion_banques`.`solde`, 0), ',', ' ')" => "solde",
		"`gestion_banques`.`cheque`" => "cheque",
		"`gestion_banques`.`observations`" => "observations",
		"`gestion_banques`.`groupe`" => "groupe",
		"`gestion_banques`.`agent`" => "agent",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('compte_emetteur' => 'Compte TIERS', 'compte_beneficiaire' => 'Compte MV', 'cheque_mv' => 'N&#176;Cheque / Virement MV', );

	$x->QueryFrom = "`gestion_banques` LEFT JOIN `gestion_comptes_1` as gestion_comptes_11 ON `gestion_comptes_11`.`idgestioncompte`=`gestion_banques`.`compte_emetteur` LEFT JOIN `gestion_comptes` as gestion_comptes1 ON `gestion_comptes1`.`idgestioncompte`=`gestion_banques`.`compte_beneficiaire` LEFT JOIN `cheques` as cheques1 ON `cheques1`.`idcheque`=`gestion_banques`.`cheque_mv` LEFT JOIN `banques` as banques1 ON `banques1`.`idbanque`=`gestion_comptes_11`.`banque` LEFT JOIN `banques` as banques2 ON `banques2`.`idbanque`=`gestion_comptes1`.`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 = 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 = "gestion_banques_view.php";
	$x->RedirectAfterInsert = "gestion_banques_view.php?SelectedID=#ID#";
	$x->TableTitle = "BANQUES";
	$x->TableIcon = "resources/table_icons/card_bank.png";
	$x->PrimaryKey = "`gestion_banques`.`idgestion_banque`";
	$x->DefaultSortField = '`gestion_banques`.`date`';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Date", "Mouvement", "Libell&#233;", "Compte TIERS", "Numero", "Banque", "Compte MV", "Numero ", "Compte ", "N&#176;Cheque / Virement MV", "N&#176;Cheque/Virement Tiers", "Montant emis", "Montant re&#231;u", "Solde", "Cheque", "Groupe", "Agent");
	$x->ColFieldName = array('date', 'mouvement', 'libelle', 'compte_emetteur', 'numero', 'banque', 'compte_beneficiaire', 'numero_cb', 'banque_cb', 'cheque_mv', 'cheque_ce', 'depense', 'recette', 'solde', 'cheque', 'groupe', 'agent');
	$x->ColNumber  = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19);

	// template paths below are based on the app main directory
	$x->Template = 'templates/gestion_banques_templateTV.html';
	$x->SelectedTemplate = 'templates/gestion_banques_templateTVS.html';
	$x->TemplateDV = 'templates/gestion_banques_templateDV.html';
	$x->TemplateDVP = 'templates/gestion_banques_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 `gestion_banques`.`idgestion_banque`=membership_userrecords.pkValue and membership_userrecords.tableName='gestion_banques' 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 `gestion_banques`.`idgestion_banque`=membership_userrecords.pkValue and membership_userrecords.tableName='gestion_banques' 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 = '`gestion_banques`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: gestion_banques_init
	$render=TRUE;
	if(function_exists('gestion_banques_init')) {
		$args=array();
		$render=gestion_banques_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 `gestion_banques`.`idgestion_banque` 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 REPLACE(FORMAT(sum(`gestion_banques`.`depense`), 0), ',', ' '), REPLACE(FORMAT(sum(`gestion_banques`.`recette`), 0), ',', ' '), REPLACE(FORMAT(sum(`gestion_banques`.`solde`), 0), ',', ' ') 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="gestion_banques-date"></td>';
			$sumRow .= '<td class="gestion_banques-mouvement"></td>';
			$sumRow .= '<td class="gestion_banques-libelle"></td>';
			$sumRow .= '<td class="gestion_banques-compte_emetteur"></td>';
			$sumRow .= '<td class="gestion_banques-numero"></td>';
			$sumRow .= '<td class="gestion_banques-banque"></td>';
			$sumRow .= '<td class="gestion_banques-compte_beneficiaire"></td>';
			$sumRow .= '<td class="gestion_banques-numero_cb"></td>';
			$sumRow .= '<td class="gestion_banques-banque_cb"></td>';
			$sumRow .= '<td class="gestion_banques-cheque_mv"></td>';
			$sumRow .= '<td class="gestion_banques-cheque_ce"></td>';
			$sumRow .= "<td class=\"gestion_banques-depense text-right\">{$row[0]}</td>";
			$sumRow .= "<td class=\"gestion_banques-recette text-right\">{$row[1]}</td>";
			$sumRow .= "<td class=\"gestion_banques-solde text-right\">{$row[2]}</td>";
			$sumRow .= '<td class="gestion_banques-cheque"></td>';
			$sumRow .= '<td class="gestion_banques-groupe"></td>';
			$sumRow .= '<td class="gestion_banques-agent"></td>';
			$sumRow .= '</tr>';

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

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