<?php
if(basename($_SERVER['PHP_SELF']) == 'expereport.php') 
$cm=" class='current'";
include'header.php';
include'connection.php';
$custo=$conde=$condi=$typo='';
$dato=$datos=$Date;
$k=$p=0;

// search for an item to be added to the chart
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
	
	$pieces = explode("@", $custo);
					$pc=$pieces[1]; // piece1
		if($pc){
		    $custo="";
		    $typo=$pc;
		}
		else{
		    $typo="";
		}
			$p=1;
		}
		
if(isset($_POST['change']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$rowid=$_POST['rowid'];
			$classe=$_POST['classe'];
			$p=$_POST['p'];
	
	$pieces = explode("@", $custo);
					$pc=$pieces[1]; // piece1
		if($pc){
		    $custo="";
		    $typo=$pc;
		}
		else{
		    $typo="";
		}
			
			if($classe)
	$sso=mysqli_query($conn, "UPDATE `expenses` SET `Expense`='$classe' WHERE `Number`='$rowid'");
	        else
	$sso=mysqli_query($conn, "UPDATE `expenses` SET `Classfy`='', `Expense`='' WHERE `Number`='$rowid'");
		}
		


		
if(isset($_POST['delo']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$rowid=$_POST['rowid'];
			$p=$_POST['p'];
	
	$pieces = explode("@", $custo);
					$pc=$pieces[1]; // piece1
		if($pc){
		    $custo="";
		    $typo=$pc;
		}
		else{
		    $typo="";
		}
			
	$sso=mysqli_query($conn, "UPDATE `expenses` SET `Status`='1' WHERE `Number`='$rowid'");
		}
	

		if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
			
			if($custo)
			    $condi="AND `expenses`.`Expense`='$custo'";
			    
			if($typo)
			    $conde="AND `expetype`.`Type`='$typo'";
?>


<script src="style/tableExport.js"></script>
<script type="text/javascript" src="style/jquery.base64.js"></script>
<script src="style/export.js"></script>


<div class="container-fluid main-content">
        <div class="page-title hidden-xs hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Finance Report
          </h2>
  
    </div>
   <div class="row">
  <div class="col-lg-2 hidden-print">
   
  <ul class="list-group">
                  
			  <li class="list-group-item">
              <a href="paidreport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Report 1
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="paireport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Report 2
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="expenserepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Expenses Report 1
                </p>
              </a></li>
                  
			  <li class="list-group-item active">
              <a href="expereport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Expenses Report 2
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="purcharepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="bankreport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Bank Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="assetreport.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Asset Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="fsalerepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Finacial Statement
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="finrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Finacial Position
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="holdrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Withholding Report
                </p>
              </a></li>
                  
			  <li class="list-group-item">
              <a href="cashrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Cashbox Report
                </p>
              </a></li>
              
              </ul>
  </div>
       
        <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row">
        <div class="col-lg-6 hidden-print">
    <div class="col-lg-5"> </div>       

                        
                 <div class="col-lg-7">
                   
			<select class="form-control" name="custo">
	<option value='' selected='selected'>SELECT CLASSIFICATION</option>
	<?php
 

    $doi=mysqli_query($conn, "SELECT `Type` FROM `expetype` GROUP BY `Type` ORDER BY `Number` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$fna=$roi['Type'];
				$typ="@$fna";
	echo"<option value='$typ' style='color:red;'> --- $fna --- </option>";
	    
	$doe=mysqli_query($conn, "SELECT `Expense` FROM `expetype` WHERE `Type`='$fna' GROUP BY `Expense` ORDER BY `Expense` ASC");
			while($roe=mysqli_fetch_assoc($doe)){
				$expe=$roe['Expense'];
	    echo"<option value='$expe'> $expe </option>";
			    }
			}
			?>
                            </select>  
					   </div></div>
					   
		<div class="col-lg-6 hidden-print">
            <div class="col-lg-4"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" id="from" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>


		  <div class="col-lg-4"> 
           <div class="input-group date" data-provide="datepicker">	
      <input class="form-control form-center" id="to" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
              </div></div>                      
                       
                       <div class="col-lg-3">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         </div> 
                      
                     
                  
            </form> 

			</div>
<?php
			 if($p)
$do=mysqli_query($conn, "SELECT `expenses`.* FROM `expenses` INNER JOIN `expetype` ON `expenses`.`Expense`=`expetype`.`Expense` WHERE `expenses`.`Status`='0' AND `expenses`.`Classfy`='EXPENSE' AND `expenses`.`Date` BETWEEN '$dato' AND '$datos' $conde $condi ORDER BY `expenses`.`Date` ASC");
	else
$do=mysqli_query($conn, "SELECT *FROM (SELECT *FROM `expenses` WHERE `Status`='0' AND `Classfy`='EXPENSE' ORDER BY `Date` DESC LIMIT 15) SUB ORDER BY `Date` ASC");
$fo=mysqli_num_rows($do);
?>


<div class="divFooter"><center><u><b>EXPENSES REPORT <?php echo"$mpri"; ?></b></u></center></div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
		<span class="pull-right">&nbsp;&nbsp;&nbsp;&nbsp;
		<a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a></span> 
			 
			 <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-default btn-xs dataExport" data-type="excel">Export <span class="caret"></span></button>
			 
			 &nbsp;&nbsp;&nbsp;&nbsp;
			 <a href="#" onclick="exportTableToExcel('tblData')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		 
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
    <table class="table table-striped table-hover" id="tblData">     
                    <thead>
                    <tr role="row"><th width='1%'> No </th>
                        <th width='7%'><div align='center'> DATE </th>
                        <th><div align='center'> TYPE </th>
                        <th><div align='center'> CLASSIFICATION </th>
                       <th><div align='center'> PAID&nbsp;TO </th>
                       <th width='25%'><div align='center'> PURPOSE </th>
                       <th><div align='center'> AMOUNT </th>
                       <th><div align='center'> RATE </th>
                       <th><div align='center'> VALUE </th>
				<th colspan='2'><div align='center'> # </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;				$tot=$tca=$tmo=$tche=$tva=0;
			while($ro=mysqli_fetch_assoc($do)){
			    $ca=$mo=$che=0;
				$code=$ro['Number'];
				$dest=$ro['Expense'];
				$dat=$ro['Date'];
				$ser=$ro['Service'];
			$sa=$ro['Purpose'];
			$amo=$ro['Amount'];
			$user=$ro['User'];
			$paid=$ro['Payto'];
			$invo=$ro['Invoice'];
			$rat=$ro['Rate'];                  $rato=number_format($rat, 2);
			$tot+=$amo;
			$val=$amo*$rat;                     $valo=number_format($val, 2);
			$tva+=$val;
			
			 $doi=mysqli_query($conn, "SELECT `Type` FROM `expetype` WHERE `Expense`='$dest' GROUP BY `Type` ORDER BY `Type` ASC");
			        $roi=mysqli_fetch_assoc($doi);
				        $typ=$roi['Type'];
				  
				  /*      
				if($ser){
		$dot=mysqli_query($conn, "SELECT `Rate` FROM `payment` WHERE `Invoice`='$ser' AND `Date`='$dat' ORDER BY `Number` DESC");
		        if($fot=mysqli_num_rows($dot)){
			        $rot=mysqli_fetch_assoc($dot);
				        $rat=$rot['Rate'];	
	$set=mysqli_query($conn, "UPDATE `expenses` SET `Rate`='$rat' WHERE `Number`='$code'");
		        }
				}
				*/
			
			$amoo=number_format($amo, 2);
			$stl="style='padding:0px; font-size:12px;'";
		 
		    print("<tr><td $stl><div align='right'>$n&nbsp;&nbsp;</td>
						<td class='text-center' $stl> $dat </td>
			<td $stl> <a href='#' data-toggle='modal' data-target='#Modal$n'>$typ</a> </td><td $stl> $dest </td><td $stl> $paid </td>
			<td $stl> $sa </td><td class='text-right' $stl> $amoo </td>
			<td class='text-right' $stl> $rato </td>
			<td class='text-right' $stl> $valo </td><td $stl width='1%'>");
		
		
		        if($_SESSION['Cfr']){
		echo"<div class='modal fade text-left' id='exampleModal$n' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' 
		style='top:220px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content text-left'>
      <div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>DELETE CONFIRMATION 
<label class='text-right pull-right'> $dat &nbsp; $dest </h5>

      </div>
      <div class='modal-body' style='height:40px;'>
    <h5 style='color:#ff0033'>Are you sure you want to delete this record ($amoo)?</h5>
      </div><br><form method='post' action=''><input type='hidden' name='rowid' value='$code'><input type='hidden' name='custo' value='$custo'>
      <input type='hidden' name='dato' value='$dato'><input type='hidden' name='datos' value='$datos'><input type='hidden' name='p' value='$p'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-success' data-dismiss='modal'>&nbsp;NO&nbsp;</button>
        <button type='submit' name='delo' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";
}




echo"<div class='modal fade' id='Modal$n' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' 
		style='top:220px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'>
      <div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>CHANGE EXPENSE TYPE 
		<label style='float:right; text-align:right;'><b> No: $code </b></label></h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:140px;'>
      <div class='form-group' style='padding-top:20px;'>
      
       <div class='col-md-6'> &nbsp;&nbsp; &nbsp;&nbsp; <b>From</b>
<select class='form-control' name='categ' style='font-size:16px; height:34px;'>
<option value='$dest'> $dest </option>";
			
echo"</select></div><div class='col-md-6'> &nbsp;&nbsp; &nbsp;&nbsp; <b>Change To</b><select class='form-control' name='classe' style='font-size:16px; height:34px;'><option value=''> </option>";

    $doi=mysqli_query($conn, "SELECT `Type` FROM `expetype` GROUP BY `Type` ORDER BY `Number` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$fna=$roi['Type'];
	    echo"<option value='' style='color:red;'> --- $fna --- </option>";
	    
	$doe=mysqli_query($conn, "SELECT `Expense` FROM `expetype` WHERE `Type`='$fna' GROUP BY `Expense` ORDER BY `Expense` ASC");
			while($roe=mysqli_fetch_assoc($doe)){
				$expe=$roe['Expense'];
	    echo"<option value='$expe'> $expe </option>";
			}
			}
			
			
            echo"</select>        
            </div></div>
      </div><input type='hidden' name='rowid' value='$code'>
      <input type='hidden' name='custo' value='$custo'>
      <input type='hidden' name='dato' value='$dato'>
      <input type='hidden' name='datos' value='$datos'>
      <input type='hidden' name='p' value='$p'>
      <div class='modal-header text-right' style='margin-top:-10px; height:50px; padding-top:10px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-danger' data-dismiss='modal' style='width:80px;'> CANCEL </button>
        <button type='submit' name='change' class='btn btn-sm btn-success' style='width:80px;'> SAVE </button>
      </div></form>
    </div>
  </div>
</div>";

						
	print("</td><form method=post action=''><td class='hidden-xs hidden-print' align='right' style='width:20px; padding:0px;'><div title='Delete' data-toggle='tooltip' data-placement='top'>
    <button type='button' class='btn btn-xs btn-danger hidden-print' style='height:22px; padding:0px; margin:0px;' data-placement='top'
	data-toggle='modal' data-target='#exampleModal$n'>&nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></div></td></form></tr>");
            	$n++;           
						}
						$tot=number_format($tot, 2);
						$tva=number_format($tva, 2);					
						?>
						
                    </tbody>
    
            <?php
	echo"<tr><th style='padding:1px;'> </th><th style='padding:1px; font-size:14px;' colspan='5'>&nbsp;&nbsp;&nbsp;&nbsp; TOTAL AMOUNT </th>
			<th class='text-right' style='padding:1px; font-size:14px;'> </th>
		<th class='text-right' colspan='2' style='padding:1px; font-size:14px;'> $tva </th><th class='text-center' colspan='2' style='padding:1px;'> -- </th></tr>";
	?>
                  </table>
              </div></div>

                  </div>                     
                
              </div><span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Click to print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a></span>
			 
			 <span class="pull-right hidden-print">
			 &nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="exportTableToExcel('tblData')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
			 
            </div></div>
                  </div>
        
   <?php
   include'footer.php';
   ?>
