<?php

namespace App\Controllers;
use App\Models\mBills;
use App\Models\mAccounts;

class Converter extends BaseController
{
    private function loadFormDefaults(){
		$data['id'] = 0;
		$data['source'] = 0;
		$data['datum'] ='';
		$data['multi'] = false;
		$data['transfer'] = false;
		$data['receiver'] = '';
		$data['total_in'] = 0;
		$data['total_out'] = 0;
		$data['validate'] = false;
		$data['scheduled'] = false;
		$data['scheduledNum'] = 0;
		$data['scheduledType'] = 0;
		for ($i=0;$i<10;$i++){
			$data['sid'][$i] = 0;
			$data['category_parent'][$i] = 0;
			$data['category'][$i] = 0;
			$data['input'][$i] = 0;
			$data['output'][$i] = 0;
			$data['comment'][$i] = '';
		}
		return $data;
	}

	public function convertDataOld(){
		$bills = model('App\Models\mBills');
		$details = model('App\Models\mBillDetails');
		$result = $bills->where('type','bill')->findAll();
		foreach( $result as $row){
			$data = array();
			$data['booking_id'] = $row->id;
			$data['category_id'] = $row->target_id;
			$data['comment'] = $row->comment;
			$data['subamount'] = $row->amount;
			$details->insert($data);
			$bills->update($row->id,['type'=>'multiple']);
			echo $row->id; echo "\n";
		}
		$result = $bills->where('type','transfer')->findAll();
		foreach( $result as $row){
			$data = array();
			$data['booking_id'] = $row->id;
			$data['category_id'] = $row->target_id;
			$data['comment'] = $row->comment;
			$data['subamount'] = $row->amount;
			$details->insert($data);
			echo $row->id; echo "\n";
		}
	}

	// only with old database style
	public function convertScheduled(){
		$scheduled = model('App\Models\mScheduled');
		$accounts = model('App\Models\mAccounts');
		$result = $scheduled->findAll();
		foreach( $result as $row){

			$book = new \Booking;
			$book = unserialize($row['billdata']);
			$zeilendata = $book->getData();
			// if ($book->getType() == 'transfer'){
			// 	echo "\n<br>";
			// 	print_r($zeilendata);
			// }
			// if ($book->getType() == 'multiple'){
			// 	echo "\n<br>";
			// 	print_r($book->getBookingDetails());
			// }
			//echo "\n\n<br><br>";		
			$data = $this->loadFormDefaults();			
			$data['id'] = $zeilendata['id'];
			$data['source'] = $zeilendata['source_id'];
			$data['datum'] =$zeilendata['datum'];
			$data['multi'] = $book->getType() == 'multiple';
			$data['transfer'] = $book->getType() == 'transfer';
			$data['validate'] = true;
			$data['scheduled'] = true;
			switch ($row['interval'])
			{
				case '2weekly':
					$data['scheduledNum'] = 2;
					$data['scheduledType'] = 'week';
					break;
				case 'monthly':
					$data['scheduledNum'] = 1;
					$data['scheduledType'] = 'month';
					break;
				case '2monthly':
					$data['scheduledNum'] = 2;
					$data['scheduledType'] = 'month';
					break;
				case '3monthly':
					$data['scheduledNum'] = 3;
					$data['scheduledType'] = 'month';
					break;
				case '6monthly':
					$data['scheduledNum'] = 6;
					$data['scheduledType'] = 'month';
					break;
				case 'yearly':
					$data['scheduledNum'] = 1;
					$data['scheduledType'] = 'year';
					break;
			}

			$data['receiver'] = $zeilendata['receiver'];
			$data['total_in'] = ($zeilendata['amount'] >= 0.0)?abs( $zeilendata['amount'] ):'';
			$data['total_out'] = ($zeilendata['amount']< 0.0)?abs( $zeilendata['amount'] ):'';

			if (($book->getType() == 'bill')||($book->getType() == 'transfer')){
				$data['input'][0] = $data['total_in'];
				$data['output'][0] = $data['total_out'];
				$data['comment'][0] = $zeilendata 	['comment'];
				$data['category'][0] = $zeilendata['target_id'];	
			}
			else{
				echo "\n<br>";$i=0;

				foreach($book->getBookingDetails() as $detail){
					echo "\n<br>";
					print_r($detail->getData(array()));
					$data['category'][$i] = $detail->getCategoryId();
					$data['input'][$i] = $detail->getAmountIn();
					$data['output'][$i] = $detail->getAmountOut();
					$data['comment'][$i] = $detail->getComment();
					$i++;
				}
			}
			unset($data['category_parent']);
			$scheduled->update($row['id'],['data'=>json_encode($data)]);
		}
		echo "Done";
	}

}
?>