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

  $find = $db->prepare("SELECT line_in , qty from item_line_out WHERE line_out_id = ?");
  $find->bindParam(1,$line_out_id);
  $find->execute();
	if($find->rowCount()>0){$result = $find->fetch(PDO::FETCH_ASSOC);
      $line_in = $result['line_in']; $qty = $result['qty'];}
	$mass = $db->prepare("SELECT outt from items_line where line_id = ?");
  $mass->bindParam(1,$line_in);
	$mass->execute();
	if($mass->rowCount()>0){$row = $mass->fetch(PDO::FETCH_ASSOC);
      $outt = $row['outt'];
      $outt = $outt - $qty ;
    }
  $new = $db->prepare("UPDATE items_line set outt = ? WHERE line_id = $line_in");
  $new->bindParam(1,$outt);
  if($new->execute()){
    $sql = $db->prepare("DELETE FROM item_line_out	WHERE line_out_id = $line_out_id LIMIT 1");
  	$sql->execute();
    if($sql->rowCount()>0){
  		echo "تم حذف السجل بنجاح" ;
  	}
  }
	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>
  ";}
}
?>
