<?php

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

class Company_projects_model extends CI_Model {

    public function get_list_forform($company_id = null) {
        if (!is_numeric($company_id))
            return null;
        $options = array();
        $this->db->select('cp.id,cp.project_name');
        $this->db->from('company_projects cp');
        if (is_numeric($company_id))
            $this->db->where('cp.company_id', $company_id);
        $this->db->order_by('cp.project_name asc');
        $result = $this->db->get()->result_array();
        if ($result) {
            foreach ($result as $data) {
                $options[$data['id']] = $data['project_name'];
            }
        }
        return $options;
    }

    public function get_list($company_id = null, $per_page = 10, $page = 1, $check_count = false) {
        $this->db->select('cp.*, count(pt.id) as amount_tasks');
        $this->db->from('company_projects cp');
        $this->db->join('projects_tasks pt','pt.project_id = cp.id','left');        
        $this->db->group_by('cp.id');
        if (is_numeric($company_id))
            $this->db->where('cp.company_id', $company_id);
        $this->db->order_by('cp.project_name asc');

        if ($check_count)
            return $this->db->get()->num_rows();
        else {
            if ($per_page != '')
                $this->db->limit($per_page);
            if ($page != 0) {
                $offset = ($page == 1) ? 0 : ($page - 1) * $per_page;
                $this->db->offset($offset);
            }
            $result = $this->db->get()->result_array();
            return $result;
        }
    }
    
    public function get_name($project_id) {
        if (!is_numeric($project_id))
            return null;
        $this->db->select('cp.project_name');
        $this->db->from('company_projects cp');
        $this->db->where('cp.id', $project_id);
        $result = $this->db->get()->row_array();
        return $result['project_name'];
    }
    

    public function get_project($project_id) {
        if (!is_numeric($project_id))
            return null;
        $this->db->select('cp.*');
        $this->db->from('company_projects cp');
        $this->db->where('cp.id', $project_id);
        $result = $this->db->get()->row_array();
        return $result;
    }    


    public function get_companyid_byproject($project_id) {
        if (!is_numeric($project_id))
            return null;
        $this->db->select('cp.company_id');
        $this->db->from('company_projects cp');
        $this->db->where('cp.id', $project_id);
        $result = $this->db->get()->row_array();
        return $result['company_id'];
    }

    public function insert($company_id, $data) {
        if (!is_numeric($company_id))
            return false;
        $data['company_id'] = $company_id;
        $add = $this->db->insert('company_projects', $data);
        return $add;
    }
    
    public function update($project_id, $data) {
        if (!is_numeric($project_id))
            return false;
        $add = $this->db->update('company_projects', $data, array('id' => $project_id));
        return $add;
    }
        
    public function delete($project_id) {
        if (!is_numeric($project_id))
            return false;
        $del = $this->db->delete('company_projects', array('id' => $project_id));
        return $del;
    }
    
    // RATES OF PROJECT
     public function check_rate_row($company_id, $project_id){

        $this->db->select('cpr.id');
        $this->db->from('company_projects_rates cpr');
        $this->db->where('cpr.project_id', $project_id);
        $this->db->where('cpr.company_id', $company_id);         
        $result = $this->db->get()->row_array();
        return $result;        
     }
    
    
    public function get_project_rates($project_id) {
        if (!is_numeric($project_id))
            return null;
        $this->db->select('cpr.*');
        $this->db->from('company_projects_rates cpr');
        $this->db->where('cpr.project_id', $project_id);
        $result = $this->db->get()->row_array();
        return $result;
    }   

    public function insert_rate($company_id, $data, $project_id) {
        if (!is_numeric($company_id))
            return false;
        
        $data['company_id'] = $company_id;
        $data['project_id'] = $project_id;
        $add = $this->db->insert('company_projects_rates', $data);
        return $add;
    }
    
    public function update_rate($project_id, $data, $project_rate_id) {

        $add = $this->db->update('company_projects_rates', $data, array('id' => $project_rate_id));
        return $add;
    }      
    
    //-=------------------------- user ----------------------------------------------
    
    public function check_rate_row_user($user_id, $project_id){
        $this->db->select('upr.id');
        $this->db->from('users_projects_rates upr');
        $this->db->where('upr.project_id', $project_id);
        $this->db->where('upr.user_id', $user_id);         
        $result = $this->db->get()->row_array();
        return $result;        
     }
    
      public function get_project_rates_user($project_id) {
        if (!is_numeric($project_id))
            return null;
        $this->db->select('upr.*');
        $this->db->from('users_projects_rates upr');
        $this->db->where('upr.project_id', $project_id);
        $result = $this->db->get()->row_array();
        return $result;
    }   
    
    public function insert_rate_user($user_id, $data, $project_id) {
        if (!is_numeric($user_id))
            return false;
        
        $data['user_id'] = $user_id;
        $data['project_id'] = $project_id;
        $add = $this->db->insert('users_projects_rates', $data);
        return $add;
    }
    
    public function update_rate_user($project_id, $data, $project_rate_id) {

        $add = $this->db->update('users_projects_rates', $data, array('id' => $project_rate_id));
        return $add;
    } 
    
    

}
