<?php
include '../includes/connection.php';
session_start();
$id_user=$_SESSION['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'];


$branch=$_GET['branc']; 
$from=$_GET['from']; 
$to=$_GET['to']; 
$status=$_GET['status']; 



if($status=="pending"){
    $status_querry="AND total_price>paid";
    $defaultquery="SELECT id, total_price,saletype, receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames from sales WHERE total_price>paid ";
}else if($status=="cleared"){
     $status_querry="AND total_price=paid";
     $defaultquery="SELECT id, total_price,saletype, receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames from sales WHERE total_price=paid";
}else{
    $defaultquery="SELECT id, total_price,saletype, receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames from sales ";
     $status_querry="";
}



if(strlen($branch)>0 && strlen($from)==0 && strlen($to)==0){
	$sql="SELECT id, total_price,saletype, receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames ,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where branch_id='$branch' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}
if(strlen($branch)>0 && strlen($from)>0 && strlen($to)==0  ){
  $sql="SELECT id, total_price,saletype,receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where DATE_FORMAT(date_time, '%Y-%m-%d')>='$from' && branch_id='$branch' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}

if(strlen($branch)>0 && strlen($from)>0 &&strlen($to)>0){

$sql="SELECT id, total_price,saletype,receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$from' AND DATE_FORMAT(date_time, '%Y-%m-%d')<='$to' && branch_id='$branch' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}


if( strlen($branch)==0 && strlen($from)>0 && strlen($to)==0){
	$sql="SELECT id, total_price,saletype,receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$from' && business_id='$business' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}


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

$sql="SELECT id, total_price,saletype,receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where DATE_FORMAT(date_time, '%Y-%m-%d')>='$from' AND DATE_FORMAT(date_time, '%Y-%m-%d')<='$to' && business_id='$business' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}

if( strlen($branch)==0 && strlen($from)==0 && strlen($to)==0 ){

$sql="SELECT id, total_price,saletype,receipt_no,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id limit 1))as usernames,(SELECT branch_name from branches where branch_id=sales.branch_id) as branch_name from sales where business_id='$business' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)".$status_querry;
}



$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {

$totalprice=0;

                                        $cou=1;
              while($row = mysqli_fetch_assoc($result)) {
        $dpprice=$row['total_price'];
                 
                $iddx=$row['id'];
                       
                       $totalprice=$totalprice+$dpprice;
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $cou; ?></td>
                                                <td><?php echo $row['branch_name']; ?></td>
                                                <?php if( $business_type==1){
                                      $saletype=$row['saletype'];
                                      if ($saletype==1) {
                                       $sst="Wholesale";
                                      }else{
                                        $sst="Retail";
                                      }
                                    ?>
                                                <td><?php echo $sst; ?></td>
                                                <?php
                                              }
                                                ?>
                                                <td>
                                                    <?php include 'sales_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['customer_name']; ?></td>,
                                                <td><?php echo $row['usernames']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['receipt_no']; ?></td>
                                                <td><?php echo $row['date_time']; ?></td>
                                                <td>
                                                 <?php
                                                if ($receipt_type==1) {
                                                  ?>
                                                  <a href="print_receipta4.php?id=<?php echo $row['id']; ?>"><span class="label label-success"><i class="icon-print icon-white"></i>&nbsp;Receipt</span> </a>
                                                  <?php
                                                }else{
                                                  ?>
                                                  <a href="print_receipt.php?id=<?php echo $row['id']; ?>"><span class="label label-success"><i class="icon-print icon-white"></i>&nbsp;Receipt</span> </a>
                                                  <?php

                                                }
                                                
                                                
                                                ?>
                                                 <br>


                                                <a onclick = "confirmationChange(this); return false;" href="../sales/sales_return.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Change</span>
                                                </td>
                                                
                                                
                                            </tr>
               <?php 
               $cou++;                             
    }
    ?>
    <tr>
      <td colspan="4"><h4>TOTAL SALES</h4></td>
      <td colspan="3"><h4> <?php echo number_format($totalprice);?></h4></td>
    </tr>
    <?php
} else {
    echo "0 results".mysqli_error($con);
}
?>