<?

if ( $action == 'index' ) {
	
	$req = $Requisitions->getAllReqs();
	foreach ($req as $cnt=>$r){
		$quotes=$Quotations->getNumber($r['id']);
		$requi=$RequisitionDetails->getNumber($r['id']);
		$reqs[$cnt]=$r;
		$reqs[$cnt]['quotes']=count($quotes);
		$reqs[$cnt]['items']=count($requi);
		
		}
	
	$tData['req']=$reqs;
	
	$data['content'] = loadTemplate('requisitions.tpl.php',$tData);
}


if ( $action == 'requisitions_delete' ) {
	
	$id = $_GET['id'];
	//check if the requisition has already got a quotation.
	$rData['reqid']=$id;
	$quot=$Quotations->find($rData);
	
	if (!$quot){
		$Requisitions->real_delete($id);
		$RequisitionDetails->deleteWhere($rData);
		
		$_SESSION['message'] = 'Requisition deleted';
		redirect('requisitions','index');
	}
	else {
		$_SESSION['error'] = 'Requisition has already received quotations. Can not be deleted';
		redirect('requisitions','index');
			
	}
}



if ($action == 'ajax_getReqDetails'){
	$reqid=$_GET['reqid'];
	
	$rd=$Requisitions->getRequisitionDetails($reqid);
	$count=$Requisitions->getCount($reqid);
	
		$obj = null;
		$obj->total = $count['total'];
		$response[''] = $obj;
		
		$i=1;
		foreach($rd as $r){
			$obj = null;
			
			$obj->reqid = $r['id'];
			$obj->prodid = $r['prodid'];
			$obj->nhifname = $r['nhifname'];
			$obj->qty = $r['qty'];
			
			$response[$i] = $obj;
			$i=$i+1;
			
		}
		
		$data['content'] = $response;
	
}


if ($action == 'ajax_getReqSuppliers'){
	$reqid=$_GET['reqid'];
	$total=$_GET['count'];
	
	$rd=$Requisitions->getRequisitionSuppliers($reqid);
	
	
		$obj = null;
		$obj->total = $total;
		$response[''] = $obj;
		
		$i=1;
		foreach($rd as $r){
			$obj = null;
			
			$obj->supplier = $r['supplier'];
			$obj->totalquotes = $r['totalquotes'];
			
			$response[$i] = $obj;
			$i=$i+1;
			
		}
		
		$data['content'] = $response;
	
}
if ($action =='requisitions_printlist'){
	$data['layout'] = 'layout_blank.tpl.php';
	$reqid=$_GET['id'];
	
	$tData['rd']=$Requisitions->getRequisitionDetails($reqid);
	$tData['count']=$Requisitions->getCount($reqid);
	
	
	$data['content'] = loadTemplate('requisitions_print.tpl.php',$tData);
}

if ($action=='requisitions_informsuppliers'){
	$reqid=$_GET['id'];
	
	$uData['status']='active';
	$users=$Users->find($uData);
	
	$rData['id']=$reqid;
	$reqs=$Requisitions->find($rData);
	
	foreach ($users as $u){
		
		
		
		////////////send mail to inform suppliers that new requisition has been added////////////////
		$email = 'Regency Medical Centre <admin@regencymedicalcentre.or.tz>';
		$cc = 'info@regencymedicalcentre.or.tz';
		$subject = 'Tender Added - Quotations Needed';
		$to = $u['username'];
		// $to = 'mahdi@powerwebtz.com';
		
		$message = 'A new list of drugs required by our hospital has been added to our online tendering software at http://www.regencymedicalcentre.or.tz/tenders';
		
		// if ($reqs[0]['location'] == 'mwanza') {
			// $message .= '<br />The goods required are to be supplied at Shree Hindu Mandal Hospital, <b>Mwanza Branch</b>';
			// $message .= '<br />The payment for the goods will also be made by <b>Mwanza Branch</b>';
			
			// }
		$message .= '<br />This list will be closed on '. fdate($reqs[0]['enddate']).', after which new quotations will not be received.';
		$message .= '<br />You are requested to log in to the system and submit your quotations.';
		$message .= '<br />Regards, <br/> Regency';
		
		$headers = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
		$headers .= "From: " . $email. "\r\n";
		$headers .= "Cc:" . $cc;
		mail($to,$subject,$message,$headers);
	
		}
	$upData['informsupplier']=0;
	$Requisitions->update($reqid, $upData);
	$_SESSION['message'] = 'Suppliers informed';
	redirect('requisitions','index');
	
	}

	
if ( $action == 'import') {
		
		
		
		if ($_FILES['products']['size'] > 0) { 
			ini_set('auto_detect_line_endings',TRUE);
			
			$file = $_FILES['products']['tmp_name']; 
			$handle = fopen($file,"r");
			
			do { 
				if ($data[0] && $data[0] != 'prodid') { 
					$prods[] = $data;
				} 
			} while ($data = fgetcsv($handle,1000,",","'")); 
			ini_set('auto_detect_line_endings',FALSE);
			
		}
		
		
		$tData['prods'] = $prods;
		$data['content'] = loadTemplate('import_reqs.tpl.php',$tData);
		
}

if ( $action == 'import_save') {
		
		$data = $_POST;
		$rows = count($data['prodid']);
		
		$dateInput = explode('/',$data['startdate']);
		$data['startdate'] = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
				
		$dateInput = explode('/',$data['enddate']);
		
		$data['enddate'] = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
		
		$reqData['startdate'] = $data['startdate'];
		$reqData['enddate'] = $data['enddate'];
		$reqData['listname'] = $data['listname'];
		$reqData['statusid'] = 1;
		$reqData['informsupplier'] = 1;
		$reqData['lpo'] = 0;
		$reqData['createdby'] = $_SESSION['member']['id'];
		$Requisitions->insert($reqData);
		$reqId = $Requisitions->lastId();
		
		$cnt=0;
		foreach ($data['prodid'] as $s){
			
				$pro = $data['prodid'][$cnt];
			
				$supp[$s][$pro]['prodid']=$s;
				$supp[$s][$pro]['nhifname']=$data['nhifname'][$cnt];
				$supp[$s][$pro]['nhifcharge']=$data['nhifcharge'][$cnt];
				$supp[$s][$pro]['qty']=$data['qty'][$cnt];
				$supp[$s][$pro]['units']=$data['units'][$cnt];
				$supp[$s][$pro]['pack']=$data['pack'][$cnt];
				
				
				
				$cnt++;
				// $suppid=$s;
			
		}
	
		
	$arraysize= count($supp);
		foreach ($supp as $s=>$ins){
			
			foreach ($ins as $p=>$in){
				
				$reqDetData['reqid'] = $reqId;
				$reqDetData['prodid'] = $in['prodid'];
				$reqDetData['nhifname'] = $in['nhifname'];
				$reqDetData['nhifcharge'] =  $in['nhifcharge'];
				$reqDetData['qty'] =  $in['qty'];
				$reqDetData['units'] =  $in['units'];
				$reqDetData['pack'] =  $in['pack'];
				
				$RequisitionDetails->insert($reqDetData);
			}
		}
		$_SESSION['message'] = 'Requisition imported';
		redirect('requisitions','index');
	}	
	
	
	if ($action == 'ajax_submitForm'){
		
		$startdate = $_GET['startdate'];
		$enddate = $_GET['enddate'];
		$listname = $_GET['listname'];
		
		##########################
		
		$reqDetData['prodid'] = $_GET['prodid'];
		$reqDetData['nhifname'] = $_GET['nhifname'];
		$reqDetData['nhifcharge'] =  $_GET['nhifcharge'];
		$reqDetData['qty'] =  $_GET['qty'];
		$reqDetData['units'] =  $_GET['units'];
		$reqDetData['pack'] =  $_GET['pack'];
		
		
		$dateInput = explode('/',$startdate);
		$startdate = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
				
		$dateInput = explode('/',$enddate);
		$enddate = $dateInput[2].'-'.$dateInput[1].'-'.$dateInput[0];
		
		
		$reqData['startdate'] = $startdate;
		$reqData['enddate'] = $enddate;
		$reqData['listname'] = $listname;
		$reqData['statusid'] = 1;
		$reqData['informsupplier'] = 1;
		$reqData['lpo'] = 0;
		$reqData['createdby'] = $_SESSION['member']['id'];
		
		//check if this list is already created, if so, add new items to this list.
		//if not then create new
		$isExist = $Requisitions->find($reqData);
		
		if ($isExist){
			$reqId = $isExist[0]['id'];
		}
		
		else {
			$Requisitions->insert($reqData);
			$reqId = $Requisitions->lastId();
		}
		
		$reqDetData['reqid'] = $reqId;
		$RequisitionDetails->insert($reqDetData);
		
		
		$response = array();
		$obj=null;
		$obj->status = 'done';
		$response[]=$obj;
		
		$data['content'] = $response;

	}