<?php
/*

	THIS MODULE IS TO BE USED ONLY WITH VIRTUAL AIRLINES USING ICREW. EDITING, REVERENGINEERING OF THIS MODULE
	IS PROHIBITED. IF FOUND OTHER WISE, CAN BE FINED UPTO $1000 USD.

	@contact hello@icrewsystems.com

	@author icrewsystems.com
	@build 02-06-2020
	@version 1.10
*/
class CharterData extends CodonData {

	public static function version() {
		return "Version: 1.01, Build: 02-06-2020";
	}
	public static function getCharterAirlineCode() {
		return self::getSettingValue('airline_code');
	}

	public static function getSettingValue($setting) {
		$sql = "SELECT * FROM `icrewsystems_charter_settings` WHERE `id` = 1";
		$result = DB::get_row($sql);

		foreach ($result as $result => $key) {
			if($result == $setting) {
				return $key;
			}
		}
	}


	public static function getAllSettings() {
		$sql = "SELECT * FROM `icrewsystems_charter_settings` WHERE `id` = 1";
		return DB::get_row($sql);
	}
	public static function getAllAirports() {
		$sql = "SELECT * FROM `icrewsystems_airports`";
		return DB::get_results($sql);
	}

	public static function getAirportData($icao) {
		$sql = "SELECT * FROM `icrewsystems_airports` WHERE `icao` = '$icao'";
		return DB::get_row($sql);
	}

	public static function addAircraftToCharter($acid) {
		$sql = "INSERT INTO `icrewsystems_charter_fleet` (`fleet_id`, `aircraft_id`) VALUES (NULL, '$acid')";
		return DB::get_row($sql);
	}

	public static function removeAircraftFromCharter($acid) {
		$sql = "DELETE FROM `icrewsystems_charter_fleet` WHERE `aircraft_id` = '$acid'";
		return DB::query($sql);
	}

	public static function getCharterFleet() {
		 $sql = "SELECT c.*, a.* FROM icrewsystems_charter_fleet c
        LEFT JOIN " . TABLE_PREFIX . "aircraft a ON a.id = c.aircraft_id
        ORDER BY a.icao ASC";
        return DB::get_results($sql);
	}

	public static function addSchedule($data) {

         if (!is_array($data)) return "Data is not an array";

		$route = SchedulesData::findSchedules(array('s.code' => $data['code'], 's.flightnum'=> $data['flightnum']));
        if(count($route)) {
        	return true;
        }
// m3rlin change price 
        $data['code'] = strtoupper($data['code']);
        $data['flightnum'] = strtoupper($data['flightnum']);
        $data['deptime'] = strtoupper($data['deptime']);
        $data['arrtime'] = strtoupper($data['arrtime']);
        $data['depicao'] = strtoupper($data['depicao']);
        $data['arricao'] = strtoupper($data['arricao']);
		$testodleg= OperationsData::getAirportDistance($data['depicao'], $data['arricao']);
		$mnoznik = CharterData::getSettingValue('price_per_mile');
	//	$mnoznik = 0.2;
				$data['price'] = $testodleg * $mnoznik;


		$deptime = explode(":", $data['deptime']);
		$data['deptime'] = $deptime[0].'.'.$deptime[1];

		$arrtime = explode(":", $data['arrtime']);
		$data['arrtime'] = $arrtime[0].'.'.$arrtime[1];


        if ($data['enabled'] == true) $data['enabled'] = 1;
        else  $data['enabled'] = 0;

        # If they didn't specify
        $data['flighttype'] = strtoupper($data['flighttype']);
        if ($data['flighttype'] == '') $data['flighttype'] = 'P';

        $data['flightlevel'] = str_replace(',', '', $data['flightlevel']);

        if(isset($data['week1'])) {

        }

        if (isset($fields['route'])) {
            $fields['route'] = str_replace('SID', '', $fields['route']);
            $fields['route'] = str_replace('STAR', '', $fields['route']);
            $fields['route'] = str_replace('DCT', '', $fields['route']);
            $fields['route'] = trim($fields['route']);
            $fields['route_details'] = '';
        }

        foreach ($data as $key => $value) {
            $data[$key] = DB::escape($value);
        }

        $data['flighttime'] = str_replace(':', '.', $data['flighttime']);


        # Do the insert based on the columns here
        $cols = array();
        $col_values = array();

        foreach ($data as $key => $value) {

            if($key == 'daysofweek' || $key == 'week1' || $key == 'week2' || $key == 'week3' || $key == 'week4') {
                $value = str_replace('7', '0', $value);
            } else {
                $value = DB::escape($value);
            }


            $cols[] = "`{$key}`";
            $col_values[] = "'{$value}'";
        }

        $cols = implode(', ', $cols);
        $col_values = implode(', ', $col_values);
        $sql = 'INSERT INTO ' . TABLE_PREFIX . "schedules ({$cols}) VALUES ({$col_values});";
        $res = DB::query($sql);

        if (DB::errno() != 0) return false;

        return true;
    }


}
?>
