<?php
session_start();
require_once('Connections/courier.php');
require_once 'functions/functions.php';
$branch_id = $_SESSION['branch_id'];

$date = date('Y-m-d');
$total_sales = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_sales FROM claims_details WHERE date_of_consultation='".$date."' AND branch_id='$branch_id'"));
$total_credit_sales = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_sales FROM claims_details WHERE mode_of_payment='credit sales' AND (payment_status='Approved' OR payment_status='Unapproved') AND branch_id='$branch_id'"));
$appointments = mysql_fetch_assoc(mysql_query("SELECT COUNT(id) AS total_appointments FROM scheduler WHERE type = 'appointment' AND patient_id > 0 AND status = 'Pending' AND start_date = '$date'"));
$reserved_frames = mysql_fetch_assoc(mysql_query("SELECT COUNT(id) AS reserved_frames FROM claims_details WHERE payment_status='Owing' AND mode_of_payment='frame reserve' AND branch_id='$branch_id'"));
$uncollected_jobs = mysql_fetch_assoc(mysql_query("SELECT COUNT(a.id) AS total_count FROM process_claim_no a left join patient_information b on a.patient_no=b.card_number left join guest_patients g ON a.patient_no=g.card_number left join job_activities_status j ON a.process_claim_no=j.process_claim_no WHERE a.audit_status='Job Received' AND j.job_status=a.audit_status AND j.status='open' AND a.patient_status = 'In Use' AND a.status_location_id='".$_SESSION['branch_id']."' ORDER BY j.id DESC"));

$last_7_days = mysql_query("SELECT DISTINCT date_of_consultation FROM claims_details WHERE branch_id='$branch_id' ORDER BY date_of_consultation ASC LIMIT 7");
while($row = mysql_fetch_assoc($last_7_days)){
    $days_of_week = $days_of_week . "'" .date('D, jS F', strtotime($row['date_of_consultation'])). "',";
}

$last_7_days_sales = mysql_query("SELECT DISTINCT date_of_consultation FROM claims_details WHERE branch_id='$branch_id' ORDER BY date_of_consultation ASC LIMIT 7");
while($last_7_days2_row = mysql_fetch_assoc($last_7_days_sales)){
    $days_of_week_gratis = $days_of_week_gratis . "" .getWeeklySales($last_7_days2_row['date_of_consultation'], 'gratis'). ",";
    $days_of_week_credit_sales = $days_of_week_credit_sales . "" .getWeeklySales($last_7_days2_row['date_of_consultation'], 'credit sales'). ",";
    $days_of_week_cash = $days_of_week_cash . "" .getWeeklySales($last_7_days2_row['date_of_consultation'], 'cash'). ",";
    $days_of_week_company = $days_of_week_company . "" .getWeeklySales($last_7_days2_row['date_of_consultation'], 'company'). ",";
    $days_of_week_insurance = $days_of_week_insurance . "" .getWeeklySales($last_7_days2_row['date_of_consultation'], 'insurance'). ",";
} 


function getSales($month, $type){
    $year = date('Y');
    $sales = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_sales FROM claims_details WHERE mode_of_payment='$type' AND MONTH(date_of_consultation)='$month' AND YEAR(date_of_consultation)='$year' AND branch_id='$branch_id'"));
    if($sales['total_sales']==''){
        $sales['total_sales'] =0;
    }
   return number_format($sales['total_sales'],2);
}

function getWeeklySales($date, $type){
    if($_SESSION['security']=='admin'){
    $sales = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_sales FROM claims_details WHERE mode_of_payment='$type' AND date_of_consultation = '$date' AND branch_id='$branch_id'"));
    }else {
    $sales = mysql_fetch_assoc(mysql_query("SELECT SUM(qty*unit_price) AS total_sales FROM claims_details WHERE mode_of_payment='$type' AND date_of_consultation = '$date' AND user_id='".$_SESSION['myMM_Userid']."' AND branch_id='$branch_id'"));
    }
    if($sales['total_sales']==''){
        $sales['total_sales'] =0;
    }
    return number_format($sales['total_sales'],2);
}

?>
<h1 class="page-title"> Optical Dashboard
                        <small>Statistics, Charts & Reports</small>
                    </h1>
                    <div class="page-bar">
                        <ul class="page-breadcrumb">
                            <li>
                                <i class="icon-home"></i>
                                <a href="index.html">Home</a>
                                <i class="fa fa-angle-right"></i>
                            </li>
                            <li>
                                <span>Dashboard</span>
                            </li>
                        </ul>
                        <div class="page-toolbar">
                            <div class="btn-group pull-right">
                                <button type="button" class="btn btn-fit-height grey-salt dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true"> Actions
                                    <i class="fa fa-angle-down"></i>
                                </button>
                                <ul class="dropdown-menu pull-right" role="menu">
                                    <li>
                                        <a href="#">
                                            <i class="icon-bell"></i> Action</a>
                                    </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-shield"></i> Another action</a>
                                    </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-user"></i> Something else here</a>
                                    </li>
                                    <li class="divider"> </li>
                                    <li>
                                        <a href="#">
                                            <i class="icon-bag"></i> Separated link</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <!-- END PAGE HEADER-->
                    <div class="row">
                        <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('summary_today_sales');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-green-sharp">
                                            <span data-counter="counterup" data-value="<?php echo number_format($total_sales['total_sales'],2); ?>">0</span>
                                            <small class="font-green-sharp">GHS</small>
                                        </h3>
                                        <small>TODAY'S TOTAL SALES</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-pie-chart"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 100%;" class="progress-bar progress-bar-success green-sharp"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('credit_sales_administrative');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-red-haze">
                                            <span data-counter="counterup" data-value="<?php echo number_format($total_credit_sales['total_sales'],2); ?>">0</span>
                                            <small class="font-red-haze">GHS</small>
                                        </h3>
                                        <small>OUTSTANDING CREDIT SALES</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-like"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 100%;" class="progress-bar progress-bar-success red-haze"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('list_reserved_frames');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-blue-sharp">
                                            <span data-counter="counterup" data-value="<?php echo $reserved_frames['reserved_frames'];?>">0</span>
                                        </h3>
                                        <small>RESERVED FRAMES</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-users"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 45%;" class="progress-bar progress-bar-success blue-sharp"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('list_of_appointments_today');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-purple-soft">
                                            <span data-counter="counterup" data-value="<?php echo $appointments['total_appointments']; ?>">0</span>
                                        </h3>
                                        <small>TODAY'S APPOINTMENTS</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-user"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 57%;" class="progress-bar progress-bar-success purple-soft"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-6 col-xs-12" style="cursor:pointer;" onclick="getstep('list_of_received_jobs_admin');">
                            <div class="dashboard-stat2 ">
                                <div class="display">
                                    <div class="number">
                                        <h3 class="font-purple-soft">
                                            <span data-counter="counterup" data-value="<?php echo $uncollected_jobs['total_count']; ?>">0</span>
                                        </h3>
                                        <small>UNCOLLECTED JOBS</small>
                                    </div>
                                    <div class="icon">
                                        <i class="icon-users"></i>
                                    </div>
                                </div>
                                <div class="progress-info">
                                    <div class="progress">
                                        <!-- <span style="width: 57%;" class="progress-bar progress-bar-success purple-soft"></span> -->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-6 col-xs-12 col-sm-12">
                            <div class="portlet light ">
                                <div class="portlet-title">
                                    <div class="caption">
                                        <span class="caption-subject bold uppercase font-dark">PAST 7 DAYS SALES</span>
                                    </div>
                                </div>
                                <div class="portlet-body">
                                <div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-6 col-xs-12 col-sm-12">
                            <div class="portlet light ">
                                <div class="portlet-title">
                                    <div class="caption ">
                                        <span class="caption-subject font-dark bold uppercase">Total Monthly Payment Mode</span>
                                    </div>
                                </div>
                                <div class="portlet-body">
                                <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
                                </div>
                            </div>
                        </div>
                    </div>

                   <head> 
                    <script src="assets/global/plugins/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
   $(document).ready(function() {
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Total Monthly Payment Mode'
    },
    subtitle: {
        text: ''
    },
    xAxis: {
        categories: [
            'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'Jul',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec'
        ],
        crosshair: true
    },
    yAxis: {
        min: 0,
        title: {
            text: 'Total Sales (GHS)'
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
        pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
            '<td style="padding:0"><b>{point.y:.1f} GHS</b></td></tr>',
        footerFormat: '</table>',
        shared: true,
        useHTML: true
    },
    plotOptions: {
        column: {
            pointPadding: 0.2,
            borderWidth: 0
        }
    },
    series: [{
        name: 'Gratis',
        data: [<?php echo getSales('01','gratis'); ?>,<?php echo getSales('02','gratis'); ?>,<?php echo getSales('03','gratis'); ?>,<?php echo getSales('04','gratis'); ?>,<?php echo getSales('05','gratis'); ?>,<?php echo getSales('06','gratis'); ?>,<?php echo getSales('07','gratis'); ?>,<?php echo getSales('08','gratis'); ?>,<?php echo getSales('09','gratis'); ?>,<?php echo getSales('10','gratis'); ?>,<?php echo getSales('11','gratis'); ?>,<?php echo getSales('12','gratis'); ?>]

    }, {
        name: 'Credit Sales',
        data: [<?php echo getSales('01','credit sales'); ?>,<?php echo getSales('02','credit sales'); ?>,<?php echo getSales('03','credit sales'); ?>,<?php echo getSales('04','credit sales'); ?>,<?php echo getSales('05','credit sales'); ?>,<?php echo getSales('06','credit sales'); ?>,<?php echo getSales('07','credit sales'); ?>,<?php echo getSales('08','credit sales'); ?>,<?php echo getSales('09','credit sales'); ?>,<?php echo getSales('10','credit sales'); ?>,<?php echo getSales('11','credit sales'); ?>,<?php echo getSales('12','credit sales'); ?>]

    }, {
        name: 'Cash',
        data: [<?php echo getSales('01','cash'); ?>,<?php echo getSales('02','cash'); ?>,<?php echo getSales('03','cash'); ?>,<?php echo getSales('04','cash'); ?>,<?php echo getSales('05','cash'); ?>,<?php echo getSales('06','cash'); ?>,<?php echo getSales('07','cash'); ?>,<?php echo getSales('08','cash'); ?>,<?php echo getSales('09','cash'); ?>,<?php echo getSales('10','cash'); ?>,<?php echo getSales('11','cash'); ?>,<?php echo getSales('12','cash'); ?>]

    }, {
        name: 'Company',
        data: [<?php echo getSales('01','company'); ?>,<?php echo getSales('02','company'); ?>,<?php echo getSales('03','company'); ?>,<?php echo getSales('04','company'); ?>,<?php echo getSales('05','company'); ?>,<?php echo getSales('06','company'); ?>,<?php echo getSales('07','company'); ?>,<?php echo getSales('08','company'); ?>,<?php echo getSales('09','company'); ?>,<?php echo getSales('10','company'); ?>,<?php echo getSales('11','company'); ?>,<?php echo getSales('12','company'); ?>]

    },
    {
        name: 'Insurance',
        data: [<?php echo getSales('01','insurance'); ?>,<?php echo getSales('02','insurance'); ?>,<?php echo getSales('03','insurance'); ?>,<?php echo getSales('04','insurance'); ?>,<?php echo getSales('05','insurance'); ?>,<?php echo getSales('06','insurance'); ?>,<?php echo getSales('07','insurance'); ?>,<?php echo getSales('08','insurance'); ?>,<?php echo getSales('09','insurance'); ?>,<?php echo getSales('10','insurance'); ?>,<?php echo getSales('11','insurance'); ?>,<?php echo getSales('12','insurance'); ?>]

    }
    ]
});



Highcharts.chart('container2', {
    chart: {
        type: 'areaspline'
    },
    title: {
        text: 'PAST 7 DAYS SALES'
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        verticalAlign: 'top',
        x: 150,
        y: 100,
        floating: true,
        borderWidth: 1,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    xAxis: {
        categories: [<?php echo $days_of_week; ?>],
        plotBands: [{ // visualize the weekend
            from: 4.5,
            to: 6.5,
            color: 'rgba(68, 170, 213, .2)'
        }]
    },
    yAxis: {
        title: {
            text: 'Total Sales (GHS)'
        }
    },
    tooltip: {
        shared: true,
        valueSuffix: ' GHS'
    },
    credits: {
        enabled: false
    },
    plotOptions: {
        areaspline: {
            fillOpacity: 0.5
        }
    },
    series: [
        {
        name: 'Gratis',
        data: [<?php echo $days_of_week_gratis; ?>]
    }, {
        name: 'Credit Sales',
        data: [<?php echo $days_of_week_credit_sales; ?>]
    },
    {
        name: 'Cash',
        data: [<?php echo $days_of_week_cash; ?>]
    },
    {
        name: 'Company',
        data: [<?php echo $days_of_week_company; ?>]
    },
    {
        name: 'Insurance',
        data: [<?php echo $days_of_week_insurance; ?>]
    }
    ]
});

});
</script>
</head>