<?php
if(!isset($_SESSION)) {  session_start(); }
ob_start();
$father = $_SESSION["user_id"];
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
if (isset($_POST["contract_id"])) {
	include "../actions/dbPDO.php";
	$dd   = $_POST["contract_id"];
	$output = array();
	$sql = $db->prepare("SELECT * FROM contracts WHERE contract_id = ? LIMIT 1");
	$sql->bindParam(1,$dd);
	$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
		$output["contract_id"]   	= $row["contract_id"];
		$output["carid"]   			= $row["carid"];
        $output["cust_id"]     		= $row["cust_id"];
		$output["cust_id2"] 		= $row["cust_id2"];
		$output["rent_days"]    	= $row["rent_days"];
    // $output["contract_expire"]	= $row["contract_expire"];
		$output["cont_status"]    	= $row["cont_status"];
        $output["used_area"]  		= $row["used_area"];
		$output["free_dd"]			= $row["free_dd"];
// calcualtes dayes of rent
		$output["date_out"]   	    = $row["date_out"];
		$output["date_in"]   		= $row["date_in"];
			$date_in   				= $row["date_in"];
			if(empty($date_in)){
				$date1 = new DateTime($row['date_out']);
				$date2 = new DateTime(date("Y-m-d"));
				$days_qty = $date2->diff($date1)->format("%a");
			} else{
				$date1 = new DateTime($row['date_out']);
				$date2 = new DateTime($row['date_in']);
				$days_qty = $date2->diff($date1)->format("%a");
			}
		$output["days_qty"]     = $days_qty;

		$output["day_cost"]   	= $row["day_cost"];
		$output["days_cost"]   	= round(($days_qty * ($row["day_cost"])),2)  ;

		$output["km_out"]    	= $row["km_out"];
		$output["km_in"]		= $row["km_in"];
		$output["free_kilo"]    = $row["free_kilo"];
		$output["kilo_qty"]  	= $row["kilo_qty"];
		$output["kilo_cost"]   	= $row["kilo_cost"];
		$output["kilos_cost"]   = $row["kilos_cost"];

		$output["time_out"]   	= $row["time_out"];
        $output["time_in"] 		= $row["time_in"];
		$time_in = $row["time_in"];
		if(empty($time_in)){
			$time1 = new DateTime($row['time_out']);
			$time2 = new DateTime(date("H:i"));
			if($time2 > $time1){ $hour_qty = $time2->diff($time1)->format("%h"); }else{$hour_qty = 0;}

		} else{
			$time1 = new DateTime($row['time_out']);
			$time2 = new DateTime($row['time_in']);
			if($time2 > $time1){ $hour_qty = $time2->diff($time1)->format("%h"); }else{$hour_qty = 0;}
		}
		$output["hour_qty"]   	= $hour_qty ;
		$output["hour_cost"]    = $row["hour_cost"];
		if($hour_qty > 9){
			$output["hours_cost"]   = $row["day_cost"];
		}else{
			$output["hours_cost"]   = round(($hour_qty * ($row["hour_cost"])),2);
		}
if(($days_qty == 0)&&($hour_qty > 6)){$output["hours_cost"] = $output["day_cost"] ; }

		$output["addtion_cost"]   = $row["addtion_cost"];
		$output["addtion_desc"]   = $row["addtion_desc"];
    	$output["violations"]     = $row["violations"];
		$output["ins_claim"]      = $row["ins_claim"];
    	$output["deduction"]	  = $row["deduction"];

		$output["next_cust"] 		= $row["next_cust"];
		$output["tamm_expire"]   	= $row["tamm_expire"];

		$output["open_comm"] 		= $row["open_comm"];
		$output["dept_comm"]   		= $row["dept_comm"];
    	$output["operating_comm"]   = $row["operating_comm"];
		$output["comm"]    			= $row["comm"];

		$output["father"]    	= $row["father"];
    	$output["branch"] 		= $row["branch"];
		$output["closer"]   	= $row["closer"];

		if(empty($output["vat_req"])){
			$output["vat_req"]   	= 0;
			$output["vat_in"]   	= 0;
			$output["after_vat"]  	= 0;
		}
		$carid = $row["carid"];
	}

	$tot_in = $db->prepare("SELECT sum(money_in) as inn from invoice WHERE contract_id =? ");
	$tot_in->bindParam(1,$dd);
	$tot_in->execute();
	if($tot_in->rowCount()>0){$toty_in = $tot_in->fetch(PDO::FETCH_ASSOC);
	$output["total_in"] = $toty_in["inn"];
}else { $output["total_in"] = "0" ;}
	$tot_out = $db->prepare("SELECT sum(money_out) as outt from outvoice WHERE contract_id = ? ");
	$tot_out->bindParam(1,$dd);
	$tot_out->execute();
	if($tot_out->rowCount()>0){$toty_out = $tot_out->fetch(PDO::FETCH_ASSOC);
	$output["total_out"] = $toty_out["outt"];
    } else
{ $output["total_out"] = 0 ;}

$tot_cut = $db->prepare("SELECT sum(money_de) as cutt from deduction WHERE contract_id = ? ");
$tot_cut->bindParam(1,$dd);
$tot_cut->execute();
if($tot_cut->rowCount()>0){$toty_cut = $tot_cut->fetch(PDO::FETCH_ASSOC);
$output["total_cut"] = $toty_cut["cutt"];
}
if(empty($output["total_cut"])){$output["total_cut"] = 0 ;}
   $output["payed"] 			= round(($output["total_in"]  - $output["total_out"]),2) ;

	$output["net_cost"] 	= round((($output["days_cost"] + $output["kilos_cost"] + $output["hours_cost"] + $output["addtion_cost"] + $output["violations"] + $output["ins_claim"])-($output["deduction"] + $output["total_cut"])),2) ;
	$output["vat_req"]  	= round(($output["net_cost"]*.05),2);
	$output["vat_in"] 		= round((($output["payed"]/105) * 5),2);
	$output["after_vat"] 	= round(($output["net_cost"]  + $output["vat_req"]),2) ;


	$vat_start 	= date('Y-m-d', strtotime("2018-01-11"));
	$date_in 	= date('Y-m-d', strtotime($date_in));
	if($date_in < $vat_start){
		$output["vat_req"]   	= 0;
		$output["vat_in"]   	= 0;
		$output["after_vat"]  	= $output["net_cost"];}
$output["wanted"] 		= round(($output["after_vat"] - $output["payed"]),2) ;
// minimumm rent cost
		$last = $db->prepare("SELECT  minn , km3 , freekilo from cars where carid = ? ");
		$last->bindParam(1,$carid);
		$last->execute();
		if($last->rowCount()>0){$free = $last->fetch(PDO::FETCH_ASSOC);
		$output["minn"] 		= $free["minn"];
		$output["km3"] 			= $free["km3"];
		$output["freekm"] 		= $free["freekilo"];
		}
	echo json_encode($output);
	}