<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php
 $pro_id  = $_GET['pro_id'];
 $fdate   = $_GET['fdate'];
 $edate   = $_GET['edate'];





$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo,a.ac_name FROM accounts a LEFT JOIN company c ON(a.coid=c.coid) WHERE a.coid='$coid'  LIMIT 1") or die(mysqli_error($conn));
$inac= mysqli_fetch_assoc($inacc);

$ac_name        = $inac['ac_name'];

$co_name        = $inac['co_name'];
$owner_name     = $inac['owner_name'];
$co_address     = $inac['co_address'];
$co_phone       = $inac['co_phone'];
$co_email       = $inac['co_email'];
$co_logo        = $inac['co_logo']; 


$product_query= mysqli_query($conn,"SELECT item.item_name,item.item_details,item.item_weight,order_list.plus,accounts.ac_name, DATE_FORMAT(order_list.datetime , '%d/%m/%Y') datetime,order_list.invoice,order_list.qty,order_list.order_type FROM order_list  
    INNER JOIN item ON (item.pro_id =order_list.pro_id)
    LEFT JOIN accounts ON (accounts.acid =order_list.acid)

 WHERE order_list.coid='$coid' AND order_list.pro_type='P' AND order_list.pro_id='$pro_id' AND date(datetime) BETWEEN '$fdate' AND '$edate'  ORDER BY date(datetime),oid   ") or die (mysqli_error($conn));
	while ($prow= mysqli_fetch_assoc($product_query)){ $data[]=$prow;}
?>

<section class="content">
 <div class="container-fluid">
  <div class="row">
    <div class="col-md-12">
      <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
        <div class="card-header" style="padding: 3px 8px;">
          <h3 class="card-title">Account Ledger</h3>
          <div class="card-tools">
            <button type="button" class="btn btn-tool" onclick="ExcelMe('#lgtable<?php echo $pro_id;?>')"  title="Download Excel">
              <i class="far fa-file-excel"></i>
            </button>
            <button type="button" onclick="BfpdfMe('#lgtable<?php echo $pro_id;?>');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
            <button type="button" class="btn btn-tool" onclick="printMe('#acledger')" title="Print Now"><i class="fas fa-print"></i></button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" data-dismiss="modal">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>      
            <div class="card-body p-1 table-responsive cscroll" id="acledger" style="max-height: 450px;">
              <table id="lgtable<?php echo $pro_id;?>" class="ttable" cellpadding="0" cellspacing="0">
                 <thead>
                    <tr>
                       <th colspan="2" rowspan="3" class="text-center">
  <?php if($co_logo != ""): ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="data:image/gif;base64,<?php echo $co_logo;?>" alt="User profile picture">
                        <?php else : ?>
                        <img id="img_data" class="profile-user-img img-fluid img-circle" style="width:80px;height:77px;" src="../../dist/img/no_img.jpg" alt="company profile">
                        <?php endif; ?>

                    </th>                    
                       <th colspan="3" rowspan="3">
                         <div style="padding-left:2px;">
                           <sss style="font-size:18px;font-weight: 600;"><?php echo $co_name;?></sss><br>
                           <sss><?php echo $owner_name;?></sss><br>
                           <sss><?php echo $co_address;?></sss><br>
                           <sss><?php echo $co_phone;?></sss> # 
                           <sss><?php echo $co_email;?></sss>
                         </div>
                       </th> 
                       <th colspan="3" class="text-center" style="font-size:13px;"><?php echo 'Singel Item Report';?></th>
                    </tr> 
                    <tr>                    
                       <th colspan="3" class="text-center" style="font-size:13px;">Inventory Ledger</th> 
                    </tr>
                    <tr>                    
                       <th colspan="3" class="text-center" style="font-size:13px;"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?></th>
                    </tr>                                       
                    <tr class="sticky-top">
                       <th class="text-center" style="width:3%;">S/l</th>
                       <th class="text-center" style="width:9%;">Date</th>
                       <th class="text-center" style="width:34%;">Particulars</th>
                       <th class="text-center" style="width:10%;">Type</th>
                       <th class="text-center" style="width:21%;">Invoice</th>
                       <th class="text-center" style="width:8%;">In</th>
                       <th class="text-center" style="width:8%;">Out</th>
                       <th class="text-center" style="width:8%;">Stock</th>
                    </tr>
                 </thead>
                 <tbody id="proshowsss"></tbody>
              </table>
            </div>
           </div>
        </div>

   </div>
  </div>
</section>
<script type="text/javascript">
    $(document).ready(function(){
        var data = '';
        var i = 0;
        var stock = 0;
        var inn = 0;
        var out = 0;
        $.each(<?php echo json_encode($data) ?>, function(key,value){
            
        if(value.plus == '+'){
            stock += Number(value.qty);
            inn = Number(value.qty);
            out ='';  
        } else if(value.plus == '-') {
            stock -= Number(value.qty);
            out = Number(value.qty);
            inn ='';
        }
        data += '<tr>';
        data += '<td class="text-center">'+ ++i +'</td>';
        data += '<td class="text-center">'+ value.datetime +'</td>';
        data += '<td class="text-left">'+ value.ac_name + ',  Ref By:   '+ value.item_name + '     ' + value.item_details + value.item_weight  +  '</td>';
        data += '<td class="text-center">' + vtype(value.order_type) + ' </td>';
        data += '<td class="text-center">'+ value.invoice +'</td>';
        data += '<td class="text-center">'+ inn +'</td>';
        data += '<td class="text-center">'+ out +'</td>';
        data += '<td class="text-center">'+ bden.format(stock) +'</td>';
        data += '</tr>';
    });
     $("#proshowsss").append(data);
  });  
</script>