<?php
/*
* date last modified 03-10-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - manage_staff.html.php
* Mysql Tables - tbl_admin, tbl_purchase_partner, tbl_admin_type
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
require_once('./includes/functions.php'); # general functions

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$pageVars = '';

//we have to find shops near to user who have product categories that in the order
if(isset($_REQUEST['order_id']) && $_REQUEST['order_id']!='') {
	$orderId = trim($_REQUEST['order_id']);
	$where = $shopIdString = '';
	$prodCatArray = $partnerArray = array();
	//we have to select shops near to this user
	$orderRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id, technician_id, job_id, partner_id FROM tbl_purchase_order WHERE id = '".$orderId."'"));
	$userId = $orderRec['user_id'];
	$techId = $orderRec['technician_id'];
	$taskId = $orderRec['job_id'];
	$shopId = $orderRec['partner_id'];
	//get user details
	$userDetailRec = mysqli_fetch_array(mysqli_query($con, "SELECT phone FROM tbl_user WHERE id = '".$userId."'"));
	//get tech details
	$techDetailRec = mysqli_fetch_array(mysqli_query($con, "SELECT phone FROM tbl_technician WHERE id = '".$techId."'"));
	//get order categories
	$productsRs = mysqli_query($con, "SELECT product_id FROM tbl_purchase_order_products WHERE order_id = '".$orderId."'");
	if(mysqli_num_rows($productsRs)>0){
		while($productsRec = mysqli_fetch_array($productsRs)){
			$prodId = $productsRec['product_id'];
			$res = mysqli_fetch_array(mysqli_query($con, 'SELECT cat_id FROM tbl_products WHERE id ='.$prodId));
			$catId = $res['cat_id'];
			//get parent id of this category; as we have only parent ids assigned to shops
			$res = mysqli_fetch_array(mysqli_query($con, 'SELECT id, parent_id FROM tbl_product_categories WHERE id ='.$catId));			
			$parentCatId = $res['parent_id'];
			if($parentCatId==0){
				$parentCatId = $res['id'];
			}
			$prodCatArray[] = $parentCatId;			
		}
		$prodCatArray = array_unique($prodCatArray);		
	}	
	//print_r($prodCatArray);
	//find shops with this cat ids
	foreach ($prodCatArray as $parentCatId) {
		$shopRs = mysqli_query($con, "SELECT partner_id FROM `tbl_purchase_partner_categories` WHERE FIND_IN_SET('".$parentCatId."',`categories`)");
		if(mysqli_num_rows($shopRs)>0){
			while($shopRec = mysqli_fetch_array($shopRs)){
				$partnerArray[] = $shopRec['partner_id'];
			}
		}
	}
	$partnerArray = array_unique($partnerArray);
	//print_r($partnerArray);
	if(count($partnerArray)>0){
		$userLat = $userLong = '';
		$shopIdString = implode(',', $partnerArray);
		//get lat long of task location
		$locationRec = mysqli_fetch_array(mysqli_query($con, "SELECT address_id, latitude, longitude FROM tbl_job WHERE id = '".$taskId."'"));
		if($locationRec['latitude']!='' &&  $locationRec['longitude']!=''){
			$userLat = $locationRec['latitude'];
			$userLong = $locationRec['longitude'];
		} else if($locationRec['address_id']>0){
			$locationRec = mysqli_fetch_array(mysqli_query($con, "SELECT latitude, longitude FROM tbl_user_address WHERE id = '".$locationRec['address_id']."'"));
			$userLat = $locationRec['latitude'];
			$userLong = $locationRec['longitude'];
		} else {
			$userRec = mysqli_fetch_array(mysqli_query($con, "SELECT latitude, longitude FROM tbl_user WHERE id = '".$userId."'"));
			$userLat = $userRec['latitude'];
			$userLong = $userRec['longitude'];
		}
		
	    			
			if($userLat!='' && $userLong!=''){		
				$select = "SELECT id, name, email, phone, building_name,(((acos(sin((".$userLat."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$userLat."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$userLong."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance
		FROM `tbl_purchase_partner` WHERE id IN (".$shopIdString.") ORDER BY distance LIMIT 10";
				$execDetails = mysqli_query($con, $select);
				$showAssign = TRUE;
				// Using pagination class
				//$pagination = new pagination_noseo($select);
				//$execDetails = $pagination->createPages(10);
				//$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
				//$pageNums = $pagination->pageNumber(5, $pageLnk);
			} else {
				$error = 'The user does not have a location (lat,long)';
			}
		
	} else {
		$error = 'No shops found to match with the order';
	}	
	
	
	//get order products		
		$selectProducts = "SELECT * FROM tbl_purchase_order_products WHERE order_id = '".$orderId."'";
		// Using pagination class
		$paginationProducts = new pagination_noseo($selectProducts);
		$execProductDetails = $paginationProducts->createPages(100);		
		$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
		$pageNums = $paginationProducts->pageNumber(5, $pageLnk);
	
} else {
	$error = 'Something went wrong';
}


?>