<?php 
session_cache_limiter( FALSE );
	session_start();

require_once("sass/connection.php"); ?>
<body>

<?php


$user_id_for_header_user = $_SESSION['user_id'] ;
$result_for_header_user = mysqli_query($connection,"SELECT * FROM users WHERE users_id  = '$user_id_for_header_user'");
		$test_for_header_user = mysqli_fetch_array($result_for_header_user);
				if (!$result_for_header_user) 
				{
				die("Error: Data not found..");
				}
				$user_name_for_header_user=$test_for_header_user['user_name'];
			$branch_id_for_header_user=$test_for_header_user['branch_id'];
			
		$result_for_branch = mysqli_query($connection,"SELECT * FROM branch WHERE branch_id  = '$branch_id_for_header_user'");
		$test_for_branch = mysqli_fetch_array($result_for_branch);
				
				$branch_name=$test_for_branch['branch_name'];



$product_name_input = $_GET['tags'];
$quantity_input = $_GET['quantity'];
$sell = $_GET['sell'];


$product_name_ex = explode("-",$product_name_input);
	$product_id_ex = $product_name_ex[0];
	$product_name_last = $product_name_ex[1];



 
 $check=mysqli_query($connection,"select * from product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id where present_stock.product_id = $product_id_ex AND present_stock.present_stock < $quantity_input and present_stock.branch_id  = '$branch_id_for_header_user'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0){echo "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>
   Sorry!!! Stock is short. 
</div>";}

else{ 

$result_for_product_insert = mysqli_query($connection,"SELECT * FROM product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id WHERE product_setup.product_id  = $product_id_ex AND present_stock.branch_id  = '$branch_id_for_header_user'");
		$test_for_product_insert = mysqli_fetch_array($result_for_product_insert);
				
				$buy_price=$test_for_product_insert['buy_price'];
				if(empty($sell)){
				$selling_price=$test_for_product_insert['selling_price'];
				}
				else {
				$selling_price=$sell ;
				}
				if(!empty($pk_sell)){
				$selling_price=$pk_sell ;
				}
				$present_stock=$test_for_product_insert['present_stock'];
				$unit_type_id=$test_for_product_insert['unit_type_id'];
				
				

 $check=mysqli_query($connection,"SELECT * FROM sale where product_id = '$product_id_ex' AND status = 'pending' AND user_id = '$user_id_for_header_user' AND branch_id = '$branch_id_for_header_user'");
$checkrows=mysqli_num_rows($check);
if($checkrows>0){
	
	
	mysqli_query($connection,"UPDATE sale SET quantity = quantity + $quantity_input where product_id = '$product_id_ex' AND status = 'pending' AND user_id = '$user_id_for_header_user' AND branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error()); 

}

else{ 

mysqli_query($connection,"INSERT INTO sale(product_id, quantity, buy_price, selling_price, status, user_id, branch_id) 
VALUES ('$product_id_ex','$quantity_input','$buy_price','$selling_price','pending','$user_id_for_header_user','$branch_id_for_header_user')")
or die(mysqli_error());


}

}


?>


<table class="table table-hover">
 										<tr>
                                        <th>Serial</th>
                                        <th>Name</th>
                                        <th>Per Unit Price</th>
                                        <th>Total Unit</th>
                                        <th>In Stock</th>
                                        <th>Total Price</th>
                                        </tr>
                                        
                                       <?php
                                        
                                        $query = "SELECT * FROM sale where status = 'pending' AND user_id = '$user_id_for_header_user' AND branch_id = '$branch_id_for_header_user'";
                                        $result = mysqli_query($connection,$query);
                                        
										$i=0;
										$grand_total = 0 ;
                                        while($row=mysqli_fetch_array($result))
                                        {
                                        
										 $i++;
                                        $sale_id = $row['sale_id'];
                                        $product_id_to = $row['product_id'];
										
										

$result_for_product = mysqli_query($connection,"SELECT * FROM product_setup INNER JOIN present_stock
ON product_setup.product_id=present_stock.product_id WHERE product_setup.product_id  = $product_id_to and present_stock.branch_id = '$branch_id_for_header_user'");
		$test_for_product = mysqli_fetch_array($result_for_product);
				
				$product_name_to=$test_for_product['product_name'];
					$present_stock=$test_for_product['present_stock'];
					 $present_stock = $present_stock ;
					 $present_stock_show = ($present_stock);
					 
				$unit_type_id=$test_for_product['unit_type_id'];
				
				$result_for_unit_type = mysqli_query($connection,"SELECT * FROM unit_type WHERE unit_type_id  = '$unit_type_id'");
		$test_for_unit_type = mysqli_fetch_array($result_for_unit_type);
				
				$unit_type_name=$test_for_unit_type['unit_type_name'];
										
										if($unit_type_name == "packet") {
										$present_stock=$test_for_product['present_stock'];
					
					 $present_stock_show = ($present_stock);
										}
										
                                         $quantity = $row['quantity'];
										 $quantity_con = $quantity ;
										  $quantity_show = ($quantity_con);
										  
										$selling_price = $row['selling_price'];
                                       $selling_price_show = ($selling_price);
                                        $total = $quantity * $selling_price ;
										
										$grand_total = $grand_total + $total ;
										
                                        $total_show = ($total);
											$carton = $row['carton'];
											$packate = $row['packate'];	
																		
                                        echo "<tr>
                                                <td>$i</td>
                                                <td>$product_name_to</td>
                                                <td>$selling_price_show</td>";
												if($carton == 0 && $packate == 0) {
												
                                               echo "<td>$quantity_show $unit_type_name</td>";
												}
												else {
												
												if($carton <> 0 && $packate == 0) {
												echo "<td>$carton sack(s)</td>";
												}
												elseif($carton == 0 && $packate <> 0) {
												echo "<td>$packate packet</td>";
												}
												else {
												echo "<td>$carton carton $packate packet</td>";
												}
												
												}
												
                                           if($carton <> 0 || $packate <> 0) {
												echo "<td>$present_stock_show sack(s)</td>";
												}
										   else {
										   echo     "<td>$present_stock_show $unit_type_name</td>";
										   }
                                            echo    "<td>$total_show</td>
                                                                                            
                                                <td>
                                                
                                             <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#myModal$i'><i class='fa fa-minus-square'></i>&nbsp; Remove</button> 
                                                </td>
                                            </tr>";
											echo "<div class='modal fade' id='myModal$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>be sure</h4>
      </div>
      <div class='modal-body'>
       are you sure want to delete <strong> $product_name_to </strong>?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='confirm_delete_sale.php?sale_id=$sale_id'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";
											}	
                                        ?>
                                        <tr>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th></th>
                                        <th>Total Price</th>
                                        <th><?php echo ($grand_total); ?><input type="hidden" class="form-control" name="have_to_pay_original" value="<?php echo ($grand_total); ?>"  id="total" /></th>
                                        </tr>
									</table>
									<?php 

?>
</body>
</html>