<?php
include_once('../actions/dbPDO.php');
$sql = $db->prepare("SELECT vat.vat_id, vat.end , vat.start , vat.date_create  , 
            contracts.date_in , contracts.time_in,contracts.date_out , contracts.time_out
            from vat , contracts 
            where vat.contract_id = contracts.contract_id");
$sql->execute();
if($sql->rowCount()>0)
  {
   $act = 1 ;
     while($row = $sql->fetch())
     {
      $end 			    = $row["end"] ;
      $dateCreate     = $row["date_create"] ;
      $date_in        = $row["date_in"];
      $time_in        = $row["time_in"];
      $date_out       = $row["date_out"];
      $time_out       = $row["time_out"];
      $vat_id         = $row["vat_id"];

      $date_in = new DateTime($date_in);
      $date1 = new DateTime($dateCreate);
      $date2 = new DateTime($end);
   if( $date1->format('U') < $date2->format('U')  ){
      $duedate = $dateCreate;
       } else if(!empty($date_in)){
         if( $date_in->format('U') > $date2->format('U')){
            $duedate      = $end.' '.$time_out;  
                }
               else{
                   $duedate      = $end.' '.$time_in;
                   }
       }else{
               $duedate      = $end.' '.$time_out; 
               }
               // echo($duedate .'   '.$vat_id);
               // echo('<br />');
               // $duedate = new DateTime($duedate);
      $upd = $db->prepare("UPDATE `vat` set duedate = ?  WHERE vat_id = ? ");
      $upd->bindParam(1,$duedate);
      $upd->bindParam(2,$vat_id);
     if( $upd->execute()){
      $act++ ;
     }
    }
    echo($act);
    
   }
?>