<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id: fees_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/fees_values.php';

/**
 * Used to save a fee
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Fees_import_values extends Fees_values
{

    protected $project_name;
    protected $project_number;
    protected $address_name;
    protected $fee_account_name;

    /**
     * Init an Fees_values object with all values from a given stdClass object
     *
     * @param   object  $values                     stdClass object with fee values to initialize a valid Fees_values object out of it
     * @param   boolean $not_override_wp_parents    NOT IN USE: Optional Do not overwrite the parent of work packages within _get_wp_allocation_vars
     *
     * @return  object  An Fees_values object with all values out of the given stdClass object
     */
    public static function init_from_values(stdClass $values, $not_override_wp_parents=false)
    {
        $obj = new Fees_import_values();
        $obj->CI = CI_Controller::get_instance();

        $obj->add_or_id = $values->id;
        $obj->project_name = $values->project_name;
        $obj->project_number = $values->project_number;
        $obj->fee_date = new UsDate(self::fixDateFormat($values->fee_date));
        $obj->fee_no = $values->fee_no;
        $obj->is_invoiced = (bool)$values->is_invoiced;
        $obj->value = float_to_usnumber($values->value);
        $obj->purpose = $values->purpose;
        $obj->turnover_rate = float_to_usnumber($values->turnover_rate);
        $obj->due_date = new UsDate(self::fixDateFormat($values->due_date));
        $obj->address_name = $values->address_name;
        $obj->fee_account_name = $values->fee_account_name;
        $obj->is_done = false;
        $obj->is_transfered = false;
        $obj->del_after_transfer = false;

        if (isset($values->finally_invoiced)) {
            $obj->finally_invoiced = (boolean) $values->finally_invoiced;
        } else {
            $obj->finally_invoiced = false;
        }

        $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('Fee_accounts_model', 'accounts');

        $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_name === $address->name)
                $this->address_id = $address->id;
        }

        $all_accounts = $this->CI->accounts->get_list();
        foreach ($all_accounts as $account) {
            if ($this->fee_account_name === $account->account_designation)
                $this->fee_account_id = $account->id;
        }
    }

    private static function fixDateFormat($excelDate) {

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

        return $result;
    }

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

    /**
     * Get fee to save
     *
     * @return  array
     */
    public function get_fee_to_save()
    {
        $data = array(
            'project_name'              => $this->project_name,
            'project_number'            => $this->project_number,
            'purpose'                   => $this->purpose,
            'fee_no'                    => $this->fee_no,
            'value'                     => $this->value,
            'fee_date'                  => $this->fee_date->mysql(),
            'address_name'              => $this->address_name,
            'turnover_rate'             => usnumber_to_float($this->turnover_rate),
            'is_invoiced'               => $this->is_invoiced,
            'fee_account_name'          => $this->fee_account_name,
            'finally_invoiced'          => $this->finally_invoiced,
            'due_date'                  => $this->due_date->mysql(),
        );

        return $data;
    }

    public function transform_to_parent() {

        $values = new stdClass();
        $values->id = 'add';
        $values->is_invoiced = $this->is_invoiced;
        $values->is_transfered = $this->is_transfered;
        $values->del_after_transfer = false;
        $values->project_id = $this->project_id;
        $values->purpose = $this->purpose;
        $values->fee_no = $this->fee_no;
        $values->fee_date = $this->fee_date;
        $values->address_id = $this->address_id;
        $values->cost_centre_id = null;
        $values->fee_category_id = $this->fee_category_id;
        $values->value = usnumber_to_float($this->value);
        $values->turnover_rate = $this->turnover_rate;
        $values->work_package_id = $this->work_package_id;
        $values->due_date = $this->due_date;
        $values->fee_account_id = $this->fee_account_id;
        $values->is_done = $this->is_done;
        $values->done_date = $this->done_date;
        $values->deduction = $this->deduction;
        $values->is_percentage = $this->is_percentage;
        $values->remited = $this->remited;
        $values->remit_purpose = $this->remit_purpose;
        $values->reminder_level = $this->reminder_level;
        $values->last_reminder_date = $this->last_reminder_date;
        $values->predecessor_id = $this->predecessor_id;
        $values->successor_id = $this->successor_id;
        $values->finally_invoiced = $this->finally_invoiced;
        $values->addi_fields = array();

        return Fees_values::init_from_values($values);
    }

    public function __sleep() {
        return array(
            'project_name',
            'project_number',
            'project_id',
            'purpose',
            'fee_no',
            'fee_date',
            'address_name',
            'address_id',
            'turnover_rate',
            'is_invoiced',
            'fee_account_name',
            'fee_account_id',
            'finally_invoiced',
            'value',
            'is_done',
            'is_transfered',
            'del_after_transfer',
            'due_date',
        );
    }

    public function __wakeup() {

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