<?php
session_start();
ob_start();

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

if(isset($_POST["out_qty"])&& isset($_POST["req_id"]))
{
 $out_qty         = $_POST["out_qty"];
 $out_line_id     = $_POST["out_line_id"];
 $out_item_id     = $_POST["out_item_id"];
 $out_unit_price  = $_POST["out_unit_price"];
//  $technician			= $_POST["technician"];
 $date_out        = $_POST["mydate"];
 $req_id          = $_POST["req_id"];
//  $kmm          		= $_POST["kmm"];
if($req_id==""){
  echo("لا يوجد طلب صيانة ... فضلا سجل طلب صيانة");
  exit();
}

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

 if(empty($date_out)){$date_out = date("Y-m-d");}
//  $date_out = date("Y-m-d");
 if(empty($kmm)){$kmm = 0 ;}
 $br = $db->prepare("SELECT actions.carid , actions.ac_date , actions.branch FROM ment_request , actions
	  				WHERE ment_request.car_id = actions.carid AND ment_request.req_id = ? AND actions.ac_date = ?");
	$br->bindParam(1,$req_id);
	$br->bindParam(2,$date_out);
	$br->execute();
	if($br->rowCount()>0){$anch = $br->fetch(PDO::FETCH_ASSOC);
		$branch = $anch['branch'];}else{$branch = 1 ;}

		//just frist use
// 	  $old = $db->prepare("SELECT line_out_id, req_id , date_out from item_line_out where date_out BETWEEN '2018-01-30' AND '2018-02-2'");
// 		$old->execute();
// 	if($old->rowCount()>0)
//  {
// 		while($rec = $old->fetch())
// 		{
// 		$line_update = $rec["line_out_id"];
// 		$req_update  = $rec["req_id"];
// 		$date_update = $rec["date_out"];

// 		$br = $db->prepare("SELECT actions.carid , actions.ac_date, actions.branch FROM ment_request , actions
// 							 WHERE ment_request.car_id = actions.carid AND ment_request.req_id = ? AND actions.ac_date = ?");
// 		$br->bindParam(1,$req_update);
// 		$br->bindParam(2,$date_update);
// 		$br->execute();
// 		if($br->rowCount()>0){$anch = $br->fetch(PDO::FETCH_ASSOC);
// 			$branch_update = $anch['branch'];}else{$branch_update = 1 ;}

// 	$Upp = $db->prepare("UPDATE item_line_out set branch = ? where line_out_id = ? LIMIT 1");
// 		$Upp->bindParam(1,$branch_update);
// 		$Upp->bindParam(2,$line_update);
// 		$Upp->execute();
// 		}
// 	}//end first use
// out qty must be less than net store_balance SELECT next_km from items where item_id = 1
// $sql = $db->prepare("SELECT qty , outt from store_balance where line_id = ? LIMIT 1");
$sql = $db->prepare("SELECT qty , outt from items_line where line_id = ? LIMIT 1");
$sql2 = $db->prepare("UPDATE items_line set outt = ? where line_id = ? LIMIT 1");
// $item = $db->prepare("SELECT next_km from items where item_id = ? LIMIT 1");

$sql3 = $db->prepare("SELECT max(line_out_id)as maxD from item_line_out");

$sql4 = $db->prepare("INSERT INTO item_line_out(line_out_id , req_id , item_id , tot_price ,
                      qty , line_in , date_out , father,branch)
                       VALUES(? ,?, ? ,? ,? ,? , ?,? ,?)");
$result = 0;
// $next_km = 0;
for($i = 0; $i<count($out_qty); $i++)
{ if($out_qty[$i] <= 0 || empty($out_qty[$i]) || !preg_match($Number,$out_qty[$i])){ continue;}
else{
  $sql->bindParam(1,$out_line_id[$i]);
  $sql->execute();
    if($sql->rowCount()>0){$row = $sql->fetch(PDO::FETCH_ASSOC);
				$net = 0 ;
        $qty = $row["qty"];
        $outt = $row["outt"];
				if(empty($outt)){$net = $qty ;}else{$net = $qty - $outt ; }
    if($net < $out_qty[$i]){ echo "برجاء مراجعة كمية المنصرف ";}
      else{
            $out2 = $outt +$out_qty[$i];
            $sql2->bindParam(1,$out2);
            $sql2->bindParam(2,$out_line_id[$i]);
          if($sql2->execute()){
					// 	$item->bindParam(1,$out_item_id[$i]);
					// 	$item->execute() ;
					// if($item->rowCount()>0){$da = $item->fetch(PDO::FETCH_ASSOC);
					// 	$next_km = $da['next_km'];}
					// 	$next_km = $next_km + $kmm ;
              $sql3->execute();
            if($sql3->rowCount()>0){$row3 = $sql3->fetch(PDO::FETCH_ASSOC);
        $maxD = $row3['maxD']+1;} else {$maxD = 1 ;}

      $tot_price4 = ($out_unit_price[$i] * $out_qty[$i]);
      $sql4->bindParam(1,$maxD);
      $sql4->bindParam(2,$req_id);
      $sql4->bindParam(3,$out_item_id[$i]);
      $sql4->bindParam(4,$tot_price4);
      $sql4->bindParam(5,$out_qty[$i]);
      $sql4->bindParam(6,$out_line_id[$i]);
      $sql4->bindParam(7,$date_out);
			// $sql4->bindParam(8,$next_km);
      $sql4->bindParam(8,$father);
			// $sql4->bindParam(9,$technician[$i]);
			$sql4->bindParam(9,$branch);
    if($sql4->execute()){
    $result = $result + 1;
    }
	}//sql2
  }
}
}
}// end of mail loop
echo "تم تسجيل $result سجل بنجاح";
}else{
echo "فضلا تأكد من الكمية المنصرفة و من وجود طلب صيانة";
}
?>