<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id: calcexp_values.php 7037 2014-03-12 16:32:31Z christian $
*/

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

require_once APPPATH.'us_libraries/Value_object.php';

/**
 * Used to save a calculate level_of_completion item
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Calcexp_level_of_completion_values extends Value_object
{
    protected $project;
    protected $project_offer;
    protected $select_type;
    protected $select_types;
    protected $vat;
    protected $due_days;
    protected $discount;
    protected $discount_days;
    protected $from_date;
    protected $to_date;
    protected $level_of_completion_billable_mode;
    protected $level_of_completion_billable_vars;
    protected $round;
    protected $round_gross;

    /**
     * Init the values from $_POST
     *
     * @return  object  An Calcexp_level_of_completion_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Calcexp_level_of_completion_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('calcexp_level_of_completion_edit');
        
        $obj->project = intval($obj->CI->input->post('filter_level_of_completion_project'));
        $obj->project_offer = intval($obj->CI->input->post('filter_level_of_completion_project_offer'));
        
        $obj->select_type = $obj->CI->input->post('calcexp_level_of_completion_select_type');
        $obj->select_types = $obj->_get_calcexp_level_of_completion_select_type_post();
        $obj->vat = usnumber_to_float($obj->CI->input->post('calcexp_level_of_completion_vat'));
        $obj->due_days = $obj->CI->input->post('calcexp_level_of_completion_due_days');
        $obj->discount = usnumber_to_float($obj->CI->input->post('calcexp_level_of_completion_discount'));
        $obj->discount_days = $obj->CI->input->post('calcexp_level_of_completion_discount_days');
        
        $obj->from_date = new UsDate($obj->CI->input->post('calcexp_level_of_completion_from_date'), null, true, null);
        $obj->to_date = new UsDate($obj->CI->input->post('calcexp_level_of_completion_to_date'), null, true, null);
        
        $obj->level_of_completion_billable_mode = $obj->CI->input->post('calcexp_level_of_completion_billable_mode');
        $obj->level_of_completion_billable_vars = $obj->_get_calcexp_level_of_completion_billable_post();
        
        $obj->round = intval($obj->CI->input->post('calcexp_level_of_completion_round'));
        $obj->round_gross = (boolean) $obj->CI->input->post('calcexp_level_of_completion_round_gross');
        
        return $obj;
    }
    
    /**
     * Init the values from a given stdClass object
     * 
     * @param   object  $values stdClass object with offers values to initialize a valid object out of it
     *
     * @return  object  An Calcexp_level_of_completion_values object with all values out of the given stdClass object
     */
    public static function init_from_values(stdClass $values)
    {
        $obj = new Calcexp_level_of_completion_values();
        $obj->CI = CI_Controller::get_instance();
        
        $obj->add_or_id = $values->id;
        
        $obj->project_id = $values->project_id;
        $obj->project_offer = intval($values->project_offer);
        
        $obj->select_type = intval($values->correspondence_type_id);
        $obj->select_types = (isset($values->corr_component_type_ids)) ? $values->corr_component_type_ids : null;
        $obj->vat = floatval($values->vat);
        $obj->due_days = (isset($values->due_days)) ? $values->due_days : '';
        $obj->discount = (isset($values->discount)) ? $values->discount : '';
        $obj->discount_days = (isset($values->discount_days)) ? $values->discount_days : '';
        
        $obj->from_date = (isset($values->from_date)) ? $values->from_date : new UsDate('');
        $obj->to_date = (isset($values->to_date)) ? $values->to_date : new UsDate('');
        
        $obj->level_of_completion_billable_mode = (isset($values->billable_mode)) ? $values->billable_mode : '';
        $obj->level_of_completion_billable_vars = $values->billable_vars;
        
        $obj->round = (isset($values->round)) ? $values->round : 0;
        $obj->round_gross = (isset($values->round_gross)) ? $values->round_gross : false;
        
        return $obj;
    }
    
    /**
     * Get an array with calcexp_level_of_completion_billable percentages
     *
     * This function loads the value form $_POST
     *
     * @return  array   Values 
     */
    private function _get_calcexp_level_of_completion_billable_post()
    {
        $post_keys = preg_grep("@^calcexp_level_of_completion_billable_[0-9]+@", array_keys($_POST));
        #$post_keys_val = preg_grep("@^calcexp_level_of_completion_billable_value_[0-9]+@", array_keys($_POST));

        $vars = array();
        foreach ($post_keys as $key) {
            $fee_areas_offer_id = intval(str_ireplace('calcexp_level_of_completion_billable_', '', $key));
            $key_val = str_replace('calcexp_level_of_completion_billable_', 'calcexp_level_of_completion_billable_value_', $key);
            $vars[$fee_areas_offer_id] = array(
                $this->CI->input->post($key),
                $this->CI->input->post($key_val),
            );
        }
        return $vars;
    }
    
    /**
     * Get an array with calcexp_level_of_completion select_types
     *
     * This function loads the value form $_POST
     *
     * @return  array   Values 
     */
    private function _get_calcexp_level_of_completion_select_type_post()
    {
        $post_keys = preg_grep("@^calcexp_level_of_completion_select_type_[0-9]+@", array_keys($_POST));
        
        $vars = array();
        foreach ($post_keys as $key) {
            $offer_id = intval(str_ireplace('calcexp_level_of_completion_select_type_', '', $key));
            $vars[$offer_id] = $this->CI->input->post($key);
        }
        return $vars;
    }
    
    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     *
     * @todo    implement!!
     */
    public function validate()
    {
        if (!is_numeric($this->add_or_id) && $this->add_or_id!=='add') {
            return 'Item ID has to be an ID';
        }
        
        if ($this->add_or_id==='add') {
            if (empty($this->select_type)) {
                return $this->CI->lang->linef('required', $this->CI->lang->line('calcexp_corr_type'));
            }
        }

        return false;
    }
    
    /**
     * Get vars to save
     *
     * @return  array
     */
    public function get_vars_to_save()
    {
        $data = array(
            'vat'                       => floatval($this->vat)/100,
            'due_days'                  => (integer)$this->due_days,
            'discount'                  => floatval($this->discount)/100,
            'discount_days'             => (integer)$this->discount_days,
            'from_date'                 => $this->from_date->mysql(),
            'to_date'                   => $this->to_date->mysql(),
            'round'                     => (integer)$this->round,
            'round_gross'               => (boolean)$this->round_gross,
        );
        
        if ($this->isNew()) {
            $data['offer_id'] = (integer)$this->project_offer;
            $data['status'] = Payments_model::S_PLANNED;
            
            $this->CI->load->model('Correspondence_types_model', 'correspondence_types', true);
            
            $correspondence_type = $this->CI->correspondence_types->get_item($this->select_type);
            switch ($correspondence_type->special_type) {
                case Correspondence_types_model::SPECIAL_TYPE_PAYMENT_INSTALLMENT:
                    $data['fee_type'] = Payments_model::T_INSTALMENT;
                    break;
                case Correspondence_types_model::SPECIAL_TYPE_PAYMENT_FINAL:
                    $data['fee_type'] = Payments_model::T_FINAL;
                    break;
                case Correspondence_types_model::SPECIAL_TYPE_COLLECTIVE_OFFER:
                case Correspondence_types_model::SPECIAL_TYPE_COLLECTIVE_BILL:
                case Correspondence_types_model::SPECIAL_TYPE_PAYMENT_COMPONENT:
                    $data['fee_type'] = 0;
                    break;
                default:
                    basic_error('The correspondence type has an unknown or invalid special_type:'.$correspondence_type->special_type, null, __CLASS__.'::'.__FUNCTION__);
                    break;
            }
            
            $data['correspondence_type_id'] = (integer)$this->select_type;
            $data['corr_component_type_ids'] = !empty($this->select_types) ? json_encode($this->select_types) : null;
        }
        
        return $data;
    }
    
    /**
     * Get calcexp_level_of_completion_billable mode to save
     *
     * @return  array
     */
    function get_level_of_completion_billable_mode() {
        return $this->level_of_completion_billable_mode;
    }

    /**
     * Get calcexp_level_of_completion_billable percentage vars to save
     *
     * @return  array
     */
    public function get_level_of_completion_billable_vars()
    {
        return $this->level_of_completion_billable_vars;
    }

    /**
     * Get project offer
     * 
     * @return  integer
     */
    function get_project_offer()
    {
        return (integer)$this->project_offer;
    }
    
} // END class Calcexp_level_of_completion_values
