<?php

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

class Program_dashboard_controller extends CI_Controller {

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

    public function index(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {   
            $data['nav'] = 'programs';
            $data['subnav'] = '';

            $this->load->view('includes/header', $data);
            $this->load->view('programs/programs_dashboard');
            $this->load->view('includes/footer');
        } 
    }
    
    public function program_invoice_report(){
        
        
        
        $sql = "SELECT id_customers, customers.customer_name, date_format(program_enrollment.created_on,'%d-%m-%Y %H:%i:%s') as 'enrolled_on', 
                    program_sessions.session_name, programs.program,
                    date_format(program_invoices.invoice_date, '%d-%m-%Y') as invoice_date,
                    program_invoices.id_program_invoices, program_invoices.invoice_date, program_invoices.for_the_month, program_invoices.paid_amount,
                    program_invoices.paid_cash, program_invoices.paid_card, program_invoices.paid_check,
                    program_invoices.payment_mode, program_invoices.program_invoice_status, program_invoices.program_invoice_remarks
                    FROM `program_invoices`
                    join program_enrollment on program_enrollment.id_program_enrollment = program_invoices.program_enrollment_id
                    join program_sessions on program_sessions.id_program_sessions = program_enrollment.program_session_id
                    join programs on programs.id_programs = program_sessions.program_id
                    join customers on program_enrollment.customer_id = customers.id_customers 
                    where  program_invoices.program_invoice_status = 'Active' ";
        if($this->input->post('startdate')!=null){
            $sql .= " AND date(program_invoices.invoice_date) >= '". $this->input->post('startdate')."' ";
            $sql .= " AND date(program_invoices.invoice_date) <= '". $this->input->post('enddate')."' ";
        }
        
        $sql .= " and program_invoices.business_id= '". $this->session->userdata('businessid') ."'
                    order by program_invoices.created_on";
        
        $query = $this->db->query($sql);
        
        $data['program_invoices'] = $query->result_array();
        
        $this->load->view('includes/header', $data);
        $this->load->view('programs/reports/program_invoice_report');
        $this->load->view('includes/footer');
        
        
        
    }
    
    public function retaildashboard() {
        // 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->retaildashboard_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->retaildashboard_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_retail_sales;
            $thisdone = $this->retaildashboard_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->retaildashboard_model->get_business_sale_year_month($previousmonth, $previousyear);
            
            $previous = $this->retaildashboard_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
            );
            $id = $result->id_retail_sales;
            $previousdone = $this->retaildashboard_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'));
            $result = $this->retaildashboard_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($result->total_sale) ? 0 : $result->total_sale
            );
            $result = $this->retaildashboard_model->add_this_month_year_sale($data);
        }

        // business_sale chart drawing
        $years = $this->retaildashboard_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->retaildashboard_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, ',');
        $this->load->view('includes/header', $data);
        $this->load->view('retail_dashboard');
        $this->load->view('includes/footer');
    }

    public function todayssale() {
        //get the posted values

        $data = $this->retaildashboard_model->get_todaysale();

        echo(json_encode($data));
    }

    public function yesterdaysale() {
        //get the posted values
        $month=$this->input->post('month');
        $year=$this->input->post('year');
        $data = $this->retaildashboard_model->get_yesterdaysale();

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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

        echo(json_encode($data));
    }

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



    public function grossing_services() {
        $month=$this->input->post('month');
        $year=$this->input->post('year');
        
        $data = $this->retaildashboard_model->grossing_services($month, $year);

        echo json_encode($data);
    }

    public function get_months_breakup(){
        
        $month=$this->input->post('month');
        $year=$this->input->post('year');
        
        
        $data['cash'] = $this->retaildashboard_model->get_month_cash($month, $year);
        $data['card'] = $this->retaildashboard_model->get_month_card($month, $year);
        $data['bank'] = $this->retaildashboard_model->get_month_bank($month, $year);
        $data['voucher'] = $this->retaildashboard_model->get_month_voucher($month, $year);
        
        echo(json_encode($data));
        
    }
    
    public function get_today_breakup(){
        
      
        $data['cash'] = $this->retaildashboard_model->get_today_cash();
        $data['card'] = $this->retaildashboard_model->get_today_card();
        $data['bank'] = $this->retaildashboard_model->get_today_bank();
        $data['voucher'] = $this->retaildashboard_model->get_today_voucher();
        
        echo(json_encode($data));
        
    }
    
    
}
