<?php

class Feedback_model extends CI_Model {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }


    function get_invoices() {
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->where('invoice_status','valid');
        $query = $this->db->get('invoice');

        return $query->result_array();
    }
    
    function insert_feedback($data){
         
           $this->db->insert('feedback', $data);
           return $this->db->insert_id();
    }
    
    function insert_details($data){
        
            $this->db->insert('feedback_details', $data);
            return $this->db->insert_id();
    }
    
    function get_month_staff($month, $year){
        
        $sql="SELECT staff_image, staff, staff_id, count(id_feedback_details) as 'Total Services',
            ifnull(round(sum(ifnull(skill,0))/count(if(skill>0,skill,null)),2),0) as 'skill',
            ifnull(round(sum(ifnull(knowledge,0))/count(if(knowledge>0,knowledge,null)),2),0) as 'knowledge',
            ifnull(round(sum(ifnull(hospitality,0))/count(if(hospitality>0,hospitality,null)),2),0) as 'hospitality',
            ifnull(round(sum(ifnull(satisfaction,0))/count(if(satisfaction>0,satisfaction,null)),2),0) as 'satisfaction',
            round(
            (
            ifnull(round(sum(ifnull(skill,0))/count(if(skill>0,skill,null)),2),0)
            + ifnull(round(sum(ifnull(knowledge,0))/count(if(knowledge>0,knowledge,null)),2),0)
            + ifnull(round(sum(ifnull(hospitality,0))/count(if(hospitality>0,hospitality,null)),2),0) 
            + ifnull(round(sum(ifnull(satisfaction,0))/count(if(satisfaction>0,satisfaction,null)),2),0)
            )/4,2) as 'Over All Rating'     , feedback_type       
            FROM 
            feedback 
            join feedback_details on feedback.id_feedback = feedback_details.feedback_id
            join staff on id_staff = feedback_details.staff_id
            where month(created_on)=".$month."
            and year(created_on)=".$year."
            and feedback.business_id='".$this->session->userdata('businessid')."'    
            group by staff_id
            order by 8 desc";
        
        $query = $this->db->query($sql);
        
        return $query->result_array();
        
    }
    
    function get_month_category($month, $year){
        $sql="SELECT service_category.service_category_image, service_category.service_category, count(id_feedback_details) as 'Total Services',
            ifnull(round(sum(ifnull(skill,0))/count(if(skill>0,skill,null)),2),0) as 'skill',
            ifnull(round(sum(ifnull(knowledge,0))/count(if(knowledge>0,knowledge,null)),2),0) as 'knowledge',
            ifnull(round(sum(ifnull(hospitality,0))/count(if(hospitality>0,hospitality,null)),2),0) as 'hospitality',
            ifnull(round(sum(ifnull(satisfaction,0))/count(if(satisfaction>0,satisfaction,null)),2),0) as 'satisfaction',
            round(
            (
            ifnull(round(sum(ifnull(skill,0))/count(if(skill>0,skill,null)),2),0)
            + ifnull(round(sum(ifnull(knowledge,0))/count(if(knowledge>0,knowledge,null)),2),0)
            + ifnull(round(sum(ifnull(hospitality,0))/count(if(hospitality>0,hospitality,null)),2),0) 
            + ifnull(round(sum(ifnull(satisfaction,0))/count(if(satisfaction>0,satisfaction,null)),2),0)
            )/4,2) as 'Over All Rating'  , feedback_type
             FROM 
            feedback 
            join feedback_details on feedback.id_feedback = feedback_details.feedback_id
            
            join business_services on business_services.id_business_services = feedback_details.service_id
            join service_category on service_category.id_service_category = business_services.service_category_id
            where month(created_on)=".$month."
            and year(created_on)=".$year."
                and feedback.business_id='".$this->session->userdata('businessid')."'    
            group by service_category
            order by 7 desc";
        
        $query = $this->db->query($sql);
        
        return $query->result_array();
    }
    
    
    function get_salon_stats($month, $year){
        
        $sql="select count(id_feedback) as 'Total Feedbacks',
        ifnull(round(sum(ifnull(qos,0))/count(IF(qos>0,qos,null)),2),0) as 'qos',
        ifnull(round(sum(ifnull(atmosphere,0))/count(IF(atmosphere>0,atmosphere,null)),2),0) as 'atmosphere',
        ifnull(round(sum(ifnull(experience,0))/count(IF(experience>0,experience,null)),2),0) as 'experience',
        ifnull(round(sum(ifnull(valueformoney,0))/count(IF(valueformoney>0,valueformoney,null)),2),0) as 'valueformoney',
        ifnull(round(sum(ifnull(recommendable,0))/count(IF(recommendable>0,recommendable,null)),2),0) as 'recommendable',
        round(
        (ifnull(round(sum(ifnull(qos,0))/count(IF(qos>0,qos,null)),2),0)
        +ifnull(round(sum(ifnull(atmosphere,0))/count(IF(atmosphere>0,atmosphere,null)),2),0)
        +ifnull(round(sum(ifnull(experience,0))/count(IF(experience>0,experience,null)),2) ,0)
        +ifnull(round(sum(ifnull(valueformoney,0))/count(IF(valueformoney>0,valueformoney,null)),2) ,0)
        +ifnull(round(sum(ifnull(recommendable,0))/count(IF(recommendable>0,recommendable,null)),2),0)
        )/4
        ,2) as 'Average'
         FROM 
            feedback 
            
            where month(created_on)=".$month."
            and year(created_on)=".$year."
                and feedback.business_id='".$this->session->userdata('businessid')."'    
            group by month(created_on),year(created_on)
            order by 7 desc";
        
          $query = $this->db->query($sql);
        
        return $query->result_array();
    }
    
     function get_feedback_listing($month,$year){
        
        $sql="select id_feedback,id_feedback_details, 
            0 as id_invoice, '' as invoice_status, 
            customers.customer_name, customers.customer_cell,  customer_type,
            service_name, 0 as service_rate, 0 as final_price,
            date_format(feedback.created_on, '%d-%m-%Y %h:%i:%s') created_on, qos, atmosphere, experience, valueformoney,
                feedback.comment, feedback_type,
                feedback_details.staff, '' as staff_image,
                skill, knowledge, hospitality, satisfaction,
                feedback_details.comments as service_comment
                from feedback
                join customers on customers.id_customers=feedback.customer_id
                join feedback_details on feedback.id_feedback= feedback_details.feedback_id
                where feedback.business_id='".$this->session->userdata('businessid')."'    
                and month(feedback.created_on)='".$month."' 
                and year(feedback.created_on)='".$year."'
                AND feedback_type='Short'
                order by id_feedback, id_feedback_details
                ";
        
        $query = $this->db->query($sql);
        $result_short =  $query->result_array();
        
        
        $sql1="select id_feedback,id_feedback_details, 
            0 as id_invoice, '' as invoice_status, 
            customers.customer_name, customers.customer_cell,  customer_type,
            feedback_details.service_name, 0 as service_rate, 0 as final_price,
            date_format(feedback.created_on, '%d-%m-%Y %h:%i:%s') created_on, qos, atmosphere, experience, valueformoney,
                feedback.comment, feedback_type,
                feedback_details.staff, '' as staff_image,
                skill, knowledge, hospitality, satisfaction,
                feedback_details.comments as service_comment
                from feedback
                join customers on customers.id_customers=feedback.customer_id
                join feedback_details on feedback.id_feedback= feedback_details.feedback_id
                join invoice on invoice.id_invoice = feedback.invoice_id
                join staff on feedback_details.staff_id = staff.id_staff
                join business_services on business_services.id_business_services= feedback_details.service_id                
                join invoice_details on invoice_details.invoice_id = invoice.id_invoice and invoice_details.service_id = feedback_details.service_id
                where feedback.business_id='".$this->session->userdata('businessid')."'    
                and month(feedback.created_on)='".$month."' 
                and year(feedback.created_on)='".$year."'
                AND feedback_type='Complete'
                order by id_feedback, id_feedback_details
                ";
        
        $query1 = $this->db->query($sql1);
        $result_complete =  $query1->result_array();
        
        
        $result = array_merge($result_short, $result_complete);
        return $result;
        
    }
    
    function get_comments($month,$year,$limit=0){
        $sql="select customer_name, comment from feedback 
                join customers on customers.id_customers=feedback.customer_id
                where month(feedback.created_on)='".$month."'
                and year(feedback.created_on)='".$year."'
                and feedback.business_id = ".$this->session->userdata('businessid')."
                and ifnull(comment,'') != '' 
                group by comment
                order by id_feedback desc ";
                if($limit>0){
                $sql.="limit ".$limit." ";
                }
            
         $query = $this->db->query($sql);
        
        return $query->result_array();
        
    }
    
    function get_config(){
        
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $query=$this->db->get('style_config');
        return $query->row();
        
    }
}