<?php

// Data functions (insert, update, delete, form) for table billeteries

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

function billeteries_insert() {
	global $Translation;

	// mm: can member insert record?
	$arrPerm = getTablePermissions('billeteries');
	if(!$arrPerm[1]) return false;

	$data = array();
	$data['link_out'] = $_REQUEST['link_out'];
		if($data['link_out'] == empty_lookup_value) { $data['link_out'] = ''; }
	$data['facture'] = $_REQUEST['facture'];
		if($data['facture'] == empty_lookup_value) { $data['facture'] = ''; }
	$data['date'] = intval($_REQUEST['dateYear']) . '-' . intval($_REQUEST['dateMonth']) . '-' . intval($_REQUEST['dateDay']);
	$data['date'] = parseMySQLDate($data['date'], '1');
	$data['service'] = $_REQUEST['service'];
		if($data['service'] == empty_lookup_value) { $data['service'] = ''; }
	$data['passager'] = $_REQUEST['passager'];
		if($data['passager'] == empty_lookup_value) { $data['passager'] = ''; }
	$data['gds'] = $_REQUEST['gds'];
		if($data['gds'] == empty_lookup_value) { $data['gds'] = ''; }
	$data['compagnie'] = $_REQUEST['compagnie'];
		if($data['compagnie'] == empty_lookup_value) { $data['compagnie'] = ''; }
	$data['parcours'] = $_REQUEST['parcours'];
		if($data['parcours'] == empty_lookup_value) { $data['parcours'] = ''; }
	$data['nature_transaction'] = $_REQUEST['nature_transaction'];
		if($data['nature_transaction'] == empty_lookup_value) { $data['nature_transaction'] = ''; }
	$data['billet'] = $_REQUEST['billet'];
		if($data['billet'] == empty_lookup_value) { $data['billet'] = ''; }
	$data['dossier'] = $_REQUEST['dossier'];
		if($data['dossier'] == empty_lookup_value) { $data['dossier'] = ''; }
	$data['prix_achat'] = $_REQUEST['prix_achat'];
		if($data['prix_achat'] == empty_lookup_value) { $data['prix_achat'] = ''; }
	$data['prix_vente'] = $_REQUEST['prix_vente'];
		if($data['prix_vente'] == empty_lookup_value) { $data['prix_vente'] = ''; }
	$data['qts'] = $_REQUEST['qts'];
		if($data['qts'] == empty_lookup_value) { $data['qts'] = ''; }
	$data['commission'] = $_REQUEST['commission'];
		if($data['commission'] == empty_lookup_value) { $data['commission'] = ''; }
	$data['frais_service'] = $_REQUEST['frais_service'];
		if($data['frais_service'] == empty_lookup_value) { $data['frais_service'] = ''; }
	$data['frais_assurance'] = $_REQUEST['frais_assurance'];
		if($data['frais_assurance'] == empty_lookup_value) { $data['frais_assurance'] = ''; }
	$data['mtva'] = $_REQUEST['mtva'];
		if($data['mtva'] == empty_lookup_value) { $data['mtva'] = ''; }
	$data['tva'] = $_REQUEST['tva'];
		if($data['tva'] == empty_lookup_value) { $data['tva'] = ''; }
	$data['tht'] = $_REQUEST['tht'];
		if($data['tht'] == empty_lookup_value) { $data['tht'] = ''; }
	$data['net_compagnie'] = $_REQUEST['net_compagnie'];
		if($data['net_compagnie'] == empty_lookup_value) { $data['net_compagnie'] = ''; }
	$data['net_a_payer'] = $_REQUEST['net_a_payer'];
		if($data['net_a_payer'] == empty_lookup_value) { $data['net_a_payer'] = ''; }
	$data['account'] = $_REQUEST['account'];
		if($data['account'] == empty_lookup_value) { $data['account'] = ''; }
	$data['reste_a_payer'] = $_REQUEST['reste_a_payer'];
		if($data['reste_a_payer'] == empty_lookup_value) { $data['reste_a_payer'] = ''; }
	$data['agent'] = parseCode('<%%creatorUsername%%>', true);
	$data['groupe'] = parseCode('<%%creatorGroup%%>', true);
	$data['observations'] = br2nl($_REQUEST['observations']);
	if($data['service']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'Service': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	if($data['passager']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'Client': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	if($data['gds']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'GDS': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	if($data['compagnie']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'Compagnie': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	if($data['parcours']== '') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">" . $Translation['error:'] . " 'Parcours': " . $Translation['field not null'] . '<br><br>';
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	if($data['prix_achat'] == '') $data['prix_achat'] = "0";
	if($data['prix_vente'] == '') $data['prix_vente'] = "0";
	if($data['qts'] == '') $data['qts'] = "1";
	if($data['commission'] == '') $data['commission'] = "0";
	if($data['frais_service'] == '') $data['frais_service'] = "0";
	if($data['frais_assurance'] == '') $data['frais_assurance'] = "0";
	if($data['mtva'] == '') $data['mtva'] = "0";
	if($data['tva'] == '') $data['tva'] = "0";
	if($data['tht'] == '') $data['tht'] = "0";
	if($data['net_compagnie'] == '') $data['net_compagnie'] = "0";
	if($data['net_a_payer'] == '') $data['net_a_payer'] = "0";
	if($data['account'] == '') $data['account'] = "0";
	if($data['reste_a_payer'] == '') $data['reste_a_payer'] = "0";

	// hook: billeteries_before_insert
	if(function_exists('billeteries_before_insert')) {
		$args = array();
		if(!billeteries_before_insert($data, getMemberInfo(), $args)) { return false; }
	}

	$error = '';
	// set empty fields to NULL
	$data = array_map(function($v) { return ($v === '' ? NULL : $v); }, $data);
	insert('billeteries', backtick_keys_once($data), $error);
	if($error)
		die("{$error}<br><a href=\"#\" onclick=\"history.go(-1);\">{$Translation['< back']}</a>");

	$recID = db_insert_id(db_link());
	// enforce pk zerofill
	$recID = str_pad($recID, sqlValue("select length(`idbilleterie`) from `billeteries` limit 1"), '0', STR_PAD_LEFT);

	// automatic link_out if passed as filterer
	if($_REQUEST['filterer_link_out']) {
		sql("update `billeteries` set `link_out`='" . makeSafe($_REQUEST['filterer_link_out']) . "' where `idbilleterie`='" . makeSafe($recID, false) . "'", $eo);
	}

	// hook: billeteries_after_insert
	if(function_exists('billeteries_after_insert')) {
		$res = sql("select * from `billeteries` where `idbilleterie`='" . makeSafe($recID, false) . "' limit 1", $eo);
		if($row = db_fetch_assoc($res)) {
			$data = array_map('makeSafe', $row);
		}
		$data['selectedID'] = makeSafe($recID, false);
		$args=array();
		if(!billeteries_after_insert($data, getMemberInfo(), $args)) { return $recID; }
	}

	// mm: save ownership data
	set_record_owner('billeteries', $recID, getLoggedMemberID());

	// if this record is a copy of another record, copy children if applicable
	if(!empty($_REQUEST['SelectedID'])) billeteries_copy_children($recID, $_REQUEST['SelectedID']);

	return $recID;
}

function billeteries_copy_children($destination_id, $source_id) {
	global $Translation;
	$requests = array(); // array of curl handlers for launching insert requests
	$eo = array('silentErrors' => true);
	$uploads_dir = realpath(dirname(__FILE__) . '/../' . $Translation['ImageFolder']);
	$safe_sid = makeSafe($source_id);

	// launch requests, asynchronously
	curl_batch($requests);
}

function billeteries_delete($selected_id, $AllowDeleteOfParents=false, $skipChecks=false) {
	// insure referential integrity ...
	global $Translation;
	$selected_id=makeSafe($selected_id);

	// mm: can member delete record?
	$arrPerm=getTablePermissions('billeteries');
	$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='billeteries' and pkValue='$selected_id'");
	$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='billeteries' and pkValue='$selected_id'");
	if(($arrPerm[4]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[4]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[4]==3) { // allow delete?
		// delete allowed, so continue ...
	}else{
		return $Translation['You don\'t have enough permissions to delete this record'];
	}

	// hook: billeteries_before_delete
	if(function_exists('billeteries_before_delete')) {
		$args=array();
		if(!billeteries_before_delete($selected_id, $skipChecks, getMemberInfo(), $args))
			return $Translation['Couldn\'t delete this record'];
	}

	sql("delete from `billeteries` where `idbilleterie`='$selected_id'", $eo);

	// hook: billeteries_after_delete
	if(function_exists('billeteries_after_delete')) {
		$args=array();
		billeteries_after_delete($selected_id, getMemberInfo(), $args);
	}

	// mm: delete ownership data
	sql("delete from membership_userrecords where tableName='billeteries' and pkValue='$selected_id'", $eo);
}

function billeteries_update($selected_id) {
	global $Translation;

	// mm: can member edit record?
	$arrPerm=getTablePermissions('billeteries');
	$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='billeteries' and pkValue='".makeSafe($selected_id)."'");
	$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='billeteries' and pkValue='".makeSafe($selected_id)."'");
	if(($arrPerm[3]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[3]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[3]==3) { // allow update?
		// update allowed, so continue ...
	}else{
		return false;
	}

	$data['link_out'] = makeSafe($_REQUEST['link_out']);
		if($data['link_out'] == empty_lookup_value) { $data['link_out'] = ''; }
	$data['facture'] = makeSafe($_REQUEST['facture']);
		if($data['facture'] == empty_lookup_value) { $data['facture'] = ''; }
	$data['date'] = intval($_REQUEST['dateYear']) . '-' . intval($_REQUEST['dateMonth']) . '-' . intval($_REQUEST['dateDay']);
	$data['date'] = parseMySQLDate($data['date'], '1');
	$data['service'] = makeSafe($_REQUEST['service']);
		if($data['service'] == empty_lookup_value) { $data['service'] = ''; }
	if($data['service']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'Service': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['passager'] = makeSafe($_REQUEST['passager']);
		if($data['passager'] == empty_lookup_value) { $data['passager'] = ''; }
	if($data['passager']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'Client': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['gds'] = makeSafe($_REQUEST['gds']);
		if($data['gds'] == empty_lookup_value) { $data['gds'] = ''; }
	if($data['gds']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'GDS': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['compagnie'] = makeSafe($_REQUEST['compagnie']);
		if($data['compagnie'] == empty_lookup_value) { $data['compagnie'] = ''; }
	if($data['compagnie']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'Compagnie': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['parcours'] = makeSafe($_REQUEST['parcours']);
		if($data['parcours'] == empty_lookup_value) { $data['parcours'] = ''; }
	if($data['parcours']=='') {
		echo StyleSheet() . "\n\n<div class=\"alert alert-danger\">{$Translation['error:']} 'Parcours': {$Translation['field not null']}<br><br>";
		echo '<a href="" onclick="history.go(-1); return false;">'.$Translation['< back'].'</a></div>';
		exit;
	}
	$data['nature_transaction'] = makeSafe($_REQUEST['nature_transaction']);
		if($data['nature_transaction'] == empty_lookup_value) { $data['nature_transaction'] = ''; }
	$data['billet'] = makeSafe($_REQUEST['billet']);
		if($data['billet'] == empty_lookup_value) { $data['billet'] = ''; }
	$data['dossier'] = makeSafe($_REQUEST['dossier']);
		if($data['dossier'] == empty_lookup_value) { $data['dossier'] = ''; }
	$data['prix_achat'] = makeSafe($_REQUEST['prix_achat']);
		if($data['prix_achat'] == empty_lookup_value) { $data['prix_achat'] = ''; }
	$data['prix_vente'] = makeSafe($_REQUEST['prix_vente']);
		if($data['prix_vente'] == empty_lookup_value) { $data['prix_vente'] = ''; }
	$data['qts'] = makeSafe($_REQUEST['qts']);
		if($data['qts'] == empty_lookup_value) { $data['qts'] = ''; }
	$data['commission'] = makeSafe($_REQUEST['commission']);
		if($data['commission'] == empty_lookup_value) { $data['commission'] = ''; }
	$data['frais_service'] = makeSafe($_REQUEST['frais_service']);
		if($data['frais_service'] == empty_lookup_value) { $data['frais_service'] = ''; }
	$data['frais_assurance'] = makeSafe($_REQUEST['frais_assurance']);
		if($data['frais_assurance'] == empty_lookup_value) { $data['frais_assurance'] = ''; }
	$data['mtva'] = makeSafe($_REQUEST['mtva']);
		if($data['mtva'] == empty_lookup_value) { $data['mtva'] = ''; }
	$data['tva'] = makeSafe($_REQUEST['tva']);
		if($data['tva'] == empty_lookup_value) { $data['tva'] = ''; }
	$data['tht'] = makeSafe($_REQUEST['tht']);
		if($data['tht'] == empty_lookup_value) { $data['tht'] = ''; }
	$data['net_compagnie'] = makeSafe($_REQUEST['net_compagnie']);
		if($data['net_compagnie'] == empty_lookup_value) { $data['net_compagnie'] = ''; }
	$data['net_a_payer'] = makeSafe($_REQUEST['net_a_payer']);
		if($data['net_a_payer'] == empty_lookup_value) { $data['net_a_payer'] = ''; }
	$data['account'] = makeSafe($_REQUEST['account']);
		if($data['account'] == empty_lookup_value) { $data['account'] = ''; }
	$data['reste_a_payer'] = makeSafe($_REQUEST['reste_a_payer']);
		if($data['reste_a_payer'] == empty_lookup_value) { $data['reste_a_payer'] = ''; }
	$data['observations'] = br2nl(makeSafe($_REQUEST['observations']));
	$data['selectedID'] = makeSafe($selected_id);

	// hook: billeteries_before_update
	if(function_exists('billeteries_before_update')) {
		$args = array();
		if(!billeteries_before_update($data, getMemberInfo(), $args)) { return false; }
	}

	$o = array('silentErrors' => true);
	sql('update `billeteries` set       `date`=' . (($data['date'] !== '' && $data['date'] !== NULL) ? "'{$data['date']}'" : 'NULL') . ', `service`=' . (($data['service'] !== '' && $data['service'] !== NULL) ? "'{$data['service']}'" : 'NULL') . ', `passager`=' . (($data['passager'] !== '' && $data['passager'] !== NULL) ? "'{$data['passager']}'" : 'NULL') . ', `gds`=' . (($data['gds'] !== '' && $data['gds'] !== NULL) ? "'{$data['gds']}'" : 'NULL') . ', `compagnie`=' . (($data['compagnie'] !== '' && $data['compagnie'] !== NULL) ? "'{$data['compagnie']}'" : 'NULL') . ', `parcours`=' . (($data['parcours'] !== '' && $data['parcours'] !== NULL) ? "'{$data['parcours']}'" : 'NULL') . ', `nature_transaction`=' . (($data['nature_transaction'] !== '' && $data['nature_transaction'] !== NULL) ? "'{$data['nature_transaction']}'" : 'NULL') . ', `billet`=' . (($data['billet'] !== '' && $data['billet'] !== NULL) ? "'{$data['billet']}'" : 'NULL') . ', `dossier`=' . (($data['dossier'] !== '' && $data['dossier'] !== NULL) ? "'{$data['dossier']}'" : 'NULL') . ', `prix_achat`=' . (($data['prix_achat'] !== '' && $data['prix_achat'] !== NULL) ? "'{$data['prix_achat']}'" : 'NULL') . ', `prix_vente`=' . (($data['prix_vente'] !== '' && $data['prix_vente'] !== NULL) ? "'{$data['prix_vente']}'" : 'NULL') . ', `qts`=' . (($data['qts'] !== '' && $data['qts'] !== NULL) ? "'{$data['qts']}'" : 'NULL') . ', `commission`=' . (($data['commission'] !== '' && $data['commission'] !== NULL) ? "'{$data['commission']}'" : 'NULL') . ', `frais_service`=' . (($data['frais_service'] !== '' && $data['frais_service'] !== NULL) ? "'{$data['frais_service']}'" : 'NULL') . ', `frais_assurance`=' . (($data['frais_assurance'] !== '' && $data['frais_assurance'] !== NULL) ? "'{$data['frais_assurance']}'" : 'NULL') . ', `mtva`=' . (($data['mtva'] !== '' && $data['mtva'] !== NULL) ? "'{$data['mtva']}'" : 'NULL') . ', `tva`=' . (($data['tva'] !== '' && $data['tva'] !== NULL) ? "'{$data['tva']}'" : 'NULL') . ', `tht`=' . (($data['tht'] !== '' && $data['tht'] !== NULL) ? "'{$data['tht']}'" : 'NULL') . ', `net_compagnie`=' . (($data['net_compagnie'] !== '' && $data['net_compagnie'] !== NULL) ? "'{$data['net_compagnie']}'" : 'NULL') . ', `net_a_payer`=' . (($data['net_a_payer'] !== '' && $data['net_a_payer'] !== NULL) ? "'{$data['net_a_payer']}'" : 'NULL') . ', `account`=' . (($data['account'] !== '' && $data['account'] !== NULL) ? "'{$data['account']}'" : 'NULL') . ', `reste_a_payer`=' . (($data['reste_a_payer'] !== '' && $data['reste_a_payer'] !== NULL) ? "'{$data['reste_a_payer']}'" : 'NULL') . ', `observations`=' . (($data['observations'] !== '' && $data['observations'] !== NULL) ? "'{$data['observations']}'" : 'NULL') . " where `idbilleterie`='".makeSafe($selected_id)."'", $o);
	if($o['error']!='') {
		echo $o['error'];
		echo '<a href="billeteries_view.php?SelectedID='.urlencode($selected_id)."\">{$Translation['< back']}</a>";
		exit;
	}


	// hook: billeteries_after_update
	if(function_exists('billeteries_after_update')) {
		$res = sql("SELECT * FROM `billeteries` WHERE `idbilleterie`='{$data['selectedID']}' LIMIT 1", $eo);
		if($row = db_fetch_assoc($res)) {
			$data = array_map('makeSafe', $row);
		}
		$data['selectedID'] = $data['idbilleterie'];
		$args = array();
		if(!billeteries_after_update($data, getMemberInfo(), $args)) { return; }
	}

	// mm: update ownership data
	sql("update `membership_userrecords` set `dateUpdated`='" . time() . "' where `tableName`='billeteries' and `pkValue`='" . makeSafe($selected_id) . "'", $eo);

}

function billeteries_form($selected_id = '', $AllowUpdate = 1, $AllowInsert = 1, $AllowDelete = 1, $ShowCancel = 0, $TemplateDV = '', $TemplateDVP = '') {
	// function to return an editable form for a table records
	// and fill it with data of record whose ID is $selected_id. If $selected_id
	// is empty, an empty form is shown, with only an 'Add New'
	// button displayed.

	global $Translation;

	// mm: get table permissions
	$arrPerm=getTablePermissions('billeteries');
	if(!$arrPerm[1] && $selected_id=='') { return ''; }
	$AllowInsert = ($arrPerm[1] ? true : false);
	// print preview?
	$dvprint = false;
	if($selected_id && $_REQUEST['dvprint_x'] != '') {
		$dvprint = true;
	}

	$filterer_link_out = thisOr(undo_magic_quotes($_REQUEST['filterer_link_out']), '');
	$filterer_service = thisOr(undo_magic_quotes($_REQUEST['filterer_service']), '');
	$filterer_passager = thisOr(undo_magic_quotes($_REQUEST['filterer_passager']), '');
	$filterer_gds = thisOr(undo_magic_quotes($_REQUEST['filterer_gds']), '');
	$filterer_compagnie = thisOr(undo_magic_quotes($_REQUEST['filterer_compagnie']), '');
	$filterer_parcours = thisOr(undo_magic_quotes($_REQUEST['filterer_parcours']), '');
	$filterer_nature_transaction = thisOr(undo_magic_quotes($_REQUEST['filterer_nature_transaction']), '');

	// populate filterers, starting from children to grand-parents

	// unique random identifier
	$rnd1 = ($dvprint ? rand(1000000, 9999999) : '');
	// combobox: link_out
	$combo_link_out = new DataCombo;
	// combobox: date
	$combo_date = new DateCombo;
	$combo_date->DateFormat = "dmy";
	$combo_date->MinYear = 1900;
	$combo_date->MaxYear = 2100;
	$combo_date->DefaultDate = parseMySQLDate('1', '1');
	$combo_date->MonthNames = $Translation['month names'];
	$combo_date->NamePrefix = 'date';
	// combobox: service
	$combo_service = new DataCombo;
	// combobox: passager
	$combo_passager = new DataCombo;
	// combobox: gds
	$combo_gds = new DataCombo;
	// combobox: compagnie
	$combo_compagnie = new DataCombo;
	// combobox: parcours
	$combo_parcours = new DataCombo;
	// combobox: nature_transaction
	$combo_nature_transaction = new DataCombo;

	if($selected_id) {
		// mm: check member permissions
		if(!$arrPerm[2]) {
			return "";
		}
		// mm: who is the owner?
		$ownerGroupID=sqlValue("select groupID from membership_userrecords where tableName='billeteries' and pkValue='".makeSafe($selected_id)."'");
		$ownerMemberID=sqlValue("select lcase(memberID) from membership_userrecords where tableName='billeteries' and pkValue='".makeSafe($selected_id)."'");
		if($arrPerm[2]==1 && getLoggedMemberID()!=$ownerMemberID) {
			return "";
		}
		if($arrPerm[2]==2 && getLoggedGroupID()!=$ownerGroupID) {
			return "";
		}

		// can edit?
		if(($arrPerm[3]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[3]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[3]==3) {
			$AllowUpdate=1;
		}else{
			$AllowUpdate=0;
		}

		$res = sql("SELECT * FROM `billeteries` WHERE `idbilleterie`='" . makeSafe($selected_id) . "'", $eo);
		if(!($row = db_fetch_array($res))) {
			return error_message($Translation['No records found'], 'billeteries_view.php', false);
		}
		$combo_link_out->SelectedData = $row['link_out'];
		$combo_date->DefaultDate = $row['date'];
		$combo_service->SelectedData = $row['service'];
		$combo_passager->SelectedData = $row['passager'];
		$combo_gds->SelectedData = $row['gds'];
		$combo_compagnie->SelectedData = $row['compagnie'];
		$combo_parcours->SelectedData = $row['parcours'];
		$combo_nature_transaction->SelectedData = $row['nature_transaction'];
		$urow = $row; /* unsanitized data */
		$hc = new CI_Input();
		$row = $hc->xss_clean($row); /* sanitize data */
	} else {
		$combo_link_out->SelectedData = $filterer_link_out;
		$combo_service->SelectedData = $filterer_service;
		$combo_passager->SelectedData = $filterer_passager;
		$combo_gds->SelectedData = $filterer_gds;
		$combo_compagnie->SelectedData = $filterer_compagnie;
		$combo_parcours->SelectedData = $filterer_parcours;
		$combo_nature_transaction->SelectedData = $filterer_nature_transaction;
	}
	$combo_link_out->HTML = '<span id="link_out-container' . $rnd1 . '"></span><input type="hidden" name="link_out" id="link_out' . $rnd1 . '" value="' . html_attr($combo_link_out->SelectedData) . '">';
	$combo_link_out->MatchText = '<span id="link_out-container-readonly' . $rnd1 . '"></span><input type="hidden" name="link_out" id="link_out' . $rnd1 . '" value="' . html_attr($combo_link_out->SelectedData) . '">';
	$combo_service->HTML = '<span id="service-container' . $rnd1 . '"></span><input type="hidden" name="service" id="service' . $rnd1 . '" value="' . html_attr($combo_service->SelectedData) . '">';
	$combo_service->MatchText = '<span id="service-container-readonly' . $rnd1 . '"></span><input type="hidden" name="service" id="service' . $rnd1 . '" value="' . html_attr($combo_service->SelectedData) . '">';
	$combo_passager->HTML = '<span id="passager-container' . $rnd1 . '"></span><input type="hidden" name="passager" id="passager' . $rnd1 . '" value="' . html_attr($combo_passager->SelectedData) . '">';
	$combo_passager->MatchText = '<span id="passager-container-readonly' . $rnd1 . '"></span><input type="hidden" name="passager" id="passager' . $rnd1 . '" value="' . html_attr($combo_passager->SelectedData) . '">';
	$combo_gds->HTML = '<span id="gds-container' . $rnd1 . '"></span><input type="hidden" name="gds" id="gds' . $rnd1 . '" value="' . html_attr($combo_gds->SelectedData) . '">';
	$combo_gds->MatchText = '<span id="gds-container-readonly' . $rnd1 . '"></span><input type="hidden" name="gds" id="gds' . $rnd1 . '" value="' . html_attr($combo_gds->SelectedData) . '">';
	$combo_compagnie->HTML = '<span id="compagnie-container' . $rnd1 . '"></span><input type="hidden" name="compagnie" id="compagnie' . $rnd1 . '" value="' . html_attr($combo_compagnie->SelectedData) . '">';
	$combo_compagnie->MatchText = '<span id="compagnie-container-readonly' . $rnd1 . '"></span><input type="hidden" name="compagnie" id="compagnie' . $rnd1 . '" value="' . html_attr($combo_compagnie->SelectedData) . '">';
	$combo_parcours->HTML = '<span id="parcours-container' . $rnd1 . '"></span><input type="hidden" name="parcours" id="parcours' . $rnd1 . '" value="' . html_attr($combo_parcours->SelectedData) . '">';
	$combo_parcours->MatchText = '<span id="parcours-container-readonly' . $rnd1 . '"></span><input type="hidden" name="parcours" id="parcours' . $rnd1 . '" value="' . html_attr($combo_parcours->SelectedData) . '">';
	$combo_nature_transaction->HTML = '<span id="nature_transaction-container' . $rnd1 . '"></span><input type="hidden" name="nature_transaction" id="nature_transaction' . $rnd1 . '" value="' . html_attr($combo_nature_transaction->SelectedData) . '">';
	$combo_nature_transaction->MatchText = '<span id="nature_transaction-container-readonly' . $rnd1 . '"></span><input type="hidden" name="nature_transaction" id="nature_transaction' . $rnd1 . '" value="' . html_attr($combo_nature_transaction->SelectedData) . '">';

	ob_start();
	?>

	<script>
		// initial lookup values
		AppGini.current_link_out__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['link_out'] : $filterer_link_out); ?>"};
		AppGini.current_service__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['service'] : $filterer_service); ?>"};
		AppGini.current_passager__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['passager'] : $filterer_passager); ?>"};
		AppGini.current_gds__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['gds'] : $filterer_gds); ?>"};
		AppGini.current_compagnie__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['compagnie'] : $filterer_compagnie); ?>"};
		AppGini.current_parcours__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['parcours'] : $filterer_parcours); ?>"};
		AppGini.current_nature_transaction__RAND__ = { text: "", value: "<?php echo addslashes($selected_id ? $urow['nature_transaction'] : $filterer_nature_transaction); ?>"};

		jQuery(function() {
			setTimeout(function() {
				if(typeof(link_out_reload__RAND__) == 'function') link_out_reload__RAND__();
				if(typeof(service_reload__RAND__) == 'function') service_reload__RAND__();
				if(typeof(passager_reload__RAND__) == 'function') passager_reload__RAND__();
				if(typeof(gds_reload__RAND__) == 'function') gds_reload__RAND__();
				if(typeof(compagnie_reload__RAND__) == 'function') compagnie_reload__RAND__();
				if(typeof(parcours_reload__RAND__) == 'function') parcours_reload__RAND__();
				if(typeof(nature_transaction_reload__RAND__) == 'function') nature_transaction_reload__RAND__();
			}, 10); /* we need to slightly delay client-side execution of the above code to allow AppGini.ajaxCache to work */
		});
		function link_out_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#link_out-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_link_out__RAND__.value, t: 'billeteries', f: 'link_out' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="link_out"]').val(resp.results[0].id);
							$j('[id=link_out-container-readonly__RAND__]').html('<span id="link_out-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gestion_transactions_view_parent]').hide(); }else{ $j('.btn[id=gestion_transactions_view_parent]').show(); }


							if(typeof(link_out_update_autofills__RAND__) == 'function') link_out_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'link_out' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_link_out__RAND__.value = e.added.id;
				AppGini.current_link_out__RAND__.text = e.added.text;
				$j('[name="link_out"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gestion_transactions_view_parent]').hide(); }else{ $j('.btn[id=gestion_transactions_view_parent]').show(); }


				if(typeof(link_out_update_autofills__RAND__) == 'function') link_out_update_autofills__RAND__();
			});

			if(!$j("#link_out-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_link_out__RAND__.value, t: 'billeteries', f: 'link_out' },
					success: function(resp) {
						$j('[name="link_out"]').val(resp.results[0].id);
						$j('[id=link_out-container-readonly__RAND__]').html('<span id="link_out-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gestion_transactions_view_parent]').hide(); }else{ $j('.btn[id=gestion_transactions_view_parent]').show(); }

						if(typeof(link_out_update_autofills__RAND__) == 'function') link_out_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_link_out__RAND__.value, t: 'billeteries', f: 'link_out' },
				success: function(resp) {
					$j('[id=link_out-container__RAND__], [id=link_out-container-readonly__RAND__]').html('<span id="link_out-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gestion_transactions_view_parent]').hide(); }else{ $j('.btn[id=gestion_transactions_view_parent]').show(); }

					if(typeof(link_out_update_autofills__RAND__) == 'function') link_out_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function service_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#service-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_service__RAND__.value, t: 'billeteries', f: 'service' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="service"]').val(resp.results[0].id);
							$j('[id=service-container-readonly__RAND__]').html('<span id="service-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }


							if(typeof(service_update_autofills__RAND__) == 'function') service_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'service' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_service__RAND__.value = e.added.id;
				AppGini.current_service__RAND__.text = e.added.text;
				$j('[name="service"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }


				if(typeof(service_update_autofills__RAND__) == 'function') service_update_autofills__RAND__();
			});

			if(!$j("#service-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_service__RAND__.value, t: 'billeteries', f: 'service' },
					success: function(resp) {
						$j('[name="service"]').val(resp.results[0].id);
						$j('[id=service-container-readonly__RAND__]').html('<span id="service-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }

						if(typeof(service_update_autofills__RAND__) == 'function') service_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_service__RAND__.value, t: 'billeteries', f: 'service' },
				success: function(resp) {
					$j('[id=service-container__RAND__], [id=service-container-readonly__RAND__]').html('<span id="service-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }

					if(typeof(service_update_autofills__RAND__) == 'function') service_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function passager_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#passager-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_passager__RAND__.value, t: 'billeteries', f: 'passager' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="passager"]').val(resp.results[0].id);
							$j('[id=passager-container-readonly__RAND__]').html('<span id="passager-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


							if(typeof(passager_update_autofills__RAND__) == 'function') passager_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'passager' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_passager__RAND__.value = e.added.id;
				AppGini.current_passager__RAND__.text = e.added.text;
				$j('[name="passager"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }


				if(typeof(passager_update_autofills__RAND__) == 'function') passager_update_autofills__RAND__();
			});

			if(!$j("#passager-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_passager__RAND__.value, t: 'billeteries', f: 'passager' },
					success: function(resp) {
						$j('[name="passager"]').val(resp.results[0].id);
						$j('[id=passager-container-readonly__RAND__]').html('<span id="passager-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

						if(typeof(passager_update_autofills__RAND__) == 'function') passager_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_passager__RAND__.value, t: 'billeteries', f: 'passager' },
				success: function(resp) {
					$j('[id=passager-container__RAND__], [id=passager-container-readonly__RAND__]').html('<span id="passager-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=clients_view_parent]').hide(); }else{ $j('.btn[id=clients_view_parent]').show(); }

					if(typeof(passager_update_autofills__RAND__) == 'function') passager_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function gds_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#gds-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_gds__RAND__.value, t: 'billeteries', f: 'gds' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="gds"]').val(resp.results[0].id);
							$j('[id=gds-container-readonly__RAND__]').html('<span id="gds-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gds_view_parent]').hide(); }else{ $j('.btn[id=gds_view_parent]').show(); }


							if(typeof(gds_update_autofills__RAND__) == 'function') gds_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'gds' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_gds__RAND__.value = e.added.id;
				AppGini.current_gds__RAND__.text = e.added.text;
				$j('[name="gds"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gds_view_parent]').hide(); }else{ $j('.btn[id=gds_view_parent]').show(); }


				if(typeof(gds_update_autofills__RAND__) == 'function') gds_update_autofills__RAND__();
			});

			if(!$j("#gds-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_gds__RAND__.value, t: 'billeteries', f: 'gds' },
					success: function(resp) {
						$j('[name="gds"]').val(resp.results[0].id);
						$j('[id=gds-container-readonly__RAND__]').html('<span id="gds-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gds_view_parent]').hide(); }else{ $j('.btn[id=gds_view_parent]').show(); }

						if(typeof(gds_update_autofills__RAND__) == 'function') gds_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_gds__RAND__.value, t: 'billeteries', f: 'gds' },
				success: function(resp) {
					$j('[id=gds-container__RAND__], [id=gds-container-readonly__RAND__]').html('<span id="gds-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=gds_view_parent]').hide(); }else{ $j('.btn[id=gds_view_parent]').show(); }

					if(typeof(gds_update_autofills__RAND__) == 'function') gds_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function compagnie_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#compagnie-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_compagnie__RAND__.value, t: 'billeteries', f: 'compagnie' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="compagnie"]').val(resp.results[0].id);
							$j('[id=compagnie-container-readonly__RAND__]').html('<span id="compagnie-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=compagnies_view_parent]').hide(); }else{ $j('.btn[id=compagnies_view_parent]').show(); }


							if(typeof(compagnie_update_autofills__RAND__) == 'function') compagnie_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'compagnie' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_compagnie__RAND__.value = e.added.id;
				AppGini.current_compagnie__RAND__.text = e.added.text;
				$j('[name="compagnie"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=compagnies_view_parent]').hide(); }else{ $j('.btn[id=compagnies_view_parent]').show(); }


				if(typeof(compagnie_update_autofills__RAND__) == 'function') compagnie_update_autofills__RAND__();
			});

			if(!$j("#compagnie-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_compagnie__RAND__.value, t: 'billeteries', f: 'compagnie' },
					success: function(resp) {
						$j('[name="compagnie"]').val(resp.results[0].id);
						$j('[id=compagnie-container-readonly__RAND__]').html('<span id="compagnie-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=compagnies_view_parent]').hide(); }else{ $j('.btn[id=compagnies_view_parent]').show(); }

						if(typeof(compagnie_update_autofills__RAND__) == 'function') compagnie_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_compagnie__RAND__.value, t: 'billeteries', f: 'compagnie' },
				success: function(resp) {
					$j('[id=compagnie-container__RAND__], [id=compagnie-container-readonly__RAND__]').html('<span id="compagnie-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=compagnies_view_parent]').hide(); }else{ $j('.btn[id=compagnies_view_parent]').show(); }

					if(typeof(compagnie_update_autofills__RAND__) == 'function') compagnie_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function parcours_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#parcours-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_parcours__RAND__.value, t: 'billeteries', f: 'parcours' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="parcours"]').val(resp.results[0].id);
							$j('[id=parcours-container-readonly__RAND__]').html('<span id="parcours-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=items_view_parent]').hide(); }else{ $j('.btn[id=items_view_parent]').show(); }


							if(typeof(parcours_update_autofills__RAND__) == 'function') parcours_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'parcours' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_parcours__RAND__.value = e.added.id;
				AppGini.current_parcours__RAND__.text = e.added.text;
				$j('[name="parcours"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=items_view_parent]').hide(); }else{ $j('.btn[id=items_view_parent]').show(); }


				if(typeof(parcours_update_autofills__RAND__) == 'function') parcours_update_autofills__RAND__();
			});

			if(!$j("#parcours-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_parcours__RAND__.value, t: 'billeteries', f: 'parcours' },
					success: function(resp) {
						$j('[name="parcours"]').val(resp.results[0].id);
						$j('[id=parcours-container-readonly__RAND__]').html('<span id="parcours-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=items_view_parent]').hide(); }else{ $j('.btn[id=items_view_parent]').show(); }

						if(typeof(parcours_update_autofills__RAND__) == 'function') parcours_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_parcours__RAND__.value, t: 'billeteries', f: 'parcours' },
				success: function(resp) {
					$j('[id=parcours-container__RAND__], [id=parcours-container-readonly__RAND__]').html('<span id="parcours-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=items_view_parent]').hide(); }else{ $j('.btn[id=items_view_parent]').show(); }

					if(typeof(parcours_update_autofills__RAND__) == 'function') parcours_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
		function nature_transaction_reload__RAND__() {
		<?php if(($AllowUpdate || $AllowInsert) && !$dvprint) { ?>

			$j("#nature_transaction-container__RAND__").select2({
				/* initial default value */
				initSelection: function(e, c) {
					$j.ajax({
						url: 'ajax_combo.php',
						dataType: 'json',
						data: { id: AppGini.current_nature_transaction__RAND__.value, t: 'billeteries', f: 'nature_transaction' },
						success: function(resp) {
							c({
								id: resp.results[0].id,
								text: resp.results[0].text
							});
							$j('[name="nature_transaction"]').val(resp.results[0].id);
							$j('[id=nature_transaction-container-readonly__RAND__]').html('<span id="nature_transaction-match-text">' + resp.results[0].text + '</span>');
							if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }


							if(typeof(nature_transaction_update_autofills__RAND__) == 'function') nature_transaction_update_autofills__RAND__();
						}
					});
				},
				width: '100%',
				formatNoMatches: function(term) { /* */ return '<?php echo addslashes($Translation['No matches found!']); ?>'; },
				minimumResultsForSearch: 5,
				loadMorePadding: 200,
				ajax: {
					url: 'ajax_combo.php',
					dataType: 'json',
					cache: true,
					data: function(term, page) { /* */ return { s: term, p: page, t: 'billeteries', f: 'nature_transaction' }; },
					results: function(resp, page) { /* */ return resp; }
				},
				escapeMarkup: function(str) { /* */ return str; }
			}).on('change', function(e) {
				AppGini.current_nature_transaction__RAND__.value = e.added.id;
				AppGini.current_nature_transaction__RAND__.text = e.added.text;
				$j('[name="nature_transaction"]').val(e.added.id);
				if(e.added.id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }


				if(typeof(nature_transaction_update_autofills__RAND__) == 'function') nature_transaction_update_autofills__RAND__();
			});

			if(!$j("#nature_transaction-container__RAND__").length) {
				$j.ajax({
					url: 'ajax_combo.php',
					dataType: 'json',
					data: { id: AppGini.current_nature_transaction__RAND__.value, t: 'billeteries', f: 'nature_transaction' },
					success: function(resp) {
						$j('[name="nature_transaction"]').val(resp.results[0].id);
						$j('[id=nature_transaction-container-readonly__RAND__]').html('<span id="nature_transaction-match-text">' + resp.results[0].text + '</span>');
						if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }

						if(typeof(nature_transaction_update_autofills__RAND__) == 'function') nature_transaction_update_autofills__RAND__();
					}
				});
			}

		<?php }else{ ?>

			$j.ajax({
				url: 'ajax_combo.php',
				dataType: 'json',
				data: { id: AppGini.current_nature_transaction__RAND__.value, t: 'billeteries', f: 'nature_transaction' },
				success: function(resp) {
					$j('[id=nature_transaction-container__RAND__], [id=nature_transaction-container-readonly__RAND__]').html('<span id="nature_transaction-match-text">' + resp.results[0].text + '</span>');
					if(resp.results[0].id == '<?php echo empty_lookup_value; ?>') { $j('.btn[id=produits_services_view_parent]').hide(); }else{ $j('.btn[id=produits_services_view_parent]').show(); }

					if(typeof(nature_transaction_update_autofills__RAND__) == 'function') nature_transaction_update_autofills__RAND__();
				}
			});
		<?php } ?>

		}
	</script>
	<?php

	$lookups = str_replace('__RAND__', $rnd1, ob_get_contents());
	ob_end_clean();


	// code for template based detail view forms

	// open the detail view template
	if($dvprint) {
		$template_file = is_file("./{$TemplateDVP}") ? "./{$TemplateDVP}" : './templates/billeteries_templateDVP.html';
		$templateCode = @file_get_contents($template_file);
	}else{
		$template_file = is_file("./{$TemplateDV}") ? "./{$TemplateDV}" : './templates/billeteries_templateDV.html';
		$templateCode = @file_get_contents($template_file);
	}

	// process form title
	$templateCode = str_replace('<%%DETAIL_VIEW_TITLE%%>', 'Liste des transactions', $templateCode);
	$templateCode = str_replace('<%%RND1%%>', $rnd1, $templateCode);
	$templateCode = str_replace('<%%EMBEDDED%%>', ($_REQUEST['Embedded'] ? 'Embedded=1' : ''), $templateCode);
	// process buttons
	if($arrPerm[1] && !$selected_id) { // allow insert and no record selected?
		if(!$selected_id) $templateCode = str_replace('<%%INSERT_BUTTON%%>', '<button type="submit" class="btn btn-success" id="insert" name="insert_x" value="1" onclick="return billeteries_validateData();"><i class="glyphicon glyphicon-plus-sign"></i> ' . $Translation['Save New'] . '</button>', $templateCode);
		$templateCode = str_replace('<%%INSERT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="insert" name="insert_x" value="1" onclick="return billeteries_validateData();"><i class="glyphicon glyphicon-plus-sign"></i> ' . $Translation['Save As Copy'] . '</button>', $templateCode);
	}else{
		$templateCode = str_replace('<%%INSERT_BUTTON%%>', '', $templateCode);
	}

	// 'Back' button action
	if($_REQUEST['Embedded']) {
		$backAction = 'AppGini.closeParentModal(); return false;';
	}else{
		$backAction = '$j(\'form\').eq(0).attr(\'novalidate\', \'novalidate\'); document.myform.reset(); return true;';
	}

	if($selected_id) {
		if(!$_REQUEST['Embedded']) $templateCode = str_replace('<%%DVPRINT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="dvprint" name="dvprint_x" value="1" onclick="$j(\'form\').eq(0).prop(\'novalidate\', true); document.myform.reset(); return true;" title="' . html_attr($Translation['Print Preview']) . '"><i class="glyphicon glyphicon-print"></i> ' . $Translation['Print Preview'] . '</button>', $templateCode);
		if($AllowUpdate) {
			$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '<button type="submit" class="btn btn-success btn-lg" id="update" name="update_x" value="1" onclick="return billeteries_validateData();" title="' . html_attr($Translation['Save Changes']) . '"><i class="glyphicon glyphicon-ok"></i> ' . $Translation['Save Changes'] . '</button>', $templateCode);
		}else{
			$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '', $templateCode);
		}
		if(($arrPerm[4]==1 && $ownerMemberID==getLoggedMemberID()) || ($arrPerm[4]==2 && $ownerGroupID==getLoggedGroupID()) || $arrPerm[4]==3) { // allow delete?
			$templateCode = str_replace('<%%DELETE_BUTTON%%>', '<button type="submit" class="btn btn-danger" id="delete" name="delete_x" value="1" onclick="return confirm(\'' . $Translation['are you sure?'] . '\');" title="' . html_attr($Translation['Delete']) . '"><i class="glyphicon glyphicon-trash"></i> ' . $Translation['Delete'] . '</button>', $templateCode);
		}else{
			$templateCode = str_replace('<%%DELETE_BUTTON%%>', '', $templateCode);
		}
		$templateCode = str_replace('<%%DESELECT_BUTTON%%>', '<button type="submit" class="btn btn-default" id="deselect" name="deselect_x" value="1" onclick="' . $backAction . '" title="' . html_attr($Translation['Back']) . '"><i class="glyphicon glyphicon-chevron-left"></i> ' . $Translation['Back'] . '</button>', $templateCode);
	}else{
		$templateCode = str_replace('<%%UPDATE_BUTTON%%>', '', $templateCode);
		$templateCode = str_replace('<%%DELETE_BUTTON%%>', '', $templateCode);
		$templateCode = str_replace('<%%DESELECT_BUTTON%%>', ($ShowCancel ? '<button type="submit" class="btn btn-default" id="deselect" name="deselect_x" value="1" onclick="' . $backAction . '" title="' . html_attr($Translation['Back']) . '"><i class="glyphicon glyphicon-chevron-left"></i> ' . $Translation['Back'] . '</button>' : ''), $templateCode);
	}

	// set records to read only if user can't insert new records and can't edit current record
	if(($selected_id && !$AllowUpdate) || (!$selected_id && !$AllowInsert)) {
		$jsReadOnly .= "\tjQuery('#date').prop('readonly', true);\n";
		$jsReadOnly .= "\tjQuery('#dateDay, #dateMonth, #dateYear').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#service').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#service_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#passager').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#passager_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#gds').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#gds_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#compagnie').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#compagnie_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#parcours').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#parcours_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#nature_transaction').prop('disabled', true).css({ color: '#555', backgroundColor: '#fff' });\n";
		$jsReadOnly .= "\tjQuery('#nature_transaction_caption').prop('disabled', true).css({ color: '#555', backgroundColor: 'white' });\n";
		$jsReadOnly .= "\tjQuery('#billet').replaceWith('<div class=\"form-control-static\" id=\"billet\">' + (jQuery('#billet').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#dossier').replaceWith('<div class=\"form-control-static\" id=\"dossier\">' + (jQuery('#dossier').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#prix_achat').replaceWith('<div class=\"form-control-static\" id=\"prix_achat\">' + (jQuery('#prix_achat').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#prix_vente').replaceWith('<div class=\"form-control-static\" id=\"prix_vente\">' + (jQuery('#prix_vente').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#qts').replaceWith('<div class=\"form-control-static\" id=\"qts\">' + (jQuery('#qts').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#commission').replaceWith('<div class=\"form-control-static\" id=\"commission\">' + (jQuery('#commission').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#frais_service').replaceWith('<div class=\"form-control-static\" id=\"frais_service\">' + (jQuery('#frais_service').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#frais_assurance').replaceWith('<div class=\"form-control-static\" id=\"frais_assurance\">' + (jQuery('#frais_assurance').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#mtva').replaceWith('<div class=\"form-control-static\" id=\"mtva\">' + (jQuery('#mtva').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#tva').replaceWith('<div class=\"form-control-static\" id=\"tva\">' + (jQuery('#tva').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#tht').replaceWith('<div class=\"form-control-static\" id=\"tht\">' + (jQuery('#tht').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#net_compagnie').replaceWith('<div class=\"form-control-static\" id=\"net_compagnie\">' + (jQuery('#net_compagnie').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#net_a_payer').replaceWith('<div class=\"form-control-static\" id=\"net_a_payer\">' + (jQuery('#net_a_payer').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#account').replaceWith('<div class=\"form-control-static\" id=\"account\">' + (jQuery('#account').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#reste_a_payer').replaceWith('<div class=\"form-control-static\" id=\"reste_a_payer\">' + (jQuery('#reste_a_payer').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('#observations').replaceWith('<div class=\"form-control-static\" id=\"observations\">' + (jQuery('#observations').val() || '') + '</div>');\n";
		$jsReadOnly .= "\tjQuery('.select2-container').hide();\n";

		$noUploads = true;
	}elseif(($AllowInsert && !$selected_id) || ($AllowUpdate && $selected_id)) {
		$jsEditable .= "\tjQuery('form').eq(0).data('already_changed', true);"; // temporarily disable form change handler
			$jsEditable .= "\tjQuery('form').eq(0).data('already_changed', false);"; // re-enable form change handler
	}

	// process combos
	$templateCode = str_replace('<%%COMBO(link_out)%%>', $combo_link_out->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(link_out)%%>', $combo_link_out->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(link_out)%%>', urlencode($combo_link_out->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(date)%%>', ($selected_id && !$arrPerm[3] ? '<div class="form-control-static">' . $combo_date->GetHTML(true) . '</div>' : $combo_date->GetHTML()), $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(date)%%>', $combo_date->GetHTML(true), $templateCode);
	$templateCode = str_replace('<%%COMBO(service)%%>', $combo_service->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(service)%%>', $combo_service->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(service)%%>', urlencode($combo_service->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(passager)%%>', $combo_passager->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(passager)%%>', $combo_passager->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(passager)%%>', urlencode($combo_passager->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(gds)%%>', $combo_gds->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(gds)%%>', $combo_gds->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(gds)%%>', urlencode($combo_gds->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(compagnie)%%>', $combo_compagnie->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(compagnie)%%>', $combo_compagnie->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(compagnie)%%>', urlencode($combo_compagnie->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(parcours)%%>', $combo_parcours->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(parcours)%%>', $combo_parcours->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(parcours)%%>', urlencode($combo_parcours->MatchText), $templateCode);
	$templateCode = str_replace('<%%COMBO(nature_transaction)%%>', $combo_nature_transaction->HTML, $templateCode);
	$templateCode = str_replace('<%%COMBOTEXT(nature_transaction)%%>', $combo_nature_transaction->MatchText, $templateCode);
	$templateCode = str_replace('<%%URLCOMBOTEXT(nature_transaction)%%>', urlencode($combo_nature_transaction->MatchText), $templateCode);

	/* lookup fields array: 'lookup field name' => array('parent table name', 'lookup field caption') */
	$lookup_fields = array('link_out' => array('gestion_transactions', 'Link fin'), 'service' => array('produits_services', 'Service'), 'passager' => array('clients', 'Client'), 'gds' => array('gds', 'GDS'), 'compagnie' => array('compagnies', 'Compagnie'), 'parcours' => array('items', 'Parcours'), 'nature_transaction' => array('produits_services', 'D&#233;signation'), );
	foreach($lookup_fields as $luf => $ptfc) {
		$pt_perm = getTablePermissions($ptfc[0]);

		// process foreign key links
		if($pt_perm['view'] || $pt_perm['edit']) {
			$templateCode = str_replace("<%%PLINK({$luf})%%>", '<button type="button" class="btn btn-default view_parent hspacer-md" id="' . $ptfc[0] . '_view_parent" title="' . html_attr($Translation['View'] . ' ' . $ptfc[1]) . '"><i class="glyphicon glyphicon-eye-open"></i></button>', $templateCode);
		}

		// if user has insert permission to parent table of a lookup field, put an add new button
		if($pt_perm['insert'] && !$_REQUEST['Embedded']) {
			$templateCode = str_replace("<%%ADDNEW({$ptfc[0]})%%>", '<button type="button" class="btn btn-success add_new_parent hspacer-md" id="' . $ptfc[0] . '_add_new" title="' . html_attr($Translation['Add New'] . ' ' . $ptfc[1]) . '"><i class="glyphicon glyphicon-plus-sign"></i></button>', $templateCode);
		}
	}

	// process images
	$templateCode = str_replace('<%%UPLOADFILE(idbilleterie)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(link_out)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(facture)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(date)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(service)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(passager)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(gds)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(compagnie)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(parcours)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(nature_transaction)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(billet)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(dossier)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(prix_achat)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(prix_vente)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(qts)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(commission)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(frais_service)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(frais_assurance)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(mtva)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(tva)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(tht)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(net_compagnie)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(net_a_payer)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(account)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(reste_a_payer)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(agent)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(groupe)%%>', '', $templateCode);
	$templateCode = str_replace('<%%UPLOADFILE(observations)%%>', '', $templateCode);

	// process values
	if($selected_id) {
		if( $dvprint) $templateCode = str_replace('<%%VALUE(idbilleterie)%%>', safe_html($urow['idbilleterie']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(idbilleterie)%%>', html_attr($row['idbilleterie']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(idbilleterie)%%>', urlencode($urow['idbilleterie']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(link_out)%%>', safe_html($urow['link_out']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(link_out)%%>', html_attr($row['link_out']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(link_out)%%>', urlencode($urow['link_out']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(facture)%%>', safe_html($urow['facture']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(facture)%%>', html_attr($row['facture']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(facture)%%>', urlencode($urow['facture']), $templateCode);
		$templateCode = str_replace('<%%VALUE(date)%%>', @date('d/m/Y', @strtotime(html_attr($row['date']))), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date)%%>', urlencode(@date('d/m/Y', @strtotime(html_attr($urow['date'])))), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(service)%%>', safe_html($urow['service']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(service)%%>', html_attr($row['service']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(service)%%>', urlencode($urow['service']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(passager)%%>', safe_html($urow['passager']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(passager)%%>', html_attr($row['passager']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(passager)%%>', urlencode($urow['passager']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(gds)%%>', safe_html($urow['gds']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(gds)%%>', html_attr($row['gds']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(gds)%%>', urlencode($urow['gds']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(compagnie)%%>', safe_html($urow['compagnie']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(compagnie)%%>', html_attr($row['compagnie']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(compagnie)%%>', urlencode($urow['compagnie']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(parcours)%%>', safe_html($urow['parcours']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(parcours)%%>', html_attr($row['parcours']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(parcours)%%>', urlencode($urow['parcours']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(nature_transaction)%%>', safe_html($urow['nature_transaction']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(nature_transaction)%%>', html_attr($row['nature_transaction']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nature_transaction)%%>', urlencode($urow['nature_transaction']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(billet)%%>', safe_html($urow['billet']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(billet)%%>', html_attr($row['billet']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(billet)%%>', urlencode($urow['billet']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(dossier)%%>', safe_html($urow['dossier']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(dossier)%%>', html_attr($row['dossier']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier)%%>', urlencode($urow['dossier']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(prix_achat)%%>', safe_html($urow['prix_achat']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(prix_achat)%%>', html_attr($row['prix_achat']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prix_achat)%%>', urlencode($urow['prix_achat']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(prix_vente)%%>', safe_html($urow['prix_vente']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(prix_vente)%%>', html_attr($row['prix_vente']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prix_vente)%%>', urlencode($urow['prix_vente']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(qts)%%>', safe_html($urow['qts']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(qts)%%>', html_attr($row['qts']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(qts)%%>', urlencode($urow['qts']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(commission)%%>', safe_html($urow['commission']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(commission)%%>', html_attr($row['commission']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(commission)%%>', urlencode($urow['commission']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(frais_service)%%>', safe_html($urow['frais_service']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(frais_service)%%>', html_attr($row['frais_service']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(frais_service)%%>', urlencode($urow['frais_service']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(frais_assurance)%%>', safe_html($urow['frais_assurance']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(frais_assurance)%%>', html_attr($row['frais_assurance']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(frais_assurance)%%>', urlencode($urow['frais_assurance']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(mtva)%%>', safe_html($urow['mtva']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(mtva)%%>', html_attr($row['mtva']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(mtva)%%>', urlencode($urow['mtva']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(tva)%%>', safe_html($urow['tva']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(tva)%%>', html_attr($row['tva']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(tva)%%>', urlencode($urow['tva']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(tht)%%>', safe_html($urow['tht']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(tht)%%>', html_attr($row['tht']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(tht)%%>', urlencode($urow['tht']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(net_compagnie)%%>', safe_html($urow['net_compagnie']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(net_compagnie)%%>', html_attr($row['net_compagnie']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(net_compagnie)%%>', urlencode($urow['net_compagnie']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(net_a_payer)%%>', safe_html($urow['net_a_payer']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(net_a_payer)%%>', html_attr($row['net_a_payer']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(net_a_payer)%%>', urlencode($urow['net_a_payer']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(account)%%>', safe_html($urow['account']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(account)%%>', html_attr($row['account']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(account)%%>', urlencode($urow['account']), $templateCode);
		if( $dvprint) $templateCode = str_replace('<%%VALUE(reste_a_payer)%%>', safe_html($urow['reste_a_payer']), $templateCode);
		if(!$dvprint) $templateCode = str_replace('<%%VALUE(reste_a_payer)%%>', html_attr($row['reste_a_payer']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(reste_a_payer)%%>', urlencode($urow['reste_a_payer']), $templateCode);
		$templateCode = str_replace('<%%VALUE(agent)%%>', safe_html($urow['agent']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(agent)%%>', urlencode($urow['agent']), $templateCode);
		$templateCode = str_replace('<%%VALUE(groupe)%%>', safe_html($urow['groupe']), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(groupe)%%>', urlencode($urow['groupe']), $templateCode);
		if($dvprint || (!$AllowUpdate && !$AllowInsert)) {
			$templateCode = str_replace('<%%VALUE(observations)%%>', safe_html($urow['observations']), $templateCode);
		}else{
			$templateCode = str_replace('<%%VALUE(observations)%%>', html_attr($row['observations']), $templateCode);
		}
		$templateCode = str_replace('<%%URLVALUE(observations)%%>', urlencode($urow['observations']), $templateCode);
	}else{
		$templateCode = str_replace('<%%VALUE(idbilleterie)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(idbilleterie)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(link_out)%%>', ( $_REQUEST['FilterField'][1]=='2' && $_REQUEST['FilterOperator'][1]=='<=>' ? $combo_link_out->SelectedData : ''), $templateCode);
		$templateCode = str_replace('<%%URLVALUE(link_out)%%>', urlencode( $_REQUEST['FilterField'][1]=='2' && $_REQUEST['FilterOperator'][1]=='<=>' ? $combo_link_out->SelectedData : ''), $templateCode);
		$templateCode = str_replace('<%%VALUE(facture)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(facture)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(date)%%>', '1', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(date)%%>', urlencode('1'), $templateCode);
		$templateCode = str_replace('<%%VALUE(service)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(service)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(passager)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(passager)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(gds)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(gds)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(compagnie)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(compagnie)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(parcours)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(parcours)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(nature_transaction)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(nature_transaction)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(billet)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(billet)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(dossier)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(dossier)%%>', urlencode(''), $templateCode);
		$templateCode = str_replace('<%%VALUE(prix_achat)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prix_achat)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(prix_vente)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(prix_vente)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(qts)%%>', '1', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(qts)%%>', urlencode('1'), $templateCode);
		$templateCode = str_replace('<%%VALUE(commission)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(commission)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(frais_service)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(frais_service)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(frais_assurance)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(frais_assurance)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(mtva)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(mtva)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(tva)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(tva)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(tht)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(tht)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(net_compagnie)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(net_compagnie)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(net_a_payer)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(net_a_payer)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(account)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(account)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(reste_a_payer)%%>', '0', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(reste_a_payer)%%>', urlencode('0'), $templateCode);
		$templateCode = str_replace('<%%VALUE(agent)%%>', '<%%creatorUsername%%>', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(agent)%%>', urlencode('<%%creatorUsername%%>'), $templateCode);
		$templateCode = str_replace('<%%VALUE(groupe)%%>', '<%%creatorGroup%%>', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(groupe)%%>', urlencode('<%%creatorGroup%%>'), $templateCode);
		$templateCode = str_replace('<%%VALUE(observations)%%>', '', $templateCode);
		$templateCode = str_replace('<%%URLVALUE(observations)%%>', urlencode(''), $templateCode);
	}

	// process translations
	foreach($Translation as $symbol=>$trans) {
		$templateCode = str_replace("<%%TRANSLATION($symbol)%%>", $trans, $templateCode);
	}

	// clear scrap
	$templateCode = str_replace('<%%', '<!-- ', $templateCode);
	$templateCode = str_replace('%%>', ' -->', $templateCode);

	// hide links to inaccessible tables
	if($_REQUEST['dvprint_x'] == '') {
		$templateCode .= "\n\n<script>\$j(function() {\n";
		$arrTables = getTableList();
		foreach($arrTables as $name => $caption) {
			$templateCode .= "\t\$j('#{$name}_link').removeClass('hidden');\n";
			$templateCode .= "\t\$j('#xs_{$name}_link').removeClass('hidden');\n";
		}

		$templateCode .= $jsReadOnly;
		$templateCode .= $jsEditable;

		if(!$selected_id) {
		}

		$templateCode.="\n});</script>\n";
	}

	// ajaxed auto-fill fields
	$templateCode .= '<script>';
	$templateCode .= '$j(function() {';


	$templateCode.="});";
	$templateCode.="</script>";
	$templateCode .= $lookups;

	// handle enforced parent values for read-only lookup fields

	// don't include blank images in lightbox gallery
	$templateCode = preg_replace('/blank.gif" data-lightbox=".*?"/', 'blank.gif"', $templateCode);

	// don't display empty email links
	$templateCode=preg_replace('/<a .*?href="mailto:".*?<\/a>/', '', $templateCode);

	/* default field values */
	$rdata = $jdata = get_defaults('billeteries');
	if($selected_id) {
		$jdata = get_joined_record('billeteries', $selected_id);
		if($jdata === false) $jdata = get_defaults('billeteries');
		$rdata = $row;
	}
	$templateCode .= loadView('billeteries-ajax-cache', array('rdata' => $rdata, 'jdata' => $jdata));

	// hook: billeteries_dv
	if(function_exists('billeteries_dv')) {
		$args=array();
		billeteries_dv(($selected_id ? $selected_id : FALSE), getMemberInfo(), $templateCode, $args);
	}

	return $templateCode;
}
?>