<?php
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['bc_id'])){
    $name   = $db->escape_var($_POST['name']);
    $bc_id  = $db->escape_var($_POST['bc_id']);

	$inq =$db->query("SELECT SUM(amount)
			FROM customers,transactions
			WHERE customers.`id`=transactions.`customers_id`
			AND customers.`bc_id`=$bc_id
			AND customers.`name`=$name
			AND type='in'
			GROUP BY type");
	$in = $inq->fetch_assoc();

	$outq =$db->query("SELECT SUM(amount)
			FROM customers,transactions
			WHERE customers.`id`=transactions.`customers_id`
			AND customers.`bc_id`=$bc_id
			AND customers.`name`=$name
			AND type='out'
			GROUP BY type");
	$out = $outq->fetch_assoc();

	echo "<script>alert($outq);</script>";

    $result = $db->query($in);
    if($result->num_rows > 0){
		$i = 0;
		while($row = $result->fetch_assoc()){
			
			$uq = $db->query("SELECT currencies.`unit` FROM assets,currencies WHERE assets.`currencies_id`=currencies.`id` AND assets.`id` = {$row['assets_id']}");
			$unit = $uq->fetch_assoc();

			echo "<tr>";
			echo "<td>" . ++$i . "</td>";
			echo "<td>" . $row['name'] ."</td>";
			echo "<td>" . $row['bc_id'] . "</td>";
			echo "<td>" . $row['phone'] . "</td>";
			echo "<td>" . $unit['unit'] . "</td>";
			echo "<td>" . $row['amount'] . "(" . $unit['unit'] . ")" . "</td>";
			echo "<td>" . $row['total_dollars'] . "</td>";
			echo "<td>" . $row['result'] . "</td>";
			if ($row['type'] == "in") {	
				echo "<td>رسیدگی</td>";
			}else {
				echo "<td>بردگی</td>";
			}
			echo "<td>" . $row['date'] . "</td>";
			echo "</tr>";
		}
		$result->free();
	}
}
/* END of query based on customer's name and bcID */

?>