<?php include("sass/icon_for_datepicker_and_datatable.php");?>
<?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$to_br = $_POST['to_br'];
	$qty = $_POST['qty'];
	$date = $_POST['date'];
	$date = date("Y-m-d", strtotime($date));
	$barcode = $_POST['barcode'];

	if(empty($errMsg))
	{
	

	
	
	
		$result_for_supplier = mysqli_query($connection,"SELECT product_id FROM product_setup WHERE barcode = '$barcode'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				$product_id=$test_for_supplier['product_id'];
				
				
				
 $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 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){$errMsg="sorry!!! stock is short.";}

else{ 	
				
				
		mysqli_query($connection,"INSERT INTO store_transfer (from_br, to_br, qty, `date`, product_id) VALUES ('$branch_id_for_header_user','$to_br','$qty','$date','$product_id')")
		or die(mysqli_error()); 
		$errMsg="Product name inserted successfully";
		
				
		mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock - '$qty' WHERE product_id = '$product_id' and branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error());
					
	$check=mysqli_query($connection,"select * from present_stock WHERE product_id = '$product_id' and branch_id = '$to_br'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>0){
			
			
		mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock + '$qty' WHERE product_id = '$product_id' and branch_id = '$to_br'")
				or die(mysqli_error()); 
			
			
		}
		
		else{	
				mysqli_query($connection,"INSERT INTO present_stock (product_id, present_stock, branch_id) VALUES ('$product_id','$qty','$to_br')")
		or die(mysqli_error());
		}
	$errMsg = "Transferred Successfully!";	
}	
}
}
?>
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-6 col-md-offset-3">
                 <!---Main Content--->
                     <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                 			<div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Product Entry</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                
                            
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post" id="login_form">
                                    <div class="box-body">
                                        
                                      <div class="form-group">
                                            <label for="exampleInputEmail1">To</label>
                                            <select name="to_br" class="form-control">
                                            
												<?php  
                                                $query = "select * from branch";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['branch_id'] . "'>" . $row_techname['branch_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Quantity</label>
                                            <input type="number" class="form-control" name="qty" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="date" required>
                                        </div>
									 <div class="form-group">
                                            <label for="exampleInputEmail1">Barcode</label>
                                            <input type="text" class="form-control" name="barcode" placeholder="write barcode" required>
                                        </div>
                                        
                                    </div>
									
									<div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                </form>
                 
                 		</div>
                 
                 		</div>
                    </div>
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->


        <!-- jQuery 2.0.2 -->
          <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>