<?php

 include("includes/dbconfig.php");
 

 
if(isset($_POST['comand'])){
	if( $_POST['comand'] ==1 || $_POST['comand'] ==2 ){
		 ?>
	<form onsubmit ="return Finish_adjust();" >
	<div class="col-md-3">
	<?php 
	if( $_POST['comand'] ==1   ){
		echo '<h4>Add on stock </h4>';
	    }else echo '<h4>Reduce on stock</h4>';
		 ?>
		 </div>
	<input type ="hidden"   id ="command"  value = "<?php echo $_POST['comand'];?>" class ="form" />
	<input type ="hidden"   id ="id"  value ="<?php echo $_POST['dataSend'];?>" class="form-control"  />
	
 
		 
						<div class="col-md-3">
							<input type ="number"   id ="adjust"  placeholder="Customer" class="form-control"   />
						</div>
						<div class="col-md-3">
							<input type="submit" value="Search" class="form-control btn btn-info"   />
						</div>
					</div>
					<br />
					 
	</form>
	<?php
	}else 	if( $_POST['comand'] ==3 ){
		 
		$data =  $_POST['dataSend'];
		$action  =$data[0]; 
		$item  =$data[1]; 
		$number  =$data[2];//$pricedata = DB::query("SELECT item_stock_level.id, 
		$pricedata = DB::query('SELECT quantity FROM item_stock_level WHERE id ='.$item.''); 
		if(DB::count() == 0){
			exit('Item not found');
		}
		ELSE {
			$quantity =  $pricedata[0]['quantity'] + $number ;
			 
			DB::query('UPDATE item_stock_level SET quantity = '.$quantity.' WHERE id  = '.$item.'');
			echo 'Operation was sucessfull';
		}
		 
	}
	else 	if( $_POST['comand'] ==4 ){
		  $personel  =    $_POST['dataSend'];
		   $receipts = DB::query('SELECT id , created_date , shop , customer_name, receipt_number, bill  FROM cash_receipt where  attendant= '.$personel.'  ');
	                     
		 if(sizeof($receipts) > 0){
			   $person_info = DB::query('SELECT first_name , display_name , phone , email   FROM personnel_details where  id= '.$personel.'  ');
			   $full_name  =  'N/A';
			   if(sizeof($person_info)>0){
				   $full_name  =  $person_info[0]['first_name'].' , '.$person_info[0]['display_name'].' , '.$person_info[0]['phone'];
				   
			   }
			    
			 
			?>
			<h4> Detailed Personel performace Report for <b><?php  echo $full_name; ?> </b></h4>
			<table class="table table-hover">
			<thead>
			<th> Receipt No</th>
			<th> Shop Name</th>
			<th> Date</th>
			<th> Customer</th>
			<th> Ammount</th>
			<th> Quantity</th>
			
			<th> Item(s)</th>
			</thead>
			<?php
                          $shops  = DB::query("SELECT id ,full_name   FROM shop  ");
                                    foreach($receipts as $receipt)
										{
											$quantity = 0; 
											$items_name  = '';
											$shop_name  = 'N/A';
											
											
											$items = DB::query('SELECT id , price_id ,quantity,is_cancelled  FROM cash_sale_item where  receipt= '.$receipt['id'].'');
	                      
											foreach($items as $item){
												 
												$quantity += $item['quantity']; 
												//stock_item'
												 
												$stock_data = DB::query('SELECT item_name , item_code ,bar_code,description  FROM stock_item WHERE  id= '.$item['price_id'].'');
												 
												foreach($stock_data as $stock){
													  $items_name   .= $stock['item_name'].' , '.$stock['item_code'].' , '.$stock['bar_code'].',  '.$stock['description'];
													 	
												}
	                       	 	
											}
											
											foreach($shops as $shop){
															if($receipt['shop'] ==$shop['id']){
																$shop_name  =  $shop['full_name'];
															}
														}
											
											echo '<tr><td>'.$receipt['receipt_number'].' </td> 
											<td> '.$shop_name.' </td>
											<td> '.$receipt['created_date'].' </td>
											<td> '.$receipt['customer_name'].' </td>
											<td>'.$receipt['bill'].' </td> <td>'.$quantity.' </td> 
											<td>'.$items_name.' </td> 
											</tr>';
											
										}                            
										  ?>
				 
								 </table>
						 <?PHP
		 }else echo 'No Data was found';
	}
	 
	 
}
 



?>