<?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'];
			$p=1;
		}
		
if(isset($_POST['change']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$rowid=$_POST['rowid'];
			$classe=$_POST['classe'];
			$p=$_POST['p'];
			
			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['setamo']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$rowid=$_POST['rowid'];
			$toamo=str_replace(',', '', $_POST['toamo']);
			$p=$_POST['p'];
			$r=$_POST['r'];
			
			if($r=='EXPENSE')
	$sso=mysqli_query($conn, "UPDATE `expenses` SET `Holdings`='$toamo' WHERE `Number`='$rowid'");
	        else
	$sso=mysqli_query($conn, "UPDATE `services` SET `Holdings`='$toamo' WHERE `Number`='$rowid'");
		}


		if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
?>


<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">
              <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 active">
              <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">	   </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 `Number`, `Date`, `User`, `Amount`, `Purpose`, `Payto`, `Expense`, `Record`, `Holdings` FROM `expenses` WHERE `Status`='0' AND `Holdings`!='0' AND `Date` BETWEEN '$dato' AND '$datos') UNION (SELECT `Number`, `Date`, `User`, `Amount`, `Purpose`, `Supplier`, `Item`, `Record`, `Holdings` FROM `services` WHERE `Status`='0' AND `Holdings`!='0' AND `Date` BETWEEN '$dato' AND '$datos') ORDER BY `Date` ASC");
	        else
$do=mysqli_query($conn, "(SELECT `Number`, `Date`, `User`, `Amount`, `Purpose`, `Payto`, `Expense`, `Record`, `Holdings` FROM `expenses` WHERE `Status`='0' AND `Holdings`!='0') UNION (SELECT `Number`, `Date`, `User`, `Amount`, `Purpose`, `Supplier`, `Item`, `Record`, `Holdings` FROM `services` WHERE `Status`='0' AND `Holdings`!='0') ORDER BY `Date` DESC LIMIT 20");
        
    $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" 
        style="font-size:12px;"><thead>
    <tr role="row"><th width='1%' style='padding:1px; font-size:12px;'> No </th>
        <th width='7%' style='padding:1px; font-size:12px;'><div align='center'> DATE </th><th style='padding:1px; font-size:12px;'><div align='center'> TYPE </th><th style='padding:1px; font-size:12px;'><div align='center'> CLASSIFICATION </th><th style='padding:1px; font-size:12px;'><div align='center'> PAID&nbsp;TO </th>
    <th width='25%' style='padding:1px; font-size:12px;'><div align='center'> PURPOSE </th><th style='padding:1px; font-size:12px; text-align:center;'> AMOUNT </th><th style='padding:1px; font-size:12px; text-align:right;'> TAXES </th><th style='padding:1px; font-size:12px; text-align:right;'> PAID </th></tr>
                    </thead><tbody>
					<?php
					$n=1;				$tamo=$ttax=$tpai=0;
			while($ro=mysqli_fetch_assoc($do)){
			    $code=$ro['Number'];
				$dest=$ro['Expense'];
				$dat=$ro['Date'];
			$sa=$ro['Purpose'];
			$tot=$ro['Amount'];
			$user=$ro['User'];
			$paid=$ro['Payto'];
			if(!$paid)
			$paid=$ro['Supplier'];
			$hold=$ro['Holdings'];
			$r=$ro['Record'];
			
			if($r=='EXPENSE'){
			    $amo=$tot+$hold;
			    $tax=$hold;
			    $pai=$tot;
			}
			else{
			    $amo=$tot;
			    $tax=$hold;
			    $pai=$amo-$hold;
			}
			
			 $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(!$typ){
			        $typ="Service Provider";
			        $a="<font color='blue'>";
			        $dest="PAYMENT";
			    }
			    else{
		$a="<a href='#' data-toggle='modal' data-target='#Modal$n'>";
			    }
			
			$amoo=number_format($amo, 2);
			$taxo=number_format($tax, 2);
			$paio=number_format($pai, 2);
			$stl="style='padding:0px; font-size:12px;'";
			$tamo+=$amo;        $ttax+=$tax;            $tpai+=$pai;
		 
		    print("<tr><td $stl><div align='right'>$n&nbsp;&nbsp;</td>
						<td class='text-center' $stl> $dat </td>
			<td $stl> $a$typ</a></font> </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> <a href='#' data-toggle='modal' data-target='#Modals$n'>$taxo</a> </td>
			<td class='text-right' $stl> $paio </td></tr>");
		
		
		        
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 text-left'>
        <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 text-left' 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='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>";






echo"<div class='modal fade' id='Modals$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 text-left'>
    <h5 class='modal-title' id='exampleModalLabel'>CHANGE WITHHOLDING AMOUNT 
		<label style='float:right; text-align:right;'><b> No: $code </b></label></h5>

      </div><form method='post' action=''>
      <div class='modal-body text-left' style='height:140px;'>
      <div class='form-group' style='padding-top:20px;'>
      
    <div class='col-md-6'> &nbsp;&nbsp; &nbsp;&nbsp; <b>WithHolding Amount</b>
<input type='text' class='form-control text-center' name='fromamo' value='$taxo' readonly>";
			
echo"</div><div class='col-md-6'> &nbsp;&nbsp; &nbsp;&nbsp; <b>Change To</b><input type='text' class='form-control text-center' name='toamo' onkeyup='format(this);' onkeypress='return isNumberKey(event)'> 
            </div></div>
      </div><input type='hidden' name='rowid' value='$code'>
      <input type='hidden' name='dato' value='$dato'>
      <input type='hidden' name='datos' value='$datos'>
      <input type='hidden' name='p' value='$p'>
      <input type='hidden' name='r' value='$r'>
      <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='setamo' class='btn btn-sm btn-success' style='width:80px;'> SAVE </button>
      </div></form>
    </div>
  </div>
</div>";

						
//	print("</td></tr>");
            	$n++;           
						}
						$tamo=number_format($tamo, 2);	
						$ttax=number_format($ttax, 2);	
						$tpai=number_format($tpai, 2);					
						?>
						
                    </tbody><thead>
    
            <?php
	echo"<tr><th style='padding:1px;'> </th><th style='padding:1px; font-size:12px;' colspan='5'>&nbsp;&nbsp;&nbsp;&nbsp; TOTAL AMOUNT </th>
		<th class='text-right' style='padding:1px; font-size:12px;'> $tamo </th>
		<th class='text-right' style='padding:1px; font-size:12px;'> $ttax </th>
		<th class='text-right' style='padding:1px; font-size:12px;'> $tpai </th>
		</tr>";
	?>
                  </thead></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';
   ?>
