<?php
if(!isset($_SESSION)) {  session_start(); }
  ob_start();
if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
if(isset($_POST["mat_supp_id"])){
  $mat_supp_id    = $_POST["mat_supp_id"];
  $start          = $_POST["start"] ;
  $end            = $_POST["end"] ;

    include "../actions/dbPDO.php";
 $output = '';
   $sql =$db->prepare("SELECT sum(items_line.outt) as outt , sum(items_line.qty) as qty ,material_in.mat_in_id , material_in.mat_supp_id ,material_in.comm , material_in.net_cost ,material_supp.mat_name, material_in.vat_out ,
    material_in.invoice_no ,material_in.mat_invoice_date
   FROM items_line , material_in , material_supp
   WHERE material_in.mat_in_id = items_line.mat_in_id  AND material_in.mat_supp_id = material_supp.mat_supp_id
   AND material_in.mat_supp_id = ?  AND material_in.mat_invoice_date between ? AND ? GROUP BY mat_in_id   order by material_in.mat_in_id   DESC, material_in.mat_supp_id LIMIT 50  ");
   $sql->bindParam(1,$mat_supp_id);
   $sql->bindParam(2,$start);
   $sql->bindParam(3,$end);
   $sql->execute();
    if($sql->rowCount()>0)
   {
      while($row = $sql->fetch())
      {
       $output .= '<tr>
                     <td>
                     <a href="wh/mat_detailes.php?id=' .$row['mat_in_id']  . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $row['invoice_no'] . '</button></a>
                     </td>
                     <td>' . $row['mat_name'] . '</td>
                     <td>' . $row['mat_invoice_date'] . '</td>
                     <td>' . $row['net_cost'] . '</td>
                     <td class = "table-info">' . $row['qty'] . '</td>
                     <td class="text-center" >' . $row['outt'] . '</td>
                     <td class="text-center" >' . $row['comm'] . '</td>
                     <td class="text-center" >' . $row['vat_out'] . '</td>
                  </tr>';
      }
   }
   else
   {
        $output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';
   }
   echo $output;
 }
 // start $end
 if(isset($_POST["first"])){
   $first = $_POST["first"] ;
   $end   = $_POST["end"] ;
     include "../actions/dbPDO.php";
  $output = '';
    $sql = $db->prepare("SELECT sum(items_line.outt) as outt , sum(items_line.qty) as qty ,material_in.mat_in_id , material_in.mat_supp_id ,material_in.comm , material_in.net_cost ,material_supp.mat_name, material_in.vat_out ,
     material_in.invoice_no ,material_in.mat_invoice_date
    FROM items_line , material_in , material_supp
    WHERE material_in.mat_in_id = items_line.mat_in_id AND material_in.mat_supp_id = material_supp.mat_supp_id
   AND material_in.mat_invoice_date between ? AND ? GROUP BY items_line.mat_in_id   order by material_in.mat_in_id   DESC LIMIT 30  ");
    $sql->bindParam(1,$first);
    $sql->bindParam(2,$end);
    $sql->execute();
     if($sql->rowCount()>0)
    {
       while($row = $sql->fetch())
       {
        $output .= '<tr>
                      <td>
                      <a href="wh/mat_detailes.php?id=' .$row["mat_in_id"]  . '"target="_blank"><button type="button" style="font-weight:bold ; font-size:14px; padding:2;" class="btn btn-danger btn-xs btn-block ">' . $row['invoice_no'] . '</button></a>
                      </td>
                      <td>' . $row["mat_name"] . '</td>
                      <td>' . $row["mat_invoice_date"] . '</td>
                      <td>' . $row["net_cost"] . '</td>
                      <td class = "table-info">' . $row["qty"] . '</td>
                      <td class="text-center" >' . $row["outt"] . '</td>
                      <td class="text-center" >' . $row["comm"] . '</td>
                      <td class="text-center" >' . $row["vat_out"] . '</td>
                   </tr>';
       }
    }
    else  {         $output .= '<tr><td colspan="8">لا يوجد سجلات ...</td></tr>';    }
    echo $output;

  }
  ?>
