<?php
function next_wanted($contract_id){
	include "../actions/dbPDO.php";
	$tot_debt 		= 0;
	$tot_payed 		= 0;
	$tot_wanted		= 0;
	$cont_start		= 0;
	$cont_end 		= 0;
	$first_pay    = 0;
	$last_pay 		= 0;
	$late 				= 0 ;
  $today = date("Y-m-d");
	$sql = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) tot_payed FROM aksat_in WHERE contract_id = ?");
	$sql->bindParam(1,$contract_id);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
				$tot_payed   				= $row["tot_payed"];
				$tot_wanted					= $row["tot_wanted"];

	$ww = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) tot_payed FROM aksat_in WHERE contract_id = ? AND start <= ? ");
	$ww->bindParam(1,$contract_id);
	$ww->bindParam(2,$today);
	$ww->execute();
	if($ww->rowCount()>0){$w = $ww->fetch(PDO::FETCH_ASSOC);
	$tot_debt = $w["tot_wanted"] - $w["tot_payed"];
}

	$last_kest = $db->prepare("SELECT min(ak_id) as ak_id from aksat_in 
								where contract_id = ? AND date_payed IS NULL");
				$last_kest->bindParam(1, $contract_id);
				$last_kest->execute();
		if ($last_kest->rowCount() > 0) {
				$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
					$maxAK = $ak['ak_id'];
				}

		$last_kest = $db->prepare("SELECT  ak_id , wanted , start,ak_no
					from aksat_in where ak_id = ? ");
				$last_kest->bindParam(1, $maxAK);
				$last_kest->execute();
				if ($last_kest->rowCount() > 0) {
						$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
						$next_ak_id = $ak['ak_id'];  $next_wanted = $ak['wanted']; $next_start = $ak['start']; $next_ak_no = $ak['ak_no'];
				}

if($next_start < $today){
	$date1 = new DateTime($next_start);
	$date2 = new DateTime($today);
	$late = $date2->diff($date1)->format("%a");
}
$data  = array();
				$data["tot_debt"] 		= $tot_debt ;
				$data["tot_payed"] 		= $tot_payed ;
				$data["tot_wanted"] 	= $tot_wanted ;
				$data["late"] 				= $late ;
				$data["next_ak_id"] 	= $next_ak_id ;
				$data["next_wanted"] 	= $next_wanted ;
				$data["next_start"] 	= $next_start ;
				$data["next_ak_no"] 	= $next_ak_no ;
}
return $data ;
}

function next_tash($contract_id){
	include "../actions/dbPDO.php";
	$tot_debt 		= 0;
	$tot_payed 		= 0;
	$tot_wanted		= 0;
	$cont_start		= 0;
	$cont_end 		= 0;
	$first_pay    = 0;
	$last_pay 		= 0;
	$late 				= 0 ;
  $today = date("Y-m-d");
	$sql = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) tot_payed FROM tash_in WHERE contract_id = ?");
	$sql->bindParam(1,$contract_id);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
				$tot_payed   				= $row["tot_payed"];
				$tot_wanted					= $row["tot_wanted"];

	$ww = $db->prepare("SELECT sum(wanted) as tot_wanted , sum(payed) tot_payed FROM tash_in WHERE contract_id = ? AND start <= ? ");
	$ww->bindParam(1,$contract_id);
	$ww->bindParam(2,$today);
	$ww->execute();
	if($ww->rowCount()>0){$w = $ww->fetch(PDO::FETCH_ASSOC);
	$tot_debt = $w["tot_wanted"] - $w["tot_payed"];
}


$last_kest = $db->prepare("SELECT min(tash_id) as tash_id FROM tash_in where contract_id = ? AND date_payed IS NULL");
$last_kest->bindParam(1,$contract_id);
$last_kest->execute();
if($last_kest->rowCount()>0){$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
	$minID = $ak['tash_id'];  
}
$last_kest = $db->prepare("SELECT  tash_id , wanted , start FROM tash_in where tash_id = ? ");
$last_kest->bindParam(1,$minID);
$last_kest->execute();
if($last_kest->rowCount()>0){$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
	$next_ak_id = $ak['tash_id'];  $next_wanted = $ak['wanted']; $next_start = $ak['start'];
}
$last_payed = $db->prepare("SELECT max(date_payed) as start from tash_in where contract_id = ? AND date_payed IS NOT NULL");
$last_payed->bindParam(1,$contract_id);
$last_payed->execute();
if($last_payed->rowCount()>0){$ta = $last_payed->fetch(PDO::FETCH_ASSOC); $last_pay = $ta['start']; }
if($next_start < $today){
	$date1 = new DateTime($next_start);
	$date2 = new DateTime($today);
	$late = $date2->diff($date1)->format("%a");
}
$data  = array();
				$data["tot_debt"] 		= $tot_debt ;
				$data["tot_payed"] 		= $tot_payed ;
				$data["tot_wanted"] 	= $tot_wanted ;
				$data["late"] 				= $late ;
				$data["next_ak_id"] 	= $next_ak_id ;
				$data["next_wanted"] 	= $next_wanted ;
				$data["next_start"] 	= $next_start ;
				$data["last_pay"] 		= $last_pay ;
}
return $data ;
}
?>
