 <?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'])) 
{
	$invoice_no = $_POST['invoice_no'];
	
	
	
		$check=mysqli_query($connection,"select * from sale where status = 'complete' and invoice_no='$invoice_no' AND branch_id  = '$branch_id_for_header_user'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows<1){$errMsg="Sorry No Transaction Was Happen In This Invoice.";}
		
		else{
		
	$query_present_stock = "SELECT * FROM sale where invoice_no ='$invoice_no' AND branch_id  = '$branch_id_for_header_user'";
                                        $result_present_stock = mysqli_query($connection,$query_present_stock);
                                        
										
										
                                        while($row_present_stock=mysqli_fetch_array($result_present_stock))
                                        {
                                        
										
                                        $quantity_prensent_stock = $row_present_stock['quantity'];
										$product_id_prensent_stock = $row_present_stock['product_id'];
										
					mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock + $quantity_prensent_stock WHERE product_id = '$product_id_prensent_stock' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
										
										}
										
										
			$query_sale_taka = "SELECT * FROM sale_taka where invoice_no ='$invoice_no'";
                                        $result_sale_taka = mysqli_query($connection,$query_sale_taka);
                                        
										
										
                                        while($row_sale_taka=mysqli_fetch_array($result_sale_taka))
                                        {
                                        
										
                                        $cash = $row_sale_taka['cash'];
										$bank = $row_sale_taka['bank'];
										$cheque_no = $row_sale_taka['cheque_no'];
										$bank_id = $row_sale_taka['bank_id'];
										
					mysqli_query($connection,"UPDATE bank_name SET balance = balance - $bank WHERE bank_id = '$bank_id'")
				or die(mysqli_error());
				
				mysqli_query($connection,"DELETE FROM bank_statement WHERE cheque_no = '$cheque_no'")
				or die(mysqli_error()); 
				
				mysqli_query($connection,"UPDATE cash SET cash = cash - $cash WHERE branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
				
			
										}
										
				mysqli_query($connection,"DELETE FROM transaction WHERE ref_id = '$invoice_no'")
				or die(mysqli_error());
				mysqli_query($connection,"DELETE FROM sale where invoice_no ='$invoice_no'")
				or die(mysqli_error());
				mysqli_query($connection,"DELETE FROM sale_taka where invoice_no ='$invoice_no'")
				or die(mysqli_error());
										
		
		
		
		
		
		 
		
		 
		$errMsg="Sale Product Successfully Return!";
	}

}
?>
<!--For connect database end-->
     
        
        <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">Product Setup</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                 
                     <div class="row">
                     
                         <div class="col-md-8 col-md-offset-2">
                   
                       
                        <?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">Sale Return</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Invoice No</label>
                                            <input type="text" class="form-control" name="invoice_no" placeholder="Input Invoice No" required>
                                        </div>  
                                        <!-- /.box-body -->

                                    <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 -->

 <?php include("sass/footer_for_datepicker_and_datatable.php");?>
    </body>
</html>