<?php

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

class Incentivizer_controller extends CI_Controller {

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

    public function incentive_calculator(){
        $data['nav'] = 'reports';
        $data['subnav'] = 'incentivizer';
            
        $data['brands'] =  $this->get_brands();
        
        //get service categories
        $data['services'] = $this->get_service_categories();
        
        
        $data['selected_date']=date('Y-m');
        
        //print_r($data['services']);exit();
        $this->load->view('includes/header',$data);
        $this->load->view('marketing/incentivizer_view');
        $this->load->view('includes/footer');
        
    }
    
    public function incentive_report_product() {
        $data['nav'] = 'reports';
        $data['subnav'] = 'incentivizer';
        
        $brands[] = $this->input->post('brands');
        $invoicedate = $this->input->post('calendar_date_product');
         
       // print_r($brands[0]); exit();
        $sql_create_table = "select invoice_products.*, business.business_name ,            
            date(invoice.invoice_date) as invoice_date
            from invoice 
            join invoice_products on invoice.id_invoice=invoice_products.invoice_id 
            join business on invoice.business_id = business.id_business
            join
            (
            SELECT  id_business_products, product, business_products.price
            FROM `business_products`
            JOIN business_brands on id_business_brands = business_products.brand_id 
            WHERE id_business_brands in (";
        $x=1;
        foreach($brands[0] as $brand){           
            
            if($x < sizeof($brands[0])){
                $sql_create_table .=  $brand .", ";
//                $sql_create_table .= " or business_brand_name like '%". $brand ."%'";
//                $sql_create_table .= " or product like '%". $brand ."%'";
                $x++;
            }else{
                $sql_create_table .=  $brand ."";
//                $sql_create_table .= " business_brand_name like '%". $brand ."%'";
//                $sql_create_table .= " or product like '%". $brand ."%'";
                $x++;
            }            
        }
               
        $sql_create_table .= "))as a
                    on a.id_business_products = invoice_products.product_id 
                WHERE
                invoice.invoice_status='valid'
                and date_format(invoice_date,'%Y-%m') = '".$invoicedate."'
                ";
//        /echo $sql_create_table; exit();
        //$this->db->query($sql_create_table);
        
        
        
        $report_sql="SELECT business_name, staff_id, staff_name, brand_name, product_name,
            month(invoice_date) as month, year(invoice_date) as year,
            sum(invoice_qty) as qty, sum(invoice_qty * discounted_price) as price
            FROM (".$sql_create_table.") as subtable 
            group by business_id, staff_id, month(invoice_date), year(invoice_date), brand_name, product_name
            order by business_id, staff_id, month(invoice_date), year(invoice_date), brand_name, product_name;";
        $query = $this->db->query($report_sql);
        $data['response_products'] = $query->result_array();
        
        
        $data['brands'] =  $this->get_brands();
        $data['services'] = $this->get_service_categories();
        $data['selected_date']=$invoicedate;
        $data['selected_brands']=$brands;
        $data['selected_date']=$invoicedate;
        
        //print_r($data);exit();
        $this->load->view('includes/header',$data);
        $this->load->view('marketing/incentivizer_view');
        $this->load->view('includes/footer');
        
        
    }
    
    
    public function incentive_report_service() {
        $data['nav'] = 'reports';
        $data['subnav'] = 'incentivizer';
        
        //$sql_drop_service_table = "drop table IF EXISTS service_incentive_table;";
        //$this->db->query($sql_drop_service_table);
        //print_r($this->input->post('services[]')); exit();
        
        $services[] = $this->input->post('services');
        $invoicedate = $this->input->post('calendar_date_service');
        
        //print_r($services[0]); exit();
        $sql_create_table = "select invoice_details.*, invoice_staff.staff_id, invoice_staff.staff_name, 
            business.business_name , 
            date(invoice.invoice_date) as invoice_date
            from invoice 
            join invoice_details on invoice.id_invoice=invoice_details.invoice_id
            join invoice_staff on invoice_detail_id = invoice_details.id_invoice_details and additional_staff = 'No'
            join business on invoice.business_id = business.id_business
            join
            (
            SELECT  id_business_services, service_name, service_rate
            FROM business_services
            JOIN service_category on id_service_category = business_services.service_category_id 
            WHERE id_service_category in (";
        $x=1;
        foreach($services[0] as $service){           
            
            if($x < sizeof($services[0])){
                $sql_create_table .=  $service .", ";
                $x++;
            }else{
                $sql_create_table .=  $service ."";
                $x++;
            }            
        }
               
        $sql_create_table .= "))as a
                    on a.id_business_services = invoice_details.service_id 
                WHERE
                invoice.invoice_status='valid'
                and date_format(invoice_date,'%Y-%m') = '".$invoicedate."'
                ";
       
      $this->db->query($sql_create_table);
        
        
        
        $report_sql="SELECT business_name, staff_id, staff_name, service_category, service_name,
            month(invoice_date) month, year(invoice_date) year,
            discounted_price as price
            FROM (".$sql_create_table.") as subtable 
            group by business_id, staff_id, month(invoice_date), year(invoice_date), service_category, service_name
            order by business_id, staff_id, month(invoice_date), year(invoice_date), service_category, service_name;";
        $query = $this->db->query($report_sql);
        $data['response_services'] = $query->result_array();
        
        $data['brands'] =  $this->get_brands();
           
        //get service categories
        $data['services'] = $this->get_service_categories();
        
        $data['selected_date']=$invoicedate;
        $data['selected_services']=$services;
        
        //print_r($data);exit();
        $this->load->view('includes/header',$data);
        $this->load->view('marketing/incentivizer_view');
        $this->load->view('includes/footer');
        
        
    }
    
    function get_service_categories(){
        
        $this->db->select('id_service_category, service_type_id, service_type, service_category.order_id, service_category, service_category.business_id, business_name, service_category_active,  concat("assets/images/category/", ifnull(service_category_image, "nu.png")) as service_category_image, \'servicetype\' flag', false);
        $this->db->join('service_type','service_type.id_service_types = service_category.service_type_id');
        $this->db->join('business','business.id_business = service_category.business_id');
        $this->db->where('service_category.service_category_active', 'Yes');       
        $Query1 = $this->db->get('service_category');
        $d1=$Query1->result_array();
        
        $this->db->select('id_package_category id_service_category, package_type_id service_type_id, service_type, package_category.order_id, service_category, package_category.business_id, business_name, service_category_active, concat("assets/images/category/", ifnull(service_category_image, "nu.png")) as service_category_image, \'packagetype\' flag', false);
        $this->db->join('package_type','package_type.id_package_type = package_category.package_type_id');
        $this->db->join('business','business.id_business = package_category.business_id');
        $this->db->where('package_category.service_category_active', 'Yes');        
        $Query2 = $this->db->get('package_category');
        $d2=$Query2->result_array();
        
        $data=[];
        $data= array_merge($d1, $d2);
        return $data;
    }
    
    function get_brands(){
        $this->db->select("business_brands.*, id_business, business_name");
        $this->db->join('business','business.id_business = business_brands.business_id');
        if($this->session->userdata('common_products')=='No'){

        } else {
            $this->db->where('business.common_products', 'Yes');
        }
        $this->db->where('business_brands.business_brand_active', 'Yes');

        $query = $this->db->get('business_brands');
        return $query->result_array();
        
    }
    
}


