<?php
        include("homeheader.php");
		
		$to_date=$_POST['to_date'];
		$from_date=$_POST['from_date'];
		

    ?>
	<link href="datepicker/datepicker3.css" rel="stylesheet">
	<link href="datepicker/datepicker3.css" rel="stylesheet">
<link href="vendors/datatables.net/jquery.dataTables.css" rel="stylesheet">
<link href="vendors/datatables.net-buttons/buttons.dataTables.min.css" rel="stylesheet">
<link href="vendors/select2/dist/css/select2.min.css" rel="stylesheet">
<link rel="stylesheet" href="vendors/morris.js/morris.css">
<style type="text/css">
    h5,h6{
        text-align:center;
    }


    @media print {
        .btn-print {
            display:none !important;
        }
        .main-footer	{
            display:none !important;
        }
        .box.box-primary {
            border-top:none !important;
        }
        .angel{
            display:none !important;
        }
        .hide-section{
            display:none; 
        }


    }
</style>
	
	<script type="text/javascript">
 function validate() {
var str=document.form1.amount.value;
       var valid="1234567890.";
	   
	   for(i=0;i<str.length;i++){
	   if(valid.indexOf(str.charAt(i))==-1) {
	   alert("Error Entering Amount To Topup!");
	   document.form1.amount.value=="";
	   document.form1.amount.focus();
	   return false;
	   }
	   }
	   if(document.form1.amount.value=="") {
		   alert("Amount to Topup not Entered! Please try again.");
		   document.form1.amount.value=="";
	       document.form1.amount.focus();
		   return false;
		   
	   }
   }
</script>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
          <div class="left_col scroll-view">
            <!--div class="navbar nav_title" style="border: 0;">
              <a href="index.html" class="site_title">
			  <div class="profile_pic" style="margin-top:-10px;">
					<img src="images/mlogo.png" alt="" class="img-circle profile_img" />
				</div>
			  <!--i class="fa fa-paw"></i--> <!--span>Instant Airtime </span></a>
            </div-->

            <div class="clearfix"></div>

			
            <!-- menu profile quick info -->
            <div class="profile">
              <div class="profile_pic">
			   <?php 
			  if($img == ""){
				  ?>
				   <img src="profileimages/noimage.gif" alt="..." class="img-circle profile_img">
				  <?php
			  }else{
				?>
				 <img src="<?php echo $photo ?>" alt="..." class="img-circle profile_img" width="40px;" height="60px;">
            <?php				
			  }
			  ?>
              </div>
             <div class="profile_info">

                <span>Welcome,</span>
				<h2> <?php $fname = $dat['fname']; $lname = $dat['lname']; $comp = $dat['company'];  if(empty($comp)){ echo ucwords($fname." ".$lname); }else{ echo ucwords($comp); }  ?> </h2>
             <!--   <h2>John Doe</h2> -->
              </div>
            </div>
            <!-- /menu profile quick info -->

            <br />

            <!-- sidebar menu -->
            <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
			  <br/>
                <h3>General</h3>
                <ul class="nav side-menu">
                  <li ><a  href="homepage.php" ><i class="fa fa-home"></i>  Home <span class="fa fa-chevron-right"></span></a> </li>
                 <li><a href="#"><i class="fa fa-building"></i> My Contacts <span class="fa fa-chevron-down"></span></a> 
				  <ul class="nav child_menu">
					    <li><a  href="addcontact.php" ><i class="fa fa-user-plus"></i>Add New Contact<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="addnewcontact.php" ><i class="fa fa-eye"></i>View Contacts<span class="fa fa-chevron-down"></span></a> 
						
						</li>
					   </ul>
				  </li>
                  <li ><a><i class="fa fa-users"></i> Groups <span class="fa fa-chevron-down"></span></a> 
				        <ul class="nav child_menu">
					   <li><a  href="creategroup.php" ><i class="fa fa-paw"></i>Create Group<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="viewgroups.php" ><i class="fa fa-eye"></i>View Groups<span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
				  </li>
                  <li  ><a><i class="fa fa-table"></i> Topup <span class="fa fa-chevron-down"></span></a>  
				   <ul class="nav child_menu">
                     	<li><a  href="sendtogroup.php" ><i class="fa fa-cloud-download"></i>Top-up Group Contacts<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="selectcontacttosend.php" ><i class="fa fa-building"></i>Select Contact(s)<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="sendtome.php" ><i class="fa fa-mobile"></i>Top-up My Line<span class="fa fa-chevron-right"></span></a> </li>
						 <li><a href="autosend-airtime.php" ><i class="fa fa-recycle"></i> Auto-Send Airtime <span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
					  				  
				  </li>
                  
				    <li class="active" ><a><i class="fa fa-gg"></i> Billing <span class="fa fa-chevron-down"></span></a> 
				   <ul class="nav child_menu">
                     <li ><a  href="purchasetome.php" ><i class="fa fa-server"></i> Top-up My Account<span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
				  </li>
				    <li><a><i class="fa fa-bar-chart-o"></i> History <span class="fa fa-chevron-down"></span></a>  
				      <!--  <ul class="nav side-menu"> -->
					   <ul class="nav child_menu">
					    <li><a  href="creditassignment.php" ><i class="fa fa-bars"></i>Credit Assignment<span class="fa fa-chevron-right"></span></a> </li>
						
					   </ul>
				   </li>
                </ul>
              </div>

            </div>
			 </div>
        </div>
		<?php
        include("topmenubar.php");
         ?>
		 <!-- page content -->
        <div class="right_col" role="main">
		  <!-- /top navigation -->
		    <div class="row">
          <!-- top tiles -->
				<div class="row tile_count">
					<div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count">
						<p>   </p>
					</div>
            
				</div>
			</div>
          <!-- /top tiles -->
		 
					<div class="x_title">
						<h2> <b>Auto-Credit Your Account Now </b></h2>
							<div class="clearfix"></div>
					</div>
					 <div class="row" >
					<div class="x_content">
					<?php include('get_top_data.php'); ?>
                    <form name="form1" class="form-horizontal form-label-left" method="post" action=""  >

						<div class="item form-group">
							<p style="margin-left:50px;font-size:14px;lene-height:2.4;">
							<b>Our Payment systems are fully automated for ease of making payments any time any day without consulting us. </b><br><br>
							1. Go to <b>M-PESA</b> and select <b>Lipa na M-PESA</b><br>
							2. Select Pay Bill option<br>
							3. Select 'Enter business no.'<br>
							4. Enter our business no. <b>748729</b> and press <b>OK</b><br>
							6. Select 'Account no.'<br>
							6. Enter <b>Your Account no.</b> <b style="color:#3CB371;"> <?php if(empty($getname['mpesa_account_name'])){ echo "Null-Enquire for one"; }else{ echo $getname['mpesa_account_name']; } ?></b> and press <b>OK</b><br>
							7. Enter Amount to credit and press <b>OK</b><br>
							8. Enter your PIN and Confirm Payment to Mobisky Innovations LTD <br>
							9. Wait for a confirmation message that will automatically update your API account<br><br>
							
							<b>You may then Refresh your account to load the update. Feel at ease with Mobisky.</b>
							
							</p>
                   
                        <div class="ln_solid"></div>
					  
                    </form>
					
                  </div>
		 </div>
				  <br/>
				
				 <div class="row" >
				   <div class="col-md-12 col-sm-12 col-xs-12">
					<div class="x_panel">
				   <h2> <b style="color:#3CB371;">Previous Account Topups by Date Range</b> </h2>
				      <div class="x_content">
				  
					<table width="1012">
					  <tr><td width="188">  
		  <div class="box-body">		   
<a  href="pdf/billingreport.php?mpesa_account_name=<?php echo $getname['mpesa_account_name'];?>  && from_date=<?php echo !empty($_POST['from_date']) ? $_POST['from_date'] : date('d/m/Y');?> && to_date=<?php echo !empty($_POST['to_date']) ? $_POST['to_date'] : date('d/m/Y');?> " target="_blank" ><i class="btn btn-success">Export PDF</i></a></td>
					  <td width="115">
<a  href="excelreports/excelbilling.php?mpesa_account_name=<?php echo $getname['mpesa_account_name'];?> && from_date=<?php echo !empty($_POST['from_date']) ? $_POST['from_date'] : date('d/m/Y');?> && to_date=<?php echo !empty($_POST['to_date']) ? $_POST['to_date'] : date('d/m/Y');?> " ><i class="btn btn-success">Export Excel</i></a></td>
					  <td width="103">
<a  href="excel/billingscsv.php?mpesa_account_name=<?php echo $getname['mpesa_account_name'];?> && from_date=<?php echo !empty($_POST['from_date']) ? $_POST['from_date'] : date('d/m/Y');?> && to_date=<?php echo !empty($_POST['to_date']) ? $_POST['to_date'] : date('d/m/Y');?> " ><i class="btn btn-success">Export CVS</i></a>	</td>
					 	   
				   
				   

                                <!-- /.form group -->
                                
                                    <div class="form-group col-md-3">
									<form method="post" action="billingreport2.php" >
                                       <td width="320">	  <label>Start Date</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <input class="form-control datepicker" name="from_date" tabindex="1" width="60px" required value="<?php echo $from_date; ?>" autocomplete="off"/>
                                        </div>
                                        <!-- /.input group -->
                        </div></td>
                                    <div class="form-group col-md-3">
                                      <td width="258">  <label>End Date</label>

                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <input class="form-control datepicker" name="to_date" tabindex="1"  required value="<?php echo $to_date; ?>" autocomplete="off"/>
                                        </div>
                                        <!-- /.input group -->
                                    </div>

                                   
                                        <!-- /.input group -->
                                   
					  </div>
      </td><td>  <button type="submit" class="btn btn-primary" name="display">Display</button>
                                   
                                        <!-- /.input group -->
                                   
                                    </div>
                                    <!-- /.form group --><br>
                            
                                    <div class="form-group col-md-3 dt_buttons_container" >
                                        
                                    </div>
                                </form>

                      </div>
							
				   </td></tr></table>
				   <table id="example1" class="table table-bordered table-striped table-responsive table-condensed">
                      <thead>
                        <tr>
						  <th>TransID</th>
						  <th>Date</th>
						  <th>Amount</th>
						  <th>Paid By</th>
                          <th>Contact</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
					  	//$to_date=$_POST['to_date'];
		//$from_date=$_POST['from_date'];
		$from_date = !empty($_POST['from_date']) ? $_POST['from_date'] : date('d/m/Y');
$to_date = !empty($_POST['to_date']) ? $_POST['to_date'] : date('d/m/Y');
					  
					  $frm  = $dat['company'];
					  $mpname = $getname['mpesa_account_name'];
					  //$us = mysql_query("select * from airtime_topup_requests where company='$frm' and customer_id='$session_id' limit 200");
					  $us = mysql_query("select * from api_topups where BillRefNumber = '$mpname' and DATE(TransTime) between '" . strToDate($from_date) . "' and '" . strToDate($to_date) . "' order by id DESC ");
						while($rws = mysql_fetch_assoc($us)){
							$id=  $rws['id']; 
							echo "<tr>";
							echo '<td>' .$rws['TransID'].'</td>';
							echo '<td>' .$rws['TransTime']. '</td>';
							echo '<td>' ."Ksh. ".$rws['TransAmount'].'</td>';
							echo '<td>' .$rws['FirstName']." ".$rws['MiddleName']." ".$rws['LastName'].'</td>';
							echo '<td>' .$rws['MSISDN'] . '</td>';
						}  
							echo "</tr>";
					  
					  ?>  
                       
                      </tbody>
                    </table>
				 </div> 
				 </div> 
			</div> 
		</div>  
				  
				  
                
		</div>
			    
		  
		  
		  
	
		  
		  
		          <!-- /page content. End of main -->

     <?php include("footerlinks.php"); ?>
	 
	  <script src="vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="vendors/nprogress/nprogress.js"></script>
    <!-- Chart.js -->
    <script src="vendors/Chart.js/dist/Chart.min.js"></script>
    <!-- gauge.js -->
    <script src="vendors/bernii/gauge.js/dist/gauge.min.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="vendors/iCheck/icheck.min.js"></script>
    <!-- Skycons -->
    <script src="vendors/skycons/skycons.js"></script>
    <!-- Flot -->
    <script src="vendors/Flot/jquery.flot.js"></script>
    <script src="vendors/Flot/jquery.flot.pie.js"></script>
    <script src="vendors/Flot/jquery.flot.time.js"></script>
    <script src="vendors/Flot/jquery.flot.stack.js"></script>
    <script src="vendors/Flot/jquery.flot.resize.js"></script>
    <!-- Flot plugins -->
    <script src="js/flot/jquery.flot.orderBars.js"></script>
    <script src="js/flot/date.js"></script>
    <script src="js/flot/jquery.flot.spline.js"></script>
    <script src="js/flot/curvedLines.js"></script>
    <!-- jVectorMap -->
    <script src="js/maps/jquery-jvectormap-2.0.3.min.js"></script>
    <!-- bootstrap-daterangepicker -->
    <script src="js/moment/moment.min.js"></script>
    <script src="js/datepicker/daterangepicker.js"></script>

		   <!-- Datatables -->
    <script src="vendors/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="vendors/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script src="vendors/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
    <script src="vendors/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
    <script src="vendors/datatables.net-buttons/js/buttons.flash.min.js"></script>
    <script src="vendors/datatables.net-buttons/js/buttons.html5.min.js"></script>
    <script src="vendors/datatables.net-buttons/js/buttons.print.min.js"></script>
    <script src="vendors/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
    <script src="vendors/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
    <script src="vendors/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="vendors/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
    <script src="vendors/datatables.net-scroller/js/datatables.scroller.min.js"></script>
    <script src="vendors/jszip/dist/jszip.min.js"></script>
    <script src="vendors/pdfmake/build/pdfmake.min.js"></script>
    <script src="vendors/pdfmake/build/vfs_fonts.js"></script>
	
	    <script src="vendors/select2/dist/js/select2.full.min.js"></script>

  

    <!-- Datatables -->
	 <script>
      $(document).ready(function() {
        var handleDataTableButtons = function() {
          if ($("#datatable-buttons").length) {
            $("#datatable-buttons").DataTable({
              dom: "Bfrtip",
              buttons: [
                {
                  extend: "copy",
                  className: "btn-sm"
                },
                {
                  extend: "csv",
                  className: "btn-sm"
                },
                {
                  extend: "excel",
                  className: "btn-sm"
                },
                {
                  extend: "pdfHtml5",
                  className: "btn-sm"
                },
                {
                  extend: "print",
                  className: "btn-sm"
                },
              ],
              responsive: true
            });
          }
        };

        TableManageButtons = function() {
          "use strict";
          return {
            init: function() {
              handleDataTableButtons();
            }
          };
        }();

        $('#datatable').dataTable();
        $('#datatable-keytable').DataTable({
          keys: true
        });

        $('#datatable-responsive').DataTable();

        $('#datatable-scroller').DataTable({
          ajax: "js/datatables/json/scroller-demo.json",
          deferRender: true,
          scrollY: 380,
          scrollCollapse: true,
          scroller: true
        });

        var table = $('#datatable-fixed-header').DataTable({
          fixedHeader: true
        });

        TableManageButtons.init();
      });
    </script>	
	
    <!-- Select2 -->
    <script>
      $(document).ready(function() {
        $(".select2_single").select2({
          placeholder: "Select a state",
          allowClear: true
        });
        $(".select2_group").select2({});
        $(".select2_multiple").select2({
       placeholder: "Chose Contacts to topup",
          allowClear: true
        });
      });
    </script>
    <!-- /Select2 -->	
		  
   
		  
    <script type="text/javascript"src="vendors/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript"src="datepicker/bootstrap-datepicker.js"></script>
    
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
    
    <script type="text/javascript"src="vendors/select2/dist/js/select2.full.min.js"></script>
    <script src="vendors/morris.js/morris.min.js"></script>
    <script src="vendors/raphael/raphael.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".datepicker").datepicker({
                format: 'dd/mm/yyyy',
                autoclose: true
            });
            $(".select2").select2();
            var table = $("#example1").DataTable();
            new $.fn.dataTable.Buttons(table, {
                buttons: [
                    'copy', 'excel', 'pdfHtml5'
                ]
            });
            table.buttons().container().appendTo($('.dt_buttons_container'));

            loadsent_sms_graph();
        });
        function loadsent_sms_graph() {
            console.log("Clicked");
            $('#graph_filter_btn').append(' <i id="graph_filter_btn_icon" class="fa fa-spinner fa-spin"></i>');
            $('#sent_sms_graph_area').show();
            $('#sent_sms_data').html("");
            hideloadreceived_sms();
            hideloadsent_sms();

            var sit = $('#userssdd').val();
            var user = {};
            user.id = sit;
            console.log(user);
            $.ajax({
                url: "sms_sales_graph_data.php",
                type: 'POST',
                data: (user),
                dataType: 'json',
                success: function (result) {

                    new Morris.Bar({
                        element: 'sent_sms_data',
                        data: result,
                        xkey: 'y',
                        ykeys: ['a'],
                        labels: ['PROFIT'],
                        xLabelMargin: 1
                    });
                    $('#graph_filter_btn_icon').remove();
                }
            });

        }

        function hideloadsent_sms_graph() {
            console.log("Clicked");
            $('#sent_sms_graph_area').hide();
        }
        function hideloadsent_sms() {
            console.log("Clicked");
            $('#sent_sms_area').hide();
        }
        function hideloadreceived_sms() {
            console.log("Clicked Close");
            $('#received_sms_area').hide();
        }
    </script>		  
		  
<?php

function strToDate($date) {
    if (!empty($date)) {
        $some_date = DateTime::createFromFormat('d/m/Y', $date);
        if (!$some_date) {
            throw new \UnexpectedValueException("could not parse the date: $date");
        } else {
            $new_date = $some_date->format('Y-m-d');
        }
        return $new_date;
    } else {
        return '';
    }
}

function dateToStr($date) {
    if (!empty($date)) {
        $some_date = DateTime::createFromFormat('Y-m-d', $date);
        if (!$some_date) {
            throw new \UnexpectedValueException("could not parse the date: $date");
        } else {
            $new_date = $some_date->format('d/m/Y');
        }
        return $new_date;
    } else {
        return '';
    }
}
