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

class Setting_model extends CI_Model{

	function get_all_app_setting()
    {
        $this->db->where('app_id', 1);
        return $this->db->get('application_setting')->row();
    }

    function update_application_setting()
    {
        $data = [
            'app_name' => $this->input->post('app_name'),
            'app_keywords' => $this->input->post('app_keywords'), 
            'app_meta_title' => $this->input->post('app_meta_title'), 
            'app_meta_description' => $this->input->post('app_meta_description'),
            'footer_about_us' => $this->input->post('footer_about_us'),
            'app_address' => $this->input->post('app_address'), 
            'app_mobile' => $this->input->post('app_mobile'), 
            'app_email' => $this->input->post('app_email'),
            'rozar_key_id' => $this->input->post('rozar_key_id'),
            'rozar_key_secret' => $this->input->post('rozar_key_secret'),
            'head_code' => $this->input->post('head_code'),
            'facebook' => $this->input->post('facebook'),
            'twitter' => $this->input->post('twitter'),
            'youtube' => $this->input->post('youtube'),
            'instagram' => $this->input->post('instagram'),
            'telegram' => $this->input->post('telegram'),
            'map_iframe' => $this->input->post('map_iframe'),
            'app_gst' => $this->input->post('app_gst'),
        ];
        if(!empty($_FILES['app_sticky_logo']['name']))
        {
            $app_sticky_logo = $this->get_all_app_setting();
            if(file_exists($app_sticky_logo->app_sticky_logo))
            {
                unlink($app_sticky_logo->app_sticky_logo);
            }
            $_FILES['file']['name'] = $_FILES['app_sticky_logo']['name'];
            $_FILES['file']['type'] = $_FILES['app_sticky_logo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['app_sticky_logo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['app_sticky_logo']['error'];
            $_FILES['file']['size'] = $_FILES['app_sticky_logo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['app_sticky_logo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['app_sticky_logo'] = 'uploads/' . $filename;

                }
            }
        }
        if(!empty($_FILES['app_footer_logo']['name']))
        {
            $app_footer_logo = $this->get_all_app_setting();
            if(file_exists($app_footer_logo->app_footer_logo))
            {
                unlink($app_footer_logo->app_footer_logo);
            }
            $_FILES['file']['name'] = $_FILES['app_footer_logo']['name'];
            $_FILES['file']['type'] = $_FILES['app_footer_logo']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['app_footer_logo']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['app_footer_logo']['error'];
            $_FILES['file']['size'] = $_FILES['app_footer_logo']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['app_footer_logo']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['app_footer_logo'] = 'uploads/' . $filename;

                }
            }
        }

        if(!empty($_FILES['fevicon']['name']))
        {
            $app_footer_logo = $this->get_all_app_setting();
            if(file_exists($app_footer_logo->fevicon))
            {
                unlink($app_footer_logo->fevicon);
            }
            $_FILES['file']['name'] = $_FILES['fevicon']['name'];
            $_FILES['file']['type'] = $_FILES['fevicon']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['fevicon']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['fevicon']['error'];
            $_FILES['file']['size'] = $_FILES['fevicon']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['fevicon']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['fevicon'] = 'uploads/' . $filename;

                }
            }
        }
        $this->db->where('app_id', 1);
        return $this->db->update('application_setting', $data);
    }

    function get_visual_setting()
    {
        $this->db->where('id', 1);
        return $this->db->get('visual_setting')->row();
    }

    function update_visual_setting()
    {
        $data = [
            'color_1'                   => $this->input->post('color_1'),
            'color_2'                    => $this->input->post('color_2'),
        ];

        $this->db->where('id', 1);
        return $this->db->update('visual_setting', $data);
    }

    function get_all_contact_details()
    {
        $this->db->order_by('DATE(lead_date)', 'desc');  
        return $this->db->get('lead_management')->result();
    }


    // Lead Start

    function get_all_lead_source()
    {
        $id = user_id();
        $this->db->where('lead_added_by', $id);
        return $this->db->get('lead_source_name')->result();
    }

    function add_new_lead()
    {
        $data = [
            'lead_name' => $this->input->post('lead_name'),
            'lead_added_by' => user_id()
        ];
        return $this->db->insert('lead_source_name', $data);
    }

    function delete_lead($id)
    {
        $user_id = user_id();
        $this->db->where('lead_added_by', $user_id);
        $this->db->where('lead_name_id', $id);
        return $this->db->delete('lead_source_name');
    }

    function update_lead($id)
    {
        $data = [
            'lead_name' => $this->input->post('lead_name'),
        ];
        $this->db->where('lead_name_id', $id);
        return $this->db->update('lead_source_name', $data);
    }

    // Lead End

    // Follow Up Start

    function get_all_followup_mode()
    {
        $id = user_id();
        $this->db->where('added_by', $id);
        return $this->db->get('student_follow_up_mode')->result();
    }

    function add_new_followup_mode()
    {
        $data = [
            'mode_name' => $this->input->post('mode_name'),
            'added_by' => user_id()
        ];
        return $this->db->insert('student_follow_up_mode', $data);
    }

    function delete_followup_mode($id)
    {
        $user_id = user_id();
        $this->db->where('added_by', $user_id);
        $this->db->where('mode_id', $id);
        return $this->db->delete('student_follow_up_mode');
    }

    function update_followup_mode($id)
    {
        $data = [
            'mode_name' => $this->input->post('mode_name'),
        ];
        $this->db->where('mode_id', $id);
        return $this->db->update('student_follow_up_mode', $data);
    }

    // Follow Up End

    // Follow Up Start

    function get_all_batch()
    {
        $id = user_id();
        $this->db->select('a.*, b.user_name');
        $this->db->from('student_batch a');
        $this->db->join('users b', 'b.user_id = a.instructor_id', 'left');
        $this->db->where('a.added_by', $id);
        return $this->db->get()->result();
    }

    function add_new_batch($id)
    {
        $data = [
            'batch_name' => $this->input->post('batch_name'),
            'batch_size' => $this->input->post('batch_size'),
            'batch_attach_course' => $this->input->post('batch_attach_course'),
            'batch_course' => $this->input->post('batch_course'),
            'batch_start_date' => $this->input->post('batch_start_date'),
            'batch_end_date' => $this->input->post('batch_end_date'),
            'batch_start_time' => $this->input->post('batch_start_time'),
            'batch_end_time' => $this->input->post('batch_end_time'),
            'batch_sunday' => $this->input->post('batch_sunday') == 1 ? 1 : 0,
            'batch_monday' => $this->input->post('batch_monday') == 1 ? 1 : 0,
            'batch_tuesday' => $this->input->post('batch_tuesday') == 1 ? 1 : 0,
            'batch_wednesday' => $this->input->post('batch_wednesday') == 1 ? 1 : 0,
            'batch_thursday' => $this->input->post('batch_thursday') == 1 ? 1 : 0,
            'batch_friday' => $this->input->post('batch_friday') == 1 ? 1 : 0,
            'batch_saturday' => $this->input->post('batch_saturday') == 1 ? 1 : 0,
            'instructor_id' => $this->input->post('instructor_id'),
            'added_by' => $id
        ];
        return $this->db->insert('student_batch', $data);
    }

    function get_all_batch_by_id($id)
    {
        $atp_id = user_id();
        $this->db->where('batch_id', $id);
        $this->db->where('added_by', $atp_id);
        return $this->db->get('student_batch')->row();
    }

    function get_all_batch_by_inst_id()
    {
        $user_id = user()->user_id;
        $this->db->where('instructor_id', $user_id);
        return $this->db->get('student_batch')->result();
    }

    function delete_batch($id)
    {
        $user_id = user_id();
        $this->db->where('added_by', $user_id);
        $this->db->where('batch_id', $id);
        return $this->db->delete('student_batch');
    }

    function update_batch($id)
    {
        $data = [
            'batch_name' => $this->input->post('batch_name'),
            'batch_size' => $this->input->post('batch_size'),
            'batch_attach_course' => $this->input->post('batch_attach_course'),
            'batch_course' => $this->input->post('batch_course'),
            'batch_start_date' => $this->input->post('batch_start_date'),
            'batch_end_date' => $this->input->post('batch_end_date'),
            'batch_start_time' => $this->input->post('batch_start_time'),
            'batch_end_time' => $this->input->post('batch_end_time'),
            'batch_sunday' => $this->input->post('batch_sunday') == 1 ? 1 : 0,
            'batch_monday' => $this->input->post('batch_monday') == 1 ? 1 : 0,
            'batch_tuesday' => $this->input->post('batch_tuesday') == 1 ? 1 : 0,
            'batch_wednesday' => $this->input->post('batch_wednesday') == 1 ? 1 : 0,
            'batch_thursday' => $this->input->post('batch_thursday') == 1 ? 1 : 0,
            'batch_friday' => $this->input->post('batch_friday') == 1 ? 1 : 0,
            'batch_saturday' => $this->input->post('batch_saturday') == 1 ? 1 : 0,
            'instructor_id' => $this->input->post('instructor_id'),
        ];
        $this->db->where('batch_id', $id);
        return $this->db->update('student_batch', $data);
    }

    // Follow Up End

    //Account Head Start

    function all_account_head()
    {
        $user_id = user_id();

        $this->db->where('acc_name_inst_id', $user_id);
        return $this->db->get('account_head_name')->result();
    }

    function add_new_account_head()
    {
        $data = [
            'acc_name' => $this->input->post('acc_name'),
            'acc_name_inst_id' => user_id()
        ];

        return $this->db->insert('account_head_name', $data);
    }

    function delete_account_head($id)
    {
        $this->db->where('acc_name_id', $id);
        return $this->db->delete('account_head_name');
    }

    function get_all_account_head_by_atp_id()
    {
        $user_id = user_id();
        $this->db->where('acc_name_inst_id', $user_id);
        return $this->db->get('account_head_name')->result();
    }

    function update_account_head($id)
    {
        $data = [
            'acc_name' => $this->input->post('acc_name'),
        ];
        $this->db->where('acc_name_id', $id);
        return $this->db->update('account_head_name', $data);
    }
    //Account Head End

    // Royalty Management Start

    public function add_package()
    {
        // $this->db->select('cp_type');
        // $this->db->where('cp_id', $this->input->post('package_programm'));
        // $pp_type = $this->db->get('course_programm')->row();

        $data['package_rand_code'] = rand(1000000000, 9999999999);
        $data['package_name'] = $this->input->post('package_name');
        $data['package_price'] = $this->input->post('package_price');
        // if($pp_type->cp_type == 1)
        // {
            $data['package_student_reg_fee'] = $this->input->post('student_reg_amt');
        // }
        // else
        // {
        //     $data['package_student_reg_fee'] = $this->input->post('student_reg_percantage');
        // }
        $data['package_lead_price'] = $this->input->post('package_lead_price');
        $data['package_digital_certificate_price'] = $this->input->post('package_digital_certificate_price');
        $data['package_live_class_price'] = $this->input->post('package_live_class_price');
        $data['package_video_course_percantage'] = $this->input->post('package_video_course_percantage');
        $data['package_balance_amount'] = $this->input->post('package_balance_amount');
        $data['package_color'] = $this->input->post('package_color');
        $data['package_order'] = $this->input->post('package_order');
        $data['package_programm'] = $this->input->post('package_programm');
        $data['package_aff_percantage'] = $this->input->post('package_aff_percantage');
        $data['is_demo_package'] = $this->input->post('is_demo_package');
        // $data['package_program_type'] = $pp_type->cp_type;

        if(!empty($_FILES['package_image']['name']))
        {
            $_FILES['file']['name'] = $_FILES['package_image']['name'];
            $_FILES['file']['type'] = $_FILES['package_image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['package_image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['package_image']['error'];
            $_FILES['file']['size'] = $_FILES['package_image']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['package_image']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['package_image'] = 'uploads/' . $filename;

                }
            }
        }
        return $this->db->insert('royalty_pricing_package', $data);
    }

    public function update_package($id)
    {
        // $this->db->select('cp_type');
        // $this->db->where('cp_id', $this->input->post('package_programm'));
        // $pp_type = $this->db->get('course_programm')->row();

        // $data['package_rand_code'] = rand(1000000000, 9999999999);
        $data['package_name'] = $this->input->post('package_name');
        $data['package_price'] = $this->input->post('package_price');
        // if($pp_type->cp_type == 1)
        // {
            $data['package_student_reg_fee'] = $this->input->post('student_reg_amt');
        // }
        // else
        // {
        //     $data['package_student_reg_fee'] = $this->input->post('student_reg_percantage');
        // }
        $data['package_lead_price'] = $this->input->post('package_lead_price');
        $data['package_digital_certificate_price'] = $this->input->post('package_digital_certificate_price');
        $data['package_live_class_price'] = $this->input->post('package_live_class_price');
        $data['package_video_course_percantage'] = $this->input->post('package_video_course_percantage');
        $data['package_balance_amount'] = $this->input->post('package_balance_amount');
        $data['package_color'] = $this->input->post('package_color');
        $data['package_order'] = $this->input->post('package_order');
        $data['package_programm'] = $this->input->post('package_programm');
        $data['package_aff_percantage'] = $this->input->post('package_aff_percantage');
        $data['is_demo_package'] = $this->input->post('is_demo_package');
        // $data['package_program_type'] = $pp_type->cp_type;

        if(!empty($_FILES['package_image']['name']))
        {
            $image = $this->get_package_by_id($id);
            if(file_exists($image->package_image))
            {
                unlink($image->package_image);
            }
            $_FILES['file']['name'] = $_FILES['package_image']['name'];
            $_FILES['file']['type'] = $_FILES['package_image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['package_image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['package_image']['error'];
            $_FILES['file']['size'] = $_FILES['package_image']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['package_image']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
            if($this->upload->do_upload('file'))
            {
                // Get data about the file
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['package_image'] = 'uploads/' . $filename;

                }
            }
        }
        $this->db->where('package_id', $id);
        return $this->db->update('royalty_pricing_package', $data);
    }

    public function get_package_by_id($id)
    {
        $this->db->where('package_id', $id);
        return $this->db->get('royalty_pricing_package')->row();
    }

    public function get_package_by_rand_code($id)
    {
        $this->db->where('package_rand_code', $id);
        return $this->db->get('royalty_pricing_package')->row();
    }

    public function get_all_package()
    {
        $this->db->order_by('package_order', 'ASC');
        return $this->db->get('royalty_pricing_package')->result();
    }

    public function get_all_package_for_atp()
    {
        $atp_prg_id = atp_details()->user_assign_programm;

        foreach (json_decode($atp_prg_id) as $key => $value) 
        {
            $this->db->or_where('package_programm', $value);
        }
        $this->db->where('is_demo_package', 0);
        $this->db->order_by('package_order', 'ASC');
        return $this->db->get('royalty_pricing_package')->result();
    }

    public function delete_package($id)
    {
        $this->db->where('package_id', $id);
        return $this->db->delete('royalty_pricing_package');
    }

    //Royalty Management End

    public function update_gst_setting($id)
    {
        $this->db->where('user_id', $id);
        return $this->db->update('users', ['user_gst' => $this->input->post('user_gst')]);    
    }

    // Expense Management Start

    function get_all_expense_by_atp_id()
    {
        $user_id = user_id();

        $this->db->select('a.*');
        $this->db->from('expense_management a');
        $this->db->where('a.expense_atp_id', $user_id);
        $this->db->order_by('expense_date', 'DESC');
        $this->db->order_by('expense_id', 'DESC');
        return $this->db->get()->result();
    }

    function add_new_expense()
    {
        $user_id = user_id();

        $data = [
            'expense_head_id' => $this->input->post('expense_head_id'),
            'expense_amount' => $this->input->post('expense_amount'),
            'expense_note' => $this->input->post('expense_note'),
            'expense_date' => $this->input->post('expense_date'),
            'expense_atp_id' => $user_id
        ];

        return $this->db->insert('expense_management', $data);
    }


    // Expense Management End

}
