<?php
include '../includes/connection.php';
session_start();
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];

$sql_businesstype=mysqli_fetch_array(mysqli_query($con,"SELECT * from business where business_id='$business'"));
$business_type=$sql_businesstype['type'];
$receipt_type=$sql_businesstype['receipt_type'];


$product_id=$_GET['product']; 
$from=$_GET['dfrom']; 
$to=$_GET['dto']; 




if(strlen($product_id)>0 && strlen($from)==0 && strlen($to)==0){
  $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch' && product_id='$product_id'";
}
if(strlen($product_id)>0 && strlen($from)>0 && strlen($to)==0  ){

  $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch' && product_id='$product_id' && date_format(date_time,'%Y-%m-%d')>='$from'";
  
}

if(strlen($product_id)>0 && strlen($from)>0 &&strlen($to)>0){
  $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch' && product_id='$product_id' && date_format(date_time,'%Y-%m-%d')>='$from' && date_format(date_time,'%Y-%m-%d')<='$to'";


}


if( strlen($product_id)==0 && strlen($from)>0 && strlen($to)==0){
  
  $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch' && date_format(date_time,'%Y-%m-%d')>='$from'";
	
}


if( strlen($product_id)==0 && strlen($from)>0 && strlen($to)>0 ){

 $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch' && date_format(date_time,'%Y-%m-%d')>='$from' && date_format(date_time,'%Y-%m-%d')<='$to'";

}

if( strlen($product_id)==0 && strlen($from)==0 && strlen($to)==0 ){
 
  $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch'";

}

        $resultp = mysqli_query($conn, $sqlxy);
                                        $nid=0;
                                        $totalq=0;
                                        $totalp=0;
                                      // output data of each row
                                     while($rown = mysqli_fetch_assoc($resultp)) {
                                      $nid++;
                                      $totalp=$totalp+$rown['quantity'];
                                      $totalq=$totalq+($rown['quantity']*$rown['unit_price']);

                                  ?>
                            <tr>
                                                      
                              <td width="40%"><?php echo $nid; ?></td>
                              <td width="40%"><?php echo $rown['date_time']; ?></td>
                              <td width="40%"><?php echo $rown['product_name']; ?></td>
                              <td width="20%"><?php echo $rown['quantity']." ".$rown['units']; ?></td>
                              <td width="20%"><?php echo $rown['unit_price']; ?></td>
                              <td width="20%"><?php echo $rown['quantity']*$rown['unit_price']; ?></td>
                              
                            </tr>
                            
                            <?php
       
    }

?>
                                           
                            <tr>
                            <td colspan="2"><strong>TOTAL</strong></td>
                              <td colspan="2"><strong><?php echo $totalp; ?></strong></td>
                              <td><strong><?php echo $totalq; ?></strong></td>
                            </tr>

