<?php
if(!isset($_SESSION)) {  session_start(); }
  ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
else{
  $father = $_SESSION["user_id"];
}
  
// include "../actions/dbPDO.php";
require_once "../magic/DataBase.php";
$dataBase = new DataBase();
	$conn = $dataBase->getConnection();

      $invoice_no         = $_POST['invoice_no'];
      $mat_supp_id        = $_POST['mat_supp_id'];
      $mat_invoice_date   = $_POST['mat_invoice_date'];
      $store_id           = $_POST['store_id'];
      $net_cost           = $_POST['net_cost'];
      $vat    			      = $_POST["vat"];
      $vat_out            = $_POST["vat_out"];
      $comm               = $_POST['comm'];

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

$maxsql= $conn->prepare("SELECT max(mat_in_id) as maxd , max(audate) as time1 FROM material_in");
$maxsql->execute();
if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
    $maxD = $row['maxd']+1;
    $start = $row['time1'] ;
  } else {$maxD = 1 ; }

if(!empty($maxD)){
$start = strtotime($start);
$date_today = date("Y-m-d H:i:s");
$end = strtotime($date_today);
// $end = $end + 3600 ;
$mano =   $end -$start  ;
if($mano < 15){
      exit();
}
}

if(empty($mat_supp_id)||empty($invoice_no)||empty($net_cost))
  {
  echo "
        <div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;' >&times;</a>
        <b>من فضلك سجل اسم المورد ورقم الفاتورة و اجمالي الفاتورة على الاقل ليتم حفظ السجل  ...</b>
        </div>
  ";
  exit();
}
if(!empty($invoice_no)){  $invoice_no = htmlspecialchars(strip_tags($invoice_no)) ; }
if(!empty($net_cost)){
    if(!preg_match($Number,$net_cost)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $net_cost إجمالي الفاتورة بعد الخصم أرقام فقط </b>
      </div>
    ";
      exit();
    }
    if((strlen($net_cost) < 0)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>اجمالي الفاتورة أقل من الصفر ... انتبه من فضلك ..<b>
      </div>
      ";
      exit();
    }
}
if(!empty($vat_out)){
    if(!preg_match($Number,$vat_out)){
      echo "
      <div class='alert alert-warning'>
      <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b> تأكد من الرقم !! $vat_out ضريبة القيمة المضافة أرقام فقط</b>
      </div>
    ";
      exit();
    }}else{$vat_out = 0 ;}
  if(!empty($comm)){ $comm = htmlspecialchars(strip_tags($comm)) ;  }else{$comm = null ;}
  if(empty($mat_invoice_date)){$mat_invoice_date = date("Y-m-d");}
  
    // existing mat_name in your database
    $firstDate = date('Y-01-01');
$sql =$conn->prepare("SELECT mat_in_id , invoice_no , mat_invoice_date FROM material_in
     WHERE   invoice_no = ?  AND mat_supp_id = ? AND mat_invoice_date > $firstDate LIMIT 1");
$sql->bindParam(1,$invoice_no);
// $sql->bindParam(2,$mat_invoice_date);
$sql->bindParam(2,$mat_supp_id);
$sql->execute();
  if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
      $mat_in_id          = $row["mat_in_id"];
      $invoice_no         = $row["invoice_no"];
      $mat_invoice_date   = $row["mat_invoice_date"];
    echo "  <div class='alert alert-danger'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
        <b>تم تسجيل بيانات $invoice_no  من قبل كود رقم $mat_in_id بتاريخ  $mat_invoice_date من نفس المورد</b>
        </div>  ";
  exit();
}else {
$myDate =date("Y-m-d");
if($vat_out > 0){
  $mass = $vat_out + 100 ;
  $net_cost = round((($net_cost / $mass)* 100),2) ;
  $vat_out = round(($net_cost * ($vat_out/100)),2) ;
}
  $sql = $conn->prepare(" INSERT INTO material_in
  (mat_in_id , invoice_no, mat_supp_id , mat_invoice_date, store_id, net_cost ,comm , date_in , father , vat , vat_out)
   VALUES (?, ?, ?, ?, ?, ? , ?, ? , ? , ? , ?)");
        $sql->bindParam(1,$maxD);
        $sql->bindParam(2,$invoice_no);
        $sql->bindParam(3,$mat_supp_id);
        $sql->bindParam(4,$mat_invoice_date);
        $sql->bindParam(5,$store_id);
        $sql->bindParam(6,$net_cost);
        $sql->bindParam(7,$comm);
        $sql->bindParam(8,$myDate);
        $sql->bindParam(9,$father);
        $sql->bindParam(10,$vat);
        $sql->bindParam(11,$vat_out);
 if($sql->execute()){echo $maxD;}
  else{ echo "<div class='alert alert-danger'>
  <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
  </div>
  ";}
}
