<?php
function grade_per($grade, $user){
	if ($user['Role']['grade'] > $grade){
		return false;
	}else{
		return true;
	}
}

function expence_types(){
	return  array('Redemption' => 'Redemption', 'Sampling' =>'Sampling', 'D&D'=>'D&D');
}

function get_units($key = ''){
	$units = array('Case'=>'Case', 'Roll'=>'Roll', 'Pack'=>'Pack');
	if ($key){
		return $units[$key];
	}else{
		return $units;
	}
}

function getaddress($lat,$lng){
	$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
	$json = @file_get_contents($url);
	$data=json_decode($json);
	$status = $data->status;
	if($status=="OK")
		return $data->results[0]->formatted_address;
	else
		return false;
}

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

	$theta = $lon1 - $lon2;
	$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
	$dist = acos($dist);
	$dist = rad2deg($dist);
	$miles = $dist * 60 * 1.1515;
	$unit = strtoupper($unit);
	
	if ($unit == "K") {
		return number_format(($miles * 1.609344), 3);
	} else if ($unit == "N") {
		return number_format(($miles * 0.8684), 3);
	} else {
		return number_format($miles, 3);
	}
}

function incentive_type(){
	return array('Redemption' => 'Redemption', 'Sampling' =>'Sampling', 'D&D'=>'D&D','Customer on Target'=>'Customer on Target', 'Dist. Incentive'=>'Dist. Incentive');
}

function vStatus($id = null){
	$status =  array('1'=>'Verified', '0'=>'Unverified');
	if($id !== null){
		return $status[$id];
	}else{
		return $status;
	}
}

function timeStr($duration = 0){
	$secs = (int)$duration;
	$units = array(
                "week"   => 7*24*3600,
                "day"    =>   24*3600,
                "hour"   =>      3600,
                "minute" =>        60,
                "second" =>         1,
        );

	// specifically handle zero
	if ( $secs == 0 ) return "0 seconds";

	$s = "";

	foreach ( $units as $name => $divisor ) {
		if ( $quot = intval($secs / $divisor) ) {
			$s .= "$quot $name";
			$s .= (abs($quot) > 1 ? "s" : "") . ", ";
			$secs -= $quot * $divisor;
		}
	}

	return substr($s, 0, -2);
}

function getMonth($key = null){
	$months =  array("01"=>"January", "02"=>"February", "03"=>"March","04"=>"April","05"=>"May","06"=>"June","07"=>"July", "08"=>"August", "09"=>"September","10"=>"October","11"=>"November","12"=>"December");
	
	if($key && isset($months[$key])){
		return $months[$key];
	}else{
		return $months;
	}
}