<?php
// This script and data application were generated by AppGini 5.71
// Download AppGini for free from https://bigprof.com/appgini/download/

	/* Configuration */
	/*************************************/

		$pcConfig = array(
			'clients' => array(   
			),
			'fournisseurs' => array(   
				'pays' => array(   
					'parent-table' => 'provenances',
					'parent-primary-key' => 'idprovenances',
					'child-primary-key' => 'idfournisseurs',
					'child-primary-key-index' => 0,
					'tab-label' => 'Fournisseurs',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/lorry.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Raison sociale', 2 => 'Pays', 3 => 'Adresse', 4 => 'Telephone', 5 => 'Email'),
					'display-field-names' => array(1 => 'raison_sociale', 2 => 'pays', 3 => 'adresse', 4 => 'telephone', 5 => 'email'),
					'sortable-fields' => array(0 => '`fournisseurs`.`idfournisseurs`', 1 => 2, 2 => '`provenances1`.`provenance`', 3 => 4, 4 => 5, 5 => 6, 6 => 7),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-fournisseurs',
					'template-printable' => 'children-fournisseurs-printable',
					'query' => "SELECT `fournisseurs`.`idfournisseurs` as 'idfournisseurs', `fournisseurs`.`raison_sociale` as 'raison_sociale', IF(    CHAR_LENGTH(`provenances1`.`provenance`), CONCAT_WS('',   `provenances1`.`provenance`), '') as 'pays', `fournisseurs`.`adresse` as 'adresse', `fournisseurs`.`telephone` as 'telephone', `fournisseurs`.`email` as 'email', `fournisseurs`.`note` as 'note' FROM `fournisseurs` LEFT JOIN `provenances` as provenances1 ON `provenances1`.`idprovenances`=`fournisseurs`.`pays` "
				)
			),
			'marchandises' => array(   
				'fournisseur' => array(   
					'parent-table' => 'fournisseurs',
					'parent-primary-key' => 'idfournisseurs',
					'child-primary-key' => 'idmarchandises',
					'child-primary-key-index' => 0,
					'tab-label' => 'Marchandises',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/box_open.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Designation', 2 => 'Reference', 5 => 'SOMME ACHATS', 6 => 'SOMME VENTES', 8 => 'QTS ACHATS', 9 => 'QTS VENTES', 10 => 'QTS STOCK'),
					'display-field-names' => array(1 => 'designation', 2 => 'reference', 5 => 'achat', 6 => 'vente', 8 => 'stockaa', 9 => 'stockvv', 10 => 'sstock'),
					'sortable-fields' => array(0 => '`marchandises`.`idmarchandises`', 1 => 2, 2 => 3, 3 => '`fournisseurs1`.`raison_sociale`', 4 => '`provenances1`.`provenance`', 5 => '`marchandises`.`achat`', 6 => '`marchandises`.`vente`', 7 => '`marchandises`.`stock`', 8 => '`marchandises`.`stockaa`', 9 => '`marchandises`.`stockvv`', 10 => '`marchandises`.`sstock`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-marchandises',
					'template-printable' => 'children-marchandises-printable',
					'query' => "SELECT `marchandises`.`idmarchandises` as 'idmarchandises', `marchandises`.`designation` as 'designation', `marchandises`.`reference` as 'reference', IF(    CHAR_LENGTH(`fournisseurs1`.`raison_sociale`), CONCAT_WS('',   `fournisseurs1`.`raison_sociale`), '') as 'fournisseur', IF(    CHAR_LENGTH(`provenances1`.`provenance`), CONCAT_WS('',   `provenances1`.`provenance`), '') as 'provenance', REPLACE(FORMAT(`marchandises`.`achat`, 0), ',', ' ') as 'achat', REPLACE(FORMAT(`marchandises`.`vente`, 0), ',', ' ') as 'vente', REPLACE(FORMAT(`marchandises`.`stock`, 0), ',', ' ') as 'stock', REPLACE(FORMAT(`marchandises`.`stockaa`, 0), ',', ' ') as 'stockaa', REPLACE(FORMAT(`marchandises`.`stockvv`, 0), ',', ' ') as 'stockvv', REPLACE(FORMAT(`marchandises`.`sstock`, 0), ',', ' ') as 'sstock' FROM `marchandises` LEFT JOIN `fournisseurs` as fournisseurs1 ON `fournisseurs1`.`idfournisseurs`=`marchandises`.`fournisseur` LEFT JOIN `provenances` as provenances1 ON `provenances1`.`idprovenances`=`marchandises`.`provenance` "
				),
				'provenance' => array(   
					'parent-table' => 'provenances',
					'parent-primary-key' => 'idprovenances',
					'child-primary-key' => 'idmarchandises',
					'child-primary-key-index' => 0,
					'tab-label' => 'Marchandises',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/box_open.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Designation', 2 => 'Reference', 5 => 'SOMME ACHATS', 6 => 'SOMME VENTES', 8 => 'QTS ACHATS', 9 => 'QTS VENTES', 10 => 'QTS STOCK'),
					'display-field-names' => array(1 => 'designation', 2 => 'reference', 5 => 'achat', 6 => 'vente', 8 => 'stockaa', 9 => 'stockvv', 10 => 'sstock'),
					'sortable-fields' => array(0 => '`marchandises`.`idmarchandises`', 1 => 2, 2 => 3, 3 => '`fournisseurs1`.`raison_sociale`', 4 => '`provenances1`.`provenance`', 5 => '`marchandises`.`achat`', 6 => '`marchandises`.`vente`', 7 => '`marchandises`.`stock`', 8 => '`marchandises`.`stockaa`', 9 => '`marchandises`.`stockvv`', 10 => '`marchandises`.`sstock`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-marchandises',
					'template-printable' => 'children-marchandises-printable',
					'query' => "SELECT `marchandises`.`idmarchandises` as 'idmarchandises', `marchandises`.`designation` as 'designation', `marchandises`.`reference` as 'reference', IF(    CHAR_LENGTH(`fournisseurs1`.`raison_sociale`), CONCAT_WS('',   `fournisseurs1`.`raison_sociale`), '') as 'fournisseur', IF(    CHAR_LENGTH(`provenances1`.`provenance`), CONCAT_WS('',   `provenances1`.`provenance`), '') as 'provenance', REPLACE(FORMAT(`marchandises`.`achat`, 0), ',', ' ') as 'achat', REPLACE(FORMAT(`marchandises`.`vente`, 0), ',', ' ') as 'vente', REPLACE(FORMAT(`marchandises`.`stock`, 0), ',', ' ') as 'stock', REPLACE(FORMAT(`marchandises`.`stockaa`, 0), ',', ' ') as 'stockaa', REPLACE(FORMAT(`marchandises`.`stockvv`, 0), ',', ' ') as 'stockvv', REPLACE(FORMAT(`marchandises`.`sstock`, 0), ',', ' ') as 'sstock' FROM `marchandises` LEFT JOIN `fournisseurs` as fournisseurs1 ON `fournisseurs1`.`idfournisseurs`=`marchandises`.`fournisseur` LEFT JOIN `provenances` as provenances1 ON `provenances1`.`idprovenances`=`marchandises`.`provenance` "
				)
			),
			'provenances' => array(   
			),
			'achats' => array(   
				'designation' => array(   
					'parent-table' => 'marchandises',
					'parent-primary-key' => 'idmarchandises',
					'child-primary-key' => 'idachats',
					'child-primary-key-index' => 0,
					'tab-label' => 'Achats',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/basket_add.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Date', 3 => 'Designation', 4 => 'Quantit&#233;', 5 => 'Prix unitaire', 6 => 'Prix total'),
					'display-field-names' => array(1 => 'date', 3 => 'designation', 4 => 'quantitea', 5 => 'punitairea', 6 => 'ptotala'),
					'sortable-fields' => array(0 => '`achats`.`idachats`', 1 => '`achats`.`date`', 2 => '`marchandises1`.`reference`', 3 => '`marchandises1`.`designation`', 4 => '`achats`.`quantitea`', 5 => '`achats`.`punitairea`', 6 => '`achats`.`ptotala`', 7 => '`achats`.`ptotala2`', 8 => '`achats`.`tqtsa`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-achats',
					'template-printable' => 'children-achats-printable',
					'query' => "SELECT `achats`.`idachats` as 'idachats', if(`achats`.`date`,date_format(`achats`.`date`,'%d/%m/%Y'),'') as 'date', IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') as 'reference', IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') as 'designation', REPLACE(FORMAT(`achats`.`quantitea`, 0), ',', ' ') as 'quantitea', REPLACE(FORMAT(`achats`.`punitairea`, 0), ',', ' ') as 'punitairea', REPLACE(FORMAT(`achats`.`ptotala`, 0), ',', ' ') as 'ptotala', REPLACE(FORMAT(`achats`.`ptotala2`, 0), ',', ' ') as 'ptotala2', REPLACE(FORMAT(`achats`.`tqtsa`, 0), ',', ' ') as 'tqtsa' FROM `achats` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`achats`.`designation` "
				)
			),
			'ventes' => array(   
				'matricule' => array(   
					'parent-table' => 'clients',
					'parent-primary-key' => 'idclients',
					'child-primary-key' => 'idventes',
					'child-primary-key-index' => 0,
					'tab-label' => 'Ventes',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/basket_delete.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Date', 5 => 'Designation', 6 => 'Quantit&#233;', 7 => 'Prix unitaire', 8 => 'Prix total'),
					'display-field-names' => array(1 => 'date', 5 => 'designation', 6 => 'quantitev', 7 => 'punitairev', 8 => 'ptotalv'),
					'sortable-fields' => array(0 => '`ventes`.`idventes`', 1 => '`ventes`.`date`', 2 => '`clients1`.`matricule`', 3 => '`clients1`.`nom_prenom`', 4 => '`marchandises1`.`reference`', 5 => '`marchandises1`.`designation`', 6 => '`ventes`.`quantitev`', 7 => '`ventes`.`punitairev`', 8 => '`ventes`.`ptotalv`', 9 => '`ventes`.`ptotalv2`', 10 => '`ventes`.`tqtsv`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-ventes',
					'template-printable' => 'children-ventes-printable',
					'query' => "SELECT `ventes`.`idventes` as 'idventes', if(`ventes`.`date`,date_format(`ventes`.`date`,'%d/%m/%Y'),'') as 'date', IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') as 'matricule', IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') as 'client', IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') as 'reference', IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') as 'designation', REPLACE(FORMAT(`ventes`.`quantitev`, 0), ',', ' ') as 'quantitev', REPLACE(FORMAT(`ventes`.`punitairev`, 0), ',', ' ') as 'punitairev', REPLACE(FORMAT(`ventes`.`ptotalv`, 0), ',', ' ') as 'ptotalv', REPLACE(FORMAT(`ventes`.`ptotalv2`, 0), ',', ' ') as 'ptotalv2', REPLACE(FORMAT(`ventes`.`tqtsv`, 0), ',', ' ') as 'tqtsv' FROM `ventes` LEFT JOIN `clients` as clients1 ON `clients1`.`idclients`=`ventes`.`matricule` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`ventes`.`designation` "
				),
				'designation' => array(   
					'parent-table' => 'marchandises',
					'parent-primary-key' => 'idmarchandises',
					'child-primary-key' => 'idventes',
					'child-primary-key-index' => 0,
					'tab-label' => 'Ventes',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/basket_delete.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Date', 5 => 'Designation', 6 => 'Quantit&#233;', 7 => 'Prix unitaire', 8 => 'Prix total'),
					'display-field-names' => array(1 => 'date', 5 => 'designation', 6 => 'quantitev', 7 => 'punitairev', 8 => 'ptotalv'),
					'sortable-fields' => array(0 => '`ventes`.`idventes`', 1 => '`ventes`.`date`', 2 => '`clients1`.`matricule`', 3 => '`clients1`.`nom_prenom`', 4 => '`marchandises1`.`reference`', 5 => '`marchandises1`.`designation`', 6 => '`ventes`.`quantitev`', 7 => '`ventes`.`punitairev`', 8 => '`ventes`.`ptotalv`', 9 => '`ventes`.`ptotalv2`', 10 => '`ventes`.`tqtsv`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-ventes',
					'template-printable' => 'children-ventes-printable',
					'query' => "SELECT `ventes`.`idventes` as 'idventes', if(`ventes`.`date`,date_format(`ventes`.`date`,'%d/%m/%Y'),'') as 'date', IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') as 'matricule', IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') as 'client', IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') as 'reference', IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') as 'designation', REPLACE(FORMAT(`ventes`.`quantitev`, 0), ',', ' ') as 'quantitev', REPLACE(FORMAT(`ventes`.`punitairev`, 0), ',', ' ') as 'punitairev', REPLACE(FORMAT(`ventes`.`ptotalv`, 0), ',', ' ') as 'ptotalv', REPLACE(FORMAT(`ventes`.`ptotalv2`, 0), ',', ' ') as 'ptotalv2', REPLACE(FORMAT(`ventes`.`tqtsv`, 0), ',', ' ') as 'tqtsv' FROM `ventes` LEFT JOIN `clients` as clients1 ON `clients1`.`idclients`=`ventes`.`matricule` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`ventes`.`designation` "
				)
			),
			'retours' => array(   
				'matricule' => array(   
					'parent-table' => 'clients',
					'parent-primary-key' => 'idclients',
					'child-primary-key' => 'idretours',
					'child-primary-key-index' => 0,
					'tab-label' => 'Retour',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/basket_error.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Date', 4 => 'Client', 5 => 'Facture', 6 => 'Designation', 7 => 'Quantit&#233;', 8 => 'Prix unitaire', 9 => 'Prix total'),
					'display-field-names' => array(1 => 'date', 4 => 'client', 5 => 'facture', 6 => 'designation', 7 => 'quantiter', 8 => 'punitairer', 9 => 'ptotalr'),
					'sortable-fields' => array(0 => '`retours`.`idretours`', 1 => '`retours`.`date`', 2 => '`marchandises1`.`reference`', 3 => '`clients1`.`matricule`', 4 => '`clients1`.`nom_prenom`', 5 => 6, 6 => '`marchandises1`.`designation`', 7 => '`retours`.`quantiter`', 8 => '`retours`.`punitairer`', 9 => '`retours`.`ptotalr`', 10 => '`retours`.`ptotalr2`', 11 => '`retours`.`tqtsr`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-retours',
					'template-printable' => 'children-retours-printable',
					'query' => "SELECT `retours`.`idretours` as 'idretours', if(`retours`.`date`,date_format(`retours`.`date`,'%d/%m/%Y'),'') as 'date', IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') as 'reference', IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') as 'matricule', IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') as 'client', `retours`.`facture` as 'facture', IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') as 'designation', REPLACE(FORMAT(`retours`.`quantiter`, 0), ',', ' ') as 'quantiter', REPLACE(FORMAT(`retours`.`punitairer`, 0), ',', ' ') as 'punitairer', REPLACE(FORMAT(`retours`.`ptotalr`, 0), ',', ' ') as 'ptotalr', REPLACE(FORMAT(`retours`.`ptotalr2`, 0), ',', ' ') as 'ptotalr2', REPLACE(FORMAT(`retours`.`tqtsr`, 0), ',', ' ') as 'tqtsr' FROM `retours` LEFT JOIN `clients` as clients1 ON `clients1`.`idclients`=`retours`.`matricule` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`retours`.`designation` "
				),
				'designation' => array(   
					'parent-table' => 'marchandises',
					'parent-primary-key' => 'idmarchandises',
					'child-primary-key' => 'idretours',
					'child-primary-key-index' => 0,
					'tab-label' => 'Retour',
					'auto-close' => true,
					'table-icon' => 'resources/table_icons/basket_error.png',
					'display-refresh' => true,
					'display-add-new' => true,
					'forced-where' => '',
					'display-fields' => array(1 => 'Date', 4 => 'Client', 5 => 'Facture', 6 => 'Designation', 7 => 'Quantit&#233;', 8 => 'Prix unitaire', 9 => 'Prix total'),
					'display-field-names' => array(1 => 'date', 4 => 'client', 5 => 'facture', 6 => 'designation', 7 => 'quantiter', 8 => 'punitairer', 9 => 'ptotalr'),
					'sortable-fields' => array(0 => '`retours`.`idretours`', 1 => '`retours`.`date`', 2 => '`marchandises1`.`reference`', 3 => '`clients1`.`matricule`', 4 => '`clients1`.`nom_prenom`', 5 => 6, 6 => '`marchandises1`.`designation`', 7 => '`retours`.`quantiter`', 8 => '`retours`.`punitairer`', 9 => '`retours`.`ptotalr`', 10 => '`retours`.`ptotalr2`', 11 => '`retours`.`tqtsr`'),
					'records-per-page' => 10,
					'default-sort-by' => 1,
					'default-sort-direction' => 'asc',
					'open-detail-view-on-click' => true,
					'display-page-selector' => true,
					'show-page-progress' => true,
					'template' => 'children-retours',
					'template-printable' => 'children-retours-printable',
					'query' => "SELECT `retours`.`idretours` as 'idretours', if(`retours`.`date`,date_format(`retours`.`date`,'%d/%m/%Y'),'') as 'date', IF(    CHAR_LENGTH(`marchandises1`.`reference`), CONCAT_WS('',   `marchandises1`.`reference`), '') as 'reference', IF(    CHAR_LENGTH(`clients1`.`matricule`), CONCAT_WS('',   `clients1`.`matricule`), '') as 'matricule', IF(    CHAR_LENGTH(`clients1`.`nom_prenom`), CONCAT_WS('',   `clients1`.`nom_prenom`), '') as 'client', `retours`.`facture` as 'facture', IF(    CHAR_LENGTH(`marchandises1`.`designation`), CONCAT_WS('',   `marchandises1`.`designation`), '') as 'designation', REPLACE(FORMAT(`retours`.`quantiter`, 0), ',', ' ') as 'quantiter', REPLACE(FORMAT(`retours`.`punitairer`, 0), ',', ' ') as 'punitairer', REPLACE(FORMAT(`retours`.`ptotalr`, 0), ',', ' ') as 'ptotalr', REPLACE(FORMAT(`retours`.`ptotalr2`, 0), ',', ' ') as 'ptotalr2', REPLACE(FORMAT(`retours`.`tqtsr`, 0), ',', ' ') as 'tqtsr' FROM `retours` LEFT JOIN `clients` as clients1 ON `clients1`.`idclients`=`retours`.`matricule` LEFT JOIN `marchandises` as marchandises1 ON `marchandises1`.`idmarchandises`=`retours`.`designation` "
				)
			)
		);

	/*************************************/
	/* End of configuration */


	$currDir = dirname(__FILE__);
	include("{$currDir}/defaultLang.php");
	include("{$currDir}/language.php");
	include("{$currDir}/lib.php");
	@header('Content-Type: text/html; charset=' . datalist_db_encoding);

	handle_maintenance();

	/**
	* dynamic configuration based on current user's permissions
	* $userPCConfig array is populated only with parent tables where the user has access to
	* at least one child table
	*/
	$userPCConfig = array();
	foreach($pcConfig as $pcChildTable => $ChildrenLookups){
		$permChild = getTablePermissions($pcChildTable);
		if($permChild[2]){ // user can view records of the child table, so proceed to check children lookups
			foreach($ChildrenLookups as $ChildLookupField => $ChildConfig){
				$permParent = getTablePermissions($ChildConfig['parent-table']);
				if($permParent[2]){ // user can view records of parent table
					$userPCConfig[$pcChildTable][$ChildLookupField] = $pcConfig[$pcChildTable][$ChildLookupField];
					// show add new only if configured above AND the user has insert permission
					if($permChild[1] && $pcConfig[$pcChildTable][$ChildLookupField]['display-add-new']){
						$userPCConfig[$pcChildTable][$ChildLookupField]['display-add-new'] = true;
					}else{
						$userPCConfig[$pcChildTable][$ChildLookupField]['display-add-new'] = false;
					}
				}
			}
		}
	}

	/* Receive, UTF-convert, and validate parameters */
	$ParentTable = $_REQUEST['ParentTable']; // needed only with operation=show-children, will be validated in the processing code
	$ChildTable = $_REQUEST['ChildTable'];
		if(!in_array($ChildTable, array_keys($userPCConfig))){
			/* defaults to first child table in config array if not provided */
			$ChildTable = current(array_keys($userPCConfig));
		}
		if(!$ChildTable){ die('<!-- No tables accessible to current user -->'); }
	$SelectedID = strip_tags($_REQUEST['SelectedID']);
	$ChildLookupField = $_REQUEST['ChildLookupField'];
		if(!in_array($ChildLookupField, array_keys($userPCConfig[$ChildTable]))){
			/* defaults to first lookup in current child config array if not provided */
			$ChildLookupField = current(array_keys($userPCConfig[$ChildTable]));
		}
	$Page = intval($_REQUEST['Page']);
		if($Page < 1){
			$Page = 1;
		}
	$SortBy = ($_REQUEST['SortBy'] != '' ? abs(intval($_REQUEST['SortBy'])) : false);
		if(!in_array($SortBy, array_keys($userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields']), true)){
			$SortBy = $userPCConfig[$ChildTable][$ChildLookupField]['default-sort-by'];
		}
	$SortDirection = strtolower($_REQUEST['SortDirection']);
		if(!in_array($SortDirection, array('asc', 'desc'))){
			$SortDirection = $userPCConfig[$ChildTable][$ChildLookupField]['default-sort-direction'];
		}
	$Operation = strtolower($_REQUEST['Operation']);
		if(!in_array($Operation, array('get-records', 'show-children', 'get-records-printable', 'show-children-printable'))){
			$Operation = 'get-records';
		}

	/* process requested operation */
	switch($Operation){
		/************************************************/
		case 'show-children':
			/* populate HTML and JS content with children tabs */
			$tabLabels = $tabPanels = $tabLoaders = '';
			foreach($userPCConfig as $ChildTable => $childLookups){
				foreach($childLookups as $ChildLookupField => $childConfig){
					if($childConfig['parent-table'] == $ParentTable){
						$TableIcon = ($childConfig['table-icon'] ? "<img src=\"{$childConfig['table-icon']}\" border=\"0\" />" : '');
						$tabLabels .= sprintf('<li%s><a href="#panel_%s-%s" id="tab_%s-%s" data-toggle="tab">%s%s</a></li>' . "\n\t\t\t\t\t",($tabLabels ? '' : ' class="active"'), $ChildTable, $ChildLookupField, $ChildTable, $ChildLookupField, $TableIcon, $childConfig['tab-label']);
						$tabPanels .= sprintf('<div id="panel_%s-%s" class="tab-pane%s"><img src="loading.gif" align="top" />%s</div>' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, ($tabPanels ? '' : ' active'), $Translation['Loading ...']);
						$tabLoaders .= sprintf('post("parent-children.php", { ChildTable: "%s", ChildLookupField: "%s", SelectedID: "%s", Page: 1, SortBy: "", SortDirection: "", Operation: "get-records" }, "panel_%s-%s");' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, addslashes($SelectedID), $ChildTable, $ChildLookupField);
					}
				}
			}

			if(!$tabLabels){ die('<!-- no children of current parent table are accessible to current user -->'); }
			?>
			<div id="children-tabs">
				<ul class="nav nav-tabs">
					<?php echo $tabLabels; ?>
				</ul>
				<span id="pc-loading"></span>
			</div>
			<div class="tab-content"><?php echo $tabPanels; ?></div>

			<script>
				$j(function(){
					/* for iOS, avoid loading child tabs in modals */
					var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
					var embedded = ($j('.navbar').length == 0);
					if(iOS && embedded){
						$j('#children-tabs').next('.tab-content').remove();
						$j('#children-tabs').remove();
						return;
					}

					/* ajax loading of each tab's contents */
					<?php echo $tabLoaders; ?>
				})
			</script>
			<?php
			break;

		/************************************************/
		case 'show-children-printable':
			/* populate HTML and JS content with children buttons */
			$tabLabels = $tabPanels = $tabLoaders = '';
			foreach($userPCConfig as $ChildTable => $childLookups){
				foreach($childLookups as $ChildLookupField => $childConfig){
					if($childConfig['parent-table'] == $ParentTable){
						$TableIcon = ($childConfig['table-icon'] ? "<img src=\"{$childConfig['table-icon']}\" border=\"0\" />" : '');
						$tabLabels .= sprintf('<button type="button" class="btn btn-default" data-target="#panel_%s-%s" id="tab_%s-%s" data-toggle="collapse">%s %s</button>' . "\n\t\t\t\t\t", $ChildTable, $ChildLookupField, $ChildTable, $ChildLookupField, $TableIcon, $childConfig['tab-label']);
						$tabPanels .= sprintf('<div id="panel_%s-%s" class="collapse"><img src="loading.gif" align="top" />%s</div>' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, $Translation['Loading ...']);
						$tabLoaders .= sprintf('post("parent-children.php", { ChildTable: "%s", ChildLookupField: "%s", SelectedID: "%s", Page: 1, SortBy: "", SortDirection: "", Operation: "get-records-printable" }, "panel_%s-%s");' . "\n\t\t\t\t", $ChildTable, $ChildLookupField, addslashes($SelectedID), $ChildTable, $ChildLookupField);
					}
				}
			}

			if(!$tabLabels){ die('<!-- no children of current parent table are accessible to current user -->'); }
			?>
			<div id="children-tabs" class="hidden-print">
				<div class="btn-group btn-group-lg">
					<?php echo $tabLabels; ?>
				</div>
				<span id="pc-loading"></span>
			</div>
			<div class="vspacer-lg"><?php echo $tabPanels; ?></div>

			<script>
				$j(function(){
					/* for iOS, avoid loading child tabs in modals */
					var iOS = /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
					var embedded = ($j('.navbar').length == 0);
					if(iOS && embedded){
						$j('#children-tabs').next('.tab-content').remove();
						$j('#children-tabs').remove();
						return;
					}

					/* ajax loading of each tab's contents */
					<?php echo $tabLoaders; ?>
				})
			</script>
			<?php
			break;

		/************************************************/
		case 'get-records-printable':
		default: /* default is 'get-records' */

			if($Operation == 'get-records-printable'){
				$userPCConfig[$ChildTable][$ChildLookupField]['records-per-page'] = 2000;
			}

			// build the user permissions limiter
			$permissionsWhere = $permissionsJoin = '';
			$permChild = getTablePermissions($ChildTable);
			if($permChild[2] == 1){ // user can view only his own records
				$permissionsWhere = "`$ChildTable`.`{$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key']}`=`membership_userrecords`.`pkValue` AND `membership_userrecords`.`tableName`='$ChildTable' AND LCASE(`membership_userrecords`.`memberID`)='".getLoggedMemberID()."'";
			}elseif($permChild[2] == 2){ // user can view only his group's records
				$permissionsWhere = "`$ChildTable`.`{$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key']}`=`membership_userrecords`.`pkValue` AND `membership_userrecords`.`tableName`='$ChildTable' AND `membership_userrecords`.`groupID`='".getLoggedGroupID()."'";
			}elseif($permChild[2] == 3){ // user can view all records
				/* that's the only case remaining ... no need to modify the query in this case */
			}
			$permissionsJoin = ($permissionsWhere ? ", `membership_userrecords`" : '');

			// build the count query
			$forcedWhere = $userPCConfig[$ChildTable][$ChildLookupField]['forced-where'];
			$query = 
				preg_replace('/^select .* from /i', 'SELECT count(1) FROM ', $userPCConfig[$ChildTable][$ChildLookupField]['query']) .
				$permissionsJoin . " WHERE " .
				($permissionsWhere ? "( $permissionsWhere )" : "( 1=1 )") . " AND " .
				($forcedWhere ? "( $forcedWhere )" : "( 2=2 )") . " AND " .
				"`$ChildTable`.`$ChildLookupField`='" . makeSafe($SelectedID) . "'";
			$totalMatches = sqlValue($query);

			// make sure $Page is <= max pages
			$maxPage = ceil($totalMatches / $userPCConfig[$ChildTable][$ChildLookupField]['records-per-page']);
			if($Page > $maxPage){ $Page = $maxPage; }

			// initiate output data array
			$data = array(
				'config' => $userPCConfig[$ChildTable][$ChildLookupField],
				'parameters' => array(
					'ChildTable' => $ChildTable,
					'ChildLookupField' => $ChildLookupField,
					'SelectedID' => $SelectedID,
					'Page' => $Page,
					'SortBy' => $SortBy,
					'SortDirection' => $SortDirection,
					'Operation' => $Operation
				),
				'records' => array(),
				'totalMatches' => $totalMatches
			);

			// build the data query
			if($totalMatches){ // if we have at least one record, proceed with fetching data
				$startRecord = $userPCConfig[$ChildTable][$ChildLookupField]['records-per-page'] * ($Page - 1);
				$data['query'] = 
					$userPCConfig[$ChildTable][$ChildLookupField]['query'] .
					$permissionsJoin . " WHERE " .
					($permissionsWhere ? "( $permissionsWhere )" : "( 1=1 )") . " AND " .
					($forcedWhere ? "( $forcedWhere )" : "( 2=2 )") . " AND " .
					"`$ChildTable`.`$ChildLookupField`='" . makeSafe($SelectedID) . "'" . 
					($SortBy !== false && $userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields'][$SortBy] ? " ORDER BY {$userPCConfig[$ChildTable][$ChildLookupField]['sortable-fields'][$SortBy]} $SortDirection" : '') .
					" LIMIT $startRecord, {$userPCConfig[$ChildTable][$ChildLookupField]['records-per-page']}";
				$res = sql($data['query'], $eo);
				while($row = db_fetch_row($res)){
					$data['records'][$row[$userPCConfig[$ChildTable][$ChildLookupField]['child-primary-key-index']]] = $row;
				}
			}else{ // if no matching records
				$startRecord = 0;
			}

			if($Operation == 'get-records-printable'){
				$response = loadView($userPCConfig[$ChildTable][$ChildLookupField]['template-printable'], $data);
			}else{
				$response = loadView($userPCConfig[$ChildTable][$ChildLookupField]['template'], $data);
			}

			// change name space to ensure uniqueness
			$uniqueNameSpace = $ChildTable.ucfirst($ChildLookupField).'GetRecords';
			echo str_replace("{$ChildTable}GetChildrenRecordsList", $uniqueNameSpace, $response);
		/************************************************/
	}
