<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}

  $branch = $_SESSION["branch"];
  $father = $_SESSION["user_id"];
  $current_datee = date("Y-m-d");
if($_POST['money_box'] !=""){
    $money_box      = $_POST['money_box'];
    // $month_date     = $_POST['myday'];
    $close_time  = date("H");
    if($close_time < 8){
      $month_date = date('Y-m-d', strtotime($current_datee. ' - 1 days'));
    }else{
      $month_date = $current_datee;
    }
    $payMethod      = $_POST['payMethod2'];
    $comm           = $_POST['comm3'];
    $receipt        = $_POST['receipt'];

$data = array();
require_once "../magic/DataBase.php";
 $database = new DataBase();

  $conn = $database->getConnection();
  
$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

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

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

if(!empty($error)){
  $data["error"] = $error ;
}else {

  $maxN = $conn->prepare("SELECT max(box_id) as maxd , max(audate) as time1 FROM boxmoney where father = ? AND branch = ?");
  $maxN->bindParam(1,$father) ;
  $maxN->bindParam(2,$branch) ;
  $maxN->execute();
  if($maxN->rowCount()>0){$row = $maxN->fetch(PDO::FETCH_ASSOC);
    $maxIN = $row['maxd'];}
    if(!empty($maxIN)){
    $start = strtotime($row['time1']);
    $date_today = date("Y-m-d H:i:s");
    $end = strtotime($date_today);
    $mano =   $end -$start  ;
    if($mano <20){
      $data["box_id"] = $maxIN ;
      $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>" ;
          echo json_encode($data);
          exit();
    }
  }

  $maxsql= $conn->prepare("SELECT max(box_id) as maxd FROM boxmoney");
  $maxsql->execute();
  if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
      $maxD = $row['maxd']+1;} else {$maxD = 1 ; }
$money_box = round($money_box,2);
  $sanad=$conn->prepare(" INSERT INTO boxmoney(box_id ,  current_datee, month_date , money_box , pay_method ,
    receipt , comm , father , branch )
   VALUES( ? , ? , ? , ? , ? , ? , ? ,?, ? )");
   $sanad->bindParam(1,$maxD);
   $sanad->bindParam(2,$current_datee);
   $sanad->bindParam(3,$month_date);
   $sanad->bindParam(4,$money_box);
   $sanad->bindParam(5,$payMethod);
   $sanad->bindParam(6,$receipt);
   $sanad->bindParam(7,$comm);
   $sanad->bindParam(8,$father);
   $sanad->bindParam(9,$branch);
   if($sanad->execute()){
  $data["box_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>" ;
  }
 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);
}
?>