<?php
  // how many days in this status
  function month_history($carid,$branch,$first_day,$last_day){
    include "../actions/dbPDO.php";
  if($branch > 0){
    $standby = 0 ; $at_rent = 0 ; $ment ; $service = 0 ; $inn = 0; $outt = 0 ;
// standby
    $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                  WHERE carstatus = 1 AND carid = ? AND branch = ? AND ac_date between ? AND ? ");
          $ac->bindParam(1,$carid);
          $ac->bindParam(2,$branch) ;
          $ac->bindParam(3,$first_day) ;
          $ac->bindParam(4,$last_day) ;
          $ac->execute();
        if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
               $standby = $day['qty'];}
    //at rent
    $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                  WHERE carstatus = 2 AND carid = ? AND branch = ? AND ac_date between ? AND ? ");
          $ac->bindParam(1,$carid);
          $ac->bindParam(2,$branch) ;
          $ac->bindParam(3,$first_day) ;
          $ac->bindParam(4,$last_day) ;
          $ac->execute();
        if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
               $at_rent = $day['qty'];}
      //at mentainance
      $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                    WHERE carstatus = 3 AND carid = ? AND branch = ? AND ac_date between ? AND ? ");
            $ac->bindParam(1,$carid);
            $ac->bindParam(2,$branch) ;
            $ac->bindParam(3,$first_day) ;
            $ac->bindParam(4,$last_day) ;
            $ac->execute();
          if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
                 $ment = $day['qty'];}
  //at service
  $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                WHERE carstatus = 5 AND carid = ? AND branch = ? AND ac_date between ? AND ? ");
        $ac->bindParam(1,$carid);
        $ac->bindParam(2,$branch) ;
        $ac->bindParam(3,$first_day) ;
        $ac->bindParam(4,$last_day) ;
        $ac->execute();
      if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
             $service = $day['qty'];}
    // total month money
$nn = $db->prepare(" SELECT sum(money_in) as inn FROM  car_in
                    WHERE carid = ? AND branch = ? AND sanad_date between ? AND ? ");
                   $nn->bindParam(1,$carid);
                   $nn->bindParam(2,$branch) ;
                   $nn->bindParam(3,$first_day) ;
                   $nn->bindParam(4,$last_day) ;
                   $nn->execute();
                 if($nn->rowCount()>0){$mon = $nn->fetch(PDO::FETCH_ASSOC);
                        $inn = $mon['inn'];}
$nn = $db->prepare(" SELECT sum(money_out) as outt FROM  car_out
                    WHERE carid = ? AND branch = ? AND sanad_out between ? AND ? ");
                  $nn->bindParam(1,$carid);
                  $nn->bindParam(2,$branch) ;
                  $nn->bindParam(3,$first_day) ;
                  $nn->bindParam(4,$last_day) ;
      $nn->execute();
            if($nn->rowCount()>0){$mon = $nn->fetch(PDO::FETCH_ASSOC);
                    $outt = $mon['outt'];}

  $data  = array();
  $data["standby"] 		   = $standby ;
  $data["at_rent"] 		   = $at_rent ;
  $data["ment"] 		     = $ment ;
  $data["service"] 		   = $service ;
  $data["tot_at_branch"] = $standby + $at_rent + $ment ;
  $data["money"] 		     = $inn - $outt ;
}else{
  $standby = 0 ; $at_rent = 0 ; $ment ; $service = 0 ; $inn = 0; $outt = 0 ;
// standby
  $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                WHERE carstatus = 1 AND carid = ? AND ac_date between ? AND ? ");
        $ac->bindParam(1,$carid);
        $ac->bindParam(2,$first_day) ;
        $ac->bindParam(3,$last_day) ;
        $ac->execute();
      if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
             $standby = $day['qty'];}
  //at rent
  $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                WHERE carstatus = 2 AND carid = ? AND ac_date between ? AND ? ");
        $ac->bindParam(1,$carid);
        $ac->bindParam(2,$first_day) ;
        $ac->bindParam(3,$last_day) ;
        $ac->execute();
      if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
             $at_rent = $day['qty'];}
    //at mentainance
    $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
                  WHERE carstatus = 3 AND carid = ? AND ac_date between ? AND ? ");
          $ac->bindParam(1,$carid);
          $ac->bindParam(2,$first_day) ;
          $ac->bindParam(3,$last_day) ;
          $ac->execute();
        if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
               $ment = $day['qty'];}
//at service
$ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
              WHERE carstatus = 5 AND carid = ? AND ac_date between ? AND ? ");
      $ac->bindParam(1,$carid);
      $ac->bindParam(2,$first_day) ;
      $ac->bindParam(3,$last_day) ;
      $ac->execute();
    if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
           $service = $day['qty'];}
  // total month money
$nn = $db->prepare(" SELECT sum(money_in) as inn FROM  car_in
                  WHERE carid = ? AND sanad_date between ? AND ? ");
                 $nn->bindParam(1,$carid);
                 $nn->bindParam(2,$first_day) ;
                 $nn->bindParam(3,$last_day) ;
                 $nn->execute();
               if($nn->rowCount()>0){$mon = $nn->fetch(PDO::FETCH_ASSOC);
                      $inn = $mon['inn'];}
$nn = $db->prepare(" SELECT sum(money_out) as outt FROM  car_out
                  WHERE carid = ?  AND sanad_out between ? AND ? ");
                $nn->bindParam(1,$carid);
                $nn->bindParam(2,$first_day) ;
                $nn->bindParam(3,$last_day) ;
    $nn->execute();
          if($nn->rowCount()>0){$mon = $nn->fetch(PDO::FETCH_ASSOC);
                  $outt = $mon['outt'];}

$data  = array();
$data["standby"] 		   = $standby ;
$data["at_rent"] 		   = $at_rent ;
$data["ment"] 		     = $ment ;
$data["service"] 		   = $service ;
$data["tot_at_branch"] = $standby + $at_rent + $ment ;
$data["money"] 		     = $inn - $outt ;
}
  return $data ;
}//end of cuurent branch and state etc..

function qrtService($carid,$start,$end){
  include "../actions/dbPDO.php";
  $ac = $db->prepare(" SELECT count(carid) as qty FROM  actions
              WHERE carstatus = 5 AND carid = ? AND ac_date between ? AND ? ");
      $ac->bindParam(1,$carid);
      $ac->bindParam(2,$start) ;
      $ac->bindParam(3,$end) ;
      $ac->execute();
    if($ac->rowCount()>0){$day = $ac->fetch(PDO::FETCH_ASSOC);
           return $day['qty'];
          }else{
            return 0 ;
          }
}
?>
