
<?php include "../actions/dbPDO.php";
      $end   = date("Y-m-d");
      $start = date('Y-m-d', strtotime($end. ' - 30 days'));
?>
<div  class="panel panel-primary" id="main_supp">
  <div class="panel-heading clearfix"style="font-size:1.2em;">
  <div class="row" style="padding:4px;">
      <div class="col-xs-3"style="float:right;">
        <label class="control-label"style="float:right;font-size: 15px"> فواتير </label>
      </div>
  </div>
  <div class="row" style="padding:4px;">
      <div class="col-xs-1"style="float:right;padding:2px;">
      </div>
      <div class="col-md-2"style="float:right;padding:2px;margin-right:10px;">
      <div class="input-group">
       <span class="input-group-addon" style="font-size: 14px;font-weight:bold;background:transparent;color:black;" >من</span>
       <input type="text" class="form-control" style="float:right;" id="start" name="start" value="<?php echo $start ; ?>" readonly />
     </div><!-- /input-group -->
     <script>
       $(function() {
         var calendar = $.calendars.instance('');
         $('#start').calendarsPicker({calendar: calendar});
         $('#end').calendarsPicker({calendar: calendar});
         });
     </script>
   </div>
   <div class="col-xs-3"style="float:right;padding:2px;">
     <div class="input-group">
      <span class="input-group-addon" style="font-size: 16px;font-weight:bold;background:transparent;color:black;" >إلي </span>
      <input type="text" class="form-control" style="float:right;" id="end" name="end"value="<?php echo $end ; ?>" readonly />
      <span class="input-group-btn">
        <button id="datt_a" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
      </span>
    </div><!-- /input-group -->
  </div>
  <div class="col-xs-5"style="float:right;">
    <input type="hidden" id="mat_supp_id" name="mat_supp_id" />
    <div class="input-group">
      <span class="input-group-addon" style="font-size: 16px;font-weight:bold;background:transparent;color:black;" >المورد</span>
      <input typ="text" style="float:right;" class="form-control" id="mat_supp_sr" name="mat_supp_sr" placeholder="بحث بالأسم"/>
      <span class="input-group-btn">
        <button id="supp_sr" type="button" class="btn btn-primary btn-xs "><i class="fa fa-search" style="font-size: 22px;font-weight:bold;color:#ff9966; padding:3px; margin:0px;" aria-hidden="true"></i></button>
      </span>
      </div><!-- /input-group -->
    </div>
  </div>
  </div>
  <div class="panel-body"  style="background-color:#00324d ; color:#cce5ff;">
    <div class="table-responsive">
         <table class="table table-bordered table-hover">
           <thead>
              <tr>
                   <th class="text-center" width="10%">رقم الفاتورة</th>
                   <th class="text-center" width="30%">المورد</th>
                   <th class="text-center" width="11%">تاريخ الفاتورة</th>
                   <th class="text-center" width="6%">المبلغ</th>
                   <th class="text-center" width="6%">الكمية</th>
                   <th class="text-center" width="6%">المنصرف</th>
                   <th class="text-center" width="25%">ملاحظات</th>
                   <th class="text-center" width="6%">VAT</th>
              </tr>
            </thead>
              <tbody id="myData">
                <?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
                   GROUP BY mat_in_id   order by material_in.mat_in_id   DESC, material_in.mat_supp_id LIMIT 50  ");
                  $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;
                  ?>
              </tbody>
						<tfoot></tfoot>
          </table>
    </div>
</div>
</div>
<?php
$supp = array();
$sql =$db->prepare("SELECT mat_supp_id , mat_name from material_supp ORDER BY mat_name") ;
$sql->execute();
    $n=0;
    if($sql->rowCount()>0)
   {
      while($row = $sql->fetch()){
             $supp[$n] = array(
                           "value" => $row["mat_name"],
                           "data" => $row["mat_supp_id"]
                         );  $n++ ;
           } }
    else {  $supp[0]= 'لا يوجد '; }
    ?>
<script>
$(document).ready(function(){
  var supp =  JSON.parse('<?php echo json_encode($supp) ; ?>');
  $('#mat_supp_sr').autocomplete({
      lookup: supp,
      maxHeight : 200,
  onSelect: function(suggestion) {
      $("#mat_supp_id").val(suggestion.data);
       }
})
        $("#supp_sr").click(function(event){
            event.preventDefault();
            var mat_supp_id    = $("#mat_supp_id").val();
            var start          = $("#start").val();
            var end            = $("#end").val();
       $.ajax({
              url     : "wh/mat_action.php",
              method  : "POST",
               data    :{mat_supp_id:mat_supp_id,start:start,end:end},
              success : function(data){ $("#myData").html(data); },
              error   :function(result){
                alert(result.statusText);
                }
            });
          });

      $("#datt_a").click(function(event){
          event.preventDefault();
          var first = $("#start").val();
          var end   = $("#end").val();
     $.ajax({
            url     : "wh/mat_action.php",
            method  : "POST",
             data    :{first:first,end:end},
            success : function(data){ $("#myData").html(data); },
            error   :function(result){
              alert(result.statusText);
              }
          });
        });
});
</script>
