 <?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'])) 
{
	$supplier_id = $_POST['supplier_id'];
	$chalan_no = $_POST['chalan_no'];
	$amount = $_POST['amount'];
	$amount_t = $amount;
	$media = $_POST['media'];
	$bank_id = $_POST['bank_id'];
	$cheque_no = $_POST['cheque_no'];
	$cheque_date = $_POST['cheque_date'];
	$date = $_POST['date'];
	if(!empty($cheque_date)) {
$newDate = date("Y-m-d", strtotime($cheque_date));
}
else {
$newDate = "";
}
if(!empty($date)) {
$date = date("Y-m-d", strtotime($date));
}
else {
$date = "";
}
	
	
	if(empty($chalan_no)) $errMsg="Write Supplier Name";
	if(empty($errMsg))
	{
	
	$check=mysqli_query($connection,"select * from supplier_payment where supplier_id='$supplier_id'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows<1){$errMsg="Sorry No Transaction Was held With This Supplier";}
		
		else{
		
		
	$check=mysqli_query($connection,"select * from supplier_setup where due<'$amount_t' and supplier_id='$supplier_id'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>1){$errMsg="Sorry! Due is lower than given amount!";}
		
		else{
		
		
	
	
	$check=mysqli_query($connection,"select * from transaction where ref_id='$chalan_no'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows>1){$errMsg="Sorry! This Invoice already used before!";}
		
		else{
		
		
	
				
		
		
		
		//mysqli_query($connection,"UPDATE supplier_payment SET $media = $media+$amount_t , due = due-$amount_t WHERE supplier_id = '$supplier_id' limit 1")
				//or die(mysqli_error()); 
		

		mysqli_query($connection,"UPDATE supplier_setup SET due = due - $amount_t WHERE supplier_id = '$supplier_id'")
				or die(mysqli_error()); 		
				
		mysqli_query($connection,"INSERT INTO sup_due_payment (supplier_id, $media, cheque_no, cheque_date, bank_id, chalan_no, date, branch_id) VALUES ('$supplier_id','$amount_t','$cheque_no','$newDate','$bank_id','$chalan_no','$date','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		if(!empty($bank_id)) {


		
		mysqli_query($connection,"INSERT INTO bank_statement (amount, ref, payment_type, reason, supplier_id, bank_id, cheque_no, cheque_date, date, branch_id) VALUES ('$amount_t','$chalan_no','credit','3','$supplier_id','$bank_id','$cheque_no','$newDate','$newDate','$branch_id_for_header_user')")
		or die(mysqli_error());
		 
		}
		else {
		
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, hint, payment_type, branch_id) VALUES ('$date','$chalan_no','$supplier_id','3','$amount_t','supplier due payment','cash','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		mysqli_query($connection,"UPDATE cash SET cash = cash - $amount_t WHERE branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error());
		
		}
				
				
		$errMsg="Supplier Due Paid Successfully!";
	
}
}
}
}
}
?>
            <!-- 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">Due Payment</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">Supplier Name</label>
                                            <input type="hidden" name="supplier_id" value="<?php echo $supplier_setup_id ;?>" />
                                            <input type="text" class="form-control" name="supplier_name" value="<?php echo $supplier_name ;?>">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Chalan No</label>
                                            <input type="text" class="form-control" name="chalan_no">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Amount</label>
                                            <input type="text" class="form-control" name="amount">
                                        </div>
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Payment Type </label>
                                             <select name="media" class="form-control">
                                             	<option value="cash">Cash</option>
                                                <option value="bank">Bank</option>
                                             </select>
                                        </div>
                                        
                                        
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Bank Name</label>
                                            <select name="bank_id" class="form-control">
                                            <option value="">Select</option>
												<?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">Cheque No</label>
                                            <input type="text" class="form-control" name="cheque_no">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Cheque Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="cheque_date">
                                        </div>
										 <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y") ; ?>">
                                        </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>