<?php namespace App\Models;

use CodeIgniter\Model;

class mScheduled extends Model {
    protected $table = 'budget_scheduled';
    protected $primaryKey = 'id';
    
    protected $useAutoIncrement = true;
    protected $returnType = 'array';
    protected $useSoftDeletes = false;
    protected $allowedFields = ['creat_date','next_date','data'];
    protected $useTimestamps = false;
    protected $skipValidation = false;

    public function calcNextDate($date, $num, $type)
	{
 		$start = strtotime( $date );
        $period = $num .' '. $type;
		$future = strtotime( '+' . $period, $start );
		$check = strtotime( '-' . $num .' '. $type, $future );
		if ( $check > $start )
			$future = strtotime( 'last day of previous month', $future );
		return date( "Y-m-d", $future );
	}

    public function getEntry($id){
        $accounts = model('App\Models\mAccounts');
        $data = $this->asObject()->find($id);
        $result = (array)json_decode($data->data);
        $result['id'] = $id;
        $result['datum'] = $data->next_date; //date( "d.m.Y", strtotime($data->next_date));
        for ($i=0;$i<10;$i++){
            $result['category_parent'][$i]= $accounts->getParent($result['category'][$i]);
        }
        return $result;
    }

    public function getEntries(){
        $quelle = model('App\Models\mAccounts');
        $result = $this->asObject()->findAll();
        foreach ($result as $value) {
            $details = json_decode($value->data);
            for ($i=0;$i<10;$i++){
                if ((floatval($details->input[$i]) == 0.0) && (floatval($details->output[$i]) == 0.0))
                    continue;
                $value->details[] = ['comment'=>$details->comment[$i], 'description'=>$quelle->getDropDownEntry($details->category[$i]), 'subamount'=>(floatval($details->input[$i])-floatval($details->output[$i]))];
            }
            //$value->details = null;
            $value->datum =  date( "d.m.Y", strtotime( $value->next_date ));
            $value->source = $quelle->getDropDownEntry($details->source);
            $value->receiver = $details->receiver;
            $value->amount = floatval($details->total_in)-floatval($details->total_out);
//            $value->amount = number_format( $value->amount , 2, ',', '.' );
        }
        return $result;
     }

    public function saveBill($data){
        $accounts = model('App\Models\mAccounts');
        $data['validate'] = true;
        $data['scheduled'] = true;
        $data['renummer'] = isset($data['renummer']);

        if ($data['transfer']==1){
            $data['receiver'] = $accounts->getDropDownEntry($data['category'][0]);
            $data['transfer'] = true;
        }
        else{
            $data['multi'] = isset($data['multiple']);
            $data['transfer'] = false;
        }
        unset($data['openamount']);
        unset($data['category_parent']);
        $data['datum'] = date( "Y-m-d", strtotime($data['datum'] ) );
        $this->set('next_date', $data['datum']);
        
        $data['total_in'] = 0;
		$data['total_out'] = 0;
        for ($i=0;$i<10;$i++){
            if ($data['category'][$i] > 0){
                $data['total_in'] += floatval($data['input'][$i]);
        		$data['total_out'] += floatval($data['output'][$i]);
            }
        }
        $this->set('data',json_encode($data));
        
        if ( 0 == $data['id'])  $this->insert();
        else $this->update($data['id']);
     }
}    