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

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(
		"`journal`.`idjournal`" => "idjournal",
		"if(`journal`.`date`,date_format(`journal`.`date`,'%d/%m/%Y'),'')" => "date",
		"`journal`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`) || CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`, ' - ', `gestion_comptes1`.`nom_compte`), '') /* Compte d&#233;bitaire */" => "compte_debit",
		"IF(    CHAR_LENGTH(`gestion_comptes2`.`num_compte`) || CHAR_LENGTH(`gestion_comptes2`.`nom_compte`), CONCAT_WS('',   `gestion_comptes2`.`num_compte`, ' - ', `gestion_comptes2`.`nom_compte`), '') /* Compte cr&#233;ditaire */" => "compte_credit",
		"REPLACE(FORMAT(`journal`.`tht`, 0), ',', ' ')" => "tht",
		"`journal`.`tva`" => "tva",
		"REPLACE(FORMAT(`journal`.`tva2`, 0), ',', ' ')" => "tva2",
		"REPLACE(FORMAT(`journal`.`ttc`, 0), ',', ' ')" => "ttc",
		"`journal`.`remarques`" => "remarques",
		"`journal`.`groupe`" => "groupe",
		"`journal`.`agent`" => "agent",
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(
		1 => '`journal`.`idjournal`',
		2 => '`journal`.`date`',
		3 => 3,
		4 => 4,
		5 => 5,
		6 => '`journal`.`tht`',
		7 => '`journal`.`tva`',
		8 => '`journal`.`tva2`',
		9 => '`journal`.`ttc`',
		10 => 10,
		11 => 11,
		12 => 12,
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(
		"`journal`.`idjournal`" => "idjournal",
		"if(`journal`.`date`,date_format(`journal`.`date`,'%d/%m/%Y'),'')" => "date",
		"`journal`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`) || CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`, ' - ', `gestion_comptes1`.`nom_compte`), '') /* Compte d&#233;bitaire */" => "compte_debit",
		"IF(    CHAR_LENGTH(`gestion_comptes2`.`num_compte`) || CHAR_LENGTH(`gestion_comptes2`.`nom_compte`), CONCAT_WS('',   `gestion_comptes2`.`num_compte`, ' - ', `gestion_comptes2`.`nom_compte`), '') /* Compte cr&#233;ditaire */" => "compte_credit",
		"REPLACE(FORMAT(`journal`.`tht`, 0), ',', ' ')" => "tht",
		"`journal`.`tva`" => "tva",
		"REPLACE(FORMAT(`journal`.`tva2`, 0), ',', ' ')" => "tva2",
		"REPLACE(FORMAT(`journal`.`ttc`, 0), ',', ' ')" => "ttc",
		"`journal`.`remarques`" => "remarques",
		"`journal`.`groupe`" => "groupe",
		"`journal`.`agent`" => "agent",
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(
		"`journal`.`idjournal`" => "N&#176;",
		"`journal`.`date`" => "Date",
		"`journal`.`libelle`" => "Libell&#233;",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`) || CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`, ' - ', `gestion_comptes1`.`nom_compte`), '') /* Compte d&#233;bitaire */" => "Compte d&#233;bitaire",
		"IF(    CHAR_LENGTH(`gestion_comptes2`.`num_compte`) || CHAR_LENGTH(`gestion_comptes2`.`nom_compte`), CONCAT_WS('',   `gestion_comptes2`.`num_compte`, ' - ', `gestion_comptes2`.`nom_compte`), '') /* Compte cr&#233;ditaire */" => "Compte cr&#233;ditaire",
		"`journal`.`tht`" => "Montant THT",
		"`journal`.`tva`" => "Taux de la TVA",
		"`journal`.`tva2`" => "TVA",
		"`journal`.`ttc`" => "Montant TTC",
		"`journal`.`remarques`" => "Remarques",
		"`journal`.`groupe`" => "Groupe",
		"`journal`.`agent`" => "Agent",
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(
		"`journal`.`idjournal`" => "idjournal",
		"if(`journal`.`date`,date_format(`journal`.`date`,'%d/%m/%Y'),'')" => "date",
		"`journal`.`libelle`" => "libelle",
		"IF(    CHAR_LENGTH(`gestion_comptes1`.`num_compte`) || CHAR_LENGTH(`gestion_comptes1`.`nom_compte`), CONCAT_WS('',   `gestion_comptes1`.`num_compte`, ' - ', `gestion_comptes1`.`nom_compte`), '') /* Compte d&#233;bitaire */" => "compte_debit",
		"IF(    CHAR_LENGTH(`gestion_comptes2`.`num_compte`) || CHAR_LENGTH(`gestion_comptes2`.`nom_compte`), CONCAT_WS('',   `gestion_comptes2`.`num_compte`, ' - ', `gestion_comptes2`.`nom_compte`), '') /* Compte cr&#233;ditaire */" => "compte_credit",
		"REPLACE(FORMAT(`journal`.`tht`, 0), ',', ' ')" => "tht",
		"`journal`.`tva`" => "tva",
		"REPLACE(FORMAT(`journal`.`tva2`, 0), ',', ' ')" => "tva2",
		"REPLACE(FORMAT(`journal`.`ttc`, 0), ',', ' ')" => "ttc",
		"`journal`.`remarques`" => "remarques",
		"`journal`.`groupe`" => "groupe",
		"`journal`.`agent`" => "agent",
	);

	// Lookup fields that can be used as filterers
	$x->filterers = array('compte_debit' => 'Compte d&#233;bitaire', 'compte_credit' => 'Compte cr&#233;ditaire', );

	$x->QueryFrom = "`journal` LEFT JOIN `gestion_comptes` as gestion_comptes1 ON `gestion_comptes1`.`idgestioncompte`=`journal`.`compte_debit` LEFT JOIN `gestion_comptes` as gestion_comptes2 ON `gestion_comptes2`.`idgestioncompte`=`journal`.`compte_credit` ";
	$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 = "journal_view.php";
	$x->RedirectAfterInsert = "journal_view.php?SelectedID=#ID#";
	$x->TableTitle = "JOURNAL";
	$x->TableIcon = "resources/table_icons/book_edit.png";
	$x->PrimaryKey = "`journal`.`idjournal`";
	$x->DefaultSortField = '`journal`.`date`';
	$x->DefaultSortDirection = 'desc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("N&#176;", "Date", "Libell&#233;", "Compte d&#233;bitaire", "Compte cr&#233;ditaire", "Montant THT", "TVA", "Montant TTC", "Remarques", "Groupe", "Agent");
	$x->ColFieldName = array('idjournal', 'date', 'libelle', 'compte_debit', 'compte_credit', 'tht', 'tva2', 'ttc', 'remarques', 'groupe', 'agent');
	$x->ColNumber  = array(1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12);

	// template paths below are based on the app main directory
	$x->Template = 'templates/journal_templateTV.html';
	$x->SelectedTemplate = 'templates/journal_templateTVS.html';
	$x->TemplateDV = 'templates/journal_templateDV.html';
	$x->TemplateDVP = 'templates/journal_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 `journal`.`idjournal`=membership_userrecords.pkValue and membership_userrecords.tableName='journal' 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 `journal`.`idjournal`=membership_userrecords.pkValue and membership_userrecords.tableName='journal' 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 = '`journal`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: journal_init
	$render=TRUE;
	if(function_exists('journal_init')) {
		$args=array();
		$render=journal_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 `journal`.`idjournal` 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(`journal`.`tht`), 0), ',', ' '), REPLACE(FORMAT(sum(`journal`.`tva2`), 0), ',', ' '), REPLACE(FORMAT(sum(`journal`.`ttc`), 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="journal-idjournal"></td>';
			$sumRow .= '<td class="journal-date"></td>';
			$sumRow .= '<td class="journal-libelle"></td>';
			$sumRow .= '<td class="journal-compte_debit"></td>';
			$sumRow .= '<td class="journal-compte_credit"></td>';
			$sumRow .= "<td class=\"journal-tht text-right\">{$row[0]}</td>";
			$sumRow .= "<td class=\"journal-tva2 text-right\">{$row[1]}</td>";
			$sumRow .= "<td class=\"journal-ttc text-right\">{$row[2]}</td>";
			$sumRow .= '<td class="journal-remarques"></td>';
			$sumRow .= '<td class="journal-groupe"></td>';
			$sumRow .= '<td class="journal-agent"></td>';
			$sumRow .= '</tr>';

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

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