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

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

require_once APPPATH.'us_libraries/Value_object_addi.php';

/**
 * Used to save a external expense
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Externalexpenses_values extends Value_object_addi
{
    protected $project_id;
    protected $is_invoiced;
    protected $is_transfered;
    protected $del_after_transfer;
    protected $purpose;
    protected $description;
    protected $date;
    protected $address_id;
    protected $fee_category_id;
    protected $value;
    protected $surcharge_used;
    protected $work_package_id;
    protected $is_hon_decr;
    protected $is_calc_from_expenses;
    protected $charged;
    protected $charge_date;
    protected $valid_from;
    protected $turnover_rate;

    protected $file;
    protected $file_type;
    protected $invoice_number;
    protected $invoice_due_date;

    protected $currency_id;
    protected $exchange_rate;
    
    /**
     * Init the externalexpenses values from $_POST
     *
     * @return  object  An Externalexpenses_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Externalexpenses_values();
        $obj->CI = CI_Controller::get_instance();
        
        $obj->CI->load->model('System_settings_model', 'system_settings');
        
        $obj->add_or_id = $obj->CI->input->post('externalexpenses_edit');
        $obj->is_invoiced = $obj->CI->input->post('externalexpenses_is_invoiced');
        $obj->is_transfered = (boolean) $obj->CI->input->post('externalexpenses_is_transfered');
        $obj->del_after_transfer = (boolean) $obj->CI->input->post('externalexpenses_del_after_transfer');
        $obj->project_id = $obj->CI->input->post('externalexpenses_project');
        $obj->purpose = $obj->CI->input->post('externalexpenses_purpose');
        $obj->description = $obj->CI->input->post('externalexpenses_description');
        $obj->date = new UsDate($obj->CI->input->post('externalexpenses_date'), null, true, null);
        $obj->address_id = $obj->CI->input->post('externalexpenses_address');
        $obj->fee_category_id = $obj->CI->input->post('externalexpenses_fee_category_name');
        $obj->value = $obj->CI->input->post('externalexpenses_value');
        $obj->surcharge_used = $obj->CI->input->post('externalexpenses_surcharge_used');
        $obj->work_package_id = $obj->CI->input->post('externalexpenses_work_package_name');
        $obj->is_hon_decr = $obj->CI->input->post('externalexpenses_is_hon_decr') ? true : false;
        $obj->is_calc_from_expenses = $obj->CI->input->post('externalexpenses_is_calc_from_expenses') ? true : false;
        $obj->charged = $obj->CI->input->post('externalexpenses_charged');
        $obj->charge_date = new UsDate($obj->CI->input->post('externalexpenses_charge_date'), null, true, null);
        $obj->valid_from = new UsDate($obj->CI->input->post('externalexpenses_valid_from'), null, true, null);
        $obj->addi_vars = $obj->CI->externalexpenses->get_addi_vars_post();
        $turnover_rate = usnumber_to_float($obj->CI->input->post('externalexpenses_turnover_rate'))/100;
        $obj->turnover_rate = float_to_usnumber($turnover_rate);

        $obj->file = $obj->CI->input->post('externalexpenses_file');
        $obj->file_type = intval($obj->CI->input->post('externalexpenses_file_type'));
        $obj->invoice_number = $obj->CI->input->post('externalexpenses_invoice_number');
        $obj->invoice_due_date = new UsDate($obj->CI->input->post('externalexpenses_invoice_due_date'), null, true, null);

        $obj->currency_id = null;
        $obj->exchange_rate = null;
        if ($obj->CI->system_settings->getboolProject_use_currency()) {
            $obj->currency_id = $obj->CI->input->post('externalexpenses_currency_id');
            $obj->exchange_rate = $obj->CI->input->post('externalexpenses_exchange_rate');
            
            if (empty($obj->currency_id)) {
                $obj->currency_id = $obj->CI->system_settings->getGlobal_currency();
            }
            if (empty($obj->exchange_rate)) {
                $obj->exchange_rate = 1;
            }
        }

        return $obj;
    }

     /**
     * Init the externalappointment values from a given stdClass object
     *
     * @param   object  $values stdClass object with extexp values to initialize a valid Externalexpenses_values object out of it
     *
     * @return  object  An Externalexpenses_values object with all values out of the given stdClass object
     */
    public static function init_from_values(stdClass $values)
    {
        $obj = new Externalexpenses_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $values->id;
        $obj->is_invoiced = $values->is_invoiced;
        $obj->is_transfered = (boolean) $values->is_transfered;
        $obj->project_id = $values->project_id;
        $obj->purpose = $values->purpose;
        $obj->description = $values->description;
        $obj->date = clone $values->date;
        $obj->address_id = $values->address_id;
        if (isset($values->fee_category_id)) {
            $obj->fee_category_id = $values->fee_category_id;
        } else {
            $obj->fee_category_id = null;
        }
        $obj->value = float_to_usnumber($values->value);
        if (isset($values->surcharge_used) && $values->surcharge_used!=='') {
            $obj->surcharge_used = float_to_usnumber($values->surcharge_used);
        } else {
            $obj->surcharge_used = null;
        }
        $obj->work_package_id = $values->work_package_id;
        $obj->is_hon_decr = (boolean) $values->is_hon_decr;
        $obj->is_calc_from_expenses = (boolean) $values->is_calc_from_expenses;
        $obj->charged = float_to_usnumber($values->charged);
        $obj->charge_date = clone $values->charge_date;
        $obj->valid_from = clone $values->valid_from;
        $obj->turnover_rate = float_to_usnumber($values->turnover_rate);

        if (isset($values->file) && $values->file!=='') {
            $obj->file = $values->file;
        }
        if (isset($values->file_type) && $values->file_type!=='') {
            $obj->file_type = intval($values->file_type);
        }

        $obj->currency_id = null;
        if (isset($values->currency_id)) {
            $obj->currency_id = $values->currency_id;
        }
        $obj->exchange_rate = null;
        if (isset($values->exchange_rate)) {
            $obj->exchange_rate = float_to_usnumber($values->exchange_rate);
        }

        if (isset($values->invoice_number)) {
            $obj->invoice_number = $values->invoice_number;
        }
        if (isset($values->invoice_due_date)) {
            $obj->invoice_due_date = $values->invoice_due_date;
        }

        if (!isset($values->addi_fields)) {
            $obj->CI->load->model('externalexpenses_model', 'externalexpenses');
            $values->addi_fields = $obj->CI->externalexpenses->get_addi_list();
        }
        self::init_addi_vars_from_values($obj, $values);
        return $obj;
    }

    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     *
     * @todo    implement!!
     */
    public function validate()
    {
        $a_id = explode(Externalexpenses_model::ID_SEPERATOR,$this->add_or_id);
        if ($this->add_or_id!=='add' && (!is_array($a_id)) && count($a_id) != 2) {
            return 'External expense ID has to be "add" or an ID';
        }
        return false;
    }

    /**
     * Get externalexpense to save
     *
     * @return  array
     */
    public function get_extexp_to_save()
    {
        $data = array(
            'project_id'                =>  intval($this->project_id),
            'purpose'                   =>  $this->purpose,
            'description'               =>  $this->description,
            'date'                      =>  $this->date->mysql(),
            'address_id'                =>  intval($this->address_id),
            'fee_category_id'           =>  intval($this->fee_category_id),
            'work_package_id'           =>  intval($this->work_package_id),
            'is_hon_decr'               =>  $this->is_hon_decr,
            'is_calc_from_expenses'     =>  $this->is_calc_from_expenses,
            'charged'                   =>  usnumber_to_float($this->charged),
            'charge_date'               =>  $this->charge_date->mysql(),
            'turnover_rate'             =>  usnumber_to_float($this->turnover_rate),
        );
        
        if (isset($this->currency_id) && isset($this->exchange_rate)) {
            $value_otcurr = usnumber_to_float($this->value);
            $exchange_rate = usnumber_to_float($this->exchange_rate);
            $data['currency_id'] = $this->currency_id;
            $data['value'] = $value_otcurr*$exchange_rate;
            $data['value_otcurr'] = $value_otcurr;
        } else {
            $data['value'] = usnumber_to_float($this->value);
        }
        
        if (isset($this->surcharge_used)) {
            $data['surcharge_used'] = usnumber_to_float($this->surcharge_used)/100;
        } else {
            $data['surcharge_used'] = null;
        }
        if (!isset($this->is_invoiced) || !$this->is_invoiced) {
            if ($this->valid_from!==null && !$this->valid_from->is_null()) {
                $data['valid_from'] = $this->valid_from->mysql();
            } else {
                $now = new UsDate('now');
                $data['valid_from'] = $now->mysqlday();
            }
        } else {
            $data['invoice_number'] = $this->invoice_number;
            if ($this->invoice_due_date!==null && !$this->invoice_due_date->is_null()) {
                $data['invoice_due_date'] = $this->invoice_due_date->mysql();
            } else {
                $data['invoice_due_date'] = null;
            }
        }
        return $data;
    }

    /**
     * Get project ID
     * 
     * @return  int
     */
    public function get_project_id(): int
    {
        return intval($this->project_id);
    }

    /**
     * Get work work package ID
     * 
     * @return  int
     */
    public function get_work_package_id(): int
    {
        return intval($this->work_package_id);
    }

    /**
     * Get the invoiced type of the  Externalexpense
     *
     * @return  boolean
     */
    public function get_invoiced_type()
    {
        $id = $this->getId();
        if( $id == 'add') {
            $type = $this->is_invoiced;
        } else {
            $a = explode(Externalexpenses_model::ID_SEPERATOR, $id);
            $type = $a[1];
        }

        switch ($type) {
            case Externalexpenses_model::TYPE_INVOICED:
                return Externalexpenses_model::TYPE_INVOICED;
                break;
            case Externalexpenses_model::TYPE_NOT_INVOICED:
                return Externalexpenses_model::TYPE_NOT_INVOICED;
                break;
            default:
                basic_error('Invoiced Type is invalid!', null, __CLASS__.'::'.__FUNCTION__);
                break;
        }
    }

    /**
     * Get the ID
     *
     * Only valid if self::isNew() === false
     *
     * @return  string      The ID
     */
    public function getId()
    {
        return $this->add_or_id;
    }

    /**
     * Check if the extexp should be deleted after transfer
     *
     * @return  boolean
     */
    public function del_after_transfer()
    {
        if( $this->is_transfered() && $this->del_after_transfer === true){
            return true;
        } else {
            return false;
        }
    }

    /**
     * Set the extexp as transfered from outside
     *
     * @return  void
     */
    public function set_transfered()
    {
        $this->is_invoiced = Externalexpenses_model::TYPE_INVOICED;
    }

    /**
     * Check if the extexp is transfered from planned to invoiced
     *
     * @return  boolean
     */
    public function is_transfered()
    {
        $id = $this->getId();
        if( $id !== 'add' && $this->is_transfered === false){
            if($this->is_invoiced != $this->get_invoiced_type() && $this->is_invoiced == Externalexpenses_model::TYPE_INVOICED){
                return true;
            }
        }
        return false;
    }

    /**
     * Get temp file name
     *
     * @return mixed
     */
    public function get_file()
    {
        return $this->file;
    }

    /**
     * Get file type id
     *
     * @return mixed
     */
    public function get_file_type()
    {
        return $this->file_type;
    }

    /**
     * Get purpose
     *
     * @return string
     */
    public function get_purpose(): string
    {
        return $this->purpose ?? '';
    }

    /**
     * Get address ID
     *
     * @return  int
     */
    public function get_address_id(): int
    {
        return intval($this->address_id);
    }

    /**
     * Get date
     *
     * @return UsDate
     */
    public function get_date(): UsDate
    {
        return $this->date;
    }

    /**
     * Get charge date
     *
     * @return UsDate
     */
    public function get_charge_date(): UsDate
    {
        return $this->charge_date;
    }


} // END class Externalexpenses_values
