<?php
session_start();
ob_start();
  $father = $_SESSION["user_id"];
  $sanad_date = date("Y-m-d");
  require_once "../magic/DataBase.php";

	$dataBase = new DataBase();
	$conn = $dataBase->getConnection();

if(isset($_POST["id4"])){
    $contract_id       = $_POST['id4'];
    $vat_id            = $_POST['vat_id2'];
    $debit_money       = $_POST['debit_money'];
    $comm              = $_POST['comm'];
$data = array();
$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

if(empty($debit_money)||empty($comm))
    {    $error["debit_money"] = "من فضلك سجل المبلغ و سبب الخصم ليتم الحفظ";   }
      if(!preg_match($Number,$debit_money)){ $error["debit_money"] = "من فضلك تأكد من رقم المبلغ" ;}

if(!empty($comm)){  $comm = htmlspecialchars(strip_tags($comm)) ;}

if(!empty($error)){
  $data["error"] = $error ;
}else {
  $after_vat    = $debit_money;
  $vat_req      = round((($debit_money * 15)/ 115),2);
  $debit_money = $after_vat - $vat_req ;
  // prevint two sanad
  $maxN = $conn->prepare("SELECT max(create_at) as time1 FROM debit_vat ");
  $maxN->execute();
  if($maxN->rowCount()>0){$row = $maxN->fetch(PDO::FETCH_ASSOC);
    $start      = strtotime($row['time1']);
  }
        $date_today = date("Y-m-d H:i:s");
        $end = strtotime($date_today);
        // $end = $end + 3600 ;
    if(empty($start)){  $mano =   $end - $start  ; }else{$mano = 99 ;}
    if($mano < 30){
      $data["msg"] = $mano ;
      // $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>" ;
    }else{
      // sum of vat and wanted
      include_once "../op/wanted.php";
      $sumvat = 0.0 ;
      $sql = $conn->prepare("SELECT sum(after_vat) as sumvat from vat where contract_id = ?");
      $sql->bindParam(1,$contract_id);
      $sql->execute();
      if($sql->rowCount()>0){
        $row = $sql->fetch(PDO::FETCH_ASSOC);
        $sumvat = $row['sumvat'] ;
        $sumvat = round($sumvat , 2);
        echo($sumvat);
      } 
        $wanted    = new  Wanted($conn,$contract_id) ;
        $pay       = $wanted->wanted() ;
        $after     = $pay["after_vat"] ;
        if($sumvat > $after){
          $over_vat = $debit_money ;
          $debit_money = 0.0 ;
        }else{
          $over_vat = 0.0;
        }
      
      //
      $maxsql= $conn->prepare("SELECT max(debit_id) as maxd FROM debit_vat");
      $maxsql->execute();
    if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
          $maxD = $row['maxd']+1;} else {$maxD = 109 ; }
          $debit_money = round($debit_money , 2);
          
    $sanad = $conn->prepare(" INSERT INTO debit_vat(debit_id , contract_id , vat_id , debit_money , over_vat ,vat_req,after_vat, comm , father   )
                   VALUES( ? , ? , ? , ?,? , ? , ? ,?,? )");
       $sanad->bindParam(1,$maxD);
       $sanad->bindParam(2,$contract_id);
       $sanad->bindParam(3,$vat_id);
       $sanad->bindParam(4,$debit_money);
       $sanad->bindParam(5,$over_vat);
       $sanad->bindParam(6,$vat_req);
       $sanad->bindParam(7,$after_vat);
       $sanad->bindParam(8,$comm);
       $sanad->bindParam(9,$father);
       if($sanad->execute()){
          $data["debit_id"] = $maxD ;
          $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>" ;
            //check if the contract is closed or not for updating its status
            // include "../op/close_up.php";
            // auto_close($contract_id) ;
            }
      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);
}
// if(isset($_POST["desc"])){
//     $debit_id         = $_POST['id'];
//     $debit_money      = $_POST['debit_money'];
//     $comm   = $_POST['desc'];
//     if(!empty($comm)){  $comm = htmlspecialchars(strip_tags($comm)) ;}
//   $save = $ded_calss->update($debit_id,$debit_money,$comm);
//   return $save ;
// }

?>
