
<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
?>

<style>
    .table tr td, th{
        font-size:14px;
    }
    .space-after {
        padding-bottom: 125px;
    }
</style>

<div class="wrapper">
    <div class="container">
        <div class="row m-t-20  hidden-print">
            <div class="col-md-12 hidden-print">
                <div class="panel panel-white m-t-20">
                    <div class="panel-body" >
                        <form action="<?php echo base_url();?>staff_performance" method="post">
                            <div class="row">
                                <div class="col-md-2">
                                    <label class="form-label">Start Date</label>
                                </div>    
                                <div class="col-md-2">
                                    <label class="form-label">End Date</label>
                                </div>    
                                <div class="col-md-2">
                                    <label class="form-label">Staff</label>
                                </div>    
                                <div class="col-md-2">
                                    <label class="form-label">Action</label>
                                </div>    
                                <div class="col-md-2"></div>
                                <div class="col-md-2"></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">
                                    <input value="<?php echo $start_date;?>" class="form-control" id="start_date" name="start_date">
                                </div>    
                                <div class="col-md-2">
                                    <input value="<?php echo $end_date;?>" class="form-control" id="end_date" name="end_date">
                                </div>    
                                <div class="col-md-2">
                                    <select class="form-control" id="staff_id" name="staff_id">
                                        <?php foreach($staff_list as $staff){?>
                                        <option <?php if($staff['id_staff']==$staff_id){echo 'selected="selected"';}?> value="<?php echo $staff['id_staff'];?>"><?php echo $staff['staff_fullname'];?></option>
                                        <?php } ?>
                                    </select>
                                </div>    
                                <div class="col-md-2">
                                    <button class="btn btn-md btn-primary" >Run</button>
                                </div>
                                <div class="col-md-2">
                                    <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light" id="btnprint" ><i class="fa fa-print"></i></a>
                                </div>
                                <div class="col-md-2"></div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>    
                
        <div class="row m-t-20" style="page-break-after: always;">
            <div class="col-md-12">            
                <div class="panel panel-white m-t-20" >
                    <div class="panel-body" >
                        <div class="row" style="margin: auto 45px auto 45px !important;">
                            <div class="col-md-12">
                                <table  class="table-box">
                                    <thead>
                                        <tr >

                                            <th colspan="4" style="text-align: center;">
                                                <h3 style="text-align: center;"><?php
                                                if (isset($business)) {
                                                    echo "<img style='' src='" . base_url() . 'assets/images/business/' . $business[0]['business_logo'] . "' alt='" . $business[0]['business_name'] . "' class='' />";
                                                } else {
                                                    echo 'SkedWise';
                                                }
                                                ?></h3>
                                            </th>

                                        </tr>
                                        <tr>
                                            <th colspan="4" style="text-align: center;">
                                                <h1 style="text-align: center; font-weight:bolder;">Staff Performance Report</h1>
                                            </th>
                                        </tr>
                                        <tr>
                                            <th class="space-after" colspan="4" style="text-align: center;">
                                                <h3 style="text-align: center;"><?php echo 'For the period of '. date_format(date_create($start_date),'j M Y') .' - ' . date_format(date_create($end_date),'j M Y'); ?></h3>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($staffs as $staff){?>
                                        <tr>
                                            <td><h3>Name<h3></td>
                                            <td><h3>: <?php echo $staff['staff_fullname'] ?></h3></td>
                                            <td></td>
                                            <td  rowspan="3" style="text-align: right;">                                                
                                                <img width="120px;" src="<?php echo base_url() . 'assets/images/staff/'; ?><?php echo $staff['staff_image'] ? $staff['staff_image'] : "nu.png"; ?>" alt="<?php echo $staff['staff_fullname'] ?>"/>
                                                
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><h3>Joining Date</h3></td>
                                            <td><h3>: <?php if(null!=$staff['staff_date_of_joining']){ echo date_format(date_create($staff['staff_date_of_joining']),"d/m/Y");} ?></h3></td>
                                            <td ></td>
                                            <!--coverd by above row-->
                                        </tr>
                                        <?php } ?>
                                        <tr><td colspan="4" class="space-after"></tr>
                                        <tr id="service_sales_heading"> 
                                            <td  colspan="4"><h2><strong>Service Sales</strong></h2></td>
                                        </tr>
                                        <tr>
                                            <td colspan="4">
                                                <table class="table table-striped" id="tbl_services">
                                                    <thead>
                                                        <tr>
                                                            <th>Service Category</th>
                                                            <th style="text-align: right">Count</th>
                                                            <th style="text-align: right">Amount</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php $service_count=0; $service_amount=0;
                                                        foreach($staff_services as $staff_service){?>
                                                        <tr>
                                                            <td ><?php echo $staff_service['service_name'] ;?></td>
                                                            <td style="text-align: right"><?php echo $staff_service['service_count'] ; $service_count = $service_count +$staff_service['service_count'] ;?></td>
                                                            <td style="text-align: right"><?php echo $staff_service['price_sum'] ; $service_amount = $service_amount + $staff_service['price_sum'] ;?></td>
                                                        </tr>
                                                        <?php } ?>
                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th></th>
                                                            <th style="text-align: right"><?php echo $service_count ;?></th>
                                                            <th style="text-align: right"><?php echo $service_amount;?></th>
                                                        </tr>
                                                    <tfoot>
                                                </table>
                                                
                                            </td>                                        
                                        </tr>
                                        <tr>
                                            <td colspan="4" class="space-after"></td>
                                        </tr>
                                        <tr>
                                            <td><h3>Number of services sold</h3></td>
                                            <td><h3><?php echo $service_count ;?></h3></td>
                                            <td><h3>Total amount of services sold</h3></td>
                                            <td><h3><?php echo number_format($service_amount,2);?></h3></td>
                                        </tr>                                        
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        
        <div class="row m-t-20" style="page-break-after: always;">
            <div class="col-md-12">            
                <div class="panel panel-white m-t-20">
                    <div class="panel-body" >
                        <div class="row" style="margin: auto 45px auto 45px !important;">
                            <div class="col-md-12">
                                <table id="tbl_servicecharts" class="table-box">
                                    <thead>
                                        <tr >

                                            <th colspan="4" style="text-align: center;">
                                                <h3 style="text-align: center;"><?php
                                                if (isset($business)) {
                                                    echo "<img style='' src='" . base_url() . 'assets/images/business/' . $business[0]['business_logo'] . "' alt='" . $business[0]['business_name'] . "' class='' />";
                                                } else {
                                                    echo 'SkedWise';
                                                }
                                                ?></h3>
                                            </th>

                                        </tr>
                                        
                                        <tr>
                                            <th class="space-after" colspan="4" style="text-align: center;">
                                                <h2 style="text-align: center;"><strong>Staff Performance Report</strong></h2>

                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td colspan="4">
                                                <div id="grossing_services" style="height: 400px;">
                                                    
                                                </div>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="4" class="space-after"></td>
                                        </tr>
                                        <tr>
                                            <td style="width:90%" colspan="3">
                                                <div id="service_sales" style="height: 400px;">
                                                    
                                                </div>
                                            </td>
                                            <td style="width:10%"></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div> 
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row m-t-20">
            <div class="col-md-12">            
                <div class="panel panel-white m-t-20">
                    <div class="panel-body" >
                        <div class="row" style="margin: auto 45px auto 45px !important;">
                            <div class="col-md-12">
                                <table id="tbl_servicecharts" class="table-box">
                                    <thead>
                                        <tr >

                                            <th  class="space-after" colspan="4" style="text-align: center;">
                                                <h3 style="text-align: center;"><?php
                                                if (isset($business)) {
                                                    echo "<img style='' src='" . base_url() . 'assets/images/business/' . $business[0]['business_logo'] . "' alt='" . $business[0]['business_name'] . "' class='' />";
                                                } else {
                                                    echo 'SkedWise';
                                                }
                                                ?></h3>
                                            </th>

                                        </tr>                                       
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td colspan="4"><h2><strong>Retail Sales</strong></h2></td>
                                        </tr>
                                        <tr>
                                            <?php $totalretail=0; $totalqty=0; foreach($staff_retail_sale as $r){
                                                $totalretail=$totalretail+$r['total'];
                                                $totalqty=$totalqty+$r['count'];
                                            }
                                            ?>
                                            <td style="width:40%"><h3>Number of Products Sold</h3></td>
                                            <td style="width:20%"><h3><?php echo $totalqty ;?></h3></td>
                                            <td style="width:40%"><h3>Total amount of Porducts Sold</h3></td>
                                            <td  style="width:20%"><h3><?php echo number_format($totalretail,2);?></h3></td>
                                        </tr>
                                        <tr>
                                            <td colspan="4" class="space-after">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="4">
                                                <div id="grossing_retail"></div>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        
        
    </div>
</div>

<script>
$(document).ready(function(){

     $('#start_date').datepicker({
            autoclose: true,
            todayHighlight: true,
            
            format: 'yyyy/mm/dd'
        });
        $('#end_date').datepicker({
                autoclose: true,
                todayHighlight: true,

                format: 'yyyy/mm/dd'
            });   

        $("#staff_id").select2();
        
        grossing_services(<?php echo json_encode($grossing_services); ?>);
    
       
    
        var staff_id = $('#staff_id option:selected').val();
        var staff_name = $('#staff_id option:selected').text();
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url();?>sales_controller/staff_sale_graph',
            data: {staff_id: staff_id, staff_name: staff_name},
            dataType: "json",
            cache: false,
            async: true,
            success: function(data) {
                yearly_sale(data);
            }
        });
    
        
        grossing_retail(<?php echo json_encode($grossing_retail); ?>);
    
    });
    
    
    function grossing_services(data){
        var mData=[];
        var total=0;
       
                
        for (x = 0; x < data.length; x++) {
            //var mObject = {name: data[x]['name'], y: parseFloat(data[x]['y'])};
            var mObject = {name: data[x]['name'].substring(0,29), y: parseFloat(data[x]['y']), x: parseFloat(data[x]['amount'])};

            mData[x]=mObject;
            total = data[x]['paid'];
        } 
        draw_grossing_services(mData, total);
       
    }
    
    function draw_grossing_services(mData){
        
        // Make monochrome colors and set them as default for all pies
        Highcharts.getOptions().plotOptions.pie.colors = (function () {
            var colors = [],
                base = '#f9c851',//Highcharts.getOptions().colors[0],
                i;

            for (i = 0; i < 12; i += 1) {
                // Start out with a darkened base color (negative brighten), and end
                // up with a much brighter color
                colors.push(Highcharts.Color(base).brighten((i - 3) / 14).get());
            }
            return colors;
        }());

        
        
          $('#grossing_services').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie',
                style: {
                        fontFamily: 'Roboto, sans-serif'
                    }
            },
            credits: false,
//            colors: ['#ff8acc',  '#71b6f9', '#fcdf9b', '#10c469', '#ffd7ed', '#99527a', '#ccff8a', '#434348', '#90ed7d', '#f7a35c', '#8085e9', 
//                '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
            title: {
                text: 'Service Category Share'
            },
            subtitle: {
                text: 'Share of your Service Categories in the selected period.'
            },
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true,
                        format: '{point.name}: {point.y:.1f}%'
                    }
                }
            },

            tooltip: {
                headerFormat: '<span style="font-size:11px; word-wrap: break-word;">{series.name}</span><br>',
                pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.x:.2f}</b><br/>'
            },
            series: [{
                name: 'Services',
                colorByPoint: true,
                data: mData
            }]
        });
    }
    
    
    function yearly_sale(seriesdata) {
                if (seriesdata.length == "") {
                    swal({
                        title: "Data Not Found",
                        //text: "Please select a date Range",
                        type: "warning",
                        confirmButtonText: 'OK!'
                    });
                    return false;
                }
                $('#service_sales').highcharts({
                    chart: {
                        type: 'spline',
                        style: {
                            fontFamily: 'Roboto, sans-serif'
                        }
                    },
                    credits: false,
                    colors: ['#ff8acc', '#59a9f8 ', '#4a59b4', '#f9c851', '#10c469', '#ff5b5b', '#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9',
                        '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
                    title: {
                        text: 'Services Sale by ' + $('#staff_id option:selected').text()
                    },
                    xAxis: {
                        categories: [
                            'Jan', 'Feb', 'Mar',
                            'Apr', 'May', 'Jun',
                            'Jul', 'Aug', 'Sep',
                            'Oct', 'Nov', 'Dec'
                        ],
//                        categories: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
                        title: {
                            text: 'Months'
                        }
                    },
                    yAxis: {
                        title: {
                            text: 'Sales'
                        },
                        plotLines: [{
                                value: 0,
                                width: 1,
                                color: '#808080'
                            }]
                    },
                    series:
                            seriesdata


                });
            }
    
    
    
    function grossing_retail(data){
       // console.log(data);
        var mData=[];
        var total=0;

        for (x = 0; x < data.length; x++) {
            //var mObject = {name: data[x]['name'], y: parseFloat(data[x]['y'])};
            var mObject = {name: data[x]['name'].substring(0,29), y: parseFloat(data[x]['y']), x: parseFloat(data[x]['paid'])};

            mData[x]=mObject;
            total = total + parseInt(data[x]['paid']);
        } 
        draw_grossing_retail(mData, total);

    }
    
    function draw_grossing_retail(mData, total){
        //console.log(mData);
        // Make monochrome colors and set them as default for all pies
        Highcharts.getOptions().plotOptions.pie.colors = (function () {
            var colors = [],
                base = '#71b6f9',//Highcharts.getOptions().colors[0],
                i;

            for (i = 0; i < 12; i += 1) {
                // Start out with a darkened base color (negative brighten), and end
                // up with a much brighter color
                colors.push(Highcharts.Color(base).brighten((i - 3) / 14).get());
            }
            return colors;
        }());

        
        
        $('#grossing_retail').highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie',
                style: {
                        fontFamily: 'Roboto, sans-serif'
                    }
            },
            credits: false,
//            colors: ['#ff8acc',  '#71b6f9', '#fcdf9b', '#10c469', '#ffd7ed', '#99527a', '#ccff8a', '#434348', '#90ed7d', '#f7a35c', '#8085e9', 
//                '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
            title: {
                text: 'Product Sale by '+$("#staff_id option:selected").text()
            },
            subtitle: {
                text: 'Percentages of <?php echo CURRENCY_CODE;?>.'+ total.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' made by Product sale of this period.'
            },
            plotOptions: {
                series: {
                    dataLabels: {
                        enabled: true,
                        format: '{point.name}: {point.y:.1f}%'
                    }
                }
            },

            tooltip: {
                headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
                pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.x:.2f}</b><br/>'
            },
            series: [{
                name: 'Products',
                colorByPoint: true,
                data: mData
            }]
        });
    }
    
    
</script>