<?php
if(!isset($_SESSION)) {  session_start(); }
  ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
  $father = $_SESSION["user_id"];
  include "../actions/dbPDO.php";

$mat_in_id          = $_POST['mat_in_idd'];
$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'];
$comm               = $_POST['comm'];
$vat    					  = $_POST["vat"];
$vat_out            = $_POST["vat_out"];

$Number ='/^[0-9]+(\.[0-9]{1,2})?$/' ;
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();
}else{
  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;}
    // existing mat_name in your database
$sql =$db->prepare("SELECT mat_in_id , invoice_no , mat_invoice_date FROM material_in
     WHERE   invoice_no = ? AND mat_invoice_date = ? AND mat_supp_id = ? LIMIT 1");
$sql->bindParam(1,$invoice_no);
$sql->bindParam(2,$mat_invoice_date);
$sql->bindParam(3,$mat_supp_id);
$sql->execute();
  if($sql->rowCount()>1){$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 {
  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) ;
  }
  if(empty($mat_invoice_date)){$mat_invoice_date = date("Y-m-d");}
    $sql = $db->prepare(" UPDATE material_in SET
      invoice_no = ?, mat_supp_id = ?, mat_invoice_date = ? , store_id = ? , net_cost = ? ,comm =? ,
         vat = ? , vat_out = ? WHERE  mat_in_id = ? ");
   $sql->bindParam(1,$invoice_no);
   $sql->bindParam(2,$mat_supp_id);
   $sql->bindParam(3,$mat_invoice_date);
   $sql->bindParam(4,$store_id);
   $sql->bindParam(5,$net_cost);
   $sql->bindParam(6,$comm);
   $sql->bindParam(7,$vat);
   $sql->bindParam(8,$vat_out);
   $sql->bindParam(9,$mat_in_id);
  if($sql->execute())
 {echo "<div class='alert alert-success'>
 <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
   <b>تم التحديث بنجاح</b>
 </div>
 ";}
  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>
  ";}
}
}
?>
