<?php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
  // $branch = $_SESSION["branch"];
  $father = $_SESSION["user_id"];
  $today = date("Y-m-d");

if($_POST['id3'] !=""){
    $contract_id   = $_POST['id3'];
    $money_in      = $_POST['money_in'];
    $printed_no    = $_POST['printed_no2'];
    $payMethod     = $_POST['payMethod2'];
    $comm          = $_POST['comm3'];

$data = array();
include "../actions/dbPDO.php";
$Number = '/^[0-9]+(\.[0-9]{1,2})?$/' ;

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

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


  $last_kest = $db->prepare("SELECT min(ak_id) as ak_id from aksat_in 
  where contract_id = ? AND date_payed IS NULL");
$last_kest->bindParam(1, $contract_id);
$last_kest->execute();
if ($last_kest->rowCount() > 0) {
$ak = $last_kest->fetch(PDO::FETCH_ASSOC);
$minAK = $ak['ak_id'];
}

  $maxN = $db->prepare("SELECT ak_id , wanted , start,ak_no from aksat_in where ak_id = ? ");
  $maxN->bindParam(1,$minAK) ;
  $maxN->execute();
  if($maxN->rowCount()>0){$row = $maxN->fetch(PDO::FETCH_ASSOC);
    $ak_id = $row['ak_id']; $ak_no = $row['ak_no']; $wanted = $row['wanted']; }
        $maxsql= $db->prepare("SELECT max(payed_id) as maxd , max(audate) as maxT FROM aksat_payed");
        $maxsql->execute();
        if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
          $maxD = $row['maxd'];
          $maxT = $row['maxT'] ;
        } else {$maxD = 0 ; }
        if(!empty($maxT)){
        $start = strtotime($maxT);
        $today = date("Y-m-d H:i:s");
        $end = strtotime($today);
        // $end = $end + 3600 ;
        $mano =   $end -$start  ;
        if($mano < 10){
          $data["ko"] = $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>" ;
              echo json_encode($data);
              exit();
        }
      }
  $money_in = round($money_in , 2); $maxD = $maxD + 1 ;
  $sanad=$db->prepare(" INSERT INTO aksat_payed(payed_id , contract_id , printed_no , date_payed , money_in , payMethod ,
          ak_id , ak_no , comm , father )
     VALUES( ? , ? , ? , ? , ? ,?, ? , ? ,?, ?)");
   $sanad->bindParam(1,$maxD);
   $sanad->bindParam(2,$contract_id);
   $sanad->bindParam(3,$printed_no);
   $sanad->bindParam(4,$today);
   $sanad->bindParam(5,$money_in);
   $sanad->bindParam(6,$payMethod);
   $sanad->bindParam(7,$ak_id);
   $sanad->bindParam(8,$ak_no);
   $sanad->bindParam(9,$comm);
   $sanad->bindParam(10,$father);
   if($sanad->execute()){
  $data["ko"] = $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);
}
?>
