<?php include("sass/icon_for_datepicker_and_datatable.php");?>
        
      <?php
	$bank_statement_id =$_REQUEST['bank_statement_id'];

	$result_for_handover_dateover_list = mysqli_query($connection,"SELECT * FROM bank_statement WHERE bank_statement_id = '$bank_statement_id'");
	
		
while($row_list=mysqli_fetch_array($result_for_handover_dateover_list))
{
	$bank_statement_id = $row_list['bank_statement_id'];
			 	$amount = $row_list['amount'];
			 	$ref = $row_list['ref'];
			 	$payment_type = $row_list['payment_type'];
				$reason = $row_list['reason'];
				$supplier_id = $row_list['supplier_id'];
			 	$customer_id = $row_list['customer_id'];
			 	$bank_id = $row_list['bank_id'];
				$cheque_no = $row_list['cheque_no'];
				$cheque_date = $row_list['cheque_date'];
				
				
			if($payment_type == "credit") {
			
			
			$check=mysqli_query($connection,"select bank_id from bank_name where balance>='$amount' and bank_id='$bank_id'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows=0){$errMsg="Sorry!! This bank has not enough money.";}
		
		else{

			mysqli_query($connection,"UPDATE bank_name SET balance = balance - $amount WHERE bank_id = $bank_id")
				or die(mysqli_error());
				
			if($supplier_id<>0) {
			
			mysqli_query($connection,"UPDATE supplier_payment SET bank = bank+$amount WHERE supplier_payment_id = '$ref'")
				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 ('$cheque_date','$ref','$supplier_id','$reason','$amount','$cheque_no','$cheque_date','$bank_id','$ref','bank','$branch_id_for_header_user')")
		or die(mysqli_error());


}
else{


if($reason==10) {
			
			mysqli_query($connection,"UPDATE accounts_expense SET bank = bank+$amount WHERE id = '$ref'")
				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 ('$cheque_date','$ref','$reason','$amount','$cheque_no','$cheque_date','$bank_id','Expense','bank','$branch_id_for_header_user')")
		or die(mysqli_error());

}			
		
				
				
			
}	

				
mysqli_query($connection,"DELETE FROM bank_statement WHERE bank_statement_id = '$bank_statement_id'")
				or die(mysqli_error()); 
		
}					
		
			elseif($payment_type == "debit") {

			mysqli_query($connection,"UPDATE bank_name SET balance = balance + $amount WHERE bank_id = $bank_id")
				or die(mysqli_error());
				
			if($customer_id<>0) {
			
			if($reason==6) {
					mysqli_query($connection,"UPDATE customer_setup SET opening_due = opening_due - $amount WHERE customer_id = '$customer_id'")
				or die(mysqli_error()); 
			
			mysqli_query($connection,"INSERT INTO cus_due_payment (customer_id, bank, cheque_no, cheque_date, bank_id, invoice_no, date, branch_id) VALUES ('$customer_id','$amount','$cheque_no','$cheque_date','$bank_id','$ref','$cheque_date','$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 ('$cheque_date','$ref','$customer_id','$reason','$amount','$cheque_no','$cheque_date','$bank_id','$ref','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
			
			}
			
			elseif($reason==7) {
			
			
					mysqli_query($connection,"UPDATE customer_setup SET due = due-$amount WHERE customer_id = '$customer_id'")
				or die(mysqli_error()); 
			
			
			mysqli_query($connection,"INSERT INTO cus_due_payment (customer_id, bank, cheque_no, cheque_date, bank_id, invoice_no, date, branch_id) VALUES ('$customer_id','$amount','$cheque_no','$cheque_date','$bank_id','$ref','$cheque_date','$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 ('$cheque_date','$ref','$customer_id','$reason','$amount','$cheque_no','$cheque_date','$bank_id','$ref','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
			
			}
			
			elseif($reason==9) {
			
			mysqli_query($connection,"UPDATE sale_taka SET bank = bank+$amount WHERE invoice_no = '$ref'")
				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 ('$cheque_date','$ref','$customer_id','$reason','$amount','$cheque_no','$cheque_date','$bank_id','sale permanent customer','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
			
			}
			
			else{
			
			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 ('$cheque_date','$ref','$customer_id','$reason','$amount','$cheque_no','$cheque_date','$bank_id','$ref','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		}


}	
else{


			

mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, debit, cheque_no, cheque_date, bank_id, hint, payment_type, branch_id) VALUES ('$cheque_date','$ref','$reason','$amount','$cheque_no','$cheque_date','$bank_id','$ref','bank','$branch_id_for_header_user')")
		or die(mysqli_error());
		

}		
		
				
				
mysqli_query($connection,"DELETE FROM bank_statement WHERE bank_statement_id = '$bank_statement_id'")
				or die(mysqli_error()); 			
}						
				
				
				
				
				}

?>

 <?php
							if (!empty($errMsg))
						{echo "$errMsg";}
						
						else{
						echo "<script>window.location='bank_pending.php'</script>";
						?>
						
						<script>
   opener.location.href = opener.location.href;
</script>

<script>
window.close();
</script>
						<?php
						
						}
						
						
	
	
						
                        ?> 

