<?php
CLASS Speed
{
	protected $em;
	
	public function hitungSpeed($filter, $set, $grup = '')
	{
		$this->em = $set->em;
		
		$date = new Date;
		$dateSpeed = date('Y-m-d', strtotime($filter->date1->value));
		$dateSpeed = $date->getPenguranganTanggal($dateSpeed, 0, 1, 0);
		$day = date("D", strtotime($dateSpeed));
		if(strtoupper($day) == 'MON'){
			$dateSpeed = $date->getPenguranganTanggal($dateSpeed, 0, 1, 0);
		}
		
		$sql = $this->em->addSelect("a.tobacco, a.dienumber, a.wb, a.client, a.merk
					, a.shift, a.machine, a.tob
                  , a.cuts
                  , a.lostmnt
                  , 
                    case when to_char(a.date, 'DY') = 'SAT' and a.hours > 8 then 5
                    when to_char(a.date, 'DY') != 'SAT' and a.hours > 9 then 7
                    else a.hours end as hours      
					
					, case when side = 'SIDE' then 0 else yield end as yield
		");
		$sql = $this->em->getFrom("bld", 'a');
		$sql = $this->em->getWhere("control = '1'");
		
		
		if(strtoupper($grup) == 'FORMAT'){
			$sql = $this->em->andWhere("a.date >= '".date('Y-m-d', strtotime($dateSpeed))."'");
			$sql = $this->em->andWhere("a.date <= '".date('Y-m-d 23:59:59', strtotime($dateSpeed))."'");
		}else{
			if(!empty($filter->date1->value)){
			  $sql = $this->em->andWhere("a.date >= '".date('Y-m-d 00:00:00', strtotime($filter->date1->value))."'");
			}
			if(!empty($filter->date2->value)){
			  $sql = $this->em->andWhere("a.date <= '".date('Y-m-d 23:59:59', strtotime($filter->date2->value))."'");
			}
		}

		if(!empty($filter->format1->value)){
			$sql = $this->em->andWhere("CONCAT(a.tobacco,a.dienumber,a.wb,a.client,a.merk) >= '".$filter->format1->value."'");
		}
		if(!empty($filter->format2->value)){
			$sql = $this->em->andWhere("CONCAT(a.tobacco,a.dienumber,a.wb,a.client,a.merk) <= '".$filter->format2->value."'");
		}
		if(!empty($filter->bale1->value)){
			$sql = $this->em->andWhere("a.bale >= '".$filter->bale1->value."'");
		}
		if(!empty($filter->bale2->value)){
			$sql = $this->em->andWhere("a.bale <= '".$filter->bale2->value."'");
		}

		$sql = $this->em->getMultiShort('a.date asc, a.nojnl asc');
		$eks = $this->em->queryEks($sql);
		
		$kode	= new Kode;
		$rep	= new Report;
		
		$res = array();
		while($row = $this->em->fetchObject($eks)){
			
			if(strtoupper($grup) == strtoupper('FORMAT')){
				$key = $kode->formatNonSpasi($row);
			}
			if(strtoupper($grup) == strtoupper('TOB')){
				$key = $row->$grup;
			}
			
			$jam = (int)$row->hours;
			$min = $row->hours - $jam;
			$min = $min * 100 / 60;
			$jam = $jam + $min;
			$row->hours = $jam;
			
			$row->lostmnt 	= $row->lostmnt / 60;
			$row->hours2	= $row->hours - $row->lostmnt;
			
			if($row->yield > 0){
				$res[$key]->yield 		= $row->yield;
			}
			$res[$key]->speed_cuts 		= $res[$key]->speed_cuts + $row->cuts;
			$res[$key]->speed_hours 	= $res[$key]->speed_hours + $row->hours;
			$res[$key]->speed_hours2 	= $res[$key]->speed_hours2 + $row->hours2;
			$res[$key]->speed_lostmnt 	= $res[$key]->speed_lostmnt + $row->lostmnt;
			
			if($res[$key]->speed_hours2 <> 0){
				$res[$key]->speed 			= $res[$key]->speed_cuts / $res[$key]->speed_hours2;
			}

			$res[$key]->cons = 0;
			if($res[$key]->yield <> 0){
				$res[$key]->cons = $res[$key]->speed * $filter->hours->value / $res[$key]->yield;
			}
		}
		return $res;
	}
}