
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];



include '../includes/connection.php';

//if( $_SESSION['role']=="Super Admin"){
//if( $permissions['view_dashboard_business_summary'] == 1){
if($_SESSION['branch_id']==0){
   $sql = "SELECT (select ISOcode from countries where id=(select Country from business where business_id='$business'))as code,(select count(id) from staff where business_id='$business')as staff_no, (select count(id) from users where business_id='$business') as users_no, (select count(id) from products where quantity>0 && business_id='$business')as instock, (select count(id) from products where quantity=0 && business_id='$business' && status=1) as outstock, (select count(id) from sales where business_id='$business') as tot_orders, (select count(id) from sales where total_price=paid && business_id='$business')as unpaid, count(id)as total_pur, (select count(id) from purchase_totals where total_price=paid && business_id='$business')as un_cleared, (select sum(quantity*selling_price) from stock where business_id='$business') as stock_value, (select sum(total_price-paid) from purchase_totals where total_price>paid && business_id='$business')as total_credits FROM purchase_totals where business_id='$business'"; 
}else{
$sql = "SELECT (select ISOcode from countries where id=(select Country from business where business_id='$business'))as code,(select count(id) from staff where branch_id='$branch')as staff_no, (select count(id) from users where branch_id='$branch') as users_no, (select count(id) from products where quantity>0 && branch_id='$branch')as instock, (select count(id) from products where quantity=0 && branch_id='$branch' && status=1) as outstock, (select count(id) from sales where branch_id='$branch') as tot_orders, (select count(id) from sales where total_price=paid && branch_id='$branch')as unpaid, count(id)as total_pur, (select count(id) from purchase_totals where total_price=paid && branch_id='$branch')as un_cleared, (select sum(quantity*selling_price) from stock where branch_id='$branch') as stock_value, (select sum(total_price-paid) from purchase_totals where total_price>paid && branch_id='$branch')as total_credits FROM purchase_totals where branch_id='$branch'";
}
                                   

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

if (mysqli_num_rows($result) > 0) {
 $row = mysqli_fetch_assoc($result);
       $sys_users=$row['users_no'];
       $sys_staff=$row['staff_no'];
       $instock=$row['instock'];
       $outstock=$row['outstock'];

       $order_sum=$row['tot_orders'];
       $un_paid_orders=$row['unpaid'];

       $percent_customer=($un_paid_orders/$order_sum)*100;

       $purchase_totals=$row['total_pur'];
       $un_cleared=$row['un_cleared'];

       $percent_supplier=($un_cleared/$purchase_totals)*100;

       $stock_value=$row['stock_value'];
       $total_credits=$row['total_credits'];
        $code = $row['code'];
    
} else {
    echo "0 results";
}
$today=date('Y-m-d');
//if( $_SESSION['role']=="Super Admin"){
if( $_SESSION['branch_id'] == 0){
//if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$daily=mysqli_fetch_array(mysqli_query($con,"SELECT sum(total_price) as sold,sum(paid) as paid from sales where EXISTS(SELECT * from sales_details where sales_details.order_id=sales.id) && date_format(date_time,'%Y-%m-%d')='$today' && business_id='$business'"));
$dailyexpense=mysqli_fetch_array(mysqli_query($con,"SELECT sum(Amount) as spent from expenses where date='$today' && business_id='$business''"));
$dailydebtpayment=mysqli_fetch_array(mysqli_query($con,"SELECT sum(amount_paid) as debtspaid from payment_logs where(NOT EXISTS (SELECT * from sales where id=payment_logs.order_id  && business_id='$business' && date_format(date_time,'%Y-%m-%d')='$today')) && date_format(date_time,'%Y-%m-%d')='$today'  && business_id='$business' && category='credit'"));
    
}else{
$daily=mysqli_fetch_array(mysqli_query($con,"SELECT sum(total_price) as sold,sum(paid) as paid from sales where EXISTS(SELECT * from sales_details where sales_details.order_id=sales.id) && date_format(date_time,'%Y-%m-%d')='$today' && branch_id='$branch'"));
$dailyexpense=mysqli_fetch_array(mysqli_query($con,"SELECT sum(Amount) as spent from expenses where date='$today' && branch_id='$branch'"));
    $dailydebtpayment=mysqli_fetch_array(mysqli_query($con,"SELECT sum(amount_paid) as debtspaid from payment_logs where(NOT EXISTS (SELECT * from sales where id=payment_logs.order_id && branch_id='$branch' && business_id='$business' && date_format(date_time,'%Y-%m-%d')='$today')) && date_format(date_time,'%Y-%m-%d')='$today' && branch_id='$branch' && business_id='$business' && category='credit'"));
}

$totalSales=$daily['sold'];
$totalpaid=$daily['paid'];
$debtcollected=$dailydebtpayment['debtspaid'];

$totalspent=$dailyexpense['spent'];
 
?>

<!DOCTYPE html>

<html class="no-js">
    
    <head>
 
        <script src="../canvasjs.min.js"></script>
        <script src="../apexcharts.js"></script>
        <link rel="stylesheet" type="text/css" href="../barIndicator.css">
      
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>


<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
  
        <script>
window.onload = function () {
 
var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    title:{
        text: "Company Revenue by Month"
    },
    axisY: {
        title: "Revenue in UGX",
        valueFormatString: "#0,,."
        
    },
    data: [{
        type: "spline",
        markerSize: 5,
        xValueFormatString: "YYYY",
        yValueFormatString: "$#,##0.##",
        xValueType: "dateTime",
        dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
    }]
});
 
chart.render();

 }
</script>
<style type="text/css">
    
        .container {
            margin: 0 auto;
            text-align: center;
        }

        .gauge_container {
            text-align: left;
            height: 450px;
            display: inline-block;
            border: 1px solid #ccc;
            margin: 40px 5px 0 5px;
        }

        .gauge {
            width: 300px;
            height: 200px;
            display: inline-block;
            -webkit-transform: translate3d(0, 0, 0);
        }

        .controls {
            text-align: left;
        }

       

        label {
            font-family: Arial;
            display: inline-block;
            width: 65px;
            margin: 0 0 5px 0;
            text-align: right;
            padding: 5px;
            color: #919191;
        }

        input {
            font-weight: bold;
            font-size: 13px;
            padding: 10px;
            border: 1px solid #ccc;
            margin: 5px;
        }

        input[disabled=disabled] {
            font-weight: normal;
            font-size: 11px;
            padding: 0 0 0 10px;
            margin: 0px;
            color: #777777;
            border-color: transparent;
            background: #fff;
        }


</style>
    <?php include("../includes/header-links.php");
$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'"));

if($permissions['view_dashboard'] != 1){
?>
<script>window.location="../sales/new_order.php"</script>
<?php
}
?>

<script src="../myjs/raphael.min.js"></script>
    <script src="../myjs/justgage.js"></script>
    <script>
        /** Random integer  */
        function getRandomInt(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        document.addEventListener("DOMContentLoaded", function (event) {
            var gg1 = new JustGage({
                id: "gg1",
                value: <?php echo $percent_customer; ?>,
                min: 0,
                max: 100,
                title: "Target",
                label: "Percentage (<?php echo round($percent_customer); ?> %)",
                pointer: true,
                textRenderer: function (val) {
                    if (val < 50) {
                        return 'Debtors';
                    } else if (val > 50) {
                        return 'Debtors';
                    } else if (val === 50) {
                        return 'Debtors';
                    }
                },
                onAnimationEnd: function () {
                    console.log('f: onAnimationEnd()');
                }
            });


        });
    </script>
    <script>
        /** Random integer  */
        function getRandomInt(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        document.addEventListener("DOMContentLoaded", function (event) {
            var gg1 = new JustGage({
                id: "gg2",
                value: <?php echo $percent_supplier; ?>,
                min: 0,
                max: 100,
                title: "Target",
                label: "Percentage (<?php echo round($percent_supplier); ?> %)",
                pointer: true,
                textRenderer: function (val) {
                    if (val < 50) {
                        return 'Creditors';
                    } else if (val > 50) {
                        return 'Creditors';
                    } else if (val === 50) {
                        return 'Creditors';
                    }
                },
                onAnimationEnd: function () {
                    console.log('f: onAnimationEnd()');
                }
            });


        });
    </script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php';?>
                
                <!--/span-->
                <?php
                 //if( $_SESSION['role']!=="General Admin"){
                if( $permissions['view_dashboard'] == 1){
                //if($permissions['business_id'] == 0 && $permissions['branch_id']==0){
                ?>
                <div class="span10" id="content">
                    <div class="row-fluid">
                        
                        	<div class="navbar">
                            <?php
                            //if( $_SESSION['role']!="Super Admin"){ 
                            if( $permissions['view_dashboard_creditors'] == 1){
                            //if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                ?>
                            	<div class="navbar-inner">
                                <div class="pull-right">
                                    <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dashboard_new_orders'] == 1){

                        
                            ?>
                                       <a href="../sales/new_order.php"> <button class="btn btn-primary"><i class="icon-shopping-cart icon-white"></i> New Order</button></a>

                                       <?php
                         }
                     //     
                             ?>
                              <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dashboard_managment_stock'] == 1){

                        
                            ?>
                                      <a href="../stock/stock.php">  <button class="btn btn-warning"><i class="icon-list-alt icon-white"></i> Manage Stock</button></a>

                                       <?php
                         }
                     //     
                             ?>
                              <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_mahange_expense'] == 1){

                        
                            ?>
                                      <a href="../expenses/expenses.php">  <button class="btn btn-success"><i class="fa fa-money"></i> Manage Expenses</button></a>
                                        <?php
                         }
                     //     
                             ?>
                                      
                                    </div>
	                                <ul class="breadcrumb">
	                                    <i class="icon-chevron-left hide-sidebar"><a href='#' title="Hide Sidebar" rel='tooltip'>&nbsp;</a></i>
	                                    <i class="icon-chevron-right show-sidebar" style="display:none;"><a href='#' title="Show Sidebar" rel='tooltip'>&nbsp;</a></i>
	                                    <li>
	                                        <a href="#">Dashboard</a> <span class="divider">/</span>	
	                                    </li>
	                                    
	                                </ul>

                            	</div>
                                <?php
                            }
                                ?>
                        	</div>
                    	</div>
                        <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left"><i class="fa fa-bar-chart"></i> Statistics</div>
                                <div class="pull-right">

                                </div>
                            </div>
                            <div class="block-content collapse in">
                               
                            <!--first section begins-->
                            <?php 
                        if( $permissions['view_daily_summary'] == 1){
                            ?>
                              <div class="span6">
                              <h3 class="text-center">Daily summary</h3>
                               <?php 
                        if( $permissions['total_sales'] == 1){
                            ?>


                                    <div style="height: 65px; width: 100%; box-shadow: 2px 2px 4px #000000;" class="row-fluid">
                                        <div class="span5" style="background: #18ad07; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../images/sales.png" style="width: 70px; height: 70px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Total sales </p>
                                                </div>
                                            </div>

                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4> <?php echo$code;?> <?php echo number_format($totalSales); ?> </h4>
                                            
                                </div>
                                 <?php 
                        if( $permissions['total_sales_details'] == 1){
                            ?>
                                <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;">
                                    <?php
                                        if ($totalSales>0) {
                                        ?>
                                    <a href="../reports/sales_today.php"><span class="badge badge-warning">View Details</span></a>
                                    <?php
                                      }
                                        ?>
                                </div>
                                        </div>
                                        <?php
                                      }
                                        ?>
                                    </div>
                                    <?php
                                      }
                                        ?>

                                                <?php 
                        if( $permissions['credit_sales'] == 1){
                            ?>

                                    <div style="height: 50px; width: 100%; box-shadow: 2px 2px 4px #000000; margin-top: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #a232a8; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../images/debtors.png" style="width: 70px; height: 50px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Credit Sales</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4><?php echo$code;?> <?php echo number_format($totalSales-$totalpaid); ?></h4>
                                        </div>
                                        <?php 
                        if( $permissions['credit_sales_details'] == 1){
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;">
                                    <?php
                                        if (($totalSales-$totalpaid)>0) {
                                        ?>
                                    <a href="../reports/sales_today.php"><span class="badge badge-warning">View Details</span></a>
                                    <?php
                                       }
                                        ?>
                                </div>
                                        </div>
                                         <?php
                                      }
                                        ?>
                                    </div>
                                     <?php
                                      }
                                        ?>

                                    <?php 
                        if( $permissions['credits_paid'] == 1){
                            ?>
                                    <div style="height: 50px; width: 100%; box-shadow: 2px 2px 4px #000000; margin-top: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #ed07a4; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../images/debtors.png" style="width: 70px; height: 50px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Credits paid</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4><?php echo$code;?> <?php echo number_format($debtcollected); ?></h4>
                                        </div>
                                        <?php 
                        if( $permissions['credits_paid_details'] == 1){
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;">
                                    <?php
                                        if (($debtcollected)>0) {
                                        ?>
                                    <a href="../reports/debtspaid_today.php"><span class="badge badge-warning">View Details</span></a>
                                    <?php
                                       }
                                        ?>
                                </div>
                                </div>
                                <?php
                                       }
                                        ?>
                                        
                                    </div>
                                    <?php
                                       }
                                        ?>
                                             <?php 
                        if( $permissions['expenses'] == 1){
                            ?>

                                    <div style="height: 50px; width: 100%; box-shadow: 2px 2px 4px #000000;  margin-top: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #d9b309; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../images/expenses.png" style="width: 70px; height: 50px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Expenses</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4"  style="padding-top: 20px;">
                                            <h4><?php echo$code;?> <?php echo number_format($totalspent); ?> </h4>
                                        </div>
                                             <?php 
                        if( $permissions['dash_expense_details'] == 1){
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;">
                                    <?php
                                        if ($totalspent>0) {
                                        ?>
                                    <a href="../reports/expenses_today.php"><span class="badge badge-warning">View Details</span></a>
                                    <?php
                                        }
                                        ?>
                                </div>
                                        </div>
                                        <?php
                                        }
                                        ?>
                                    </div>
                                    <?php
                                        }
                                        ?>
                                    <?php 
                        if( $permissions['dash_cash_at_hand'] == 1){
                            ?>
                                    <div style="height: 65px; width: 100%; box-shadow: 2px 2px 4px #000000;  margin-top: 10px;margin-bottom: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #08bda5; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span4">
                                                    <img src="../images/cash.png" style="width: 70px; height: 65px;">
                                                </div>
                                                <div class="span8">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Cash At hand</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4><?php echo$code;?> <?php echo number_format($totalpaid+$debtcollected-$totalspent); ?> </h4>
                                        </div>
                                        <?php 
                        if( $permissions['dash_cash_at_hand_details'] == 1){
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;">
                                    <?php
                                        if ($totalpaid>0) {
                                        ?>
                                    <a href="../reports/sales_today.php"><span class="badge badge-warning">View Details</span></a>
                                    <?php
                                }
                                    ?>
                                </div>
                                        </div>
                                        <?php
                                        }
                                        ?>
                                    </div>
                                    <?php
                                        }
                                        ?>
                                    
                                    </div>
                                    <!--enfirst section ends and second begins -->
                                     <?php
                         }
                     //     
                             ?>



<?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['view_general_summary'] == 1){

                        
                            ?>

                                
                                <div class="span6">
                                <h3 class="text-center">General Summary</h3>
                                   <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['view_stock_value_details'] == 1){

                        
                            ?>
                                    <div style="height: 70px; width: 100%; box-shadow: 2px 2px 4px #000000;" class="row-fluid">
                                        <div class="span5" style="background: #FA833A; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../includes/stockvalue.png" style="width: 70px; height: 70px;">
                                                </div>
                                               
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Stock Value</p>
                                                </div>
                               
                                            </div>

                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4> <?php echo$code;?> <?php echo number_format($stock_value); ?> </h4>
                                            
                                </div>
                                <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['view_stock_value_details'] == 1){

                        
                            ?>
                                <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;"><a href="../stock/stockvalue.php"><span class="badge badge-warning">View Details</span></a>
                                </div>
                                        </div>
                                         <?php
                         }
                     
                             ?>
                                    </div>
                                                <?php
                         }
                     
                             ?>
                                 <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_creditors'] == 1){

                        
                            ?>

                                    <div style="height: 70px; width: 100%; box-shadow: 2px 2px 4px #000000; margin-top: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #FAC93A; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../includes/debtots.png" style="width: 70px; height: 70px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Creditors</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4><?php echo$code;?> <?php echo number_format($total_credits); ?></h4>
                                        </div>
                                         <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['creditors_details'] == 1){

                        
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;"><a href="../purchases/creditors.php"><span class="badge badge-warning">View Details</span></a>
                                </div>
                                        </div>
                                           <?php
                         }
                     
                             ?>
                                    </div>
                                    <?php
                         }
                     
                             ?>
                              <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_instock'] == 1){

                        
                            ?>

                                    <div style="height: 70px; width: 100%; box-shadow: 2px 2px 4px #000000;  margin-top: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #42D812; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span5">
                                                    <img src="../includes/stock.png" style="width: 70px; height: 70px;">
                                                </div>
                                                <div class="span7">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">In stock</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4"  style="padding-top: 20px;">
                                            <h4><?php echo $instock; ?> Products</h4>
                                        </div>
                                         <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_instock_details'] == 1){

                        
                            ?>

                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;"><a href="../stock/in_stock.php"><span class="badge badge-warning">View Details</span></a>
                                </div>
                                        </div>
                                                <?php
                         }
                     
                             ?>
                                    </div>
                                          <?php
                         }
                     
                             ?> <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_out_of_stock'] == 1){

                        
                            ?>
                                    <div style="height: 70px; width: 100%; box-shadow: 2px 2px 4px #000000;  margin-top: 10px;margin-bottom: 10px;" class="row-fluid">
                                        <div class="span5" style="background: #125DD8; height: 100%">
                                            <div class="row-fluid">
                                                <div class="span4">
                                                    <img src="../includes/outstock.png" style="width: 70px; height: 70px;">
                                                </div>
                                                <div class="span8">
                                                    <p class="pull-left" style="color: white; font-size: 20px;
                                                    position: center; height: 100%; margin-top: 20px">Out of stock</p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="span4" style="padding-top: 20px;">
                                            <h4><?php echo $outstock; ?> Products</h4>
                                        </div>
                                         <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['dash_out_of_stock_details'] == 1){

                        
                            ?>
                                        <div class="span3">
                                    <div class="pull-right" style="margin-top: 8px;"><a href="../stock/outof_stock.php"><span class="badge badge-warning">View Details</span></a>
                                </div>
                                        </div>
                                        <?php
                         }
                     
                             ?>
                                    </div>
                                      <?php
                         }
                     //     
                             ?>
                                    
                                    </div>
                                               <?php
                         }
                     //     
                             ?>
                                </div>
                                <hr>&nbsp;
                                <hr>
                                <div class="row-fluid">
                            <!--<div class="span2"></div>-->
                            <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['most_demanded_prod_graph'] == 1){

                        
                            ?>
                        <div class="span5 chart">
                                    <h3 class="text-center"  style="margin-top: 0px;">Most Demanded Product</h3>
                                    <div id="hero-bar" style="height: 300px;"></div>
                                </div>
                                 <?php
                         }
                     //     
                             ?>
                           <!--     <div class="span2"></div>
                            
                         </div>
                        <hr>
                        <div class="row-fluid">
                        <div class="span2"></div>-->
                        <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['sales_summary_graph'] == 1){

                        
                            ?>
                        <div class="span7 chart">
                                    <h3 class="text-center"  style="margin-top: 0px;">Sales Summary <?php echo date("Y"); ?></h3>
                                    <div id="chart">
                                </div>
                                 <?php
                         }
                     //     
                             ?>
                            
                        </div>
                        <div class="span2"></div>
                            </div>
                        </div>
                        
                        <!-- /block -->
                    </div>
                    
                </div>
                <?php
            }
                ?>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->
        <script src="../vendors/jquery-1.9.1.min.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="../vendors/morris/morris.css">


        <script src="../vendors/jquery.knob.js"></script>
        <script src="../vendors/raphael-min.js"></script>
        <script src="../vendors/morris/morris.min.js"></script>

       
        <script src="../vendors/flot/jquery.flot.js"></script>
        <script src="../vendors/flot/jquery.flot.categories.js"></script>
        <script src="../vendors/flot/jquery.flot.pie.js"></script>
        <script src="../vendors/flot/jquery.flot.time.js"></script>
        <script src="../vendors/flot/jquery.flot.stack.js"></script>
        <script src="../vendors/flot/jquery.flot.resize.js"></script>

        <script src="../assets/scripts.js"></script>
        
        <script src="../assets/scripts.js"></script>
        <script type="text/javascript">
            var options = {
  chart: {
    height: 250,
    type: "line",
    stacked: false
  },
  dataLabels: {
    enabled: false
  },
  colors: ["#FF1654", "#247BA0"],
  series: [
    {
      name: "Series A",
      data: [1.4, 2, 2.5, 1.5, 2.5, 2.8, 3.8, 4.6]
    },
    {
      name: "Series B",
      data: [20, 29, 37, 36, 44, 45, 50, 58]
    }
  ],
  stroke: {
    width: [4, 4]
  },
  plotOptions: {
    bar: {
      columnWidth: "20%"
    }
  },
  xaxis: {
    categories: ["Jan", "Feb", "Mar", "Apr", "May",, "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  },
  yaxis: [
    {
      axisTicks: {
        show: true
      },
      axisBorder: {
        show: true,
        color: "#FF1654"
      },
      labels: {
        style: {
          color: "#FF1654"
        }
      },
      title: {
        text: "Series A"
      }
    },
    {
      opposite: true,
      axisTicks: {
        show: true
      },
      axisBorder: {
        show: true,
        color: "#247BA0"
      },
      labels: {
        style: {
          color: "#247BA0"
        }
      },
      title: {
        text: "Series B"
      }
    }
  ],
  tooltip: {
    shared: false,
    intersect: true,
    x: {
      show: false
    }
  },
  legend: {
    horizontalAlign: "left",
    offsetX: 40
  }
};

var chart = new ApexCharts(document.querySelector("#chart2"), options);

chart.render();

        </script>
<?php
$mdate=date("Y");
//if( $_SESSION['role']=="Super Admin"){ 
//if( $permissions['sales_summary_graph'] == 1){
if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$sql = "SELECT DATE_FORMAT(date_time, '%m')as month, min(DATE_FORMAT(date_time, '%m'))as minmonth, sum(quantity*selling_price)as sales FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && business_id='$business' group by DATE_FORMAT(date_time, '%m')";
$sql2="SELECT min(DATE_FORMAT(date_time, '%m'))as minmonth FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && business_id='$business' group by DATE_FORMAT(date_time, '%m')";
}else{
 $sql = "SELECT DATE_FORMAT(date_time, '%m')as month, min(DATE_FORMAT(date_time, '%m'))as minmonth, sum(quantity*selling_price)as sales FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && branch_id='$branch' group by DATE_FORMAT(date_time, '%m')";
$sql2="SELECT min(DATE_FORMAT(date_time, '%m'))as minmonth FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && branch_id='$branch' group by DATE_FORMAT(date_time, '%m')";    
}
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);

if (mysqli_num_rows($result) > 0) {
    $arr=array();
   // $arrmonths=array();
    $rowx = mysqli_fetch_assoc($result2);
    $minmonth=$rowx['minmonth'];
    for($mon=0; $mon<=$minmonth-1; $mon++){
        $arr[$mon]=0;
        $aa=$mon;
    }



    
   $nn=$aa;    
    while($row = mysqli_fetch_assoc($result)) {
        $arr[$nn]=$row['sales'];
        
        //$arrmonths[$mon]=$row['month'];
        $nn++;
        //$mon++;

    }
    //$arr=array();
   implode(", ",$arr);
} else {
    echo "0 results";
}
?>         
        <script type="text/javascript">
            var options = {
  chart: {
    type: 'line'
  },
  series: [{
    name: 'sales',
    data: <?php echo "[".implode(", ",$arr)."]"; ?>
  }],
  xaxis: {
    categories: ["Jan","Feb","Mar","Apr","May","Jun","Jul", "Aug","Sept", "Oct", "Nov", "Dec"]
  }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();
        </script>
        <script>
        $(function() {
            // Easy pie charts
            $('.chart').easyPieChart({animate: 1000});
        });
        </script>
        <script>
        $(function() {
            var data = [ ["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9] ];

            $.plot("#catchart", [ data ], {
                series: {
                    bars: {
                        show: true,
                        barWidth: 0.6,
                        align: "center"
                    }
                },
                xaxis: {
                    mode: "categories",
                    tickLength: 0
                }
            });

            var data = [],
            series = Math.floor(Math.random() * 6) + 3;

            for (var i = 0; i < series; i++) {
                data[i] = {
                    label: "Series" + (i + 1),
                    data: Math.floor(Math.random() * 100) + 1
                }
            }

            $.plot('#piechart1', data, {
                series: {
                    pie: { 
                        show: true,
                        radius: 1,
                        label: {
                            show: true,
                            radius: 3/4,
                            formatter: labelFormatter,
                            background: { 
                                opacity: 0.5,
                                color: '#000'
                            }
                        }
                    }
                },
                legend: {
                    show: false
                }
            });

            $.plot('#piechart2', data, {
                series: {
                    pie: {
                        show: true,
                        radius: 1,
                        tilt: 0.5,
                        label: {
                            show: true,
                            radius: 1,
                            formatter: labelFormatter,
                            background: {
                                opacity: 0.8
                            }
                        },
                        combine: {
                            color: '#999',
                            threshold: 0.1
                        }
                    }
                },
                legend: {
                    show: false
                }
            });


            

        function euroFormatter(v, axis) {
            return v.toFixed(axis.tickDecimals) + "€";
        }

        function doPlot(position) {
            $.plot("#timechart", [
                { data: oilprices, label: "Oil price ($)" },
                { data: exchangerates, label: "USD/EUR exchange rate", yaxis: 2 }
            ], {
                xaxes: [ { mode: "time" } ],
                yaxes: [ { min: 0 }, {
                    // align if we are to the right
                    alignTicksWithAxis: position == "right" ? 1 : null,
                    position: position,
                    tickFormatter: euroFormatter
                } ],
                legend: { position: "sw" }
            });
        }

        doPlot("right");

        });

        // Morris Bar Chart
        Morris.Bar({
            element: 'hero-bar',
            data: [
<?php
//if( $_SESSION['role']=="Super Admin"){
if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$sql = "SELECT (select product_name from products where id=sales_details.product_id && business_id='$business')as product_name, count(id) as quantity, (select price from products where id=sales_details.product_id && business_id='$business')as price FROM sales_details where business_id='$business' group by product_id limit 4";

}else{
$sql = "SELECT (select product_name from products where id=sales_details.product_id && branch_id='$branch')as product_name, count(id) as quantity, (select price from products where id=sales_details.product_id && branch_id='$branch')as price FROM sales_details where branch_id='$branch' group by product_id limit 4";
}

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    
    while($row = $result->fetch_assoc()) {
        $sold=$row['quantity'];
        ?>{product: '<?php echo $row['product_name']; ?>', Sales: <?php echo $sold; ?>},<?php
    }
} else {
    echo "0 results";
}
?>

            ],
            xkey: 'product',
            ykeys: ['Sales'],
            labels: ['Sales'],
            barRatio: 0.4,
            barWidth: 10,
            xLabelMargin: 20,
            hideHover: 'auto',
            barColors: ["#3d88ba"]
        });


        // Morris Donut Chart
        Morris.Donut({
            element: 'hero-donut',
            data: [
                {label: 'Direct', value: 25 },
                {label: 'Referrals', value: 40 },
                {label: 'Search engines', value: 25 },
                {label: 'Unique visitors', value: 10 }
            ],
            colors: ["#30a1ec", "#76bdee", "#c4dafe"],
            formatter: function (y) { return y + "%" }
        });


        // Morris Line Chart
        var tax_data = [
            {"period": "2013-04", "visits": 2407, "signups": 660},
            {"period": "2013-03", "visits": 3351, "signups": 729},
            {"period": "2013-02", "visits": 2469, "signups": 1318},
            {"period": "2013-01", "visits": 2246, "signups": 461},
            {"period": "2012-12", "visits": 3171, "signups": 1676},
            {"period": "2012-11", "visits": 2155, "signups": 681},
            {"period": "2012-10", "visits": 1226, "signups": 620},
            {"period": "2012-09", "visits": 2245, "signups": 500}
        ];
        Morris.Line({
            element: 'hero-graph',
            data: tax_data,
            xkey: 'period',
            xLabels: "month",
            ykeys: ['visits', 'signups'],
            labels: ['Visits', 'User signups']
        });



        // Morris Area Chart
        Morris.Area({
            element: 'hero-area',
            data: [
                {period: '2010 Q1', iphone: 2666, ipad: null, itouch: 2647},
                {period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441},
                {period: '2010 Q3', iphone: 4912, ipad: 1969, itouch: 2501},
                {period: '2010 Q4', iphone: 3767, ipad: 3597, itouch: 5689},
                {period: '2011 Q1', iphone: 6810, ipad: 1914, itouch: 2293},
                {period: '2011 Q2', iphone: 5670, ipad: 4293, itouch: 1881},
                {period: '2011 Q3', iphone: 4820, ipad: 3795, itouch: 1588},
                {period: '2011 Q4', iphone: 15073, ipad: 5967, itouch: 5175},
                {period: '2012 Q1', iphone: 10687, ipad: 4460, itouch: 2028},
                {period: '2012 Q2', iphone: 8432, ipad: 5713, itouch: 1791}
            ],
            xkey: 'period',
            ykeys: ['iphone', 'ipad', 'itouch'],
            labels: ['iPhone', 'iPad', 'iPod Touch'],
            lineWidth: 2,
            hideHover: 'auto',
            lineColors: ["#81d5d9", "#a6e182", "#67bdf8"]
          });



        // Build jQuery Knobs
        $(".knob").knob();

        function labelFormatter(label, series) {
            return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
        }
        </script>
    </body>

</html>