<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();

  $branch = $_SESSION["branch"];
  $father = $_SESSION["user_id"];
  $sanad_date = date("Y-m-d");
  include "../actions/dbPDO.php";
  include_once "../vat/deduction.php";
  $ded_calss = new Deduction($db);

if(isset($_POST["id4"])){
    $contract_id   = $_POST['id4'];
    $money_de      = $_POST['money_de'];
    $printed_no    = $_POST['printed3_no'];
    $description   = $_POST['description'];
$data = array();
$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

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

if(!empty($printed_no)){   $printed_no  = htmlspecialchars(strip_tags($printed_no)) ;}
if(!empty($description)){  $description = htmlspecialchars(strip_tags($description)) ;}
if(!empty($error)){
  $data["error"] = $error ;
}else {
  // prevint two sanad
  $maxN = $db->prepare("SELECT max(audate) as time1 FROM deduction ");
  $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 < 15){
      $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{
      $maxsql= $db->prepare("SELECT max(de_id) as maxd FROM deduction");
      $maxsql->execute();
    if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
          $maxD = $row['maxd']+1;} else {$maxD = 1 ; }
          $money_de = round($money_de , 2);
    $sanad=$db->prepare(" INSERT INTO deduction(de_id , contract_id , printed_no , sanad_date , money_de , description , father , branch  )
       VALUES( ? , ? , ? , ? , ? , ? , ? , ?)");
       $sanad->bindParam(1,$maxD);
       $sanad->bindParam(2,$contract_id);
       $sanad->bindParam(3,$printed_no);
       $sanad->bindParam(4,$sanad_date);
       $sanad->bindParam(5,$money_de);
       $sanad->bindParam(6,$description);
       $sanad->bindParam(7,$father);
       $sanad->bindParam(8,$branch);
       if($sanad->execute()){
          $data["de_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"])){
    $de_id         = $_POST['id'];
    $money_de      = $_POST['money_de'];
    $description   = $_POST['desc'];
    if(!empty($description)){  $description = htmlspecialchars(strip_tags($description)) ;}
  $save = $ded_calss->update($de_id,$money_de,$description);
  return $save ;
}

?>
