<?php

include '../includes/connection.php';
session_start();
$id_user=$_SESSION['id'];
$business=$_SESSION['business_id'];
$branch_id=$_SESSION['branch_id'];
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  permissions,users WHERE  permissions.role_id = users.role AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch_id'"));

$branch=$_GET['branc'];
$from=$_GET['dfrom'];
$to=$_GET['dto'];



if(strlen($branch)>0 && strlen($from)==0 && strlen($to)==0){
$supl=mysqli_query($con,"SELECT distinct purchase_id from purchases where branch_id='$branch'");
$nid=0;
$total=0; 
while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['purchase_id']; 


$sql= mysqli_query($con,"SELECT id, total_price,invoice_number, AddedOn,(select  name from suppliers where supplier_id=(select supplier from purchases where purchase_id=purchase_totals.id limit 1)) as supplier ,(SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where id='$supid' && branch_id='$branch' order by id desc");


if (mysqli_num_rows($sql) > 0) {
 
                                   ?>

                                        
                                        <?php
                                      
                                       
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];
                $nid++;
                        
                    ?>
                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                
                                              
                                                <td><?php echo $row['supplier'];; ?></td>
                                               
                                                <?php if( $permissions['purchases_return_button']==1){
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                                
                                            </tr>
                                
                                
                                
                          
               <?php                             
    }
    
} else {
    echo "<tr><td>No results</td><td></td><td></td><td></td><td></td></tr>";
    return 0;
    }
}
?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php





}









if(strlen($branch)>0 && strlen($from)>0 && strlen($to)==0){
$supl=mysqli_query($con,"SELECT distinct purchase_id from purchases where  date_format(date_time,'%Y-%m-%d')>='$from' && branch_id='$branch' ");
$nid=0;
$total=0;
while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['purchase_id']; 


$sql= mysqli_query($con,"SELECT id, total_price,invoice_number,AddedOn,(select name from suppliers where supplier_id=(select supplier from purchases where purchase_id=purchase_totals.id limit 1)) as supplier,(SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where id='$supid' && branch_id='$branch' order by id desc");


if (mysqli_num_rows($sql) > 0) {
    
                                   ?>

                                        
                                        <?php
                                      
                                        
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];

                $nid++;
                        
                    ?>
                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                  <?php if( $permissions['purchases_return_button'] == 1){
                                               
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                                
                                                
                                            </tr>
                                
                                
                                
                           
               <?php                             
    }
    
} else {
    echo "<tr><td>waaa results</td><td></td><td></td><td></td><td></td></tr>";
    return 0;
    }
}
?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php


}

if(strlen($branch)>0 && strlen($from)>0 && strlen($to)>0){
$supl=mysqli_query($con,"SELECT distinct purchase_id from purchases  where date_format(date_time,'%Y-%m-%d')>='$from' AND date_format(date_time,'%Y-%m-%d')<='$to' && branch_id='$branch'");
$nid=0;
$total=0;
while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['purchase_id']; 


$sql= mysqli_query($con,"SELECT id, total_price,invoice_number, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  name from suppliers where supplier_id=(select supplier from purchases where purchase_id=purchase_totals.id limit 1)) as supplier, (SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where id='$supid' AND DATE_FORMAT(AddedOn, '%Y-%m-%d')>='$from' && branch_id='$branch' order by id desc");


if (mysqli_num_rows($sql) > 0) {
   
                                   ?>

                                        
                                        <?php
                                      
                                        
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];
                $nid++;
                        
                    ?>
                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                               
                                                <?php if( $permissions['purchases_return_button'] == 1){
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                                
                                            </tr>
               
               <?php                             
    }
    
} else {
    echo "<tr><td>No results</td><td></td><td></td><td></td><td></td></tr>";
    return 0;
    }
}
?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php


}


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

$sql= mysqli_query($con,"SELECT id, total_price,invoice_number, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  name from suppliers where supplier_id=(select  distinct supplier from purchases where purchase_id=purchase_totals.id limit 1)) as supplier,(SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where DATE_FORMAT(AddedOn, '%Y-%m-%d')>='$from' && business_id='$business' order by id desc");
if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
                                    <?php
                                        $nid=0;
                                        $total=0;
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];
                $nid++;
                        
                    ?>
                                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                           
                                                <?php if( $permissions['purchases_return_button'] == 1 ){
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                                
                                            </tr>
               <?php                             
    }
    ?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php
} else {
    echo "<tr><td>No results</td><td></td><td></td><td></td><td></td></tr>";
    return 0;
}

}

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

$sql= mysqli_query($con,"SELECT id, total_price,invoice_number, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  name from suppliers where supplier_id=(select  distinct supplier from purchases where purchase_id=purchase_totals.id limit 1)) as supplier,(SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where DATE_FORMAT(AddedOn, '%Y-%m-%d')>='$from' AND DATE_FORMAT(AddedOn, '%Y-%m-%d')<='$to' && business_id='$business' order by id desc");
if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
                                    <?php
                                        $nid=0;
                                        $total=0;
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];
                $nid++;
                        
                    ?>
                                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                           
                                                
                                                <?php if( $permissions['purchases_return_button'] == 1 ){
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                            </tr>
               <?php                             
    }
    ?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php
} else {
    echo "<tr><td>No results</td><td></td><td></td><td></td><td></td></tr>";
    return 0;
}
}

?>
                         




<?php


//}



















?>
                                            
                                       