<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Servicesdashboard_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('servicesdashboard_model');
        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
    }

    public function servicesdashboard() {
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            // business_sale data inserting
            $previousmonth = date('M', strtotime('first day of previous month'));
            $previousyear = date('Y', strtotime('first day of previous month'));

            $month = date('M', strtotime('first day of this month'));
            $year = date('Y', strtotime('first day of this month'));

            $result = $this->servicesdashboard_model->get_business_sale_year_month($month, $year);

            if ($result) {
                //update for this month
                $start = date('Y-m-d', strtotime('first day of this month'));
                $end = date('Y-m-d', strtotime('last day of this month'));
                $thismonth = $this->servicesdashboard_model->get_this_month_year_sale($start, $end);
                $data = array(
                    'business_id' => $this->session->userdata('businessid'),
                    'month' => date('M', strtotime($start)),
                    'year' => date('Y', strtotime($start)),
                    'total_sale' => empty($thismonth->total_sale) ? 0 : $thismonth->total_sale
                );
                $id = $result->id_services_sales;
                $thisdone = $this->servicesdashboard_model->update_this_month_year_sale($data, $id);

            } else {
                //update for previous month
                $start = date('Y-m-d', strtotime('first day of previous month'));
                $end = date('Y-m-d', strtotime('last day of previous month'));

                $result = $this->servicesdashboard_model->get_business_sale_year_month($previousmonth, $previousyear);

                $previous = $this->servicesdashboard_model->get_this_month_year_sale($start, $end);
                $data = array(
                    'business_id' => $this->session->userdata('businessid'),
                    'month' => date('M', strtotime($start)),
                    'year' => date('Y', strtotime($start)),
                    'total_sale' => empty($previous->total_sale) ? 0 : $previous->total_sale
                );
                if($result){
                $id = $result->id_services_sales;
                }else {
                    $id=0;
                }
                $previousdone = $this->servicesdashboard_model->update_this_month_year_sale($data, $id);


                //add new for this month
                $start = date('Y-m-d', strtotime('first day of this month'));
                $end = date('Y-m-d', strtotime('last day of this month'));
                $thismonth = $this->servicesdashboard_model->get_this_month_year_sale($start, $end);
                $data = array(
                    'business_id' => $this->session->userdata('businessid'),
                    'month' => date('M', strtotime($start)),
                    'year' => date('Y', strtotime($start)),
                    'total_sale' => empty($thismonth->total_sale) ? 0 : $thismonth->total_sale
                );
                $thisdone = $this->servicesdashboard_model->add_this_month_year_sale($data);
            }

            // business_sale chart drawing
            $years = $this->servicesdashboard_model->get_business_sale_years();

            $html = "";

            foreach ($years as $y) {

                $months = array(
                    $y->year => array(
                        'Jan' => 0, 'Feb' => 0, 'Mar' => 0, 'Apr' => 0,
                        'May' => 0, 'Jun' => 0, 'Jul' => 0, 'Aug' => 0,
                        'Sep' => 0, 'Oct' => 0, 'Nov' => 0, 'Dec' => 0
                    )
                );

                $sales = $this->servicesdashboard_model->get_business_sales($y->year);

                $html .= "{ name: '$y->year', ";
                $html .= "data: [";

                foreach ($sales as $sale) {
                    foreach ($months[$y->year] as $key => $value) {
                        if ($sale->month == $key) {
                            $months[$y->year][$key] = $sale->total_sale;
                        }
                    }
                }

                foreach ($months[$y->year] as $m) {
                    $html .= $m . ",";
                }

                $html .= "] ";

                $html .= "},";
            }

            $data['sales'] = rtrim($html, ',');
            $data['chart']='chart';
            $data['nodatatable']='nodatatable';
            $this->load->view('includes/header', $data);
            $this->load->view('services_dashboard');
            $this->load->view('includes/footer');
        }
    }

    public function todayssale() {
        //get Todays Sale
        $data['Today'] = $this->servicesdashboard_model->get_todaysale();
        //Get Yesterdays Sale
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data['Yesterday'] = $this->servicesdashboard_model->get_yesterdaysale();

        
        echo(json_encode($data));
    }

    public function yesterdaysale() {
        //get the posted values
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_yesterdaysale();
               
        echo(json_encode($data));
    }

    public function monthsale() {
        //get the posted values
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_month($month, $year);

        echo(json_encode($data));
    }

    public function yearsale() {
        //get the posted values
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_year($month, $year);

               
        echo(json_encode($data));
    }

    public function monthlysale() {
        //get the posted values
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_monthly($month, $year);

               
        echo(json_encode($data));
    }

    public function dailysale() {
        //get the posted values
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_daily($month, $year);

               
        echo(json_encode($data));
    }

    public function get_month_commission() {
        
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->get_month_commission($month, $year);

               
        echo(json_encode($data));
    }
    
   

    public function top_4_clients() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->top_4_clients($month, $year);

               
        echo(json_encode($data));
    }

    public function top_4_staff() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        $data = $this->servicesdashboard_model->top_4_staff($month, $year);
               
        echo(json_encode($data));
    }



    public function grossing_services() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        $data = $this->servicesdashboard_model->grossing_services($month, $year);
         
        echo json_encode($data);
    }

    public function card_payments() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        $data = $this->servicesdashboard_model->get_month_card($month, $year);
         
        echo json_encode($data);
    }
    
    public function cash_payments() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        $data = $this->servicesdashboard_model->get_month_cash($month, $year);
         
        echo json_encode($data);
    }
    public function voucher_payments() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        $data = $this->servicesdashboard_model->get_month_voucher($month, $year);
         
        echo json_encode($data);
    }
    
    public function bank_payments() {
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        $data = $this->servicesdashboard_model->get_month_bank($month, $year);
         
        echo json_encode($data);
    }
    
    
    public function get_todays_breakup(){
        
        $data['cash'] = $this->servicesdashboard_model->get_todaysale_cash();
        $data['card'] = $this->servicesdashboard_model->get_todaysale_card();
        $data['bank'] = $this->servicesdashboard_model->get_todaysale_bank();
        $data['voucher'] = $this->servicesdashboard_model->get_todaysale_voucher();
        $data['Yesterday'] = $this->servicesdashboard_model->get_yesterdaysale();
               
        echo(json_encode($data));
        
    }
    
    public function get_months_breakup(){
        
        $month=$this->input->get('month');
        $year=$this->input->get('year');
        
        
        $data['cash'] = $this->servicesdashboard_model->get_month_cash($month, $year);
        $data['card'] = $this->servicesdashboard_model->get_month_card($month, $year);
        $data['bank'] = $this->servicesdashboard_model->get_month_bank($month, $year);
        $data['voucher'] = $this->servicesdashboard_model->get_month_voucher($month, $year);
        
               
        echo(json_encode($data));
        
    }
    
    
}
