<?php
if (!defined("ROOT_PATH"))
{
	header("HTTP/1.1 403 Forbidden");
	exit;
}
class pjDateModel extends pjAppModel
{
	protected $primaryKey = 'id';

	protected $table = 'dates';

	protected $schema = array(
		array('name' => 'id', 'type' => 'int', 'default' => ':NULL'),
		array('name' => 'foreign_id', 'type' => 'int', 'default' => ':NULL'),
		array('name' => 'type', 'type' => 'enum', 'default' => ':NULL'),
		array('name' => 'date_from', 'type' => 'date', 'default' => ':NULL'),
		array('name' => 'date_to', 'type' => 'date', 'default' => ':NULL'),
		array('name' => 'start_time', 'type' => 'time', 'default' => ':NULL'),
		array('name' => 'end_time', 'type' => 'time', 'default' => ':NULL'),
		array('name' => 'monday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'tuesday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'wednesday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'thursday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'friday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'saturday_dayoff', 'type' => 'enum', 'default' => 'T'),
		array('name' => 'sunday_dayoff', 'type' => 'enum', 'default' => 'T'),
	    array('name' => 'is_dayoff', 'type' => 'enum', 'default' => 'F')
	);

	protected $validate = array(
		'rules' => array(
			'foreign_id' => array(
				'pjActionNumeric' => true,
				'pjActionRequired' => true
			)
		)
	);

	public static function factory($attr=array())
	{
		return new pjDateModel($attr);
	}
	
	public function getDailyWorkingTime($foreign_id, $date, $type='calendar')
	{
		$arr = $this->reset()
			->where('t1.foreign_id', $foreign_id)
			->where('t1.type', $type)
			->where('t1.date_from <=', $date)
			->where('t1.date_to >=', $date)
			->orderBy('t1.start_time ASC')
			->limit(1)
			->findAll()
			->getData();
			
		if (empty($arr))
		{
			return false;
		}
		$arr = $arr[0];

		$wt = array();
		$d = getdate(strtotime($arr['start_time']));
		$wt['start_hour'] = $d['hours'];
		$wt['start_minutes'] = $d['minutes'];
	
		$d = getdate(strtotime($arr['end_time']));
		$wt['end_hour'] = $d['hours'];
		$wt['end_minutes'] = $d['minutes'];
		
		$wt['start_ts'] = strtotime($date . " " . $arr['start_time']);
		$wt['end_ts'] = strtotime($date . " " . $arr['end_time']);

		return $wt;
	}
	
	public function getRangeWorkingTime($foreign_id, $date_from, $date_to, $type='calendar')
	{
		$_arr = array();
		$from = strtotime($date_from);
		$to = strtotime($date_to);
		if ($from > $to)
		{
			$tmp = $from;
			$from = $to;
			$to = $tmp;
		}
		for ($i = $from; $i <= $to; $i += 86400)
		{
			$_arr[date("Y-m-d", $i)] = array();
		}
		
		$arr = $this
			->reset()
			->where('t1.foreign_id', $foreign_id)
			->where('t1.type', $type)
			->where('t1.date_to >=', $date_from)
			->where('t1.date_from <=', $date_to)
			->orderBy('t1.start_time ASC')
			->findAll()
			->getData();

		foreach ($arr as $item)
		{
			$startDate = new DateTime($item['date_from']);
			$endDate = new DateTime($item['date_to']);

			if ($startDate->getTimestamp() <= $endDate->getTimestamp()) {
				while ($startDate->getTimestamp() <= $endDate->getTimestamp()) {
					$currentDate = $startDate->format('Y-m-d');
					$_arr[$currentDate] = $item;

					$d = getdate(strtotime($item['start_time']));
					$_arr[$currentDate]['start_hour'] = $d['hours'];
					$_arr[$currentDate]['start_minutes'] = $d['minutes'];

					$d = getdate(strtotime($item['end_time']));
					$_arr[$currentDate]['end_hour'] = $d['hours'];
					$_arr[$currentDate]['end_minutes'] = $d['minutes'];

					$_arr[$currentDate]['start_ts'] = strtotime($currentDate . " " . $item['start_time']);
					$_arr[$currentDate]['end_ts'] = strtotime($currentDate . " " . $item['end_time']);

					# Lunch
					$d = getdate(strtotime($item['start_lunch']));
					$_arr[$currentDate]['lunch_start_hour'] = $d['hours'];
					$_arr[$currentDate]['lunch_start_minutes'] = $d['minutes'];

					$d = getdate(strtotime($item['end_lunch']));
					$_arr[$currentDate]['lunch_end_hour'] = $d['hours'];
					$_arr[$currentDate]['lunch_end_minutes'] = $d['minutes'];

					$_arr[$currentDate]['lunch_start_ts'] = strtotime($currentDate . " " . $item['start_lunch']);
					$_arr[$currentDate]['lunch_end_ts'] = strtotime($currentDate . " " . $item['end_lunch']);

					$startDate->add(new DateInterval('P1D'));
				}
			}
		}
		
		return $_arr;
	}
		
	public function getDatesOff($foreign_id, $month, $year, $type='calendar')
	{
		$numOfDays = date("t", mktime(0, 0, 0, $month, 1, $year));
		$_arr = array();
		for ($i = 1; $i <= $numOfDays; $i++)
		//foreach (range(1, $numOfDays) as $i)
		{
			$_arr[date("Y-m-d", mktime(0, 0, 0, $month, $i, $year))] = array();
		}
		
		//->where('t1.is_dayoff', 'T')
		$arr = $this->reset()
			->where('t1.foreign_id', $foreign_id)
			->where('t1.type', $type)
			->where('MONTH(t1.date)', $month)
			->where('YEAR(t1.date)', $year)
			->orderBy('t1.date ASC')
			->findAll()
			->getData();

		foreach ($arr as $v)
		{
			$_arr[$v['date']] = $v;
			$_arr[$v['date']]['start_ts'] = strtotime($v['date'] . " " . $v['start_time']);
			$_arr[$v['date']]['end_ts'] = strtotime($v['date'] . " " . $v['end_time']);
		}
		
		return $_arr;
	}
}
?>