<?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'); }

require_once APPPATH . 'us_libraries/Report_data.php';

/**
 * Used to create, save, load and assign data for the report
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Budget_plan_analysis extends Report_data {

    /**
     * Create the data
     *
     * @param   object      $request_vars           Request vars
     * @param   integer     $fee_multi_mode_set     Optional Fee multiplier set item_id
     * @return  void
     */
    public function create_data($request_vars, $fee_multi_mode_set=null) {
        self::$CI->load->model('Projects_model', 'projects', true);
        self::$CI->load->model('Total_costs_model', 'total_costs');
        self::$CI->load->model('Work_packages_model', 'work_packages');

        self::init_temp_table();

        $projects = self::$CI->projects->get_simple_list($request_vars->old_and_future, !$request_vars->display_like_workinput);

        foreach ($projects as $project) {

            $totals = self::$CI->work_packages->get_list($project->id);

            $request_vars_local = new stdClass();
            $request_vars_local->project = $project->id;
            $request_vars_local->view_is_private_project = true;
            $total_hours = self::$CI->total_costs->get_times_hours_all($request_vars_local, false);

            foreach ($totals as $total) {
                $planned_hours = (isset($total->budget_planned_hours)) ? $total->budget_planned_hours : 0;
                $threshold = (isset($total->budget_threshold)) ? $total->budget_threshold : 0;

                $total_hours_value = 0;
                foreach ($total_hours as $total_hour) {
                    if ($total_hour->work_package_id === $total->id) {
                        $total_hours_value = (isset($total_hour->total)) ? $total_hour->total : 0;
                    }
                }

                $data = array(
                    'id' => $total->project_id,
                    'project' => $total->project,
                    'work_area' => $total->name,
                    'planned_hours' => $planned_hours,
                    'threshold' => $threshold,
                    'threshold_hours' => $planned_hours * $threshold,
                    'total_hours' => $total_hours_value,
                    'total_hours_pctg' => ($planned_hours > 0) ? $total_hours_value / $planned_hours : 0,
                );

                self::$tempdb->simple_query(self::$tempdb->insert_string(self::$temptable, $data));
            }
        }
        unset($totals);
    }

    /**
     * Assign values of the report to smarty
     *
     * @param   object  $request_vars   Optional Request vars
     *
     * @return  void
     */
    public function assign($request_vars = null) {
        $tempdb = self::$tempdb;
        $temptable = self::$temptable;

        $tempdb->select('*');

        $tempdb->where('threshold_hours < total_hours');
        $tempdb->where('planned_hours > ', 0);
        $tempdb->where('threshold > ', 0);
        $tempdb->where('total_hours > ', 0);

        $tempdb->order_by('total_hours_pctg', 'desc');

        $query = $tempdb->get($temptable);
        self::$CI->smarty->assign('data_list', $query->result());
    }

    /**
     * Create the temp table
     *
     * @param   object  $request_vars           Request vars
     * @return  void
     */
    public function init_temp_table($request_vars=null)
    {
        self::$CI->load->library('US_TempDB', '', 'tempdb');

        $temptable = us_uniqid('budget_plan', true);
        self::$CI->tempdb->add_field('id', 'integer');
        self::$CI->tempdb->add_field('project', 'varchar(200)');
        self::$CI->tempdb->add_field('work_area', 'varchar(200)');
        self::$CI->tempdb->add_field('planned_hours', 'double default 0');
        self::$CI->tempdb->add_field('threshold', 'double default 0');
        self::$CI->tempdb->add_field('threshold_hours', 'double default 0');
        self::$CI->tempdb->add_field('total_hours', 'double default 0');
        self::$CI->tempdb->add_field('total_hours_pctg', 'double default 0');

        $tempdb = self::$CI->tempdb->create_table($temptable, US_TempDB::ENGINE_MEMORY);

        self::$tempdb = $tempdb;
        self::$temptable = $temptable;
    }

}

