<?php 
  session_start();
  if($_SESSION['name']=='') {
   header("Location:login.php");
  }
  $CURDATE        = date('Y-m-d');
  $new_date       = date('Y-m-d', strtotime($CURDATE . " + 2 day"));

  $lr             = "SELECT * FROM `lr_mst` WHERE DATE(l_date) = '".$CURDATE."' AND  company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(created_datetime)>='".$_SESSION['from_year']."' AND DATE(created_datetime)<='".$_SESSION['to_year']."'";
  $lr_res         = mysqli_query($conn,$lr);
  $total_records  = mysqli_num_rows($lr_res);

  $vendor     = "SELECT * FROM `vendor_master` WHERE `status` = '0'";
  $vendor_res = mysqli_query($conn,$vendor);
  $vendor_num = mysqli_num_rows($vendor_res);
  
  $truck     = "SELECT * FROM `truck_mst`";
  $truck_res = mysqli_query($conn,$truck);
  $truck_num = mysqli_num_rows($truck_res);

  $lrs     = "SELECT * FROM `lr_mst` WHERE  company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(created_datetime)>='".$_SESSION['from_year']."' AND DATE(created_datetime)<='".$_SESSION['to_year']."'";
  $lrs_res = mysqli_query($conn,$lrs);
  $lrs_num = mysqli_num_rows($lrs_res);

  $bill = "SELECT * FROM `billing` WHERE DATE(bill_date) = '".$CURDATE."' AND  company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(bill_date)>='".$_SESSION['from_year']."' AND DATE(bill_date)<='".$_SESSION['to_year']."'";
  $bill_res = mysqli_query($conn,$bill);
  $bill_num = mysqli_num_rows($bill_res);
  
  $emp_mst = mysqli_query($conn, "SELECT * FROM `emp_mst` WHERE `status` = '0'");
  $emp_nos = mysqli_num_rows($emp_mst); 

  $contract     = mysqli_query($conn,"SELECT * FROM `contract`");
  $contract_num = mysqli_num_rows($contract);

  $con_data = "SELECT * FROM `contract` WHERE DATE(c_date) = '".$CURDATE."' AND  company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(created_datetime)>='".$_SESSION['from_year']."' AND DATE(created_datetime)<='".$_SESSION['to_year']."'";
  $con_data_res   = mysqli_query($conn,$con_data);
  $total_contract = mysqli_num_rows($con_data_res);

  $lr_master = mysqli_query($conn,"SELECT * FROM `lr_mst` WHERE  company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."' AND DATE(eway_ex_date)>='".$_SESSION['from_year']."' AND DATE(eway_ex_date)<='".$new_date."' ORDER BY eway_ex_date ASC");
  $lr_master_num = mysqli_num_rows($lr_master);

?>
<section class="content-header">
  <h3 class="capital font15 bold">Dashboard</h3><hr/>
</section>
<?php if($_SESSION['module'] == 'BO'){ ?>
<section class="content">
   <div class="row">
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=vendor/list.php">
            <div class="small-box bg-purple">
               <div class="inner">
                  <h3><?php echo $vendor_num;?></h3>
                  <p>VENDOR</p>
               </div>
               <div class="icon">
                  <i class="fa fa-users" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=truck_mst/list.php">
            <div class="small-box bg-yellow">
               <div class="inner">
                  <h3><?php echo $truck_num;?></h3>
                  <p>TRUCK</p>
               </div>
               <div class="icon">
                  <i class="fa fa-truck" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=lr/list.php">
            <div class="small-box bg-aqua">
               <div class="inner">
                  <h3><?php echo $lrs_num;?></h3>
                  <p>LR BOOKED</p>
               </div>
               <div class="icon">
                  <i class="fa fa-bold" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=emp_mst/list.php">
            <div class="small-box bg-green">
               <div class="inner">
                  <h3><?php echo $emp_nos;?></h3>
                  <p>EMPLOYEE</p>
               </div>
               <div class="icon">
                  <i class="fa fa-etsy" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
   </div>
   <div class="row">
      <section class="col-lg-6">
        <div class="box box-primary">
          <div class="box-header">
             <h3 class="box-title">TODAY'S LR BOOKING</h3>
          </div>
          <div class="box-body">
             <div class="table-responsive">
                <table class="table table-bordered table-striped list_data no-margin">
                   <thead>
                      <tr>
                         <th class="text-center">LR NO</th>
                         <th class="text-center">FROM</th>
                         <th class="text-center">TO</th>
                         <th class="text-center">CONSIGNOR</th>
                         <th class="text-center">CONSIGNEE</th>
                         <th class="text-center">WEIGHT</th>
                         <th class="text-center">PCS</th>
                      </tr>
                   </thead>
                   <tbody>
                      <?php 
                         $count=$total_records;  if($count>0){
                         while ($lr_row = mysqli_fetch_array($lr_res)) { 
                         $vendor = "SELECT * FROM `vendor_master` WHERE `id` = '".$lr_row['consignor_name']."'";
                         $vendor_res = mysqli_query($conn,$vendor);
                         $vendor_row = mysqli_fetch_array($vendor_res);
                         
                         $vendor1 = "SELECT * FROM `vendor_master` WHERE `id` = '".$lr_row['consignee_name']."'";
                         $vendor1_res = mysqli_query($conn,$vendor1);
                         $vendor1_row = mysqli_fetch_array($vendor1_res);
                         ?>
                      <tr align="center">
                         <td><a href="#" id="<?php echo $lr_row['lr_no'];?>" onclick="viewData(this);"><?php echo $lr_row['lr_no'];?></a></td>
                         <td><?php echo $lr_row['from_place'];?></td>
                         <td><?php echo $lr_row['to_place'];?></td>
                         <td><?php echo $vendor_row['name'];?></td>
                         <td><?php echo $vendor1_row['name'];?></td>
                         <td><?php 
                            if($lr_row['weight'] == ''){
                              echo "-";
                            }else{
                              echo $lr_row['weight'];
                            }
                            ?></td>
                         <td><?php 
                            if($lr_row['pcs'] == ''){
                              echo "-";
                            }else{
                              echo $lr_row['pcs'];
                            }
                            ?></td>
                      </tr>
                      <?php } }else{ ?>
                      <tr>
                         <td colspan="7" align="center"><b><?php echo "NO LR BOOKED...!!!!"; ?></b></td>
                      </tr>
                      <?php } ?>
                   </tbody>
                </table>
             </div>
          </div>
        </div>
      </section>
      <section class="col-lg-6">
        <div class="box box-primary">
          <div class="box-header">
             <h3 class="box-title">TODAY'S BILL</h3>
          </div>
          <div class="box-body">
             <div class="table-responsive">
                <table class="table table-bordered table-striped list_data no-margin">
                   <thead>
                      <tr>
                        <th class="text-center" width="10">SR.NO</th>
                        <th class="text-center" width="80">BILL NO</th>
                        <th class="text-center">VENDOR</th>
                        <th class="text-center" width="100">NET AMOUNT</th>
                        <th class="text-center" width="100">GROSS AMOUNT</th>
                      </tr>
                   </thead>
                   <tbody>
                      <?php 
                        $cou = 1;
                        $total_net_amnt   = 0;
                        $total_gross_amnt = 0;
                        $counts=$bill_num;  if($counts>0){
                        while ($bill_row = mysqli_fetch_array($bill_res)) {
                        $vendor = "SELECT * FROM `vendor_master` WHERE `id` = '".$bill_row['vendor_id']."'";
                        $vendor_res = mysqli_query($conn,$vendor);
                        $vendor_row = mysqli_fetch_array($vendor_res);
                        $total_net_amnt   += $bill_row['net_amt'];
                        $total_gross_amnt += $bill_row['gross_amt']; 
                      ?>
                      <tr align="center">
                         <td><?php echo $cou++;?></td>
                         <td><a href="#" id="<?php echo $bill_row['invoice_no'];?>" onclick="BillCopyPrint(this);"><?php echo $bill_row['invoice_no'];?></a></td>
                         <td><?php echo $vendor_row['name'];?></td>
                         <td align="right"><?php echo customFormatMoney($bill_row['net_amt']);?></td>
                         <td align="right"><?php echo customFormatMoney($bill_row['gross_amt']);?></td>
                      </tr>
                      <?php }  ?>
                      <tr>
                        <td colspan="3" class="bold">TOTAL</td>
                        <td align="right"><?php echo customFormatMoney($total_net_amnt)?></td>
                        <td align="right"><?php echo customFormatMoney($total_gross_amnt)?></td>
                      </tr>
                      <?php }else{ ?>
                      <tr>
                         <td colspan="7" align="center"><b><?php echo "NO BILL BOOKED...!!!!"; ?></b></td>
                      </tr>
                      <?php } ?>
                   </tbody>
                </table>
             </div>
          </div>
        </div>
      </section>
   </div>
   <div class="row">
      <section class="col-lg-12">
        <div class="box box-primary">
          <div class="box-header">
             <h3 class="box-title">E-WAY BILL EXPIRED ALERT</h3>
          </div>
          <div class="box-body">
             <div class="table-responsive scroll-bar">
                <table class="table table-bordered list_data no-margin">
                   <thead>
                      <tr>
                         <th width="5%" class="text-center">LR NO</th>
                         <th width="5%" class="text-center">DATE</th>
                         <th width="7%" class="text-center">TRUCK NO</th>
                         <th width="5%" class="text-center">FROM</th>
                         <th width="5%" class="text-center">TO</th>
                         <th width="20%" class="text-center">CONSIGNOR</th>
                         <th width="20%" class="text-center">CONSIGNEE</th>
                         <th width="10%" class="text-center">E-WAY BILL NO</th>
                         <th width="5%" class="text-center">BILL DATE</th>
                         <th width="10%" class="text-center">E-WAY EXP. DATE</th>
                      </tr>
                   </thead>
                   <tbody>
                    <?php 
                    $counts=$lr_master_num;  if($counts>0){
                      while ($lr_rows = mysqli_fetch_array($lr_master)) {
                         $vendors = "SELECT * FROM `vendor_master` WHERE `id` = '".$lr_rows['consignor_name']."'";
                         $vendors_res = mysqli_query($conn,$vendors);
                         $vendors_row = mysqli_fetch_array($vendors_res);
                         
                         $vendors1 = "SELECT * FROM `vendor_master` WHERE `id` = '".$lr_rows['consignee_name']."'";
                         $vendors1_res = mysqli_query($conn,$vendors1);
                         $vendors1_row = mysqli_fetch_array($vendors1_res);

                    ?>
                      <tr align="center" <?php echo $lr_rows['eway_ex_date'] <= $CURDATE?'class="divtoBlink"':''; ?>>
                         <td><?php echo $lr_rows['lr_no'];?></td>
                         <td><?php echo risFormatDate($lr_rows['l_date']);?></td>
                         <td><?php echo $lr_rows['truck_no'];?></td>
                         <td><?php echo $lr_rows['from_place'];?></td>
                         <td><?php echo $lr_rows['to_place'];?></td>
                         <td align="left"> <?php echo $vendors_row['name'];?></td>
                         <td align="left"> <?php echo $vendors1_row['name'];?></td>
                         <td><?php echo $lr_rows['eway_bill_no'];?></td>
                         <td><?php echo risFormatDate($lr_rows['eway_date']);?></td>
                         <td><?php echo risFormatDate($lr_rows['eway_ex_date']);?></td>
                      </tr>
                      <?php } }else{ ?>
                      <tr>
                         <td colspan="11" align="center"><b><?php echo "NO DATA FOUND...!!!!"; ?></b></td>
                      </tr>
                      <?php } ?>
                   </tbody>
                </table>
             </div>
          </div>
        </div>
      </section>
   </div>
</section>
<?php } ?>

<?php if($_SESSION['module'] == 'BR'){ ?>
<section class="content">
   <div class="row">
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=vendor/list.php">
            <div class="small-box bg-purple">
               <div class="inner">
                  <h3><?php echo $vendor_num;?></h3>
                  <p>VENDOR</p>
               </div>
               <div class="icon">
                  <i class="fa fa-users" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=truck_mst/list.php">
            <div class="small-box bg-yellow">
               <div class="inner">
                  <h3><?php echo $truck_num;?></h3>
                  <p>TRUCK</p>
               </div>
               <div class="icon">
                  <i class="fa fa-truck" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=contract/list.php">
            <div class="small-box bg-aqua">
               <div class="inner">
                  <h3><?php echo $contract_num;?></h3>
                  <p>CONTRACT</p>
               </div>
               <div class="icon">
                  <i class="fa fa-bold" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
      <div class="col-lg-3 col-xs-6">
         <a href="index.php?page=emp_mst/list.php">
            <div class="small-box bg-green">
               <div class="inner">
                  <h3><?php echo $emp_nos;?></h3>
                  <p>EMPLOYEE</p>
               </div>
               <div class="icon">
                  <i class="fa fa-etsy" aria-hidden="true"></i>
               </div>
            </div>
         </a>
      </div>
   </div>
    <div class="row">
      <section class="col-lg-12">
        <div class="box box-primary">
          <div class="box-header">
             <h3 class="box-title">TODAY'S CONTRACT</h3>
          </div>
          <div class="box-body">
             <div class="table-responsive">
                <table class="table table-bordered table-striped list_data no-margin">
                   <thead>
                      <tr>
                        <th width="10%" class="text-center">CODE</th>
                        <th width="13%" class="text-center">FROM</th>
                        <th width="13%" class="text-center">TO</th>
                        <th class="text-center">VENDOR</th>
                        <th width="8%" class="text-center">AMOUNT</th>
                        <th width="8%" class="text-center">ADVANCE</th>
                        <th width="8%" class="text-center">BALANCE</th>
                        <th width="8%" class="text-center">NET AMOUNT</th>
                      </tr>
                   </thead>
                   <tbody>
                      <?php
                      $master_vendor = combine(getAllRecords('vendor_master'), 'id', 'name'); 
                      $total_count=$total_contract;  if($total_count>0){
                      while ($con_row = mysqli_fetch_assoc($con_data_res)) { ?>
                      <tr align="center">
                         <!-- <td><a href="#" id="<?php echo $con_row['code'];?>" onclick="viewData(this);"><?php echo $con_row['code'];?></a></td> -->
                         <td><?php echo $con_row['code'];?></td>
                         <td><?php echo $con_row['from_place'];?></td>
                         <td><?php echo $con_row['to_place'];?></td>
                         <td><?php echo @$master_vendor[$con_row['vendor_id']];?></td>
                         <td><?php echo $con_row['amount'];?></td>
                         <td><?php echo $con_row['advance'];?></td>
                         <td><?php echo $con_row['total_amnt'];?></td>
                         <td><?php echo $con_row['net_amnt'];?></td>
                      </tr>
                      <?php } }else{ ?>
                      <tr>
                         <td colspan="8" align="center"><b><?php echo "NO CONTRACT BOOKED...!!!!"; ?></b></td>
                      </tr>
                      <?php } ?>
                   </tbody>
                </table>
             </div>
          </div>
        </div>
      </section>
    </div>
</section>
<?php } ?>

<script type="text/javascript">
function viewData(e) {
    var $i = $(e).attr("id");
    $.ajax({
        url: "lr/view.php",
        method: "post",
        data: {
            lr_no: $i
        },
        success: function(data) {
            $('#employee_detail').html(data);
            $('#dataModal').modal("show");
        }
    });
}

function BillCopyPrint(e) {
    var $i = $(e).attr("id");
    $.ajax({
        url: "bill/bill_copy_print.php",
        method: "post",
        data: {
            id: $i
        },
        success: function(data) {
            $('#employee_detail').html(data);
            $('#dataModal').modal("show");
        }
    });
}
</script>

<script type="text/javascript">
  var $div2blink = $(".divtoBlink");
  var backgroundInterval = setInterval(function(){
    $div2blink.toggleClass("backgroundRed");
  },1000)
</script>
<style type="text/css">
  .backgroundRed{
    background: #dab6b6;
  }

  #divtoBlink{
    -webkit-transition: background 1.0s ease-in-out;
    -ms-transition:     background 1.0s ease-in-out;
    transition:         background 1.0s ease-in-out;
  }
</style>