<?php

if (!defined("ROOT_PATH"))

{

	header("HTTP/1.1 403 Forbidden");

	exit;

}

class pjFrontPublic extends pjFront

{

	public function __construct()

	{

		parent::__construct();

		

		$this->setAjax(true);

		

		$this->setLayout('pjActionEmpty');

	}

	public function pjActionSearch()

	{

		

		$this->setAjax(true);



		if ($this->isXHR() || $this->_get->check('_escaped_fragment_'))

		{	

			

			$maxValues = pjFleetModel::factory()

			->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

			->select("*")  // Select all columns from the row

			->where('passengers = (SELECT MAX(passengers) FROM ' . pjFleetModel::factory()->getTable() . ')')  // Add subquery to get the row with max passengers

			->limit(1)  // Limit to 1 result

			->findAll()

			->getData();



			$maxValuesluggage = pjFleetModel::factory()

			->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

			->select("*")  // Select all columns from the row

			->where('passengers = (SELECT MAX(luggage) FROM ' . pjFleetModel::factory()->getTable() . ')')  // Add subquery to get the row with max passengers

			->limit(1)  // Limit to 1 result

			->findAll()

			->getData();



			$maxValuesTrolleys = pjFleetModel::factory()

			->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

			->select("*")  // Select all columns from the row

			->where('luggage_trolleys = (SELECT MAX(luggage_trolleys) FROM ' . pjFleetModel::factory()->getTable() . ')')  // Add subquery to get the row with max passengers

			->limit(1)  // Limit to 1 result

			->findAll()

			->getData();



			$maxValuesKoffers = pjFleetModel::factory()

			->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

			->select("*")  // Select all columns from the row

			->where('luggage_koffers = (SELECT MAX(luggage_koffers) FROM ' . pjFleetModel::factory()->getTable() . ')')  // Add subquery to get the row with max passengers

			->limit(1)  // Limit to 1 result

			->findAll()

			->getData();





			// Retrieve the max values from the result

			$maxPassengers = !empty($maxValues) ? $maxValues[0]['passengers'] : 1;

			$maxLuggage = !empty($maxValuesluggage) ? $maxValuesluggage[0]['luggage'] : null;



			$maxTrolleys = !empty($maxValuesTrolleys) ? $maxValuesTrolleys[0]['luggage_trolleys'] : null;

			$maxKoffers = !empty($maxValuesKoffers) ? $maxValuesKoffers[0]['luggage_koffers'] : null;



			// Set the values for use in your view or further processing

			$this->set('maxPassengers', $maxPassengers);

			$this->set('maxLuggage', $maxLuggage);

			$this->set('maxTrolleys', $maxTrolleys);

			$this->set('maxKoffers', $maxKoffers);



		    if($this->_post->check('tbs_search'))

			{

			    $date_time = pjDateTime::formatDate($this->_post->toString('booking_date'), $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($this->_post->toString('booking_time')));

				$date_time_ts = strtotime($date_time);

				if(time() + $this->option_arr['o_hour_earlier'] * 3600 > $date_time_ts)

				{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 120));

				}

					

				if($this->_is('search'))

				{

					$this->_unset('search');

				}

				$this->_set("search", $this->_post->raw());

				

				pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200));

			}

		}

	}

		

	public function pjActionFleetsApr_06()

		{



			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['search']))

				{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

				

					$pjFleetModel = pjFleetModel::factory();

					$fleet_arr = $pjFleetModel

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->where('t1.status', 'T')

						->where('t1.passengers >=', $passengers)

						->where('t1.luggage >=', $luggage)

						->orderBy("price ASC")

						->findAll()->getData();

					

					$this->set('fleet_arr', $fleet_arr);

					

					

					$fleet_price_arr = 0;



					//print_r($fleet_price_arr); echo "out if";

					/** Srart Search By Pin Code */



					$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						if ($from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('fleet_price_arr', $fleet_price_arr);

						}





					//print_r($fleet_price_arr); echo "after if";

					/** End seach Pin Code */

					



					if(empty($fleet_price_arr)){

						$pjCityModel = pjCityModel::factory();



						$from_city_arr = $pjCityModel

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					$to_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					

					$extra_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					if ($from_city_arr && $extra_city_arr) {

							$extra_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $extra_city_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('extra_price_arr', $extra_price_arr);

						}

					

					if ($from_city_arr && $to_city_arr) {

						$fleet_price_arr = pjFleetPriceModel::factory()

							->where('t1.from_city', $from_city_arr['id'])

							->where('t1.to_city', $to_city_arr['id'])

							->findAll()->getDataPair('fleet_id', 'price');

						$this->set('fleet_price_arr', $fleet_price_arr);

					}

					} 

				

					if(isset($_SESSION[$this->defaultStore]['payment_method']))

					{

					    unset($_SESSION[$this->defaultStore]['payment_method']);

					}

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}				

			}

		}

	public function pjActionFleetsApr22()

		{

			

			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['search']))

				{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

			

					$data = array();



					$data['locale_id'] = !empty($this->_get->toInt('locale')) ? $this->_get->toInt('locale') : null;

				

					$data['from_city'] = !empty($SEARCH['from_city']) ? $SEARCH['from_city'] : null;

					$data['to_city'] = !empty($SEARCH['to_city']) ? $SEARCH['to_city'] : null;

					$data['extra_city'] = !empty($SEARCH['extra_city']) ? $SEARCH['extra_city'] : null;

					$data['from_city_postalcode'] = !empty($SEARCH['from_city_postalcode']) ? $SEARCH['from_city_postalcode'] : null;

					$data['extra_stop_postalcode'] = !empty($SEARCH['extra_stop_postalcode']) ? $SEARCH['extra_stop_postalcode'] : null;

					$data['to_city_postalcode'] = !empty($SEARCH['to_city_postalcode']) ? $SEARCH['to_city_postalcode'] : null;

					$data['extra_stop_distance'] = isset($SEARCH['extra_stop_distance']) ? $SEARCH['extra_stop_distance'] : null;

					$data['extra_stop_address'] = isset($SEARCH['extra_stop_address']) && is_array($SEARCH['extra_stop_address']) ? json_encode($SEARCH['extra_stop_address'], JSON_UNESCAPED_UNICODE) : null;



					$data['pickup_address'] = !empty($SEARCH['pickup_address']) ? $SEARCH['pickup_address'] : null;

					$data['return_address'] = !empty($SEARCH['return_address']) ? $SEARCH['return_address'] : null;

					

					$data['booking_date'] = !empty($SEARCH['booking_date']) ? pjDateTime::formatDate($SEARCH['booking_date'], $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($SEARCH['booking_time'])) : null;



					$data['return_date'] = !empty($SEARCH['return_date'])

					? pjDateTime::formatDate($SEARCH['return_date'], $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($SEARCH['return_time']))

					: null;

				

					$data['booking_time'] = !empty($SEARCH['booking_time']) ? $SEARCH['booking_time'] : null;

					$data['extra_stop'] = !empty($SEARCH['extra_stop_address']) && is_array($SEARCH['extra_stop_address']) && count(array_filter($SEARCH['extra_stop_address'])) ? 1 : 0;

					$data['return_status'] = isset($SEARCH['return_status']) ? $SEARCH['return_status'] : null;

					

					$data['return_time'] = !empty($SEARCH['return_time']) ? $SEARCH['return_time'] : null;

					$data['passengers'] = isset($SEARCH['passengers']) ? $SEARCH['passengers'] : null;

					$data['luggage'] = isset($SEARCH['luggage']) ? $SEARCH['luggage'] : null;

					$data['distance'] = isset($SEARCH['distance']) ? $SEARCH['distance'] : null;

					$data['created'] = date('Y-m-d H:i:s'); // Use current time, like :NOW()

					$data['ip'] = $_SERVER['REMOTE_ADDR'] ?? null;



					$pjFleetModel = pjFleetModel::factory();

					$fleet_arr = $pjFleetModel

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->where('t1.status', 'T')

						->where('t1.passengers >=', $passengers)

						->where('t1.luggage >=', $luggage)

						->orderBy("price ASC")

						->findAll()->getData();

					

					$this->set('fleet_arr', $fleet_arr);

								

					$fleet_price_arr = 0;



					

					/** Srart Search By Pin Code */



					$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						if ($from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('fleet_price_arr', $fleet_price_arr);

						}



					/** End seach Pin Code */

					



					if(empty($fleet_price_arr)){

						

						$pjCityModel = pjCityModel::factory();



						$from_city_arr = $pjCityModel

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					$to_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					

					$extra_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					if ($from_city_arr && $extra_city_arr) {

							$extra_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $extra_city_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('extra_price_arr', $extra_price_arr);

						}

					

					if ($from_city_arr && $to_city_arr) {

						$fleet_price_arr = pjFleetPriceModel::factory()

							->where('t1.from_city', $from_city_arr['id'])

							->where('t1.to_city', $to_city_arr['id'])

							->findAll()->getDataPair('fleet_id', 'price');

						$this->set('fleet_price_arr', $fleet_price_arr);

					}

					} 



					$allFleetData = [];



					// Add from $fleet_price_arr

					if (!empty($fleet_price_arr) && is_array($fleet_price_arr)) {

						foreach ($fleet_price_arr as $fleet_id => $price_id) {

							$allFleetData[] = [

								'fleet_id' => $fleet_id,

								'price_id' => $price_id

							];

						}

					}



					// 2. Then: Add only unmatched from $fleet_arr

					if (!empty($fleet_arr) && is_array($fleet_arr)) {

						foreach ($fleet_arr as $fleet) {

							$fleetId = $fleet['id'];

							// print_r($fleetId);

							if (!isset($fleet_price_arr[$fleetId])) {

								$allFleetData[] = [

									'fleet_id' => $fleetId,

									'price_id' => $fleet['start_fee']

								];

							}

						}

					}

						

					// Save all combined unique entries

					foreach ($allFleetData as $fleetData) {

						$multiData = $data; // Clone original base data

						$multiData['fleet_id'] = $fleetData['fleet_id'];

						$multiData['price_id'] = $fleetData['price_id'];

					

						// Create a unique hash to prevent duplicate saves

						$hashData = $multiData;

						unset($hashData['created'], $hashData['ip']); // Remove fields that vary

						$searchHash = md5(json_encode($hashData));

					

						// Use fleet_id as key in session to allow multiple distinct records

						if (!isset($_SESSION['last_search_hash'][$fleetData['fleet_id']]) || $_SESSION['last_search_hash'][$fleetData['fleet_id']] !== $searchHash) {

							$_SESSION['last_search_hash'][$fleetData['fleet_id']] = $searchHash;

							pjUserSearchHistoryModel::factory($multiData)->insert();

						}

					}

					

					if(isset($_SESSION[$this->defaultStore]['payment_method']))

					{

					    unset($_SESSION[$this->defaultStore]['payment_method']);

					}

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}				

			}

		}

	public function pjActionCheckoutApr22()

	{

		

		if($this->isXHR())

		{

			if (isset($_SESSION[$this->defaultStore]) &&

					count($_SESSION[$this->defaultStore]) > 0 &&

					isset($_SESSION[$this->defaultStore]['fleet_id']))

			{

				if($this->_post->check('lbs_checkout'))

				{

					$_SESSION[$this->defaultForm] = $this->_post->raw();

						

					pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200));

				}else{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

					

					$fleet_arr = pjFleetModel::factory()

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description,  (SELECT ($distance * `TP`.price) FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();

					$this->set('fleet_arr', $fleet_arr);

					

				

										

					$fixed_price = 0;

						/** Srart Search By Pin Code */



					$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

					$from_city_postal_code_arr = $pjCityPostalcodeModel

						->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

						->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

						->limit(1)->findAll()->getDataIndex(0);

						

					if ($fleet_arr && $from_city_postal_code_arr && $to_city_postal_code_arr) {

						$fleet_price_arr = pjFleetPostalCodePriceModel::factory()

							->where('t1.from_city', $from_city_postal_code_arr['id'])

							->where('t1.to_city', $to_city_postal_code_arr['id'])

							 ->where('t1.fleet_id', $fleet_arr['id'])

							->limit(1)->findAll()->getDataIndex(0);

							

							if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

								$fixed_price = $fleet_price_arr['price'];

								$fleet_arr['price'] = $fleet_price_arr['price'];

							}

						}

							/** End seach Pin Code */

									

						if($fixed_price === 0){

							$pjCityModel = pjCityModel::factory();

							$from_city_arr = $pjCityModel

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					   $to_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

						->limit(1)->findAll()->getDataIndex(0);

						

					$fleet_ext_arr = pjFleetModel::factory()

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->where('t1.status', 'T')

						->where('t1.passengers >=', $passengers)

						->where('t1.luggage >=', $luggage)

						->orderBy("price ASC")

						->findAll()->getData();



						$this->set('fleet_ext_arr', $fleet_ext_arr);



						$extra_city_arr = $pjCityModel->reset()

							->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

							->limit(1)->findAll()->getDataIndex(0);

							

							if ($fleet_arr && $from_city_arr && $extra_city_arr) {

							$extra_price_arr = pjFleetPriceModel::factory()

							->where('t1.from_city', $from_city_arr['id'])

							->where('t1.to_city', $extra_city_arr['id'])

							->where('t1.fleet_id', $fleet_arr['id'])

							->limit(1)->findAll()->getDataIndex(0);

								if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

									$fixed_price += $extra_price_arr['price'];

									$fleet_arr['price'] = $extra_price_arr['price'];

								}

							}

							if(empty($extra_price_arr['price'])){

							

								$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

							};

							

						

							if ($fleet_arr && $from_city_arr && $to_city_arr) {

								$fleet_price_arr = pjFleetPriceModel::factory()

									->where('t1.from_city', $from_city_arr['id'])

									->where('t1.to_city', $to_city_arr['id'])

									->where('t1.fleet_id', $fleet_arr['id'])

									->limit(1)->findAll()->getDataIndex(0);

								if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

									$fixed_price += $fleet_price_arr['price'];

									$fleet_arr['price'] = $fleet_price_arr['price'];

								}

							}

							

						}

						//print_r($extra_price_arr); die('h0eree');

						

					$this->set('country_arr', pjBaseCountryModel::factory()

					    ->select('t1.*, t2.content AS country_title')

					    ->join('pjMultiLang', "t2.model='pjBaseCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					    ->where('t1.status', 'T')

					    ->orderBy('`country_title` ASC')

					    ->findAll()

					    ->getData()

					    );

					

					$pjFleetExtraModel = pjFleetExtraModel::factory()

						->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjExtra', "t1.extra_id=t3.id", 'left')

						->select("t1.*, t2.content as name, t3.price, t3.per")

						->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

						->orderBy("name ASC");

					$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();

					$this->set('avail_extra_arr', $avail_extra_arr);

					



					$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();

					$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

					//print_r( $extra_price_arr);

					$this->set('price_arr', $price_arr);

					$this->set('passengers', $passengers);

					$this->set('extra_id_arr', $extra_id_arr);

					$this->set('fleet_price_arr', $fleet_price_arr);

					$this->set('extra_price_arr', $extra_price_arr);

					

					$bank_account = pjMultiLangModel::factory()

					->select('t1.content')

					->where('t1.model','pjOption')

					->where('t1.locale', $this->getLocaleId())

					->where('t1.field', 'o_bank_account')

					->limit(1)

					->findAll()->getDataIndex(0);

					$this->set('bank_account', $bank_account ? $bank_account['content'] : '');

					

					if(pjObject::getPlugin('pjPayments') !== NULL)

					{

					    $this->set('payment_option_arr', pjPaymentOptionModel::factory()->getOptions($this->getForeignId()));

					    $this->set('payment_titles', pjPayments::getPaymentTitles($this->getForeignId(), $this->getLocaleId()));

					}else{

					    $this->set('payment_titles', __('payment_methods', true));

					}

				}

			}else{

				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

			}

		}

	}

	

	public function pjActionGetPricesApr22()

	{

		

		if($this->isXHR())

		{

			if (isset($_SESSION[$this->defaultStore]) &&

					count($_SESSION[$this->defaultStore]) > 0 &&

					isset($_SESSION[$this->defaultStore]['fleet_id']))

			{

				$SEARCH = $this->_get('search');

				$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

				$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

				$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

					

				$pjFleetExtraModel = pjFleetExtraModel::factory()

					->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					->join('pjExtra', "t1.extra_id=t3.id", 'left')

					->select("t1.*, t2.content as name, t3.price, t3.per")

					->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

					->orderBy("name ASC");

				$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();

				$this->set('avail_extra_arr', $avail_extra_arr);

	

				$extra_id_arr = $this->_post->check('extra_id') && is_array($this->_post->toArray('extra_id')) ? array_keys($this->_post->toArray('extra_id')) : array();

				

			

				$fixed_price = 0;



				/** Srart Search By Pin Code */

				 $pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

							

						if ($from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_postal_code_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								 ->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

								->limit(1)->findAll()->getDataIndex(0);

								

								if ($fleet_postal_code_price_arr && (float)$fleet_postal_code_price_arr['price'] > 0) {

									$fixed_price = $fleet_postal_code_price_arr['price'];

									

								}

						}

						

				/** End seach Pin Code */



					if($fixed_price === 0){

							$pjCityModel = pjCityModel::factory();

							$from_city_arr = $pjCityModel

								->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

								->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

								->limit(1)->findAll()->getDataIndex(0);

								

							$to_city_arr = $pjCityModel->reset()

								->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

								->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

								->limit(1)->findAll()->getDataIndex(0);



							$fleet_ext_arr = pjFleetModel::factory()

								->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

								->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

								->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

								->where('t1.status', 'T')

								->where('t1.passengers >=', $passengers)

								->where('t1.luggage >=', $luggage)

								->orderBy("price ASC")

								->findAll()->getData();

			

							$extra_city_arr = $pjCityModel->reset()

								->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

								->limit(1)->findAll()->getDataIndex(0);

								

								if ($from_city_arr && $extra_city_arr) {

								$extra_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $extra_city_arr['id'])

								->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

								->limit(1)->findAll()->getDataIndex(0);

									if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

										$fixed_price += $extra_price_arr['price'];

										$fleet_arr['price'] = $extra_price_arr['price'];

									}

								}

								

							if(empty($extra_price_arr['price']))

								{ 

									$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

								};	

						if ($from_city_arr && $to_city_arr) {

							$fleet_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $to_city_arr['id'])

								->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

								->limit(1)->findAll()->getDataIndex(0);

							if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

								$fixed_price = $fleet_price_arr['price'];

							}

						}



					}

				



				$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

	

				$this->set('price_arr', $price_arr);

				$this->set('passengers', $passengers);

				$this->set('extra_id_arr', $extra_id_arr);

			}else{

				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

			}

		}

	}

	

	public function pjActionPreviewApr22()

	{

		

		if($this->isXHR())

		{

			if (isset($_SESSION[$this->defaultStore]) &&

					count($_SESSION[$this->defaultStore]) > 0 &&

					isset($_SESSION[$this->defaultStore]['fleet_id']))

			{

				$SEARCH = $this->_get('search');

				$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

				$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

				$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

				

				$fleet_arr = pjFleetModel::factory()

					->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					->select("t1.*, t2.content as fleet, (SELECT ($distance * `TP`.price) FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

					->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();

				$this->set('fleet_arr', $fleet_arr);

				

				$fixed_price = 0;



				

					/** Srart Search By Pin Code */



					$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

					$from_city_postal_code_arr = $pjCityPostalcodeModel

						->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

						->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

						->limit(1)->findAll()->getDataIndex(0);

						

					if ($fleet_arr && $from_city_postal_code_arr && $to_city_postal_code_arr) {

						$fleet_postal_code_price_arr = pjFleetPostalCodePriceModel::factory()

							->where('t1.from_city', $from_city_postal_code_arr['id'])

							->where('t1.to_city', $to_city_postal_code_arr['id'])

							 ->where('t1.fleet_id', $fleet_arr['id'])

							->limit(1)->findAll()->getDataIndex(0);

							

							if ($fleet_postal_code_price_arr && (float)$fleet_postal_code_price_arr['price'] > 0) {

								$fixed_price = $fleet_postal_code_price_arr['price'];

								$fleet_arr['price'] = $fleet_postal_code_price_arr['price'];

							}

					}

					/** End seach Pin Code */



				if($fixed_price === 0){



			

				$pjCityModel = pjCityModel::factory();

				$from_city_arr = $pjCityModel

					->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

					->limit(1)->findAll()->getDataIndex(0);

					

				$to_city_arr = $pjCityModel->reset()

					->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

					->limit(1)->findAll()->getDataIndex(0);



				$fleet_ext_arr = pjFleetModel::factory()

					->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

					->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

					->where('t1.status', 'T')

					->where('t1.passengers >=', $passengers)

					->where('t1.luggage >=', $luggage)

					->orderBy("price ASC")

					->findAll()->getData();



				$extra_city_arr = $pjCityModel->reset()

					->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

					->limit(1)->findAll()->getDataIndex(0);

					

					if ($fleet_arr && $from_city_arr && $extra_city_arr) {

					$extra_price_arr = pjFleetPriceModel::factory()

					->where('t1.from_city', $from_city_arr['id'])

					->where('t1.to_city', $extra_city_arr['id'])

					->where('t1.fleet_id', $fleet_arr['id'])

					->limit(1)->findAll()->getDataIndex(0);

						if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

							$fixed_price += $extra_price_arr['price'];

							$fleet_arr['price'] = $extra_price_arr['price'];

						}

					}

					

					if(empty($extra_price_arr['price']))

						{ 

							$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

						};

						

					if ($fleet_arr && $from_city_arr && $to_city_arr) {

						$fleet_price_arr = pjFleetPriceModel::factory()

							->where('t1.from_city', $from_city_arr['id'])

							->where('t1.to_city', $to_city_arr['id'])

							->where('t1.fleet_id', $fleet_arr['id'])

							->limit(1)->findAll()->getDataIndex(0);

							

						if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

							$fixed_price += $fleet_price_arr['price'];

							$fleet_arr['price'] = $fleet_price_arr['price'];

						}

					}

					

				$this->set('fleet_price_arr', $fleet_price_arr);

				$this->set('fleet_arr', $fleet_arr);

				$this->set('extra_price_arr', $extra_price_arr);

				

				}





				$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();



				$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

					

					

				$this->set('price_arr', $price_arr);

				$this->set('passengers', $passengers);

				

				$this->set('country_arr', pjBaseCountryModel::factory()

				    ->select('t1.*, t2.content AS country_title')

				    ->join('pjMultiLang', "t2.model='pjBaseCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

				    ->find($_SESSION[$this->defaultForm]['c_country'])

				    ->getData()

				    );

				

				$bank_account = pjMultiLangModel::factory()

				->select('t1.content')

				->where('t1.model','pjOption')

				->where('t1.locale', $this->getLocaleId())

				->where('t1.field', 'o_bank_account')

				->limit(1)

				->findAll()->getDataIndex(0);

				$this->set('bank_account', $bank_account ? $bank_account['content'] : '');

				

				if(pjObject::getPlugin('pjPayments') !== NULL)

				{

				    $this->set('payment_option_arr', pjPaymentOptionModel::factory()->getOptions($this->getForeignId()));

				    $this->set('payment_titles', pjPayments::getPaymentTitles($this->getForeignId(), $this->getLocaleId()));

				}else{

				    $this->set('payment_titles', __('payment_methods', true));

				}

			}else{

				pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

			}

		}

	}

	

	public function pjActionFleets()

		{

			

			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['search']))

				{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$trolleys = !empty($SEARCH['luggage_trolleys']) ? $SEARCH['luggage_trolleys'] : 0;

					$koffers = !empty($SEARCH['luggage_koffers']) ? $SEARCH['luggage_koffers'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

					$data = array();



					$data['locale_id'] = !empty($this->_get->toInt('locale')) ? $this->_get->toInt('locale') : null;

				

					$data['from_city'] = !empty($SEARCH['from_city']) ? $SEARCH['from_city'] : null;

					$data['to_city'] = !empty($SEARCH['to_city']) ? $SEARCH['to_city'] : null;

					$data['extra_city'] = !empty($SEARCH['extra_city']) ? $SEARCH['extra_city'] : null;

					$data['from_city_postalcode'] = !empty($SEARCH['from_city_postalcode']) ? $SEARCH['from_city_postalcode'] : null;

					$data['extra_stop_postalcode'] = !empty($SEARCH['extra_stop_postalcode']) ? $SEARCH['extra_stop_postalcode'] : null;

					$data['to_city_postalcode'] = !empty($SEARCH['to_city_postalcode']) ? $SEARCH['to_city_postalcode'] : null;

					$data['extra_stop_distance'] = isset($SEARCH['extra_stop_distance']) ? $SEARCH['extra_stop_distance'] : null;

					$data['extra_stop_address'] = isset($SEARCH['extra_stop_address']) && is_array($SEARCH['extra_stop_address']) ? json_encode($SEARCH['extra_stop_address'], JSON_UNESCAPED_UNICODE) : null;



					$data['pickup_address'] = !empty($SEARCH['pickup_address']) ? $SEARCH['pickup_address'] : null;

					$data['return_address'] = !empty($SEARCH['return_address']) ? $SEARCH['return_address'] : null;

					

					$data['booking_date'] = !empty($SEARCH['booking_date']) ? pjDateTime::formatDate($SEARCH['booking_date'], $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($SEARCH['booking_time'])) : null;



					$data['return_date'] = !empty($SEARCH['return_date'])

					? pjDateTime::formatDate($SEARCH['return_date'], $this->option_arr['o_date_format']) . ' ' . date("H:i:s", strtotime($SEARCH['return_time']))

					: null;

				

					$data['booking_time'] = !empty($SEARCH['booking_time']) ? $SEARCH['booking_time'] : null;

					$data['extra_stop'] = !empty($SEARCH['extra_stop_address']) && is_array($SEARCH['extra_stop_address']) && count(array_filter($SEARCH['extra_stop_address'])) ? 1 : 0;

					$data['return_status'] = isset($SEARCH['return_status']) ? $SEARCH['return_status'] : null;

					

					$data['return_time'] = !empty($SEARCH['return_time']) ? $SEARCH['return_time'] : null;

					$data['passengers'] = isset($SEARCH['passengers']) ? $SEARCH['passengers'] : null;

					$data['luggage'] = isset($SEARCH['luggage']) ? $SEARCH['luggage'] : null;

					$data['luggage_trolleys'] = isset($SEARCH['luggage_trolleys']) ? $SEARCH['luggage_trolleys'] : null;

					$data['luggage_koffers'] = isset($SEARCH['luggage_koffers']) ? $SEARCH['luggage_koffers'] : null;

					$data['distance'] = isset($SEARCH['distance']) ? $SEARCH['distance'] : null;

					$data['created'] = date('Y-m-d H:i:s'); // Use current time, like :NOW()

					$data['ip'] = $_SERVER['REMOTE_ADDR'] ?? null;



					$pjFleetModel = pjFleetModel::factory();

					$fleet_arr = $pjFleetModel

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->where('t1.status', 'T')

						->where('t1.passengers >=', $passengers)

						->where('t1.luggage >=', $luggage)

						->where('t1.luggage_trolleys >=', $trolleys)

						->where('t1.luggage_koffers >=', $koffers)

						->orderBy("price ASC")

						->findAll()->getData();

					

					$this->set('fleet_arr', $fleet_arr);

								

					$fleet_price_arr = 0;



					

					/** Srart Search By Pin Code */



					$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						if ($from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('fleet_price_arr', $fleet_price_arr);

						}



					/** End seach Pin Code */

					



					if(empty($fleet_price_arr)){

						

						$pjCityModel = pjCityModel::factory();



						$from_city_arr = $pjCityModel

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					$to_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					

					$extra_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

						->limit(1)->findAll()->getDataIndex(0);

					

					if ($from_city_arr && $extra_city_arr) {

							$extra_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $extra_city_arr['id'])

								->findAll()->getDataPair('fleet_id', 'price');

							$this->set('extra_price_arr', $extra_price_arr);

						}

					

					if ($from_city_arr && $to_city_arr) {

						$fleet_price_arr = pjFleetPriceModel::factory()

							->where('t1.from_city', $from_city_arr['id'])

							->where('t1.to_city', $to_city_arr['id'])

							->findAll()->getDataPair('fleet_id', 'price');

						$this->set('fleet_price_arr', $fleet_price_arr);

					}

					} 



					$allFleetData = [];



					// Add from $fleet_price_arr

					if (!empty($fleet_price_arr) && is_array($fleet_price_arr)) {

						foreach ($fleet_price_arr as $fleet_id => $price_id) {

							$allFleetData[] = [

								'fleet_id' => $fleet_id,

								'price_id' => $price_id

							];

						}

					}



					// 2. Then: Add only unmatched from $fleet_arr

					if (!empty($fleet_arr) && is_array($fleet_arr)) {

						foreach ($fleet_arr as $fleet) {

							$fleetId = $fleet['id'];

							// print_r($fleetId);

							if (!isset($fleet_price_arr[$fleetId])) {

								$allFleetData[] = [

									'fleet_id' => $fleetId,

									'price_id' => $fleet['start_fee']

								];

							}

						}

					}

						

					// Save all combined unique entries

					foreach ($allFleetData as $fleetData) {

						$multiData = $data; // Clone original base data

						$multiData['fleet_id'] = $fleetData['fleet_id'];

						$multiData['price_id'] = $fleetData['price_id'];

					

						// Create a unique hash to prevent duplicate saves

						$hashData = $multiData;

						unset($hashData['created'], $hashData['ip']); // Remove fields that vary

						$searchHash = md5(json_encode($hashData));

					

						// Use fleet_id as key in session to allow multiple distinct records

						if (!isset($_SESSION['last_search_hash'][$fleetData['fleet_id']]) || $_SESSION['last_search_hash'][$fleetData['fleet_id']] !== $searchHash) {

							$_SESSION['last_search_hash'][$fleetData['fleet_id']] = $searchHash;

							pjUserSearchHistoryModel::factory($multiData)->insert();

						}

					}

					

					



					// if (!empty($fleet_price_arr) && is_array($fleet_price_arr)) {

					// 	// Get the first key-value pair from the array

					// 	$fleet_id = array_key_first($fleet_price_arr);

					// 	$price_id = $fleet_price_arr[$fleet_id];

					

					// 	$data['fleet_id'] = $fleet_id;

					// 	$data['price_id'] = $price_id;

					// } else {

					// 	$data['fleet_id'] = 0;

					// 	$data['price_id'] = !empty($fleet_arr[0]['start_fee']) ? $fleet_arr[0]['start_fee'] : 0;

					// }

					

					// $hashData = $data;

					// unset($hashData['created'], $hashData['ip']);



					// $searchHash = md5(json_encode($hashData));



					// if (!isset($_SESSION['last_search_hash']) || $_SESSION['last_search_hash'] !== $searchHash) {

					// 	$_SESSION['last_search_hash'] = $searchHash;

					// 	$id = pjUserSearchHistoryModel::factory($data)->insert()->getInsertId();

					// }

					

					if(isset($_SESSION[$this->defaultStore]['payment_method']))

					{

					    unset($_SESSION[$this->defaultStore]['payment_method']);

					}

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}				

			}

		}



	public function pjActionCheckout()

		{

			

			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['fleet_id']))

				{

					if($this->_post->check('lbs_checkout'))

					{

						$_SESSION[$this->defaultForm] = $this->_post->raw();

							

						pjAppController::jsonResponse(array('status' => 'OK', 'code' => 200));

					}else{

						$SEARCH = $this->_get('search');

						$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

						$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

						$trolleys = !empty($SEARCH['luggage_trolleys']) ? $SEARCH['luggage_trolleys'] : 0;

						$koffers = !empty($SEARCH['luggage_koffers']) ? $SEARCH['luggage_koffers'] : 0;

						$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

						

						$fleet_arr = pjFleetModel::factory()

							->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

							->select("t1.*, t2.content as fleet, t3.content as description,  (SELECT ($distance * `TP`.price) FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

							->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();

						$this->set('fleet_arr', $fleet_arr);

						

					

											

						$fixed_price = 0;

							/** Srart Search By Pin Code */



						$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

							

						if ($fleet_arr && $from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								 ->where('t1.fleet_id', $fleet_arr['id'])

								->limit(1)->findAll()->getDataIndex(0);

								

								if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

									$fixed_price = $fleet_price_arr['price'];

									$fleet_arr['price'] = $fleet_price_arr['price'];

								}

							}

								/** End seach Pin Code */

										

							if($fixed_price === 0){

								$pjCityModel = pjCityModel::factory();

								$from_city_arr = $pjCityModel

							->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						   $to_city_arr = $pjCityModel->reset()

							->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

							->limit(1)->findAll()->getDataIndex(0);

							

						$fleet_ext_arr = pjFleetModel::factory()

							->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

							->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

							->where('t1.status', 'T')

							->where('t1.passengers >=', $passengers)

							->where('t1.luggage >=', $luggage)

							->where('t1.luggage_trolleys >=', $trolleys)

							->where('t1.luggage_koffers >=', $koffers)

							->orderBy("price ASC")

							->findAll()->getData();



							$this->set('fleet_ext_arr', $fleet_ext_arr);



							$extra_city_arr = $pjCityModel->reset()

								->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

								->limit(1)->findAll()->getDataIndex(0);

								

								if ($fleet_arr && $from_city_arr && $extra_city_arr) {

								$extra_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $extra_city_arr['id'])

								->where('t1.fleet_id', $fleet_arr['id'])

								->limit(1)->findAll()->getDataIndex(0);

									if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

										$fixed_price += $extra_price_arr['price'];

										$fleet_arr['price'] = $extra_price_arr['price'];

									}

								}

								if(empty($extra_price_arr['price'])){

								

									$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

								};

								

							

								if ($fleet_arr && $from_city_arr && $to_city_arr) {

									$fleet_price_arr = pjFleetPriceModel::factory()

										->where('t1.from_city', $from_city_arr['id'])

										->where('t1.to_city', $to_city_arr['id'])

										->where('t1.fleet_id', $fleet_arr['id'])

										->limit(1)->findAll()->getDataIndex(0);

									if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

										$fixed_price += $fleet_price_arr['price'];

										$fleet_arr['price'] = $fleet_price_arr['price'];

									}

								}

								

							}

							//print_r($extra_price_arr); die('h0eree');

							

						$this->set('country_arr', pjBaseCountryModel::factory()

						    ->select('t1.*, t2.content AS country_title')

						    ->join('pjMultiLang', "t2.model='pjBaseCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						    ->where('t1.status', 'T')

						    ->orderBy('`country_title` ASC')

						    ->findAll()

						    ->getData()

						    );

						

						$pjFleetExtraModel = pjFleetExtraModel::factory()

							->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->join('pjExtra', "t1.extra_id=t3.id", 'left')

							->select("t1.*, t2.content as name, t3.price, t3.per")

							->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

							->orderBy("name ASC");

						$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();

						$this->set('avail_extra_arr', $avail_extra_arr);

						



						$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();

						$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

						//print_r( $extra_price_arr);

						$this->set('price_arr', $price_arr);

						$this->set('passengers', $passengers);

						$this->set('extra_id_arr', $extra_id_arr);

						$this->set('fleet_price_arr', $fleet_price_arr);

						$this->set('extra_price_arr', $extra_price_arr);

						

						$bank_account = pjMultiLangModel::factory()

						->select('t1.content')

						->where('t1.model','pjOption')

						->where('t1.locale', $this->getLocaleId())

						->where('t1.field', 'o_bank_account')

						->limit(1)

						->findAll()->getDataIndex(0);

						$this->set('bank_account', $bank_account ? $bank_account['content'] : '');

						

						if(pjObject::getPlugin('pjPayments') !== NULL)

						{

						    $this->set('payment_option_arr', pjPaymentOptionModel::factory()->getOptions($this->getForeignId()));

						    $this->set('payment_titles', pjPayments::getPaymentTitles($this->getForeignId(), $this->getLocaleId()));

						}else{

						    $this->set('payment_titles', __('payment_methods', true));

						}

					}

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}

			}

		}

	

	public function pjActionGetPrices()

		{

			

			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['fleet_id']))

				{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$trolleys = !empty($SEARCH['luggage_trolleys']) ? $SEARCH['luggage_trolleys'] : 0;

					$koffers = !empty($SEARCH['luggage_koffers']) ? $SEARCH['luggage_koffers'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

						

					$pjFleetExtraModel = pjFleetExtraModel::factory()

						->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.extra_id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjExtra', "t1.extra_id=t3.id", 'left')

						->select("t1.*, t2.content as name, t3.price, t3.per")

						->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

						->orderBy("name ASC");

					$avail_extra_arr = $pjFleetExtraModel->findAll()->getData();

					$this->set('avail_extra_arr', $avail_extra_arr);

		

					$extra_id_arr = $this->_post->check('extra_id') && is_array($this->_post->toArray('extra_id')) ? array_keys($this->_post->toArray('extra_id')) : array();

					

				

					$fixed_price = 0;



					/** Srart Search By Pin Code */

					 $pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

							$from_city_postal_code_arr = $pjCityPostalcodeModel

								->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

								->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

								->limit(1)->findAll()->getDataIndex(0);

							

							$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

								->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

								->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

								->limit(1)->findAll()->getDataIndex(0);

								

							if ($from_city_postal_code_arr && $to_city_postal_code_arr) {

								$fleet_postal_code_price_arr = pjFleetPostalCodePriceModel::factory()

									->where('t1.from_city', $from_city_postal_code_arr['id'])

									->where('t1.to_city', $to_city_postal_code_arr['id'])

									 ->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

									->limit(1)->findAll()->getDataIndex(0);

									

									if ($fleet_postal_code_price_arr && (float)$fleet_postal_code_price_arr['price'] > 0) {

										$fixed_price = $fleet_postal_code_price_arr['price'];

										

									}

							}

							

					/** End seach Pin Code */



						if($fixed_price === 0){

								$pjCityModel = pjCityModel::factory();

								$from_city_arr = $pjCityModel

									->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

									->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

									->limit(1)->findAll()->getDataIndex(0);

									

								$to_city_arr = $pjCityModel->reset()

									->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

									->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

									->limit(1)->findAll()->getDataIndex(0);



								$fleet_ext_arr = pjFleetModel::factory()

									->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

									->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

									->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

									->where('t1.status', 'T')

									->where('t1.passengers >=', $passengers)

									->where('t1.luggage >=', $luggage)

									->where('t1.luggage_trolleys >=', $trolleys)

									->where('t1.luggage_koffers >=', $koffers)

									->orderBy("price ASC")

									->findAll()->getData();

				

								$extra_city_arr = $pjCityModel->reset()

									->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

									->limit(1)->findAll()->getDataIndex(0);

									

									if ($from_city_arr && $extra_city_arr) {

									$extra_price_arr = pjFleetPriceModel::factory()

									->where('t1.from_city', $from_city_arr['id'])

									->where('t1.to_city', $extra_city_arr['id'])

									->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

									->limit(1)->findAll()->getDataIndex(0);

										if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

											$fixed_price += $extra_price_arr['price'];

											$fleet_arr['price'] = $extra_price_arr['price'];

										}

									}

									

								if(empty($extra_price_arr['price']))

									{ 

										$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

									};	

							if ($from_city_arr && $to_city_arr) {

								$fleet_price_arr = pjFleetPriceModel::factory()

									->where('t1.from_city', $from_city_arr['id'])

									->where('t1.to_city', $to_city_arr['id'])

									->where('t1.fleet_id', $_SESSION[$this->defaultStore]['fleet_id'])

									->limit(1)->findAll()->getDataIndex(0);

								if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

									$fixed_price = $fleet_price_arr['price'];

								}

							}



						}

					



					$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

		

					$this->set('price_arr', $price_arr);

					$this->set('passengers', $passengers);

					$this->set('extra_id_arr', $extra_id_arr);

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}

			}

		}

	

	public function pjActionPreview()

		{

			

			if($this->isXHR())

			{

				if (isset($_SESSION[$this->defaultStore]) &&

						count($_SESSION[$this->defaultStore]) > 0 &&

						isset($_SESSION[$this->defaultStore]['fleet_id']))

				{

					$SEARCH = $this->_get('search');

					$passengers = !empty($SEARCH['passengers']) ? $SEARCH['passengers'] : 0;

					$luggage = !empty($SEARCH['luggage']) ? $SEARCH['luggage'] : 0;

					$trolleys = !empty($SEARCH['luggage_trolleys']) ? $SEARCH['luggage_trolleys'] : 0;

					$koffers = !empty($SEARCH['luggage_koffers']) ? $SEARCH['luggage_koffers'] : 0;

					$distance = !empty($SEARCH['distance']) ? $SEARCH['distance'] : 0;

					

					$fleet_arr = pjFleetModel::factory()

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, (SELECT ($distance * `TP`.price) FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->find($_SESSION[$this->defaultStore]['fleet_id'])->getData();

					$this->set('fleet_arr', $fleet_arr);

					

					$fixed_price = 0;



						/** Srart Search By Pin Code */



						$pjCityPostalcodeModel = pjCityPostalcodeModel::factory();

						$from_city_postal_code_arr = $pjCityPostalcodeModel

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

						

						$to_city_postal_code_arr = $pjCityPostalcodeModel->reset()

							->join('pjMultiLang', "t2.model='pjCityPostalcode' AND t2.foreign_id=t1.id AND t2.field='postalcode' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

							->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city_postalcode'])))

							->limit(1)->findAll()->getDataIndex(0);

							

						if ($fleet_arr && $from_city_postal_code_arr && $to_city_postal_code_arr) {

							$fleet_postal_code_price_arr = pjFleetPostalCodePriceModel::factory()

								->where('t1.from_city', $from_city_postal_code_arr['id'])

								->where('t1.to_city', $to_city_postal_code_arr['id'])

								 ->where('t1.fleet_id', $fleet_arr['id'])

								->limit(1)->findAll()->getDataIndex(0);

								

								if ($fleet_postal_code_price_arr && (float)$fleet_postal_code_price_arr['price'] > 0) {

									$fixed_price = $fleet_postal_code_price_arr['price'];

									$fleet_arr['price'] = $fleet_postal_code_price_arr['price'];

								}

						}

						/** End seach Pin Code */



					if($fixed_price === 0){



				

					$pjCityModel = pjCityModel::factory();

					$from_city_arr = $pjCityModel

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['from_city'])))

						->limit(1)->findAll()->getDataIndex(0);

						

					$to_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['to_city'])))

						->limit(1)->findAll()->getDataIndex(0);



					$fleet_ext_arr = pjFleetModel::factory()

						->join('pjMultiLang', "t2.model='pjFleet' AND t2.foreign_id=t1.id AND t2.field='fleet' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

						->join('pjMultiLang', "t3.model='pjFleet' AND t3.foreign_id=t1.id AND t3.field='description' AND t3.locale='".$this->getLocaleId()."'", 'left outer')

						->select("t1.*, t2.content as fleet, t3.content as description, (SELECT `TP`.price FROM `".pjPriceModel::factory()->getTable()."` AS `TP` WHERE `TP`.fleet_id=t1.id AND (`TP`.`start` <= $distance AND $distance <= `TP`.`end`) LIMIT 1 ) AS price")

						->where('t1.status', 'T')

						->where('t1.passengers >=', $passengers)

						->where('t1.luggage >=', $luggage)

						->where('t1.luggage_trolleys >=', $trolleys)

						->where('t1.luggage_koffers >=', $koffers)

						->orderBy("price ASC")

						->findAll()->getData();



					$extra_city_arr = $pjCityModel->reset()

						->join('pjMultiLang', "t2.model='pjCity' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')->where('LOWER(TRIM(t2.content))', strtolower(trim(@$SEARCH['extra_city'])))

						->limit(1)->findAll()->getDataIndex(0);

						

						if ($fleet_arr && $from_city_arr && $extra_city_arr) {

						$extra_price_arr = pjFleetPriceModel::factory()

						->where('t1.from_city', $from_city_arr['id'])

						->where('t1.to_city', $extra_city_arr['id'])

						->where('t1.fleet_id', $fleet_arr['id'])

						->limit(1)->findAll()->getDataIndex(0);

							if ($extra_price_arr && (float)$extra_price_arr['price'] > 0) {

								$fixed_price += $extra_price_arr['price'];

								$fleet_arr['price'] = $extra_price_arr['price'];

							}

						}

						

						if(empty($extra_price_arr['price']))

							{ 

								$fixed_price += $SEARCH['extra_stop_distance'] * $fleet_ext_arr[0]['price'];

							};

							

						if ($fleet_arr && $from_city_arr && $to_city_arr) {

							$fleet_price_arr = pjFleetPriceModel::factory()

								->where('t1.from_city', $from_city_arr['id'])

								->where('t1.to_city', $to_city_arr['id'])

								->where('t1.fleet_id', $fleet_arr['id'])

								->limit(1)->findAll()->getDataIndex(0);

								

							if ($fleet_price_arr && (float)$fleet_price_arr['price'] > 0) {

								$fixed_price += $fleet_price_arr['price'];

								$fleet_arr['price'] = $fleet_price_arr['price'];

							}

						}

						

					$this->set('fleet_price_arr', $fleet_price_arr);

					$this->set('fleet_arr', $fleet_arr);

					$this->set('extra_price_arr', $extra_price_arr);

					

					}





					$extra_id_arr = isset($_SESSION[$this->defaultForm]['extra_id']) && is_array($_SESSION[$this->defaultForm]['extra_id']) ? array_keys($_SESSION[$this->defaultForm]['extra_id']) : array();



					$price_arr = pjAppController::calPrice($_SESSION[$this->defaultStore]['fleet_id'], $distance, $passengers, $extra_id_arr, $this->option_arr, $fixed_price);

						

						

					$this->set('price_arr', $price_arr);

					$this->set('passengers', $passengers);

					

					$this->set('country_arr', pjBaseCountryModel::factory()

					    ->select('t1.*, t2.content AS country_title')

					    ->join('pjMultiLang', "t2.model='pjBaseCountry' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

					    ->find($_SESSION[$this->defaultForm]['c_country'])

					    ->getData()

					    );

					

					$bank_account = pjMultiLangModel::factory()

					->select('t1.content')

					->where('t1.model','pjOption')

					->where('t1.locale', $this->getLocaleId())

					->where('t1.field', 'o_bank_account')

					->limit(1)

					->findAll()->getDataIndex(0);

					$this->set('bank_account', $bank_account ? $bank_account['content'] : '');

					

					if(pjObject::getPlugin('pjPayments') !== NULL)

					{

					    $this->set('payment_option_arr', pjPaymentOptionModel::factory()->getOptions($this->getForeignId()));

					    $this->set('payment_titles', pjPayments::getPaymentTitles($this->getForeignId(), $this->getLocaleId()));

					}else{

					    $this->set('payment_titles', __('payment_methods', true));

					}

				}else{

					pjAppController::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => ''));

				}

			}

		}



	public function pjActionGetPaymentForm()

	{

	    if ($this->isXHR())

	    {

	        $arr = pjBookingModel::factory()->find($this->_get->toInt('booking_id'))->getData();

	        if(pjObject::getPlugin('pjPayments') !== NULL)

	        {

	            $pjPlugin = pjPayments::getPluginName($arr['payment_method']);

	            if(pjObject::getPlugin($pjPlugin) !== NULL)

	            {

	                $client = pjClientModel::factory()

	                ->select("t1.*, t2.email as c_email, t2.name as c_name, t2.phone as c_phone")

	                ->join('pjAuthUser', "t1.foreign_id=t2.id", 'left outer')

	                ->find($arr['client_id'])->getData();

	                

	                $this->set('params', $pjPlugin::getFormParams(array('payment_method' => $arr['payment_method']), array(

	                    'locale_id'	 => $this->getLocaleId(),

	                    'return_url'	=> $this->option_arr['o_thankyou_page'],

	                    'id'			=> $arr['id'],

	                    'foreign_id'	=> $this->getForeignId(),

	                    'uuid'		  => $arr['uuid'],

	                    'name'		  => @$client['c_name'],

	                    'email'		 => @$client['c_email'],

	                    'phone'		 => @$client['c_phone'],

	                    'amount'		=> $arr['deposit'],

	                    'cancel_hash'   => sha1($arr['uuid'].strtotime($arr['created']).PJ_SALT),

	                    'currency_code' => $this->option_arr['o_currency'],

	                )));

	            }

	            

	            if ($arr['payment_method'] == 'bank')

	            {

	                $bank_account = pjMultiLangModel::factory()

	                ->select('t1.content')

	                ->where('t1.model','pjOption')

	                ->where('t1.locale', $this->getLocaleId())

	                ->where('t1.field', 'o_bank_account')

	                ->limit(1)

	                ->findAll()->getDataIndex(0);

	                $this->set('bank_account', $bank_account ? $bank_account['content'] : '');

	            }

	        }

	        

	        $this->set('arr', $arr);

	        $this->set('get', $this->_get->raw());

	    }

	}

}

?>