<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
$branch = $_SESSION["branch"];
$father = $_SESSION["user_id"];
include "../actions/dbPDO.php";
if($_POST['cust_id'] !=""){
    $rent_days   = $_POST["rent_days"];                $used_area     = $_POST["used_area"];
    $cust_id     = $_POST["cust_id"];                  $tamm_days     = $_POST["tamm_days"];
    $carid       = $_POST["carid"];
    $day_cost    = $_POST["day_cost"];                 $km_out        = $_POST["km_out"];
    $kilo_cost   = $_POST["kilo_cost"];                $free_kilo     = $_POST["free_kilo"];
    $comm        = $_POST["comm"];
    $cust_id2    = $_POST["cust_id2"];
    $start       = $_POST["start"] ;    $tash      = $_POST["tash"] ;

$data = array();
$error = array();
$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;
if(empty($tash)||($tash < 0)){ $tash = 0 ;}
if(empty($carid) || empty($cust_id)||empty($tamm_days) || empty($day_cost)|| empty($km_out)|| empty($tash))
    {  $error["msg22"] ="<div class='alert alert-danger'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b>من فضلك سجل بيانات العقد كاملة ليتم حفظ العقد  </b>
      </div> " ;    }
    if(!empty($carid)){
      $sql = $db->prepare("SELECT monthly , freekilo , km2 , carstatus FROM cars WHERE carid = '$carid' LIMIT 1");
      $sql->execute();
      if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
            $minn       = $row["monthly"];
            $freekilo   = $row["freekilo"];
            $km3        = $row["km2"];
            $carstatus  = $row["carstatus"];
          }
          if($carstatus != 1){exit() ;}
      if($day_cost < $minn){  $day_cost  = $minn ;}
      if($kilo_cost < $km3){  $kilo_cost = $km3 ;}
      if($free_kilo < $freekilo){  $free_kilo = $freekilo ;}
    }
if(!preg_match($Number,$tamm_days)){ $error["tamm_days"] = " أرقام فقط.." ;}
if(!preg_match($Number,$day_cost)){ $error["day_cost"] = "  أرقام صحيحة فقط.." ;}
if(!preg_match($Number,$km_out)){ $error["km_out"] = " أرقام فقط.." ;}
if(empty($used_area)){$used_area = "جدة و مكة";}
if(!empty($used_area)){ $used_area = htmlspecialchars(strip_tags($used_area)) ;   }
if(empty($rent_days)){$rent_days = "يومين";}
if(!empty($rent_days)){ $rent_days = htmlspecialchars(strip_tags($rent_days)) ;   }
if(!empty($comm)){ $comm = htmlspecialchars(strip_tags($comm)) ;   }

    // existing contract to current customer
    $sql = $db->prepare("SELECT dgree FROM customers  WHERE cust_id = ? LIMIT 1");
    $sql->bindParam(1,$cust_id);
    $sql->execute();
    if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $dgree   = $row["dgree"];}
      if($dgree < 50){$error["comm"] = "مستاجر قائمة سوداء إنتبه من فضلك" ;}
$rent_days = "شهر";
if(!empty($error)){
  $data["error"] = $error ;
  }else {
  $maxCONT = "0";
            $maxsql= $db->prepare("SELECT max(contract_id) as maxd FROM aksat_contract");
            $maxsql->execute();
            if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
              $maxCONT = $row['maxd']+1; }
  $tamm_expire = date('Y-m-d', strtotime(' + '.$tamm_days.' days'));
    $sql=$db->prepare(" INSERT INTO aksat_contract
        (contract_id , rent_days , used_area , cust_id ,tamm_expire , carid , day_cost , km_out , kilo_cost , free_kilo, comm ,
          cust_id2 , start ,cost ,kest,status,tash,branch  ,father)
    VALUES (?, ?, ?, ?, ?, ? , ?, ?, ?, ?,  ?, ?, ?,0.0,0.0,3, ? ,?,?)");
      $sql->bindParam(1,$maxCONT);
      $sql->bindParam(2,$rent_days);
      $sql->bindParam(3,$used_area);
      $sql->bindParam(4,$cust_id);
      $sql->bindParam(5,$tamm_expire);
      $sql->bindParam(6,$carid);
      $sql->bindParam(7,$day_cost);
      $sql->bindParam(8,$km_out);
      $sql->bindParam(9,$kilo_cost);
      $sql->bindParam(10,$free_kilo);
      $sql->bindParam(11,$comm);
      $sql->bindParam(12,$cust_id2);
      $sql->bindParam(13,$start);
      $sql->bindParam(14,$tash);
      $sql->bindParam(15,$branch);
      $sql->bindParam(16,$father);
      if($sql->execute()){
  $data["msg"]= "<div class='alert alert-success'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                <b> تم تسجيل العقد بنجاح </b></div>" ;
  $car= $db->prepare("UPDATE cars set carstatus = 2 where carid=".$carid."");
  $car->execute();
  $maxsql= $db->prepare("SELECT max(tash_id) as maxd FROM tash_in");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxD = $row['maxd']+1;} else {$maxD = 1 ; }
      $end   = date('Y-m-d', strtotime($start. ' + 1 month'));
      $end   = date('Y-m-d', strtotime($end. ' -1 days'));
  $sanad=$db->prepare(" INSERT INTO tash_in(tash_id , contract_id , wanted ,  start ,endd )
                          VALUES( ? , ? , ? , ?, ? )");
   $sanad->bindParam(1,$maxD);
   $sanad->bindParam(2,$maxCONT);
   $sanad->bindParam(3,$tash);
   $sanad->bindParam(4,$start);
   $sanad->bindParam(5,$end);
   if($sanad->execute()){
   $data["msg33"] = "<div class='alert alert-success'>
              <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                <b> تم تسجيل السند بنجاح.. </b></div> ";
              }
      else{ $data["msg33"] = "<div class='alert alert-danger'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
                    <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b></div>  ";}
  }
   else{ $data["msg"] = "<div class='alert alert-danger'>
 <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
   <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
 </div> " ; }
}
echo json_encode($data);
}
