<?php include("sass/icon_for_datepicker_and_datatable.php");?>

      

   
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>
 <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$amount = $_POST['amount'];
	$amount_t = $amount;
	$media = $_POST['media'];
	
	$supplier_id = $_POST['supplier_id'];
	$customer_id = $_POST['customer_id'];
	
	$bank_id = $_POST['bank_id'];
	
	$chalan_no = $_POST['chalan_no'];
	$cheque_no = $_POST['cheque_no'];
	$cheque_date = $_POST['cheque_date'];
	if(!empty($cheque_date)) {
$newDate = date("Y-m-d", strtotime($cheque_date));
}
else {
$newDate = "";
}
	$description = $_POST['description'];
	$date = date("Y-m-d");
	
	if(empty($amount)) $errMsg="write the amount of taka";
	if(empty($errMsg))
	{
			if($media == "sup_debit") {

		$check=mysqli_query($connection,"select * from supplier_payment where check_number='$cheque_no' AND chalan_no ='$chalan_no' AND branch_id  = '$branch_id_for_header_user'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows<1){$errMsg="Sorry!! this cheque/chalan number has no transaction history";}
		
		else{ 
				
		
		if(!empty($bank_id)) {


				
	mysqli_query($connection,"UPDATE supplier_payment SET due = due + $amount_t WHERE check_number='$cheque_no' AND chalan_no ='$chalan_no' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		mysqli_query($connection,"UPDATE bank_statement SET status = 1 WHERE ref = '$chalan_no' AND cheque_no='$cheque_no' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$description','$amount_t','$cheque_no','$newDate','$bank_id','supplier check bounce','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, cheque_no, cheque_date, bank_id, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$supplier_id','$description','$amount_t','$cheque_no','$newDate','$bank_id','supplier check bounce','due','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		 
		}
		
		$errMsg="Cheque has been returned.";
		}
				
				
		
			}
			
			
			
			
			
		
		
		
		
		if($media == "cus_credit") {

		$check=mysqli_query($connection,"select * from sale_taka where invoice_no ='$chalan_no' AND cheque_no='$cheque_no' AND branch_id  = '$branch_id_for_header_user'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows<1){$errMsg="Sorry!! this cheque/chalan number has no transaction history";}
		
		else{ 
				
		
		if(!empty($bank_id)) {
		
		mysqli_query($connection,"UPDATE bank_statement SET status = 1 WHERE cheque_no='$cheque_no' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());
				
		mysqli_query($connection,"UPDATE sale_taka SET due = due + $amount_t WHERE invoice_no ='$chalan_no' AND cheque_no='$cheque_no' AND branch_id  = '$branch_id_for_header_user'")
				or die(mysqli_error());		
		
		
		 mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, credit, cheque_no, cheque_date, bank_id, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','13','$amount_t','$cheque_no','$newDate','$bank_id','customer check bounce','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, customer_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$customer_id','13','$amount_t','$cheque_no','$newDate','$bank_id','customer check bounce','due','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		 
		}
		
		$errMsg="Cheque has been returned";
		}
				
				
		
			}
		
		
			
			
}
}
?>
            <!-- 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">
                        
                 <!---Main Content--->
                 
                 			<?php 
							
							if (!empty($_REQUEST['supplier_setup_id']))
						{$supplier_setup_id = $_REQUEST['supplier_setup_id'];
						}
						else {
						$supplier_setup_id = 0 ;
						}
							
							$result_for_header_user = mysqli_query($connection,"SELECT supplier_name FROM supplier_setup WHERE supplier_id  = '$supplier_setup_id'");
		$test_for_header_user = mysqli_fetch_array($result_for_header_user);
				
				$supplier_name=$test_for_header_user['supplier_name'];
							
							?>
                            
                           <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">Cheque Bounce</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">Money Amount </label>
                                            <input type="text" class="form-control" name="amount" placeholder="write here" required>
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Transaction Type</label>
                                             <select name="media" class="form-control">
                                             	<option value="sup_debit">Supplier Cheque Return</option>
                                                <option value="cus_credit">Wholeseller Cheque Return</option>
                                             </select>
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Supplier</label>
                                            <select name="supplier_id" class="form-control">
                                             <option value="">Select</option>
                                            
												<?php  
                                                $query = "select * from supplier_setup WHERE branch_id  = '$branch_id_for_header_user'";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['supplier_id'] . "'>" . $row_techname['supplier_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                            
                                            
                                        </div>
                                        
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Wholeseller Name</label>
                                            <select name="customer_id" class="form-control">
 <option value="">Select</option>
                                            
												<?php  
                                                $query = "select * from customer_setup WHERE branch_id  = '$branch_id_for_header_user'";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['customer_id'] . "'>" . $row_techname['customer_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                            
                                            
                                        </div>
                                        
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Name</label>
                                            <select name="bank_id" class="form-control">
												<?php  
                                                $query_bank = "select * from bank_name";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['bank_id'] . "'>" . $row_bank['bank_name'] . "</option>";}?> 
                                            
                                            </select>
                                            
                                            
                                            
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Chalan Number</label>
                                            <input type="text" class="form-control" name="chalan_no" id="" placeholder="write chalan number" required>
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Number</label>
                                            <input type="text" class="form-control" name="cheque_no" id="" placeholder="write here" required>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="cheque_date" placeholder="write here">
                                        </div>
                                        
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Details</label>
                                            <input type="text" class="form-control" name="description" id="" placeholder="describe here" required>
                                        </div>
                                    </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 -->

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


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