<?php

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

class Salesdashboard_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('salesdashboard_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 {
            
                $this->dashboard();
            
        }
        
    }
    
    public function dashboard(){
        //exit();
        $startdate = $this->input->post('startdate');
        $enddate = $this->input->post('enddate');
        //echo $startdate; exit();
        if(null==$startdate || $startdate==''){
            $startdate= date('Y-m-d');
        }
        if(null==$enddate || $enddate==''){
            $enddate= date('Y-m-d');
        }
        
        $data['chart']='chart';
        $data['nodatatable']='nodatatable';
        $data['nav'] = 'reports';
        $data['subnav'] = 'salesdashboard';
        
        $data['totalbills']=$this->salesdashboard_model->totalbills($startdate, $enddate);
        
        $data['payment_breakup']=$this->salesdashboard_model->payment_breakup($startdate, $enddate);
        
        $data['service_details']=$this->salesdashboard_model->service_details($startdate, $enddate);
        $data['product_details']=$this->salesdashboard_model->product_details($startdate, $enddate);
        
        $data['canceledbills']=$this->salesdashboard_model->canceledbills($startdate, $enddate);
        
        $data['advance']=$this->salesdashboard_model->advance($startdate, $enddate);
        $data['vouchers']=$this->salesdashboard_model->vouchers($startdate, $enddate);
        
        $data['discounts']=$data['totalbills']->invoice_discount+$data['product_details']->product_discount+$data['service_details']->service_discount;
        
        $data['expenses']=$this->salesdashboard_model->expenses($startdate, $enddate);
        
        $data['billitems']=$this->billitems($startdate, $enddate, $data['totalbills']->totalbills);
        
        //$data['grossingservices'] = $this->salesdashboard_model->grossing_services($startdate, $enddate);
        
        $data['startdate']=$startdate; $data['enddate']=$enddate;
        
        $this->load->view('includes/header', $data);
        $this->load->view('sales_dashboard');
        $this->load->view('includes/footer');
    }
    
    public function billitems($startdate,$enddate, $total){
        $billitems=[];
        $formated="";
        $service=$this->salesdashboard_model->billitems_service($startdate, $enddate);
        $retail=$this->salesdashboard_model->billitems_retail($startdate, $enddate);
        
        array_push($billitems, $service);
        array_push($billitems, $retail );
        $formated="[";
        $x=1;
        foreach($billitems as $bi){
            $y = $bi->y > 0 ? $bi->y : 0 ;
            $paid = $bi->paid > 0 ? $bi->paid : 0 ;
            $formated.='{name:"'.$bi->name.'",y:'.$y.', paid:'.$paid.'}';
            if($x < sizeof($billitems)){
                $formated.=",";
            }
            $x++;
        }
        $formated.="]";
        return $formated;
       // return json_encode($billitems);
        
    }
    
    public function grossing_services() {
        $startdate = $this->input->post('startdate');
        $enddate = $this->input->post('enddate');
        //echo $startdate; exit();
        if(null==$startdate || $startdate==''){
            $startdate= date('Y-m-d');
        }
        if(null==$enddate || $enddate==''){
            $enddate= date('Y-m-d');
        }
        
        $data = $this->salesdashboard_model->grossing_services($startdate, $enddate);
         
        echo json_encode($data);
    }
    
    
    
}