<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id: externalexpenses_values.php 18893 2019-06-06 14:56:41Z christian $
*/

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

require_once APPPATH.'us_libraries/value_objects/externalexpenses_values.php';

/**
 * Used to save a external expense
 */
class Externalexpenses_import_values extends Externalexpenses_values
{
    protected $project_name;
    protected $project_number;
    protected $address;
    protected $work_package;

    public static function init_from_upload_values(stdClass $values) {

        $obj = new Externalexpenses_import_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = 'add';
        $obj->project_name = $values->project_name;
        $obj->project_number = $values->project_number;
        $obj->purpose = $values->purpose;
        $obj->address = $values->address;
        $obj->value = $values->value;
        $obj->work_package = $values->work_package;
        $obj->date = new UsDate(self::fixDateFormat($values->date));
        $obj->turnover_rate = $values->turnover_rate;
        $obj->description = $values->description;
        $obj->is_calc_from_expenses = $values->is_calc_from_expenses;
        $obj->charged = $values->charged;
        $obj->charge_date = new UsDate(self::fixDateFormat($values->charge_date));
        $obj->surcharge_used = $values->surcharge_used;
        $obj->is_invoiced = $values->is_invoiced;

        $obj->_prepare_ids();

        return $obj;
    }

    private function _prepare_ids() {

        $this->CI->load->model('Projects_model', 'projects');
        $this->CI->load->model('Addresses_model', 'addresses');
        $this->CI->load->model('Work_packages_model', 'packages');

        $proj_req_vars = new stdClass();

        if (isset($this->project_number))
            $proj_req_vars->project_number = $this->project_number;
        else
            $proj_req_vars->project_name = $this->project_name;

        $this->project_id = $this->CI->projects->get_list($proj_req_vars, null, null, null, 'id', 1, false, false)[0]->id;

        $all_addresses = $this->CI->addresses->get_list();
        foreach ($all_addresses as $address) {
            if ($this->address === $address->name)
                $this->address_id = $address->id;
        }

        $all_work_packages = $this->CI->packages->get_list($this->project_id);
        foreach ($all_work_packages as $package) {
            if ($this->work_package === $package->name)
                $this->work_package_id = $package->id;
        }
    }

    private static function fixDateFormat($excelDate) {

        $result = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::toFormattedString($excelDate, 'YYYY-MM-DD HH:MM:SS');

        return $result;
    }

    /**
     * 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), 'cost_centre_id' => intval($this->cost_centre_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),

                      'project_name' => $this->project_name, 'project_number' => $this->project_number, 'address' => $this->address, 'work_package' => $this->work_package,);

        if (isset($this->surcharge_used)) {
            $data['surcharge_used'] = usnumber_to_float($this->surcharge_used) / 100;
        }
        else {
            $data['surcharge_used'] = null;
        }

        return $data;
    }

    public function transform_to_parent() {

        $values = new stdClass();
        $values->id = $this->add_or_id;
        $values->is_invoiced = $this->is_invoiced ?? false;
        $values->is_transfered = false;
        $values->project_id = $this->project_id;
        $values->purpose = $this->purpose;
        $values->description = $this->description;
        $values->date = $this->date;
        $values->address_id = $this->address_id;
        $values->cost_centre_id = null;
        $values->fee_category_id = null;
        $values->value = $this->value;
        $values->surcharge_used = $this->surcharge_used ?? false;
        $values->work_package_id = $this->work_package_id;
        $values->is_hon_decr = $this->is_hon_decr ?? false;
        $values->is_calc_from_expenses = $this->is_calc_from_expenses ?? false;
        $values->charged = $this->charged;
        $values->charge_date = $this->charge_date;
        $values->valid_from = new UsDate('now');
        $values->turnover_rate = $this->turnover_rate ?? 0;
        $values->addi_fields = array();

        return Externalexpenses_values::init_from_values($values);
    }

    public function __sleep() {
        return array(
            'add_or_id',
            'project_id',
            'is_invoiced',
            'is_transfered',
            'del_after_transfer',
            'purpose',
            'description',
            'date',
            'address_id',
            'cost_centre_id',
            'fee_category_id',
            'value',
            'surcharge_used',
            'work_package_id',
            'is_hon_decr',
            'is_calc_from_expenses',
            'charged',
            'charge_date',
            'valid_from',
            'turnover_rate',
            'currency_id',
            'exchange_rate',
            'project_name',
            'project_number',
            'address',
            'work_package',
        );
    }

    public function __wakeup() {

        $this->CI = CI_Controller::get_instance();
    }
} // END class Externalexpenses_values
