<?php
if ($_REQUEST ['date']) {
	$date_from = new DateTime ( date ( '01.m.Y' ,strtotime( $_REQUEST ['date'])));
	$date_to = new DateTime (  date ( 't.m.Y' ,strtotime( $_REQUEST ['date'])));
	$date = new DateTime($_REQUEST ['date']);
} else {
	$date_from = new DateTime ( date ( '01.m.Y' ) );
	$date_to = new DateTime ( date ( 't.m.Y' ) );
	$date = new DateTime();
}


class WorkCostByWorker {
	public $date_from;
	public $date_to;
	public $db;
	public $category;
	public $workerTime;
	public $workerCost;
	public $totalh;
	public $totalc;
	public $totalQuantityPw;
	public $totalCostPw;
	
	public function __construct(DateTime $date_from, DateTime $date_to, $category) {
		$this->date_from = $date_from;
		$this->date_to = $date_to;
		$this->category=$category;
		$this->db = $GLOBALS ['db'];
	}
	
	public function getWorkerList() {
		$this->db = $GLOBALS ['db'];

		
		$query = "select id,CONCAT(first_name,' ',last_name) as name from ecmworkers where deleted=0";

		$res = $this->db->query ( $query );

		$workers=[];
		while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
			$workers[$dane['id']]=$dane['name'];
		}

		return $workers;
	}
	
	public function getWorkerCostInMont(){
		$query="select worker_id,price from ecmworkcosts where deleted=0 and date>='".$this->date_from->format("Y-m-d")."'";
		$res = $this->db->query ( $query );
		
		$this->workerCost=[];
		while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
			$this->workerCost[$dane['worker_id']]=$dane['price'];
		}
		

	}
	
	public function getWokerTimeInMonth(){
		$query="select  worker_id,SEC_TO_TIME( SUM( TIME_TO_SEC( TIMEDIFF(time_to, time_from)) ) ) AS timeSum from ecmworkcards where date>='".$this->date_from->format("Y-m-d")."' and date<='".$this->date_to->format("Y-m-d")."' and deleted=0 group by worker_id";
		
		$res = $this->db->query ( $query );
		
		$this->workerTime=[];
		while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
			$this->workerTime[$dane['worker_id']]['time']=$dane['timeSum'];
			
			$parts = explode(':', $dane['timeSum']);
		
			$this->workerTime[$dane['worker_id']]['work_hour_cost']=round($this->workerCost[$dane['worker_id']]/($parts[0] + floor(($parts[1]/60)*100) / 100),2);
		}

	}
	
	public function getProductTimeByWorkers(){
		$query="select  w.product_id,p.code,p.name,w.worker_id,SEC_TO_TIME( SUM( TIME_TO_SEC( TIMEDIFF(w.time_to, w.time_from)) ) ) AS timeSum,sum(w.quantity) as quantity from ecmworkcards w
inner join ecmproducts p on p.id=w.product_id
 where w.date>='".$this->date_from->format("Y-m-d")."' and w.date<='".$this->date_to->format("Y-m-d")."' and w.deleted=0 group by w.product_id,w.worker_id";
		
		$res = $this->db->query ( $query );
		
		$workers=$this->getWorkerList();

		$this->data=[];
		$this->totalh=0;
		$this->totalc=0;
		while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
			$this->data[$dane['product_id']]['name']=$dane['name'];
			$this->data[$dane['product_id']]['indeks']=create_guid ();
			$this->data[$dane['product_id']]['code']=$dane['code'];
			$parts = explode(':', $dane['timeSum']);
			$this->data[$dane['product_id']]['hours']=$this->data[$dane['product_id']]['hours']+($parts[0] + floor(($parts[1]/60)*100) / 100);
			$this->totalh=$this->totalh+($parts[0] + floor(($parts[1]/60)*100) / 100);
			$this->data[$dane['product_id']]['cost']=$this->data[$dane['product_id']]['cost']+($this->workerTime[$dane['worker_id']]['work_hour_cost']*($parts[0] + floor(($parts[1]/60)*100) / 100));
			$this->data[$dane['product_id']]['workers'][$dane['worker_id']]['name']=$workers[$dane['worker_id']];
			$this->data[$dane['product_id']]['workers'][$dane['worker_id']]['quantity']=$dane['quantity'];
			$this->data[$dane['product_id']]['workers'][$dane['worker_id']]['hours']=($parts[0] + floor(($parts[1]/60)*100) / 100);
			$this->data[$dane['product_id']]['workers'][$dane['worker_id']]['cost']=($this->workerTime[$dane['worker_id']]['work_hour_cost']*($parts[0] + floor(($parts[1]/60)*100) / 100));
			
			$this->totalc=$this->totalc+$this->data[$dane['product_id']]['cost'];
	
		}
		
		return $this->data;
	}
	
	public function getProductByPW(){
		$query="select i.ecmproduct_id as product_id,sum(i.quantity) as pw_quantity,sum(i.total) as pw_total,p.code,p.name from ecmstockdocinsideinitems i 
inner join ecmstockdocinsideins d on d.id = i.ecmstockdocinsidein_id 
inner join ecmproducts p on p.id=i.ecmproduct_id
where
  d.register_date>='".$this->date_from->format("Y-m-d")."' and d.register_date<='".$this->date_to->format("Y-m-d")."' and d.deleted=0 GROUP by i.ecmproduct_id";
		$res = $this->db->query ( $query );

		$this->totalQuantityPw=0;
		$this->totalCostPw=0;
		
		while ( $dane = $this->db->fetchByAssoc ( $res ) ) {

			$this->data[$dane['product_id']]['name']=$dane['name'];
			if($this->data[$dane['product_id']]['indeks']=="")create_guid ();
			$this->data[$dane['product_id']]['code']=$dane['code'];
			$this->data[$dane['product_id']]['pw_quantity']=$dane['pw_quantity'];
			$this->data[$dane['product_id']]['pw_total']=$dane['pw_total'];
			
			$this->totalCostPw=$this->totalCostPw+$this->data[$dane['product_id']]['pw_total'];
			$this->totalQuantityPw=$this->totalQuantityPw+$this->data[$dane['product_id']]['pw_quantity'];
		}
	
		return $this->data;
	}
	
	
}

$wcbw = new WorkCostByWorker($date_from,$date_to);
$wcbw->getWorkerCostInMont();
$wcbw->getWokerTimeInMonth();
$wcbw->getProductTimeByWorkers();
$data=$wcbw->getProductByPW();

function compareByName($a, $b) {
	return strcmp($a["code"], $b["code"]);
}
usort($data, 'compareByName');

$smarty = new Sugar_Smarty ();

$smarty->assign ( "data", $data );
$smarty->assign ( "totalc", $wcbw->totalc);
$smarty->assign ( "totalh", $wcbw->totalh);
$smarty->assign ( "totalQuantityPw", $wcbw->totalQuantityPw);
$smarty->assign ( "totalCostPw", $wcbw->totalCostPw);
$smarty->assign ( "date", $date->format('d.m.Y'));

if($_REQUEST['to_pdf']=='1'){
	$smarty->assign ( "dateformat", $date->format('mY'));
	include_once ("include/MPDF57/mpdf.php");
	$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 5, 5, 5, 5 );
	$content=  $smarty->fetch ( 'modules/EcmReports/tpls/PDF/KosztProdukcjiWgPracownikow.html' );
	
	$p->WriteHTML ( $content );
	//echo $content;
	// draw PDF
	
	$p->Output ();
} else {
	echo $smarty->display ( 'modules/EcmReports/tpls/KosztProdukcjiWgPracownikow.html' );
}


?>