<?php
	error_reporting(1);
	ini_set('display_errors', true );

	require_once ('../phpobjects/common/global.php');

	function makeLogEntry($data){
		$fm = \Keysoft\FileMaker\Client::getInstance();
        $response = $fm->create('Audit', $data);
        return;
    }

	function getMemberByField($field, $id, $selectData = NULL){
        $fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => $field . " eq ".$id,
            'count' => true,
			'select' => isset($selectData) ? $selectData : NULL,
        ]);
        $response = $fm->get('Owners', $odataQuery ,$allPages = true );
		return $response;
    }

	function getPetByField($field, $id, $selectData = NULL){
        $fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => $field . " eq ".$id,
            'count' => true,
			'select' => isset($selectData) ? $selectData : NULL,
        ]);
        $response = $fm->get('Pets', $odataQuery ,$allPages = true );
		return $response;
    }
	
	function getMemberByChip($id){
        $fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "\"Chip No\" eq '".$id."'",
			//'select' => ['email'],
            'count' => true
        ]);
        $response = $fm->get('Chips', $odataQuery ,$allPages = true );
        
		return $response;
    }	
	function getVetByImplanterNumber($id){
        $fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "\"implanter_number\" eq '".$id."' and vet_email eq '=*'",
			//'select' => ['email'],
            'count' => true
        ]);
        $response = $fm->get('Vets', $odataQuery ,$allPages = true );
        
		return $response;
    }
	function getVetByLicenceNumber($id){
		$fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "\"vet_license_number\" eq '".$id."' and vet_email eq '=*'",
			//'select' => ['email'],
            'count' => true
        ]);
        $response = $fm->get('Vets', $odataQuery ,$allPages = true );
        
		return $response;
    }

	function getWebContent($code){
		$fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "contentCode eq '".$code."'",
			'select' => ['content'],
            'count' => true
        ]);
        $response = $fm->get('webContent', $odataQuery ,$allPages = true );
        
        // Check for not found
        if (!$response['success'] || $response["data"]['count'] < 1)
        {
            // Redirect
            fRedirect('index.php?err_code=No'.$code.'WebContentRecord');
            fCleanup();
        }
		$content = $response['data']['value'][0]['content'];

		return $content;
    }

	function getVetByVetID($id){
		$fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "_pk_vet_id eq ".$id,
            'count' => true
        ]);
        $response = $fm->get('Vet', $odataQuery ,$allPages = true );
        
		
		$vet = $response['data']['value'][0];

		return $vet;
    }
	function getVetCompaniesByVetID($id){
		$fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "_sk_vet_id eq ".$id." and vet_main_contact eq 'Yes'",
            'count' => true
        ]);
        $response = $fm->get('Company_Vet_Relation', $odataQuery, $allPages = true );

		return $response;
    }
	function getTransactionFromCookie($cookeid) {
		$fm = \Keysoft\FileMaker\Client::getInstance();
		$odataQuery = $fm->buildODataQuery([
			'filter' => "cookie_transaction_id eq '".$cookieid."'",
			'select' => ['content'],
			'count' => true
		]);
		$response = $fm->get('Web_Payments', $odataQuery, $allPages = true );

		return $response; 
	}

	function getVetClinicsByVetID($id){
        $fm = \Keysoft\FileMaker\Client::getInstance();
        $odataQuery = $fm->buildODataQuery([
			'filter' => "_sk_vet_id eq ".$id." and dontDisplayOnWeb eq null",
            'count' => true
        ]);
        $response = $fm->get('Company_Vet_Relation', $odataQuery ,$allPages = true );
       
		return $response;
    }

	function getSuburbs($state) {
		$fm = \Keysoft\FileMaker\Client::getInstance();
		$filters = [
			"State eq '{$state}'",
		];
		$odataQuery = $fm->buildODataQuery([
			'filter' => implode(' and ', $filters),
			'count'  => true
		]);
		$suburbsResult = $fm->get('Suburbs', $odataQuery, $allPages = true);
		$suburbsData=$suburbsResult['data']['value'];
        $suburbs = array();
        foreach ($suburbsData as $suburbsLine) {
            $suburbs[]= $suburbsLine['Suburb'];  
        }
        asort($suburbs);
		return $suburbs;
	}


	function getPostCodes($state,$suburb) {
		$fm = \Keysoft\FileMaker\Client::getInstance();
		$filters = [
			"State eq '{$state}'",
			"Suburb eq '{$suburb}'"
		];
		$odataQuery = $fm->buildODataQuery([
			'filter' => implode(' and ', $filters),
			'count'  => true
		]);
		$postcodesResult = $fm->get('Suburbs', $odataQuery, $allPages = true);
        $postcodeData=$postcodesResult['data']['value'];
        $postcodes = array();
        foreach ($postcodeData as $postcodeLine){
            $postcodes[]= $postcodeLine['Postcode'];  
        }
        asort($postcodes);
		return $postcodes;
	}

	function popup($values, $name, $selected_value = '', $extras = '') {
		$popup = '<select name="' . $name . '" ' . $extras . '>';
		$popup.= '<option value="">- Please select -</option>';
		foreach($values as $value) {
			if (strtoupper($value) == strtoupper($selected_value)) {
				$selected = 'selected="selected"';
			} else {
				$selected = '';
			}
			$popup.= '<option value="' . $value . '" ' . $selected . '>' . $value . '</option>';
		}
		$popup.= '</select>';
		return $popup;
	}


	// ! CULLED
	/*
		function getMemberByID($id){
			$fm = \Keysoft\FileMaker\Client::getInstance();
			$odataQuery = $fm->buildODataQuery([
				'filter' => "_pk_member_id eq ".$id,
				'select' => ['email'],
				'count' => true
			]);
			$response = $fm->get('Owners', $odataQuery ,$allPages = true);
			
			return $response;
		}
		function getMemberByFullID($id){ // getMemberByField("cMemberID", (), ['email'])
			$fm = \Keysoft\FileMaker\Client::getInstance();
			$odataQuery = $fm->buildODataQuery([
				'filter' => "cMemberID eq '".$id."'",
				'select' => ['email'],
				'count' => true
			]);
			$response = $fm->get('Owners', $odataQuery ,$allPages = true );
			
			return $response;
		}

	*/
?>