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

// search for an item to be displayed
if(isset($_POST['search']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$conde="AND `Date` BETWEEN '$dato' AND '$datos'";
			$p=1;
		}
		

// add new classification
if(isset($_POST['saven']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$p=$_POST['p'];
			if($p=='1')
		$conde="AND `Date` BETWEEN '$dato' AND '$datos'";
		
		    $classi=$_POST['classi'];
		    $categ=$_POST['categ'];
	$so=mysqli_query($conn, "INSERT INTO `expetype` (`Date`, `Type`, `Expense`, `Status`) VALUES ('$Date', '$categ', '$classi', '0')");
		}
		
		
	if(isset($_POST['updo']))
		{
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$custo=$_POST['custo'];
			$rowid=$_POST['rowid'];
			$dati=$_POST['dati'];
			$paid=$_POST['paid'];
			$invo=$_POST['invo'];
			$sp=$_POST['sp'];
			$conde="AND `Date` BETWEEN '$dato' AND '$datos'";
			$p=$_POST['p'];
		
		
			$pieces = explode("-", $sp);
				$sp = $pieces[0]; // piece1
				$hl = 0;
				if($sp=='EXPENSE'){
			$classe=$_POST['classe'];
			$hl=str_replace(',', '', $_POST['holding']);
				}
				if($sp=='ASSET')
			$classe=$_POST['classi'];
				if($sp=='OTHER')
			$classe=$_POST['classa'];
			
			if($sp=='EXPENSE' AND $classe=='')
			    $sp='';
			
	$sso=mysqli_query($conn, "UPDATE `expenses` SET `Date`='$dati', `Classfy`='$sp', `Invoice`='$invo', `Payto`='$paid', `Expense`='$classe', `Holdings`='$hl' WHERE `Number`='$rowid'");
		}

	

		if($dato==$datos)
			$mpri="ON $dato";
		else
			$mpri="FROM $dato TO $datos";
			
			if($custo)
			    $condi="AND `Store`='$custo'";
?>

<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
          </h2>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">  

    <li class="list-group-item">
	  <a href="salesrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Sales Report
                </p>
              </a></li> 

    <li class="list-group-item">
	  <a href="purchase.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Purchase Orders
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="trans.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Service Providers
                </p>
              </a></li> 

    <li class="list-group-item active">
	  <a href="experepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Payout/Expenses
                </p>
              </a></li>  

    <li class="list-group-item">
	  <a href="zreport.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Daily Z-Report
                </p>
              </a></li>   

    <li class="list-group-item">
	  <a href="taxrepo.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;VAT/TAX Report
                </p>
              </a></li>    

    <li class="list-group-item">
	  <a href="assets.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Assets Report
                </p>
              </a></li> 
                       
            </ul><br><br><br>
  </div>
  
  
  <?php
  	echo"<div class='modal fade' id='Modal-Class' 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'>ADD NEW CLASS 
		<label style='float:right; text-align:right;'><b> </b></label></h5>

      </div><form method='post' action=''>
      <div class='modal-body' style='height:120px;'>
      <div class='form-group' style='padding-top:20px;'>
      
       <div class='col-md-6 text-center'> 
<select class='form-control' name='categ' style='font-size:16px; height:34px;'>
<option value=''> Select Category </option>";

    $doi=mysqli_query($conn, "SELECT `Type` FROM `expetype` GROUP BY `Type` ORDER BY `Type` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$fna=$roi['Type'];
	    echo"<option value='$fna'> $fna </option>";
			}
			
			
            echo"</select></div><div class='col-md-6 text-center'> 
    <input name='classi' class='form-control' type='text' style='text-align:left;'>        
            </div></div>
      </div><input type='hidden' name='p' value='$p'>
      <input type='hidden' name='dato' value='$dato'>
      <input type='hidden' name='datos' value='$datos'>
      <input type='hidden' name='custo' value='$custo'>
      <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='saven' class='btn btn-sm btn-success' style='width:80px;'> SAVE </button>
      </div></form>
    </div>
  </div>
</div>";
?>
       
        <form action="" method="post" class="form-horizontal "> 
        <div class="col-lg-10">
                  <div class="row">

                    <div class="col-lg-2" style="padding-left:80px;">
      <a href='exrepo.php' style="color:blue;font-weight:bold;font-size:16px"> Expenses&nbsp;Report </a>
      </div> 

				  <div class="col-lg-2"> </div>        

                         
                       <div class="col-lg-8 hidden-print">
                 <div class="col-lg-3">
                   
			<select class="form-control" name="custo">
				<option value='' selected='selected'>SELECT POINT</option>
			 <?php
			$doi=mysqli_query($conn, "SELECT `Store` FROM `expenses` WHERE `Status`='0' AND `Store`!='' GROUP BY `Store` ORDER BY `Store` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$desto=$roi['Store'];
				if($desto==$custo)
					$sle="selected='selected'";
				else
					$sle='';
			echo"<option value='$desto' $sle> $desto </option>";
			}
			?>    
                            </select>  
					   </div>
            <div class="col-lg-3"> 
           <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-3"> 
           <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-2">
                        <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 *FROM `expenses` WHERE `Status`='0' AND `Classfy`='' $conde $condi ORDER BY `Date` ASC");
	else
$do=mysqli_query($conn, "SELECT *FROM (SELECT *FROM `expenses` WHERE `Status`='0' AND `Classfy`='' 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> No </th>
                        <th width='7%'><div align='center'> DATE </th>
                        <th><div align='center'> LOCATION </th>
                       <th><div align='center'> PAID&nbsp;TO </th>
                       <th><div align='center'> PURPOSE </th>
                       <th><div align='center'> CASH </th>
					   <th><div align='center'> MOMO </th>
					   <th><div align='center'> CHEQUE </th>
				<th colspan='2' width='5%'><div align='center'> OPTIONS </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$n=1;				$tot=$tca=$tmo=$tche=0;
			while($ro=mysqli_fetch_assoc($do)){
			    $ca=$mo=$che=0;
				$code=$ro['Number'];
				$dest=$ro['Store'];
				$dat=$ro['Date'];
			$sa=$ro['Purpose'];
			$amo=$ro['Amount'];
			$user=$ro['User'];
			$mode=$ro['Payment'];
			$paid=$ro['Payto'];
			$invo=$ro['Invoice'];
			$hl=number_format($ro['Holdings']);
			$tot+=$amo;
			if($mode=='CASH')
			$ca=$amo;
			if($mode=='MOMO')
			$mo=$amo;
			if($mode=='CHEQUE')
			$che=$amo;
			
			$cao=number_format($ca, 2);
			$moo=number_format($mo, 2);
			$cheo=number_format($che, 2);
			$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> $dest </td><td $stl> $paid </td>
			<td $stl> $sa </td><td class='text-right' $stl> $cao </td>
			        <td class='text-right' $stl> $moo </td>
						<td $stl class='text-right'> $cheo ");
		
		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'>Do you want to delete this record?</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='Modaldis$n' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document'><div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'>PAYOUT &nbsp;&nbsp;&nbsp;&nbsp; 
     
        <button class='pull-right close' data-dismiss='modal' type='button'>&times;</button> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
        
        <label style='float:right; text-align:right'> $amoo </label> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left'><div class='row'>
			 	
		 <div class='form-group'>	 
			  <div class='col-md-5' align='right'>
			  <label class='control-label' style='padding-top:10px;'> Date</label></div>	

	 <div class='col-md-6'>
	    <div class='input-group date' data-provide='datepicker'>
          <input name='dati' class='form-control text-center' type='text' value='$dat' onkeypress='return isNumberKey(event)' required>
	  <span class='input-group-addon'><i class='lnr lnr-calendar-full'></i></span></div></div></div><div class='row'> </div>
		
		 <div class='form-group'>	 
			  <div class='col-md-5' align='right'>
		<label class='control-label' style='padding-top:10px;'>Paid to </label></div>	

	 <div class='col-md-6'>
          <input name='paid' class='form-control' type='text' value='$paid' required>
            </div>
		</div>
		
		 <div class='form-group'>	 
			  <div class='col-md-5' align='right'>
		<label class='control-label' style='padding-top:10px;'>Invoice No </label></div>	

	 <div class='col-md-6'>
          <input name='invo' class='form-control text-center' type='text' OnKeyup='return cUpper(this);' value='$invo' required>
            </div>
		</div>

			
        <div class='form-group'>
			<div class='col-md-5' align='right'> 
    <label class='control-label' style='padding-top:10px;'>Payout Type</label>
        </div>
            <div class='col-md-6'>
<select class='form-control' name='sp' style='font-size:16px; height:34px;' onchange='showDiv(this.value);' required>
<option value='N/A-$n' selected> SELECT TYPE </option>
<option value='EXPENSE-$n'> EXPENSE </option>
<option value='ASSET-$n'> ASSET </option>
<option value='OTHER-$n'> OTHER </option>
            </select>
            </div></div>
            
   <div class='form-group'>
  <div id='EXPENSE-$n' class='hiddenDiv' style='border:1px solid #6666ff; border-radius:5px; padding:0px; color:#000099; font-size:14px; 
 font-weight:normal; height:140px; padding-top:20px; width:96%; margin:0px 0px 0px 20px; float:center;'>
 
 	<div class='col-md-5' align='right'> 
<label class='control-label' style='padding-top:10px;'>Classification</label>
        </div>
            <div class='col-md-6'><div class='input-group'>
<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><span class='input-group-addon'><a href='#' data-placement='top' data-toggle='modal' data-target='#Modal-Class'  data-dismiss='modal'>
		 <i class='lnr lnr-sync'></i></a></span>
	
	</div>
            </div><div class='row'> </div>
            
    	<div class='col-md-5' align='right'> 
<label class='control-label' style='padding-top:10px;'><br>If Withholding</label>
        </div>
            <div class='col-md-6'><br>
<input type='text' class='form-control' name='holding' onkeyup='format(this);' onkeypress='return isNumberKey(event)' style='font-size:16px; height:34px; text-align:center;' placeholder='Withholding Amount' value='$hl'>
            </div>
            
            
   
   </div></div>
   
   
   <div class='form-group'>
  <div id='ASSET-$n' class='hiddenDiv' style='border:1px solid #6666ff; border-radius:5px; padding:15px; color:#000099; font-size:14px; 
 font-weight:normal; height:80px; padding-top:20px; width:96%; margin:0px 0px 0px 20px; float:center;'>
 
 	<div class='col-md-5' align='right'> 
<label class='control-label' style='padding-top:10px;'>Asset Type</label>
        </div>
            <div class='col-md-6'>
<select class='form-control' name='classi' style='font-size:16px; height:34px;'>
<option value=''> </option>";

    $doi=mysqli_query($conn, "SELECT `Asset` FROM `assetype` GROUP BY `Asset` ORDER BY `Asset` ASC");
			while($roi=mysqli_fetch_assoc($doi)){
				$fna=$roi['Asset'];
	    echo"<option value='$fna'> $fna </option>";
			}
			
			
            echo"</select>
            </div>
   
   </div></div>
   
   
   <div class='form-group'>
  <div id='OTHER-$n' class='hiddenDiv' style='border:1px solid #6666ff; border-radius:5px; padding:15px; color:#000099; font-size:14px; 
 font-weight:normal; height:80px; padding-top:20px; width:96%; margin:0px 0px 0px 20px; float:center;'>
 
 	<div class='col-md-5' align='right'> 
<label class='control-label' style='padding-top:10px;'>Specify</label>
        </div>
            <div class='col-md-6'>
<input type='text' class='form-control' name='classa' style='font-size:16px; height:34px;'>
            </div>
   
   </div></div>
   
		<div class='row'> </div>
		
		
		
	</div></div>
	
	<div class='modal-header text-right' style='height:50px; padding-top:15px;'>	<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'>
		<button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:80px; height:26px;'> CANCEL </button>
        <button type='submit' name='updo' class='btn btn-xs btn-success' style='width:80px; height:26px;'> UPDATE </button></div>
      </form>
  </div></div>
</div>";
						
				print("</td><td class='hidden-xs hidden-print' align='right' style='width:20px; padding:0px;'><div title='Edit' data-toggle='tooltip' data-placement='top'>
                          <button type='button' class='btn btn-xs btn-warning hidden-print' style='height:20px; padding:0px; margin:0px;' data-placement='top'
						  data-toggle='modal' data-target='#Modaldis$n'>
						  &nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></div></td></form>
						  
						  <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:20px; 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++;           $tca+=$ca;           $tmo+=$mo;          $tche+=$che;
						}
						$tot=number_format($tot, 2);
						$tca=number_format($tca, 2);
						$tmo=number_format($tmo, 2);
						$tche=number_format($tche, 2);					
						?>
						
                    </tbody>	
    <tr><th style="padding:1px;"> </th><th style="padding:1px;" colspan='4'>&nbsp;&nbsp;&nbsp;&nbsp; </th>
		<th class='text-right' style="padding:1px; font-size:12px;"><?php echo $tca ?></th>
		<th class='text-right' style="padding:1px 2px 1px 2px; font-size:12px;"><?php echo $tmo ?></th>
		<th class='text-right' style="padding:1px; font-size:12px;"><?php echo $tche ?></th>
		<th class='text-center' colspan='2' style="padding:1px;"> -- </th></tr>
	<tr><th style="padding:1px;"> </th><th style="padding:1px; font-size:12px;" colspan='3'>&nbsp;&nbsp;&nbsp;&nbsp; TOTAL AMOUNT </th>
			<th class='text-right' style="padding:1px;"> </th>
			<th class='text-right' colspan='3' style="padding:1px; font-size:12px;"><?php echo $tot ?></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';
   ?>
