<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id$
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

/**
 * Used to create, save, load and assign data for the report
 */
class Staff_timelog_times
{
    protected $projects_list = null;
    protected $overtime_rates = null;
    
    /**
     * The main CI object
     */
    protected $CI;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->CI = CI_Controller::get_instance(); //$this does not work - Libs only get assigned to the module at load
    }
    
    /**
     * Init is_included_in_time_sheet projects
     * 
     * @return  array
     */
    public function get_projects()
    {
        $this->CI->load->model('Projects_model', 'projects');
        
        $this->projects_list = $this->CI->projects->get_list_for_overtime_calculation();
        
        return $this->projects_list;
    }
    
    /**
     * Init overtime rates
     * 
     * @return  array
     */
    public function get_overtime_rates()
    {
        $this->CI->load->model('Overtime_rates_model', 'overtime_rates');
        
        $this->overtime_rates = $this->CI->overtime_rates->get_list();
        
        return $this->overtime_rates;
    }

    /**
     * Get the total times list
     *
     * @param   integer $staff_id   Staff ID
     * @param   object  $from       From date
     * @param   object  $to         To date
     * @return  array
     */
    public function get_for_total_times($staff_id, UsBasicDateTime $from, UsBasicDateTime $to)
    {
        if (!isset($this->projects_list)) {
            basic_error('Call self::get_projects() before!', null, __CLASS__.'::'.__FUNCTION__);
        }
        if (!isset($this->overtime_rates)) {
            basic_error('Call self::get_overtime_rates() before!', null, __CLASS__.'::'.__FUNCTION__);
        }
        
        $this->CI->load->model('Total_times_model', 'total_times');
        
        $projects = array();
        foreach ($this->projects_list as $value) {
            array_push($projects, $value->id);
        }
        
        $overtime_rates = array();
        foreach ($this->overtime_rates as $overtime_rate) {
            array_push($overtime_rates, $overtime_rate->id);
        }
        
        $home_office_use = $this->CI->system_settings->getboolAdd_home_office();
        
        return $this->CI->total_times->get_for_period($staff_id, $from, $to, $projects, $overtime_rates, 'day', false, true, $home_office_use);
    }
    

} // END class Staff_timelog_times
