<?php
// This script and data application were generated by AppGini 5.76
// 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/retours.php");
	include("$currDir/retours_dml.php");

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

	// Fields that can be displayed in the table view
	$x->QueryFieldsTV = array(   
		"`retours`.`idretours`" => "idretours",
		"if(`retours`.`date`,date_format(`retours`.`date`,'%d/%m/%Y'),'')" => "date",
		"IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') /* Reference */" => "reference",
		"IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* Client */" => "client",
		"`retours`.`facture`" => "facture",
		"IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') /* Designation */" => "designation",
		"REPLACE(FORMAT(`retours`.`quantiter`, 0), ',', ' ')" => "quantiter",
		"REPLACE(FORMAT(`retours`.`punitairer`, 0), ',', ' ')" => "punitairer",
		"REPLACE(FORMAT(`retours`.`ptotalr`, 0), ',', ' ')" => "ptotalr",
		"REPLACE(FORMAT(`retours`.`ptotalr2`, 0), ',', ' ')" => "ptotalr2",
		"REPLACE(FORMAT(`retours`.`tqtsr`, 0), ',', ' ')" => "tqtsr"
	);
	// mapping incoming sort by requests to actual query fields
	$x->SortFields = array(   
		1 => '`retours`.`idretours`',
		2 => '`retours`.`date`',
		3 => '`marchandises1`.`reference`',
		4 => '`clients1`.`matricule`',
		5 => '`clients1`.`nom_prenom`',
		6 => 6,
		7 => '`marchandises1`.`designation`',
		8 => '`retours`.`quantiter`',
		9 => '`retours`.`punitairer`',
		10 => '`retours`.`ptotalr`',
		11 => '`retours`.`ptotalr2`',
		12 => '`retours`.`tqtsr`'
	);

	// Fields that can be displayed in the csv file
	$x->QueryFieldsCSV = array(   
		"`retours`.`idretours`" => "idretours",
		"if(`retours`.`date`,date_format(`retours`.`date`,'%d/%m/%Y'),'')" => "date",
		"IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') /* Reference */" => "reference",
		"IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* Client */" => "client",
		"`retours`.`facture`" => "facture",
		"IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') /* Designation */" => "designation",
		"REPLACE(FORMAT(`retours`.`quantiter`, 0), ',', ' ')" => "quantiter",
		"REPLACE(FORMAT(`retours`.`punitairer`, 0), ',', ' ')" => "punitairer",
		"REPLACE(FORMAT(`retours`.`ptotalr`, 0), ',', ' ')" => "ptotalr",
		"REPLACE(FORMAT(`retours`.`ptotalr2`, 0), ',', ' ')" => "ptotalr2",
		"REPLACE(FORMAT(`retours`.`tqtsr`, 0), ',', ' ')" => "tqtsr"
	);
	// Fields that can be filtered
	$x->QueryFieldsFilters = array(   
		"`retours`.`idretours`" => "ID",
		"`retours`.`date`" => "Date",
		"IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') /* Reference */" => "Reference",
		"IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') /* Matricule */" => "Matricule",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* Client */" => "Client",
		"`retours`.`facture`" => "Facture",
		"IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') /* Designation */" => "Designation",
		"`retours`.`quantiter`" => "Quantit&#233;",
		"`retours`.`punitairer`" => "Prix unitaire",
		"`retours`.`ptotalr`" => "Prix total",
		"`retours`.`ptotalr2`" => "SOMME TOTAL RETOUR",
		"`retours`.`tqtsr`" => "QTS TOTAL RETOUR"
	);

	// Fields that can be quick searched
	$x->QueryFieldsQS = array(   
		"`retours`.`idretours`" => "idretours",
		"if(`retours`.`date`,date_format(`retours`.`date`,'%d/%m/%Y'),'')" => "date",
		"IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') /* Reference */" => "reference",
		"IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') /* Matricule */" => "matricule",
		"IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') /* Client */" => "client",
		"`retours`.`facture`" => "facture",
		"IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') /* Designation */" => "designation",
		"REPLACE(FORMAT(`retours`.`quantiter`, 0), ',', ' ')" => "quantiter",
		"REPLACE(FORMAT(`retours`.`punitairer`, 0), ',', ' ')" => "punitairer",
		"REPLACE(FORMAT(`retours`.`ptotalr`, 0), ',', ' ')" => "ptotalr",
		"REPLACE(FORMAT(`retours`.`ptotalr2`, 0), ',', ' ')" => "ptotalr2",
		"REPLACE(FORMAT(`retours`.`tqtsr`, 0), ',', ' ')" => "tqtsr"
	);

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

	$x->QueryFrom = "`retours` LEFT JOIN `clients` as clients1 ON `clients1`.`idclients`=`retours`.`matricule` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`retours`.`designation` ";
	$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 = 100;
	$x->QuickSearch = 1;
	$x->QuickSearchText = $Translation["quick search"];
	$x->ScriptFileName = "retours_view.php";
	$x->RedirectAfterInsert = "retours_view.php?SelectedID=#ID#";
	$x->TableTitle = "RETOURS";
	$x->TableIcon = "resources/table_icons/basket_error.png";
	$x->PrimaryKey = "`retours`.`idretours`";
	$x->DefaultSortField = '`retours`.`date`';
	$x->DefaultSortDirection = 'asc';

	$x->ColWidth   = array(  150, 150, 150, 150, 150, 150, 150);
	$x->ColCaption = array("Date", "Client", "Facture", "Designation", "Quantit&#233;", "Prix unitaire", "Prix total");
	$x->ColFieldName = array('date', 'client', 'facture', 'designation', 'quantiter', 'punitairer', 'ptotalr');
	$x->ColNumber  = array(2, 5, 6, 7, 8, 9, 10);

	// template paths below are based on the app main directory
	$x->Template = 'templates/retours_templateTV.html';
	$x->SelectedTemplate = 'templates/retours_templateTVS.html';
	$x->TemplateDV = 'templates/retours_templateDV.html';
	$x->TemplateDVP = 'templates/retours_templateDVP.html';

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

	// 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 `retours`.`idretours`=membership_userrecords.pkValue and membership_userrecords.tableName='retours' 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 `retours`.`idretours`=membership_userrecords.pkValue and membership_userrecords.tableName='retours' 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 = '`retours`';
		$x->QueryWhere = '';
		$x->DefaultSortField = '';
	}
	// hook: retours_init
	$render=TRUE;
	if(function_exists('retours_init')){
		$args=array();
		$render=retours_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 `retours`.`idretours` 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(`retours`.`ptotalr`), 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="retours-date"></td>';
			$sumRow .= '<td class="retours-client"></td>';
			$sumRow .= '<td class="retours-facture"></td>';
			$sumRow .= '<td class="retours-designation"></td>';
			$sumRow .= '<td class="retours-quantiter"></td>';
			$sumRow .= '<td class="retours-punitairer"></td>';
			$sumRow .= "<td class=\"retours-ptotalr text-right\">{$row[0]}</td>";
			$sumRow .= '</tr>';

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

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