<!DOCTYPE html>
<html>
    
    <head>
        <title>Sales Statistics</title>
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
            <script src="../mycharts/Chart.min.js"></script>
    <script src="../mycharts/samples/utils.js"></script>
    <style>
    canvas{
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
    }
    </style>
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.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_sales_analysis_by_Graph'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>

        ?>


        <div class="container-fluid">
            <div class="row-fluid">
                 <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">
                      <!-- morris stacked chart -->
                    <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="span12">
                                <div class="muted pull-left" class="span4">Monthly Sales Analysis</div>

                                          <?php
                        if( $permissions['filter_sales_analysis'] == 1){
                            ?>
                                <div align="right" class="span8"><a href="salescash.php"><button class="btn btn-info"><i class="icon-list-alt icon-white"> </i>Filter</button></a></div>
                                <?php
                         }
                         ?>


                                </div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                <form method="POST" action="">
                                    <label>Year</label>
                                            <input type="year" value="<?php echo date('Y');?>"  name="year" class="form-control"   required="required" />
                                            
                                            <button type="submit" name="filter" class="btn btn-info"><li class="fa fa-filter">filter</li></button>
                                </form>
                                    <div style="height: 50%;">
                                        <canvas id="canvas"></canvas>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>

    



                   

                  


                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>

<?php
if (isset($_POST['filter'])) {
$mdate=$_POST['year'];
    
}else{
$mdate=date("Y");

}
if( $_SESSION['role']=="Super Admin"){ 
$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";
}
?>    


        <!--/.fluid-container-->
        <script>
        var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        var config = {
            type: 'line',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July','August','September', 'October', 'November', 'December'],
                datasets: [{
                    label: 'Total Sales',
                    backgroundColor: window.chartColors.blue,
                    borderColor: window.chartColors.blue,
                    data: <?php echo "[".implode(", ",$arr)."]"; ?>,
                    fill: false,
                }]
            },
            options: {
                responsive: true,
                title: {
                    display: true,
                    text: 'Sales Curve For <?php echo "$mdate";?>'
                },
                tooltips: {
                    mode: 'index',
                    intersect: false,
                },
                hover: {
                    mode: 'nearest',
                    intersect: true
                },
                scales: {
                    xAxes: [{
                        display: true,
                        scaleLabel: {
                            display: true,
                            labelString: 'Month'
                        }
                    }],
                    yAxes: [{
                        display: true,
                        scaleLabel: {
                            display: true,
                            labelString: 'Sales (UGX)'
                        }
                    }]
                }
            }
        };

        window.onload = function() {
            var ctx = document.getElementById('canvas').getContext('2d');
            window.myLine = new Chart(ctx, config);
        };

        

      
    </script>
        <link rel="stylesheet" href="../vendors/morris/morris.css">


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

        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/flot/jquery.flot.js"></script>
        <script src="..//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>
        
    </body>

</html>