<?
ob_start();
session_start();
ini_set('display_errors', '0');
//date_default_timezone_set("Africa/Nairobi");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

error_reporting(E_ALL ^ E_NOTICE);
//set_time_limit(0);
//require 'phplot.php';

//include_once './conf.php';

include_once './mysql.php';
include_once './forms.php';

include_once './functions.php';
include_once './reports.php';

include_once './navigation/navigations.php';
function Addstff($first,$last,$wage,$id){
        $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$id=mysqli_real_escape_string($id);
$t=mysqli_query($dbc,("select * from labor where id_num='$id'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into labor(first,last,id_num,wage)
values('$first','$last','$id','$wage')"));
 echo '<span class="green">Staff Succesfully Added</span></br>';

}
else{
 echo '<span class="red">Sorry Staff Already Exists</span></br>';}


}
function Addadv($name,$amnt){
        $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$name=mysqli_real_escape_string($dbc,($name));
	$amnt=mysqli_real_escape_string($dbc,($amnt));
	//$perc=mysql_real_escape_string($perc);
	//$date=mysql_real_escape_string($date);
	$hi=mysqli_query($dbc,("select * from adv where lab_id='$name' and amnt='$amnt' and MONTH(date)=MONTH(NOW())"));
	if(mysqli_num_rows($hi)==0){
$nj=mysqli_query($dbc,("insert into adv(lab_id,amnt,date)values('$name','$amnt',NOW())"));
echo '<span class="green">Advance Succesfully captured</span></br>';
}else{
	echo '<span class="red">Sorry Record already Exists</span></br>';}
	mysqli_close($dbc);
}
function Viewstaff(){
    	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from labor
"));
  echo '
     
<table border="1" width="500" cellspacing="0" class="tables"> 
                                                        <thead>
                                                            <tr>
															<th><left>Edit </th> 
															<th><left>First Name</th> 	<th><left>Last Name</th>	<th><left>Salary </th>  
             	<th><left>Staff ID</th>  	
			   	<th><left>Delete</th> 
                                                            </tr>

                                                        </thead>';

														while($row=mysqli_fetch_array($r)){

    echo'<tr><td><a  rel="facebox" href=edit_stff.php?id=' . $row["lab_id"] .'>Edit Staff</a></td><td>' . (($row['first'])) . '</td><td>' . (($row['last'])) . '</td><td>' . (($row['id_num'])) . '</td><td>' . (($row['wage'])) . '</td><td><a  rel="facebox" href=delete_stff.php?id=' . $row["lab_id"] .'>Delete Staff</a></td>

	</tr>';
}
							echo "</table>";
}
function reps_adv($date1,$date2){
    	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
  if($date1!=''&&$date2!=''){
$r=mysqli_query($dbc,("select concat_ws(' ', first,last) as name,lab_id,date, amnt from adv inner join labor using(lab_id) where date between '$date1' and '$date2'
 

"));


}

;

 ?><div align="center"><b>Salary Advance Reports Between<?echo $date1;?> And <?echo $date2?></b></div>
  <table class="blue" border="1" style="background-color:#ffffff"
        frame="BOX" width="980" align="center" 
 cellpadding="5" cellspacing="0" style="font-family:Bell Gothic,Verdana,Arial">
<tr><td><b>Name</b></td><td><b>Amount</b></td><td><b>Date</b></td>
 </tr><?
 while($row=mysqli_fetch_array($r)){
 echo '<tr>

  <td>'.$row['name'].'</td>
 <td>'.($row['amnt']).'</td><td>'.($row['date']).'</td>
  
 

</tr>';
$totald+=($row['amnt']);


 }
 echo '<tr>

  <td colspan="0"><center><b>TOTAL </td>
   <td><b>'.number_format($totald).'</td>
   
 </table>';
 
 }
function minii($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from products where product_id='$id'"));
$row=mysqli_fetch_array($r);
$mt=$row['left_p'];
return $mt;
mysqli_close($dbc);
}
function AddCash($name,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));

$t=mysqli_query($dbc,("select * from cashier where amnt='$name' and date='$date'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into cashier(amnt,date)
values('$name','$date')"));
 echo '<span class="green">Succesful</span></br>';

}
else{
 echo '<span class="red">Sorry Record Already Exists</span></br>';}

mysqli_close($dbc);
}
function delCash($name,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));

$t=mysqli_query($dbc,("select * from cashier where amnt='$name' and date='$date'"));
if(mysqli_num_rows($t)>0){
$y=mysqli_query($dbc,("delete from  cashier where amnt='$name' and date='$date'"));
 echo '<span class="green">Deletion of Record Succesful</span></br>';

}
else{
 echo '<span class="red">Sorry No Such Record  Exists</span></br>';}

mysqli_close($dbc);
}
function SDate($no){
$s=mysqli_query($dbc,("select * from lpo where invoice='$no'"));
$row=mysqli_fetch_array($s);
$date=$row['mat_date'];
return $date;

mysqli_close($dbc);
}

function Reprint($receipt){
include('mysql.php');
$s="select * from sales where number='$receipt' AND type='INVOICE'";
$sql=mysqli_query($dbc,$s);
$l="select * from sales where number='$receipt'";
$sq=mysqli_query($dbc,$l);
$rew=mysqli_fetch_array($sq);
$dat=$rew['date'];
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                  
                  
                
        
				   <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
				  
			
									 <tr><td colspan="2"><center>Receipt Number #:: '.$receipt.'</center></td></tr>			 		
						
				 <tr><td colspan="2"><center>Date:' . $dat . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"   style="font-size: 26px">ITEM</th><th width="30"   style="font-size: 26px">QTY
				</th><th width="30"   style="font-size: 26px">PRICE
				</th><th align="right" width="200"   style="font-size: 26px">AMOUNT</th></tr>';
   while ($row = mysqli_fetch_array($sql)) {
        echo '<tr>
		<td   style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td   style="font-size: 26px">' . (($row['qty'])) . '</td>
				<td   style="font-size: 26px">' . (number_format($row['price'])) . '</td>
		<td align="right"   style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr>';
        $total+=($row['total_p']);
		$qty=+$row['qty'];
		
    } echo'		<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td   style="font-size: 26px"><b>TOTAL</td>
		<td   style="font-size: 26px"><b>' . (($qty)) . '</td>
		<td align="right"   style="font-size: 26px"><b>' . round($total) . '</td></tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>  
 
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}
function Reprin($receipt){
include('mysql.php');
$s="select * from sales where number='$receipt'";
$sql=mysqli_query($dbc,$s);
$l="select * from sales where number='$receipt'";
$sq=mysqli_query($dbc,$l);
$rew=mysqli_fetch_array($sq);
$dat=$rew['date'];

$lj="select type,amount  from x_report where receipt='$receipt'";
$qq=mysqli_query($dbc,$lj);
$raw=mysqli_fetch_array($qq);
$typ=$raw['type'];
$amn=$raw['amount'];
if($typ=='BANK'&&$amn>0){
    $bank=$amn;
}else{
    $bank='0';
}
if($typ=='CASH'&&$amn>0){
    $cash=$amn;
}else{
    $cash='0';
}
if($typ=='MPESA'&&$amn>0){
    $mpesa=$amn;
}else{
    $mpesa='0';
}
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
             
                
                
 
				    <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
									 <tr><td colspan="2"><center>Receipt Number #:: '.$receipt.'</center></td></tr>			 		
						
				 <tr><td colspan="2"><center>Date:' . $dat . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"   style="font-size: 26px">ITEM</th><th width="30"   style="font-size: 26px">QTY
				</th><th width="30"   style="font-size: 26px">PRICE
				</th><th align="right" width="200"   style="font-size: 26px">AMOUNT</th></tr>';
   while ($row = mysqli_fetch_array($sql)) {
        echo '<tr>
		<td   style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td   style="font-size: 26px">' . (($row['qty'])) . '</td>
				<td   style="font-size: 26px">' . (number_format($row['price'])) . '</td>
		<td align="right"   style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr>';
        $total+=($row['total_p']);
		$qty=+$row['qty'];
		
    } echo'		<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td   style="font-size: 26px"><b>TOTAL</td>
		<td   style="font-size: 26px"><b>' . (($qty)) . '</td>
		<td align="right"   style="font-size: 26px"><b>' . round($total) . '</td>
		<tr><td></td><th style="font-size: 26px">MPESA </th><td style="font-size: 26px"> ' . ($mpesa) . '</td></tr>
  <tr><td></td><th style="font-size: 26px">PDQ</th><td style="font-size: 26px"> ' .($bank) . '</td></tr>
   <tr><td></td><th style="font-size: 26px">CASH </th><td style="font-size: 26px"> ' . ($cash) . '</td></tr>
 
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr></tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>  
 
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}

 function LedgerBalance($id,$date){
	     $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
 $group=GetGroup($id);
  if($group==4||$group==1||$group==7||$group==6||$group==9||$group==10){
$r=mysqli_query($dbc,("select sum(debit)-sum(credit)as balance from ledgers where account_id='$id' and date<'$date'"));
}
if($group==3||$group==2||$group==5||$group==14){
$r=mysqli_query($dbc,("select sum(credit)-sum(debit)as balance from ledgers where account_id='$id' and date<'$date'"));
}
$row=mysqli_fetch_array($r);
$balance=$row['balance'];
return $balance;
mysqli_close($dbc);
}
function ledgerbalance1($id,$date,$dat){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$group=GetGroup($id);
if($group==3||$group==2||$group==5||$group==14){
$r=mysqli_query($dbc,("select sum(credit)-sum(debit)as balance from ledgers where account_id='$id' and date between '$date1' and '$date2'"));
$row=mysqli_fetch_array($r);
$balance=$row['balance'];
return $balance;
}
mysqli_close($dbc);
}	
function UserAudit($user,$activity,$date,$ip){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("insert into user_trail(user,action,time,date,ip)
values('$user','$activity',NOW(),NOW(),'$ip')"));
mysqli_close($dbc);
}
function ReverseReceipts($no){
//include(mysql.php);
	$con=mysqli_connect("localhost","vosmcyqy_gtr","n7Y8RY765@@!|3@PLasd","vosmcyqy_rots");
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$ro=mysqli_query($dbc,("select * from sales where number='$no' and type='INVOICE' group by sale_id"));
if(mysqli_num_rows($ro)>0){
$t=mysqli_query($dbc,("select * from sales where number='$no' and type='INVOICE' group by sale_id"));
$gl=mysqli_query($dbc,("select amount,cust from x_report where receipt='$no' and type='INVOICE'"));
$vb=mysqli_fetch_array($gl);
  $dii=$vb['cust'];
  $amn=$vb['amount'];
while($tyy=mysqli_fetch_array($t)){
 $name= mysqli_real_escape_string($dbc,($tyy['p_name']));
$qty=$tyy['qty'];
$session=$tyy['session'];
$g=mysqli_query($dbc,("update products set left_p=left_p+'$qty' where p_name='$name'"));}
//$gr=mysqli_query($dbc,("update accounts set debt=debt-'$amn' where id='$dii' limit 1"));
$der=mysqli_query($dbc,("delete from sales where number='$no' and type='INVOICE'"));
$drs=mysqli_query($con,("delete from sales where number='$no' and type='INVOICE' and src='AFRICANA SHOP'"));
$drr=mysqli_query($dbc,("delete from x_report where receipt='$no' and type='INVOICE'"));

$b=mysqli_query($dbc,("delete from ledgers where session='$session'"));

echo '<span class="green">Receipt Transactions Succesfully Reversed</span></br>';
}else{
echo '<span class="red">Reversal Not Possible Receipt Does Not Exist</span></br>';
}
session_regenerate_id();
mysqli_close($dbc);
mysqli_close($con);
}
function ReverseReceipt($no){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	    	$con=mysqli_connect("localhost","vosmcyqy_gtr","n7Y8RY765@@!|3@PLasd","vosmcyqy_rots");
$session=session_id();

$ro=mysqli_query($dbc,("select * from sales where number='$no' and type='RECEIPT'  group by sale_id"));
if(mysqli_num_rows($ro)>0){
$t=mysqli_query($dbc,("select p_name,session,qty from sales where number='$no' and type='RECEIPT' "));
while($tyy=mysqli_fetch_array($t)){
$name=$tyy['p_name'];
 $qty=$tyy['qty'];
 $session=$tyy['session'];
$g=mysqli_query($dbc,("update products set left_p=left_p+'$qty' where p_name='$name'"));}
$drr=mysqli_query($dbc,("delete from x_report where receipt='$no' and session='$session' and type!='INVOICE'"));
$d=mysqli_query($dbc,("delete from sales where number='$no' and type='RECEIPT'"));
$dc=mysqli_query($con,("delete from sales where number='$no' and type='RECEIPT' and src='AFRICANA SHOP'"));

$b=mysqli_query($dbc,("delete from ledgers where session='$session'"));
echo '<span class="green">Receipt Transactions Succesfully Reversed</span></br>';
}else{
echo '<span class="red">Reversal Not Possible Receipt Does Not Exist</span></br>';
}
session_regenerate_id();
mysqli_close($dbc);
mysqli_close($con);
}
function AddSubP($name,$id,$status,$type){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc($dbc,($name)));

$r=mysqli_query($dbc,("select * from sub_accounts where name='$name' "));

if(mysqli_num_rows($r)==0){

$t=mysqli_query($dbc,("insert into sub_accounts(name,account)values('$name','$id')"));
$idd=mysqli_insert_id($dbc);
$y=mysqli_query($dbc,("insert into pgroups(id,name,group_id,status)
values('$idd','$name','$status','$type')"));




 echo '<span class="green">Vote Succesfully Added</span></br>';}else{
 echo '<span class="red">Vote Already Exists</span></br>';}
 mysqli_close($dbc);
}
function PayrollVote($name,$id,$status,$idd){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$t=mysqli_query($dbc,("select * from pgroups where name='$name'"));
if(mysqli_num_rows($t)==0){
$yI=mysqli_query($dbc,("insert into accounts(name)values('$name')"));
$ide=mysqli_insert_id($dbc);
$D=mysqli_query($dbc,("insert into pgroups(id,name,group_id,status)
values('$ide','$name','$id','$status')"));
$c=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$ide','$idd')"));

 echo '<span class="green">Payroll Vote Succesfully Added</span></br>';
}
else{
 echo '<span class="red">Sorry Payroll Vote Already Exists</span></br>';}

mysqli_close($dbc);
}
function Vname($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from pgroups where id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['name'];
return $name;
mysqli_close($dbc);
}

function AdjustDeductions($id,$eid,$amount,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("insert into varance(group_id,employee_id,date,credit)values('$id','$eid','$date','$amount')"));
 echo '<span class="green">Deduction Succesfully Adjusted</span></br>';
 mysqli_close($dbc);
}
function Employees($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from employee where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['names'];
return $name;
mysqli_close($dbc);
}
function GetBasic(){
$r=mysqli_query($dbc,("select * from pgroups where name='BASIC SALARY' "));

$f=mysqli_fetch_array($r);
$g=$f['id'];

return $g;
mysqli_close($dbc);
}
 function GetPayeP($id,$date){
	     $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(debit)-200 as gross from fixed where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$fixed=$row['gross'];
$bas=GetBasic();
$r=mysqli_query($dbc,("select sum(debit) as gross from fixed where employee_id='$id' and group_id='$bas'"));
$row=mysqli_fetch_array($r);
$basic=$row['gross'];

$r=mysqli_query($dbc,("select sum(debit) as gross from varance where employee_id='$id' and MONTH(date)=MONTH('$date')"));
$row=mysqli_fetch_array($r);
$var=$row['gross'];
$gross=$fixed+$var;



//$gross=62000;
$dd=mysqli_query($dbc,("SELECT * FROM tax WHERE '$gross' BETWEEN mini and maxi"));
$rew=mysqli_fetch_array($dd);
$mini = $rew['mini'];
$rate = $rew['rate'];
$tax_id=$rew['tax_id'];
$taxable=$gross-$mini;
  $firstpaye =$taxable*$rate/100;
$id2=$tax_id-1;
$y=mysqli_query($dbc,("SELECT tax FROM tax WHERE tax_id='$id2'"));

$row = mysqli_fetch_array($y);
$tax = $row['tax'];
$paye=$tax+$firstpaye-(1162-$insurance);
if($paye<=0){
$paye=0.0;
}
return $paye;
mysqli_fetch_array($dbc);
mysqli_close($dbc);
}
function GetDeductions($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(credit) as gross from fixed where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$fixed=$row['gross'];
$r=mysqli_query($dbc,("select sum(credit) as gross from varance where employee_id='$id' and MONTH(date)=MONTH('$date')"));
$row=mysqli_fetch_array($r);
$var=$row['gross'];
$gross=$fixed+$var;
//$gross=62000;

return $gross;

mysqli_close($dbc);
}
function EmployeeNo($id){
$r=mysqli_query($dbc,("select * from employee where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['eno'];
return $name;
mysqli_close($dbc);
}
function IdNo($id){
$r=mysqli_query($dbc,("select * from employee where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['id_no'];
return $name;
mysqli_close($dbc);
}
function AdjustBonuses($id,$eid,$amount,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("insert into varance(group_id,employee_id,date,debit)values('$id','$eid','$date','$amount')"));
 echo '<span class="green">Bonus Succesfully Adjusted</span></br>';
 mysqli_close($dbc);
}
function CommitedPayroll($date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$session=session_id();
$staff=GetStaff();
$r=mysqli_query($dbc,("insert into ledgers(date,credit,account_id,
froms,sub,session)(select '$date'as date,debit,'$staff' as 
account_id,group_id as froms,group_id,'$session' as session from fixed where debit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,debit,account_id,
froms,sub,session)(select '$date'as date,debit,group_id 
as account_id,employee_id ,group_id,'$session' as session from fixed where debit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,credit,froms,
account_id,sub,session)(select '$date'as date,credit,'$staff' 
as account_id,group_id as froms,group_id,'$session' as session from fixed where credit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,debit,froms,
account_id,sub,session)(select '$date'as date,credit,group_id as account_id,employee_id ,
group_id,'$session' as session from fixed where credit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,credit,account_id,
froms,sub,session)(select '$date'as date,debit,'$staff' as account_id,group_id as 
froms,group_id,'$session' as session from varance where debit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,debit,account_id,
froms,sub,session)(select '$date'as date,debit,group_id as account_id,employee_id ,
group_id,'$session' as session from varance where debit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,credit,froms,
account_id,sub,session)(select '$date'as date,credit,'$staff' as account_id,group_id as 
froms,group_id,'$session' as session from varance where credit>0 group by id)"));
$r=mysqli_query($dbc,("insert into ledgers(date,debit,froms,
account_id,sub,session)(select '$date'as date,credit,group_id 
as account_id,employee_id ,group_id,'$session' as session from varance where credit>0 group by id)"));
$r=mysqli_query($dbc,("insert into employee_table(employee_id,date,
debit,group_id)(select employee_id,'$date' as date,debit,group_id from fixed where debit>0 group by id)"));
$r=mysqli_query($dbc,("insert into employee_table(employee_id,date,
credit,group_id)(select employee_id,'$date' as date,credit,
group_id from fixed where credit>0 group by id)"));
$r=mysqli_query($dbc,("insert into employee_table(employee_id,date,
debit,group_id)(select employee_id,'$date' as date,debit,group_id from varance where debit>0 and month(date)=month('$date') group by id)"));
$r=mysqli_query($dbc,("insert into employee_table(employee_id,date,
credit,group_id)(select employee_id,'$date' as date,credit,group_id from varance where credit>0 and month(date)=month('$date') group by id)"));
$d=mysqli_query($dbc,("select * from ledgers where session='$session'"));
while($row=mysqli_fetch_array($d)){
$idd=GetPV($row['account_id']);
$from=GetPV($row['froms']);
$id=$row['id'];
$h=mysqli_query($dbc,("update ledgers set account_id='$idd',froms='$from' where id='$id'"));

}
$t=mysqli_query($dbc,("update ledgers set description='STAFf SALARIES' where session='$session'"));
echo '<span class="green">Payroll Succesfully Added</span></br>';
mysqli_close($dbc);
}
function GetPV($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from sub_accounts where id='$id'"));
if(mysqli_num_rows($r)>0){
$f=mysqli_fetch_array($r);
$g=$f['account'];
}else{
$g=$id;
}
return $g;
mysqli_close($dbc);
}
function GetPId(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from pgroups where name='PAYE' "));

$f=mysqli_fetch_array($r);
$g=$f['id'];

return $g;
mysqli_close($dbc);
}
function GetCustomer($session){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from ledgers where session='$session' and debit>0"));
$row=mysqli_fetch_array($r);
$name=Acc($row['account_id']);
return $name;
mysqli_close($dbc);
}
function InsertPaye($date,$p){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from employee"));
while($row=mysqli_fetch_array($r)){
$session=session_id();
$id=GetStaff();
$paye=GetPayeP($id,$date);
$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,date,session)
values('$paye','$id','$p','$date','$session')"));
$t=mysqli_query($dbc,("insert into ledgers(credit,account_id,froms,date,session)
values('$paye','$p','$id','$date','$session')"));
$t=mysqli_query($dbc,("insert into employee_table(credit,employee_id,group_id,date)
values('$paye','$id','$p','$date')"));

}
mysqli_close($dbc);
}
function GetTaxableP($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(debit)-200 as gross from fixed where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$fixed=$row['gross'];
$bas=GetBasic();
$r=mysqli_query($dbc,("select sum(debit) as gross from fixed where employee_id='$id' and group_id='$bas'"));
$row=mysqli_fetch_array($r);
$basic=$row['gross'];
$basic=15/100*$basic;


$r=mysqli_query($dbc,("select sum(debit) as gross from varance where employee_id='$id' and MONTH(date)=MONTH('$date')"));
$row=mysqli_fetch_array($r);
$var=$row['gross'];
$gross=$fixed+$var;
//$gross=62000;

return $gross;
mysqli_close($dbc);
}
function PostCheque($date,$bank,$cheque){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$rp=mysqli_query($dbc,("select sum(debit)-sum(credit)as net,bank,employee.employee_id AS employee_id from employee_table
inner join employee on employee.employee_id=employee_table.employee_id

where month(employee_table.date)=month('$date')
and status='NOT PAID'
and year(employee_table.date)=year('$date')
and bank='$bank'

group by employee_table.employee_id"));
$session=session_id();
while($row=mysqli_fetch_array($rp)){
$staff=GetStaff();
$employee=$row['employee_id'];
$net=$row['net'];
$nn+=$net;
$g=mysqli_query($dbc,("update employee_table set status='PAID' where employee_id='$employee' and month(date)=month('$date')
and year(date)=year('$date')"));
$ur=mysqli_query($dbc,("insert into ledgers(credit,account_id,sub,date,receipt,froms,session,invoice)
values('$net','$bank','$staff','$date','$rec','$employee','$session','$cheque')"));
//$r=mysqli_query("insert into supplier_ledgers(credit,supplier_id,date,code,session)values('$amount','$payment','$date','$invoice','$session')");

$i=mysqli_query($dbc,("insert into ledgers(debit,account_id,sub,date,receipt,froms,session,invoice)
values('$net','$staff','$employee','$date','$rec','$bank','$session','$cheque')"));
$name=SubName($employee);


$ne=number_format($net);
 echo "<span class='green'>$name salary of $ne succesfully debited their account</span></br>";
 session_regenerate_id();
 mysqli_close($dbc);
}
BankCredit($bank,$nn,$date,$cheque,$session);

}
function GetGross($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(debit)-200 as gross from fixed where employee_id='$id'"));
$row=mysqli_fetch_array($r);
$fixed=$row['gross'];



$r=mysqli_query($dbc,("select sum(debit) as gross from varance where employee_id='$id' and MONTH(date)=MONTH('$date')"));
$row=mysqli_fetch_array($r);
$var=$row['gross'];
$gross=$fixed+$var;
//$gross=62000;

return $gross;
mysqli_close($dbc);
}
function GetNet($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$paye=GetPayeP($id,$date);
$gross=GetGross($id,$date);
$deductions=GetDeductions($id,$date);
$net=$gross+200-($paye+$deductions);
return $net;
mysqli_close($dbc);
}


function DeletePayrollVote($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
if($id!=1){
$t=mysqli_query($dbc,("select * from pgroups where id='$id'"));
if(mysqli_num_rows($t)>0){
$y=mysqli_query($dbc,("delete from pgroups where id='$id'"));
 echo '<span class="green">Payroll Vote Succesfully Deleted</span></br>';
}
else{
 echo '<span class="red">Sorry Payroll Vote Does Not Exists</span></br>';}


}else{
 echo '<span class="red">Sorry Paye Vote cannot be deleted!-(It\'s A system Constant)</span></br>';

}
mysqli_close($dbc);
}
function AddEmployee($name,$no,$id,$phone,$pin,$nssf,$nhif,$dep,$pos,$bank,$account){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$bank=mysqli_real_escape_string($dbc,($bank));
$pos=mysqli_real_escape_string($dbc,($pos));
$dep=mysqli_real_escape_string($dbc,($dep));
$staff=GetStaff();
$t=mysqli_query($dbc,("select * from employee where id_no='$id'"));
if(mysqli_num_rows($t)==0){
$e=mysqli_query($dbc,("insert into sub_accounts(name,account)values('$name','$staff')"));
 echo '<span class="green">Employee Succesfully Added</span></br>';
 $if=mysqli_insert_id($dbc);

 //$k=mysqli_query("insert into group_accounts(account_id,group_id)values('$if','11')");
 $y=mysqli_query($dbc,("insert into employee(employee_id,names,id_no,pin,nssf_no,nhif_no,position,dep,phone,bank,account,eno)
values('$if','$name','$id','$pin','$nssf','$nhif','$pos','$dep','$phone','$bank','$account','$no')"));
 
 
 $_SESSION['employee']=$if;
}
else{
 echo '<span class="red">Sorry Employee Already Exists</span></br>';}

mysqli_close($dbc);
}
function AddAsset($name,$dep,$price,$date,$desc,$payment,$main){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$desc=mysqli_real_escape_string($dbc,($desc));
$t=mysqli_query($dbc,("select * from sub_accounts where name='$name'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into sub_accounts(name,account)
values('$name','$main')"));
 echo '<span class="green">Asset Succesfully Added</span></br>';
 $id=mysqli_insert_id($dbc);
 $session=session_id();

  $q=mysqli_query($dbc,("INSERT into ledgers(date,credit,
  description,sub,froms,session,account_id,dep)VALUES('$date','$price','$desc','$id','$main','$session','$payment','$dep')"));

  $r=mysqli_query($dbc,("INSERT into ledgers(date,debit,
  description,sub,froms,session,account_id,dep)VALUES('$date','$price','$desc','$id','$payment','$session','$main','$dep')"));
}
else{
 echo '<span class="red">Sorry Asset Already Exists</span></br>';}
 session_regenerate_id();

mysqli_close($dbc);
}
function LpS($no){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$s=mysqli_query($dbc,("select * from lpo where invoice='$no'"));
$row=mysqli_fetch_array($s);
$date=Mode($row['supplier']);
return $date;

mysqli_close($dbc);
}
function LDate($no){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$s=mysqli_query($dbc,("select * from lpo where invoice='$no'"));
$row=mysqli_fetch_array($s);
$date=$row['date'];
return $date;
mysqli_close($dbc);

}
function ChangePassT($user,$old,$new,$new2){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$user=mysqli_real_escape_string($dbc,($user));
$old=mysqli_real_escape_string($dbc,($old));
$new=mysqli_real_escape_string($dbc,($new));
$new2=mysqli_real_escape_string($dbc,($new2));
$q =mysqli_query($dbc,( "SELECT *,user_id, user_name
 FROM users WHERE
 user_name='$user' AND password=SHA1('$old')
"));
if(mysqli_num_rows($q)==1){
if($new==$new2){
$r=mysqli_query($dbc,("update users set password=SHA1('$new') where user_name='$user' and password=SHA1('$old') limit 1"));
 echo '<span class="green">User Password Succesfully Updated</span></br>';
}else{
 echo '<span class="red">Your Passwordd dont match</span></br>';
}

}else{

 echo '<span class="red">Your Cant Change Another Username Password</span></br>';
}

mysqli_close($dbc);

}
function minimum($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from products where product_id='$id'"));
$row=mysqli_fetch_array($r);
$minimum=$row['minimum'];
return $minimum;
mysqli_close($dbc);
}
function stcokist($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from products where product_id='$id'"));
$row=mysqli_fetch_array($r);
$minimum=$row['stockist'];
return $minimum;
mysqli_close($dbc);
}
function maximum($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from products where product_id='$id'"));
$row=mysqli_fetch_array($r);
$minimum=$row['price'];
return $minimum;
mysqli_close($dbc);
}
function AddCategory($name){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));

$t=mysqli_query($dbc,("select * from categories where name='$name'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into categories(name)
values('$name')"));
 echo '<span class="green">Category Succesfully Added</span></br>';

}
else{
 echo '<span class="red">Sorry Category Already Exists</span></br>';}

mysqli_close($dbc);
}
function AddDeposit($name,$amount,$date,$id,$phone,$froms){
	//    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$e=mysqli_query("select * from depo");
$ee=mysqli_fetch_array($e);
$number=$ee['number'];
$t=mysqli_query("update depo set number=number+1");
$dep=GetDeposit();
$session=session_id();
$r=mysqli_query("insert into deposits(name,date,amount,id_no,phone,number)values('$name','$date','$amount','$id','$phone','$number')");
$l=mysqli_query("insert into ledgers(account_id,froms,date,description,credit,session,receipt)value('$dep','$froms','$date','CUSTOMER DEPOSITS','$amount','$session','$number')");
$l=mysqli_query("insert into ledgers(account_id,froms,date,description,debit,session,receipt)value('$froms','$dep','$date','CUSTOMER DEPOSITS','$amount','$session','$number')");
session_regenerate_id();


ob_end_clean();
header("location:deposit_slips.php?n=$number&s=$froms");
}
function GetDeposit(){
$r=mysqli_query("select * from accounts where name='CUSTOMER DEPOSITS'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name)values('CUSTOMER DEPOSITS')");
$id=mysqli_insert_id($dbc);
$h=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','2')");

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;

}
function GetDebt(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='DEBT SWAP'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('DEBT SWAP')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','2')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetStaff(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query("select * from accounts where name='STAFF SALARIES'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name)values('STAFF SALARIES')");
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','11')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetEquity(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='RETAINED EARNINGS'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('RETAINED EARNINGS')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','14')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetDep(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query("select * from accounts where name='ASSETS DEPRECIATION'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('ASSETS DEPRECIATION')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}

function DebtSwap($date,$amount,$supplier){
$id= GetDebt();
$r=mysqli_query("select * from dswap");
$row=mysqli_fetch_array($r);
$number=$row['code'];
$_SESSION['SWAP']=$number;
$l=mysqli_query("insert into debtswap(number,date,amount,supplier)values('$number',
'$date','$amount','$supplier')");
$session=session_id();
$f=mysqli_query("insert into ledgers(account_id,froms,debit,date,session,amount)values('$supplier','$id','$date','$session','$amount')");
$f=mysqli_query("insert into ledgers(account_id,froms,credit,date,session,amount)values('$id','$supplier','$date','$session','$amount')");
$r=mysqli_query("update dswap set code=code+1");
$url='swapnotes.php';
session_regenerate_id();
ob_end_clean();
header("Location: $url");
}
function GetStock(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='STOCK'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('STOCK')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','4')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function AddUser($name,$user,$pass1,$pass2,$dep,$level,$phone){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$user=mysqli_real_escape_string($dbc,($user));
if($pass1==$pass2){
$okay=1;
}else{
$okay=0;
}
	UserAudit($_SESSION['user_id'],"Added User $name",date("Y/m/d"),$ip);
$r=mysqli_query($dbc,("select * from users where user_name='$user'"));
if(mysqli_num_rows($r)==0&&$okay==1){
$t=mysqli_query($dbc,("insert into users(name,user_name,password,level_id,department,phone)
values('$name','$user',SHA1('$pass1'),'2','$dep','$phone')"));
 echo '<span class="green">User Account Succesfully Created</span></br>';
}
if(mysqli_num_rows($r)>0){
echo '<span class="red">That Username Is Already Registered</span></br>';
}
if($okay==0){
echo '<span class="red">Your Password Did Not Match</span></br>';}
mysqli_close($dbc);
}
function Supplier($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query("select * from accounts where id='$id'");
$row=mysqli_fetch_array($t);
$name=$row['name'];
return $name;
mysqli_close($dbc);
}

function AddYear($start,$end){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$t=mysqli_query($dbc,("select * from financial_year where start between='$start' and '$end'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into financial_year(start,end)
values('$start','$end')"));


 echo '<span class="green">Financial Year Succesfully Added</span></br>';
}else{
 echo '<span class="red">Sorry Financial Year Cannot Be Created</span></br>';}
mysqli_close($dbc);
}

function AddSuppliers($name,$phone,$add){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$t=mysqli_query($dbc,("select * from accounts where name='$name'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into accounts(name,phone,address)
values('$name','$phone','$add')"));
$id=mysqli_insert_id($dbc);
$d=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values
('$id','2')"));
 echo '<span class="green">Supplier Succesfully Added</span></br>';
}else{
 echo '<span class="red">Sorry supplier Already Exists</span></br>';}
mysqli_close($dbc);
}
function GetSALES(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='SALES'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('SALES')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','3')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetDiscount(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='SALES DISCOUNT'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('SALES DISCOUNT')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetCash(){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='CASH'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('CASH')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','4')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetTaxGoods(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='TAX ON GOODS'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('TAX ON GOODS')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetCostG(){
     $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='COST OF GOODS SOLD'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('COST OF GOODS SOLD')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetDamages(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='DAMAGES'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('DAMAGES')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function BuysRec($buy,$rec,$sss,$cc,$session){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,
froms,session,date,description)values('$buy','$rec','$sss','$cc','$session',NOW(),'COST OF GOODS SOLD')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,
session,date,description)values('$buy','$cc','$sss','$rec','$session',NOW(),'COST OF GOODS SOLD')"));

mysqli_close($dbc);
}
function DiscRec($buy,$rec,$sss,$cc,$session){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query("insert into ledgers(credit,receipt,account_id,froms,session,date,description)values('$buy','$rec','$sss','$cc','$session',NOW(),'SALES DISCOUNT')");

$t=mysqli_query("insert into ledgers(debit,account_id,froms,receipt,session,date,description)values('$buy','$cc','$sss','$rec','$session',NOW(),'SALES DISCOUNT')");

mysqli_close($dbc);
}
function Damaged($buy,$rec,$sss,$cc,$session){

$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,froms,session,
date,description)values('$buy','$rec','$sss','$cc','$session',NOW(),'COST OF GOODS DAMAGED')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,session,
date,description)values('$buy','$cc','$sss','$rec','$session',NOW(),'COST OF GOODS DAMAGED')"));

mysqli_close($dbc);
}
function BuysRecC($buy,$rec,$sss,$cc,$session,$date){
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,
froms,session,date,description)values('$buy','$rec','$sss','$cc','$session','$date','COST OF GOODS SOLD')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,
session,date,description)values('$buy','$cc','$sss','$rec','$session','$date','COST OF GOODS SOLD')"));
mysqli_close($dbc);

}
function SalesRec($tor,$rec,$sl,$ch,$session){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,
	froms,session,date,description)values('$tor','$rec','$sl','$ch','$session',NOW(),'SALES')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,
session,date,description)values('$tor','$ch','$sl','$rec','$session',NOW(),'SALES')"));

mysqli_close($dbc);
}
function SalesRecOthers($tor,$rec,$sl,$ch,$session,$desc){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,
	froms,session,date,description)values('$tor','$rec','$sl','$ch','$session',NOW(),'$desc')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,
session,date,description)values('$tor','$ch','$sl','$rec','$session',NOW(),'$desc')"));

mysqli_close($dbc);
}
function SalesRecIN($tor,$rec,$sl,$ch,$session,$expected){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$t=mysqli_query($dbc,("insert into ledgers(credit,invoice,account_id,froms,session,date,description,mat_date)values
	('$tor','$rec','$sl','$ch','$session',NOW(),'SALES VIA INVOICE','$expected')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,invoice,session,date,description,mat_date)
values('$tor','$ch','$sl','$rec','$session',NOW(),'SALES VIA INVOICE','$expected')"));

mysqli_close($dbc);
}
function SalesRecINC($tor,$rec,$sl,$ch,$session,$expected,$delivery){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$t=mysqli_query($dbc,("insert into ledgers(credit,invoice,account_id,froms,session,date,description,mat_date,dat)values
	('$tor','$rec','$sl','$ch','$session','$delivery','SALES VIA INVOICE','$expected',NOW())"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,invoice,session,date,description,mat_date,dat)
values('$tor','$ch','$sl','$rec','$session','$delivery','SALES VIA INVOICE','$expected',NOW())"));

mysqli_close($dbc);
}
function SalesRecINCD($tor,$rec,$sl,$ch,$session,$expected,$delivery,$number){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
	$t=mysqli_query($dbc,("insert into ledgers(credit,receipt,account_id,froms,session,date,description,mat_date)values
	('$tor','$rec','$sl','$ch','$session','$delivery','SALES USING DEPOSIT SLIP $number','$expected')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,froms,receipt,session,date,description,mat_date)
values('$tor','$ch','$sl','$rec','$session','$delivery','SALES USING DEPOSIT SLIP $number','$expected')"));

mysqli_close($dbc);
}
function PurchasesRecord($date,$CODE,$TOTALL,$session,$expected,$froms,$supplier){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query($dbc,("insert into ledgers(credit,invoice,froms,session,account_id,mat_date,description,date)values
('$TOTALL','$CODE','$froms','$session','$supplier','$expected','PURCHASES','$date')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,invoice,session,froms,mat_date,description,date)values('$TOTALL',
'$froms','$CODE','$session','$supplier','$expected','PURCHASES','$date')"));

mysqli_close($dbc);
}
function PurchasesVat($date,$v,$vat,$kra,$CODE,$session){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$t=mysqli_query($dbc,("insert into ledgers(credit,invoice,froms,session,account_id,description,date)values('$vat','$CODE','$kra','$session','$v',
'PURCHASES VAT','$date')"));

$t=mysqli_query($dbc,("insert into ledgers(debit,account_id,invoice,session,froms,description,date)values('$vat','$kra','$CODE','$session',
'$v','PURCHASES VAT','$date')"));

mysqli_close($dbc);
}
function GetKra(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='K.R.A'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('K.R.A')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','2')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function GetVat(){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where name='VAT'"));
if(mysqli_num_rows($r)==0){
$f=mysqli_query($dbc,("insert into accounts(name)values('VAT')"));
$id=mysqli_insert_id($dbc);
$h=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values('$id','1')"));

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;
mysqli_close($dbc);
}
function login($user,$pass){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$q =mysqli_query($dbc,( "SELECT *,user_id, user_name
 FROM users WHERE
 user_name='$user' AND password=SHA1('$pass')
"));
if (mysqli_num_rows($q) == 1) {
$row=mysqli_fetch_array($q);
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user'] = $row['user_name'];
$_SESSION['name']=$row['name'];
$_SESSION['level']=$row['level_id'];
$_SESSION['department']=$row['department'];
if($_SESSION['department']==1){
$url='index.php';}
if($_SESSION['department']==2){
$url='indexxx.php';}
if($_SESSION['department']==3){
$url='indexx.php';

}
if($_SESSION['department']==4){
$url='module.php';

}
}else{
$_SESSION['login']='USERNAME & PASSWORD COMBINATIONS NOT VALID';
$url='login.php';
}
mysqli_close($dbc);
ob_end_clean();
header("Location: $url");
}
function  IncomePayments($customer,$payment,$date,$amount){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$session=session_id();
$r=mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session,dat)values
('$amount','$customer','$date','$invoice','$payment','$session',NOW())"));

$rt=mysqli_query($dbc,("insert into ledgers(debit,account_id,date,
receipt,froms,session,dat)values('$amount','$payment','$date','$invoice','$customer','$session',NOW())"));
if($r&&$rt){
echo "<span class='green'>Debtor Payment Succesfull </span></br>";
}else{
     echo("Error description: " . mysqli_error($dbc));
}
//$bg=mysqli_query($dbc,("update accounts set debt=debt-'$amount' where id='$customer' limit 1"));
$ko=mysqli_query($dbc,("select name from accounts where id='$payment'"));
    $flo=mysqli_fetch_array($ko);
	  $name=$flo['name'];
$hy=mysqli_query($dbc,("insert into x_report(amount,type,time,date,session,cust,refe)values
('$amount','$name','$date',NOW(),'$session','$customer','DEBT PAYMENT')"));
session_regenerate_id();
mysqli_close($dbc);
}
function  IncomeRecorddC($income,$customer,$date,$amount,$invoice){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$session=session_id();
$r=mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session)
values('$amount','$income','$date','$invoice','$customer','$session')"));
//$f=mysqli_query("insert into customer_ledgers(customer_id,date,credit,receipt,session)values('$income','$date','$amount','$invoice','$session')");
$r=mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session)
values('$amount','$customer','$date','$invoice','$income','$session')"));
echo "<span class='green'>Income Recording Succesfull </span></br>";
session_regenerate_id();
mysqli_close($dbc);
}
function  BasicSupplier($supplier,$ptype,$date,$amount,$tcode,$desc){
$desc=mysqli_real_escape_string($desc);
$session=session_id();
$r=mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,
session,description)values('$amount','$ptype','$date','$tcode','$supplier','$session','$desc')"));
//$f=mysqli_query("insert into customer_ledgers(customer_id,date,credit,receipt,session)values('$income','$date','$amount','$invoice','$session')");
$r=mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,
description)values('$amount','$supplier','$date','$tcode','$ptype','$session','$desc')"));
echo "<span class='green'>Supplier Payment Recording Succesfull </span></br>";
session_regenerate_id();
mysqli_close($dbc);
}
function AddCustomer($name,$phone,$add,$credit,$bal){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$t=mysqli_query($dbc,("select * from accounts where name='$name'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into accounts(name,phone,address,descc,stat)
values('$name','$phone','$add','$credit','CUSTOMER')"));
$id=mysqli_insert_id($dbc);

$d=mysqli_query($dbc,("insert into group_accounts(account_id,group_id)values
('$id','4')"));
$bh=mysqli_query($dbc,("insert into ledgers(credit,account_id,date,froms,description,dat)
values('$bal','1',NOW(),'$id','OPENING BALANCE',NOW())"));
$hb=mysqli_query($dbc,("insert into ledgers(debit,account_id,date,froms,description,dat)
values('$bal','$id',NOW(),'1','OPENING BALANCE',NOW())"));
 echo '<span class="green">Customer Succesfully Added</span></br>';
}else{
 echo '<span class="red">Sorry Customer Already Exists</span></br>';}
mysqli_close($dbc);
}
function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}


function teller($ip){

if($ip=='127.0.0.1'){
$teller='1';


}
if($ip=='192.168.0.101'){
$teller='2';


}
if($ip=='192.168.0.102'){
$teller='3';


}
if($ip=='192.168.0.103'){
$teller='4';


}
if($ip=='192.168.0.105'){
$teller='5';


}
if($ip=='192.168.0.104'){
$teller='6';


}
return $teller;}
function ReceiptOthers($receipt,$type,$bank,$number,$given){
	ini_set('display_errors', 0);
	$user=$_SESSION['user_name'];
ini_set('display_errors', 0);
include('mysql.php');
$te=type($type);
$r="select *,time(NOW()) as t from sales where number='$receipt'";
$sql=mysqli_query($dbc,$r);
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
                <tr><td ><h3><b><center></b></h3></td></tr>
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
									 <tr><td colspan="2"><center>RECEIPT :' . $receipt  . '</center></td></tr>			 		
				 <tr><td colspan="2"><center>Till: ' . $tiller . '</center></td></tr> 		
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"   style="font-size: 26px">ITEM</th><th width="30"   style="font-size: 26px">QTY
				</th><th align="right" width="200"   style="font-size: 26px">AMOUNT</th></tr>
				';
    $total = 0;
	$vat=0;
    while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td   style="font-size: 26px">
		' . ($row['system']) . '</td><td></td><td  style="font-size: 26px" align="right">' . ($row['qty']) . ' X ' . number_format($row['price'],2) . '</td><tr></tr>
		<tr><td  style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td  style="font-size: 26px">
		</td><td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];
    }
	echo '<table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
 
 
	
	 <tr><th style="font-size: 20px"><u>TOTAL QTY</th><th width="300" style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;<u>VATABLE AMT </th><th style="font-size: 20px"><u>
VAT </th></tr>
 	 <tr><td  style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td><td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.number_format(($total*84/100)) .'</td><td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;'.number_format($vat) .'</th></tr>
   <tr><td></td><th  style="font-size: 26px">TOTAL </th><td  style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>';
   if($te=='CHEQUE'){
   echo'
   <tr><td></td><th  style="font-size: 26px">PAYMENT TYPE</th><td  style="font-size: 26px"> ' . $te . '</td></tr>
      <tr><td></td><th  style="font-size: 26px">BANK </th><td  style="font-size: 26px"> ' . $bank. '</td></tr>
   <tr><td></td><th  style="font-size: 26px">CHEQUE NO </th><td  style="font-size: 26px"> ' . $number. '</td></tr>
   <tr><td></td><th  style="font-size: 26px">AMOUNT</th><td  style="font-size: 26px"> ' .number_format($given). '</td></tr>';}
   if($te=='CREDIT NOTES'){
   echo'
   <tr><td></td><th  style="font-size: 26px">PAYMENT TYPE</th><td  style="font-size: 26px"> ' . $te . '</td></tr>

   <tr><td></td><th  style="font-size: 26px">AMOUNT</th><td  style="font-size: 26px"> ' .number_format($given). '</td></tr>';}
    if($te=='MPESA'){
   echo'
   <tr><td></td><th  style="font-size: 26px">PAYMENT TYPE</th><td  style="font-size: 26px"> ' . $te . '</td></tr>
      <tr><td></td><th  style="font-size: 26px">CODE</th><td  style="font-size: 26px"> ' . $number. '</td></tr>
   <tr><td></td><th  style="font-size: 26px">AMOUNT </th><td  style="font-size: 26px"> ' . number_format($given). '</td></tr>';}?><? echo '
    <tr><th><hr style="border-top: dotted 3px;" /></th>
				 <th><hr style="border-top: dotted 3px;" /></th><th>
				 <hr style="border-top: dotted 3px;" /></th><th><hr style="border-top: dotted 3px;" /></th></tr>
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr></table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td   style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$time.'</center></td></tr>
				<tr><th   style="font-size: 26px"><center>Thank for shopping with us</center></th></tr>
			
				 <tr><th><hr style="border-top: dotted 3px;" /></th>
				 <th><hr style="border-top: dotted 3px;" /></th><th>
				 <hr style="border-top: dotted 3px;" /></th><th><hr style="border-top: dotted 3px;" /></th></tr>
				 <tr><td><h4></h4></td></tr>
				<tr><td style="font-size: 17px"> <div align="center"><b>GOODS ONCE SOLD CANNOT BE RE-ACCEPTED<td></div></tr>
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();

}
function DepositSlip($type,$number){
	ini_set('display_errors', 0);
	$user=$_SESSION['user'];


$te=type($type);
$r=mysqli_query("select *,time(NOW()) as t from deposits where number='$number'");
$row=mysqli_fetch_array($r);
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
                   
                
                   
      
                     <tr><td ><h3><b>WATER WORLD IRRIGATION<center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX:8341-00200 NAIROBI TEL:0729004527/0737099989</center></td></tr>
				 <tr><td colspan="2"><center>PIN:P051346532B</center></td></tr>
				 <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 20px"><center>SLIP NO '.$number.'</center></td></tr>
				 <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 20px"><center>DEPOSIT SLIP GENERATED BY '.$user.'  </center></td></tr>
				  <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 20px"><center>GENERATION  TIME '.$row['t'].'</center></td></tr>
				 <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 18px"><left>CLIENT NAME <u>'.$row['name'].'</center></td></tr>
				  <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 18px"><left>CLIENT PHONE <u>'.$row['phone'].' </center></td></tr>
				   <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 18px"><left>CLIENT  ID NO <u>'.$row['id_no'].' </center></td></tr>
				 <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 18px"><left>Date:' . (date("Y/m/d")) . '</center></td></tr>
 <tr><th  style="font-size: 24px"><center>MPESA TILL NUMBER:894972</center></th></tr>
					 <tr><td colspan="2" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 15px"><center><b>*THIS IS NOT A RECEIPT*</b></center></td></tr>
<tr><th>
				<hr size="10" noshade> </hr> </th>
			
			
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							
				   </table>
<table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
<tr><td><b>NARRATION</td><td><b></th><td><b>AMOUNT</td></tr>';

echo '<tr>
<td  style="font-size: 26px">CUSTOMER DEPOSITS</td><td  style="font-size: 26px"></td>
<td  style="font-size: 26px">' . number_format(($row['amount'])) . '</td>
</tr>';


echo '<tr>
<td  style="font-size: 26px"><b>TOTAL</td><td  style="font-size: 26px"></td>
<td  style="font-size: 26px"><b>' . number_format(($row['amount'])) . '</td>
</tr>';
echo'<tr><th>
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th>
				<th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>';
              echo'

<tr><th> 
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th>
				<th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>	  </table>
				
				
				<br><br>
			   </div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();

}
function type($id){
if($id==1){
$type='MPESA';}
if($id==2){
$type='CREDIT NOTES';}

if($id==3){
$type='CHEQUE';}
return $type;


}
function CXreport($date,$picked,$mpesa,$cheque){
include('conf.php');

//$r=mysqli_query("select * ,time(NOW())as f from user_audit where date=date(NOW())
 //and user='$user'
 //and till='$till'
 //and status='ACTIVE' order by id desc limit 1");

 //$row=mysqli_fetch_array($r);
 //$time=$row['f'];
//$gh=$time;
 $t=mysqli_query("insert into cash_pick(date,pick,mpesa,swipe)
 values(NOW(),$picked',$mpesa','$cheque')");
  //$l=mysqli_query("select sum(cash)as a from cpick where till='$till' and date=date(NOW())");

 /*$lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$picked','$actual','CASH PICKED','$session')");
   $l=mysqli_query("select sum(amount)as a from petty where till='$till' and date=date(NOW())");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$petty','$actual','PETTY CASH','$session')");
 $l=mysqli_query("select sum(amount)as a from x_report where till='$till' 
 and date=date(NOW()) and type='CASH' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
  $l=mysqli_query("select sum(amount)as a from x_report where till='$till' and date=date(NOW()) 
  and type='CREDIT NOTES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actua=$lol['a'];
 $actuall=$actual+$actua;
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$cash','$actuall','CASH','$session')");
  $l=mysqli_query("select sum(amount)as a from x_report where till='$till' and date=date(NOW()) 
  and type='CREDIT NOTES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$credit','$actual','CREDIT NOTES','$session')");
  $l=mysqli_query("select sum(amount)as a from x_report where till='$till' and date=date(NOW()) and type='MPESA' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$mpesa','$actual','MPESA','$session')");
  $l=mysqli_query("select sum(amount)as a from x_report where till='$till' and date=date(NOW()) and type='CHEQUES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,typ,session)
 values('$user',NOW(),NOW(),'$till','$cheque','$actual','CHEQUES','$session')");


 $y=mysqli_query("select * from cash_pick where  till='$till' and session='$session' and
 date=date(NOW())");*/

 
 
 //$f=mysqli_query("update x_report set status='NOT ACTIVE' where  date=date(NOW()) and status='ACTIVE' and till='$till'");
 $id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
                   
                
                   
                
                <tr><td ><h3><b><center></b></h3></td></tr>
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>Z-REPORT GENERATED BY '.$user.' FOR Till '.$till.' AT '.$time.'</center></td></tr>
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
<tr><th>
				<hr size="10" noshade> </hr> </th>
			
			
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							
				   <tr><td></td><th  style="font-size: 26px">Generation Time </th><td  style="font-size: 26px"> ' .$time . '</td>
				   </tr></table>
<table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
<tr><td><b>TYPE</td><td><b>SYSTEM</th><td><b>ACTUAL</td><td><b>VARIATION</td></tr>';
while($row=mysqli_fetch_array($y)){
echo '<tr>
<td  style="font-size: 26px">' . (($row['TYPE'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['actual'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['amountt'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['amountt']-$row['actual'])) . '</td></tr>';
$totali+=($row['amountt']-$row['actual']);
$actuali+=$row['amountt'];
$systemi+=$row['actual'];

}echo '<tr>
<td  style="font-size: 26px"><b>TOTAL</td>
<td  style="font-size: 26px"><b>' . number_format(($systemi)) . '</td>
<td  style="font-size: 26px"><b>' . number_format(($actuali)) . '</td>
<td  style="font-size: 26px"><b>' . number_format(($totali)) . '</td></tr>';
echo'<tr><th>
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th><th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>';
              echo'<tr><td> TOTAL VAT</td><td  style="font-size: 26px"><b>:' . number_format(vatZ($till)). '</td></tr>
<tr><td>TOTAL CUSTOMERS</td><td  style="font-size: 26px"><b>:' . number_format(CustomersTill($till)) . '</td></tr> 
<tr><th>
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th><th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>	  </table><br><br>
			   </div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 
 
}
function CXreportU($user,$till,$cash,$credit,$mpesa,$cheque,$session,$float,$picked,$petty,$u){
include('conf.php');

$r=mysqli_query("select * ,time(NOW())as f from user_audit where date=date(NOW())
 and user='$user'
 and till='$till'
 and status='ACTIVE' order by id desc limit 1");

 $row=mysqli_fetch_array($r);
 $time=$row['f'];
$gh=$time;
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$float','3000','FLOAT','$session','$u')");
  $l=mysqli_query("select sum(cash)as a from cpick where userr='$u' and date=date(NOW())");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$picked','$actual','CASH PICKED','$session','$u')");
   $l=mysqli_query("select sum(amount)as a from petty where userr='$u' and date=date(NOW())");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$petty','$actual','PETTY CASH','$session','$u')");
 $l=mysqli_query("select sum(amount)as a from x_report where user='$u' and till='$till'  
 and date=date(NOW()) and type='CASH' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
  $l=mysqli_query("select sum(amount)as a from x_report where user='$u' and till='$till' and date=date(NOW()) 
  and type='CREDIT NOTES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actua=$lol['a'];
 $actuall=$actual+$actua;
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$cash','$actuall','CASH','$session','$u')");
  $l=mysqli_query("select sum(amount)as a from x_report where user='$u' and till='$till'  and date=date(NOW()) 
  and type='CREDIT NOTES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$credit','$actual','CREDIT NOTES','$session','$u')");
  $l=mysqli_query("select sum(amount)as a from x_report where user='$u' and till='$till' and date=date(NOW()) and type='MPESA' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$mpesa','$actual','MPESA','$session','$u')");
  $l=mysqli_query("select sum(amount)as a from x_report where user='$u' and till='$till'  and date=date(NOW()) and type='CHEQUES' and status='ACTIVE'");

 $lol=mysqli_fetch_array($l);
 $actual=$lol['a'];
 $t=mysqli_query("insert into cash_pick(user,time,date,till,amountt,actual,type,session,userr)
 values('$user',NOW(),NOW(),'$till','$cheque','$actual','CHEQUES','$session','$u')");


 $y=mysqli_query("select * from cash_pick where  userr='$u' and till='$till'  and session='$session' and
 date=date(NOW())");

 
 
 $f=mysqli_query("update x_report set status='NOT ACTIVE' where  date=date(NOW()) and status='ACTIVE' and user='$u'");
 $id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
                   
                
                   
                
                <tr><td ><h3><b><center></b></h3></td></tr>
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>Z-REPORT GENERATED BY '.$user.' FOR CASHIER '.$u.' @ Till '.$till.' AT '.$time.'</center></td></tr>
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
<tr><th>
				<hr size="10" noshade> </hr> </th>
			
			
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							
				   <tr><td></td><th  style="font-size: 26px">Generation Time </th><td  style="font-size: 26px"> ' .$time . '</td>
				   </tr></table>
<table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
<tr><td><b>TYPE</td><td><b>SYSTEM</th><td><b>ACTUAL</td><td><b>VARIATION</td></tr>';
while($row=mysqli_fetch_array($y)){
echo '<tr>
<td  style="font-size: 26px">' . (($row['TYPE'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['actual'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['amountt'])) . '</td>
<td  style="font-size: 26px">' . number_format(($row['amountt']-$row['actual'])) . '</td></tr>';
$totali+=($row['amountt']-$row['actual']);
$actuali+=$row['amountt'];
$systemi+=$row['actual'];

}echo '<tr>
<td  style="font-size: 26px"><b>TOTAL</td>
<td  style="font-size: 26px"><b>' . number_format(($systemi)) . '</td>
<td  style="font-size: 26px"><b>' . number_format(($actuali)) . '</td>
<td  style="font-size: 26px"><b>' . number_format(($totali)) . '</td></tr>';
echo'<tr><th>
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th><th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>';
              echo'<tr><td> TOTAL VAT</td><td  style="font-size: 26px"><b>:' . number_format(vatZ($till)). '</td></tr>
<tr><td>TOTAL CUSTOMERS</td><td  style="font-size: 26px"><b>:' . number_format(CustomersTill($till)) . '</td></tr> 
<tr><th>
				<hr size="10" noshade>  </hr></th><th>
				<hr size="10" noshade> </hr> </th><th>
				<hr size="10" noshade>  </hr></th>
				<th>
				<hr size="10" noshade>  </hr></th>
				</tr>	  </table><br><br>
			   </div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 
 
}
function receiptCount(){
//include('conf.php');
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select distinct(number) from sales where month(date)=month(NOW())"));

$number=mysqli_num_rows($r);
return $number;
mysqli_close($dbc);
}
function buyingP($code){
//require_once('conf.php');
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from products where code='$code'"));

$row=mysqli_fetch_array($r);
$buying=$row['buying'];
return $buying;
mysqli_close($dbc);
}
function Vat($code){
//require_once('conf.php');
$r=mysqli_query($dbc,("select * from products where code='$code'"));

$row=mysqli_fetch_array($r);
$buying=$row['vat'];
return $buying;
mysqli_close($dbc);
}
function CreditNote($credit,$receipt){
include('mysql.php');
$s="select * from return_in where receipt='$receipt' and credit='$credit'";
$sql=mysqli_query($dbc,$s);
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                 
                   
                
                

				  <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
				  
									 <tr><td colspan="2"><center>CREDIT NOTE #:: '.$credit.'</center></td></tr>			 		
						
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th><th width="30"  style="font-size: 26px">PRICE
				</th><th align="right" width="200"  style="font-size: 26px">AMOUNT</th></tr>';
   while ($row = mysqli_fetch_array($sql)) {
        echo '<tr>
		<td  style="font-size: 26px">' . (strtoupper($row['product'])) . '</td>
		<td  style="font-size: 26px">' . (($row['qty'])) . '</td>
				<td  style="font-size: 26px">' . (number_format($row['price'])) . '</td>
		<td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr>';
        $total+=($row['total_p']);
		$qty=+$row['qty'];
		
    } echo'		<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td  style="font-size: 26px"><b>TOTAL</td>
		<td  style="font-size: 26px"><b>' . (($qty)) . '</td>
		<td align="right"  style="font-size: 26px"><b>' . number_format($total,2) . '</td></tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>  
 <div align="left"><b>CREDIT NOTE IS VALID FOR 90 DAYS ONLY FROM ' . (date("Y/m/d")) . '</h4></center></div>	
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}
function CredittNote($credit,$receipt){
include('mysql.php');
$s="select * from rett where cod='$receipt' and ret='$credit'";
$sql=mysqli_query($dbc,$s);
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
                
			
                     <tr><td ><h3><b>WATER WORLD IRRIGATION<center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX:8341-00200 NAIROBI TEL:0729004527/0737099989</center></td></tr>
				 <tr><td colspan="2"><center>PIN:P051346532B</center></td></tr>
			
									 <tr><td colspan="2"><center>CREDIT NOTE #:: '.$credit.'</center></td></tr>			 		
						
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th></th></tr>';
   while ($row = mysqli_fetch_array($sql)) {
        echo '<tr>
		<td  style="font-size: 26px">' . (strtoupper($row['product'])) . '</td>
		<td  style="font-size: 26px">' . (($row['qty'])) . '</td>
				</tr>';
       
		
    } echo'		<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td  style="font-size: 26px"><b>TOTAL</td>
		<td  style="font-size: 26px"><b>' . (($qty)) . '</td>
		</tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>  
 <div align="left"><b>CREDIT NOTE IS VALID FOR 90 DAYS ONLY FROM ' . (date("Y/m/d")) . '</h4></center></div>	
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}
function DebitNote($credit,$receipt,$supplier){
include('mysql.php');
$s="select * from return_out
inner join products on products.product_id=return_out.product_id where receipt='$receipt' and credit='$credit'";
$sql=mysqli_query($dbc,$s);
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
              
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
									 <tr><td colspan="2"><center>DEBIT NOTE #:: '.$credit.'</center></td></tr>			 		
									 <tr><td colspan="2"><center>SUPPLIER #:: '.$supplier.'</center></td></tr>	
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th><th align="right" width="200"  style="font-size: 26px">AMOUNT</th></tr>';
   while ($row = mysqli_fetch_array($sql)) {
        echo '<tr>
		<td  style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td  style="font-size: 26px">' . (($row['qty'])) . '</td>
		<td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr>';
        $total+=($row['total_p']);
		$qty=+$row['qty'];
		
    } echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td  style="font-size: 26px"><b>TOTAL</td>
		<td  style="font-size: 26px"><b>' . (($qty)) . '</td>
		<td align="right"  style="font-size: 26px"><b>' . number_format($total,2) . '</td></tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}
function SwaNote($credit){
include('mysql.php');
$s="select * from debtswap where number='$credit'";
$sql=mysqli_query($dbc,$s);
$row=mysqli_fetch_array($sql);
$amount=$row['amount'];
$supplier=$row['supplier'];
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
              
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
									 <tr><td colspan="2"><center>CREDIT NOTE #:: '.$credit.'</center></td></tr>			 		
									 <tr><td colspan="2"><center>SUPPLIER #:: '.Mode($supplier).'</center></td></tr>	
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">';
							 
 echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td  style="font-size: 26px"><b>TOTAL VALUE</td>
		<td  style="font-size: 26px"><b></td>
		<td align="right"  style="font-size: 26px"><b>' . number_format($amount,2) . '</td></tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
}
function Addexp($name){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));

$t=mysqli_query($dbc,("select * from expe where ex_nam='$name'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into expe(ex_nam)
values('$name')"));
 echo '<span class="green">Expense Name Succesfully Added</span></br>';

}
else{
 echo '<span class="red">Sorry Expense Name Already Exists</span></br>';}

mysqli_close($dbc);
}
function recexp($name,$amn,$date,$descri){
        $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));
$amn=mysqli_real_escape_string($dbc,($amn));
//$name=mysqli_real_escape_string($name);
$t=mysqli_query($dbc,("select * from expens where ex_id='$name' and descr='$descri' and amnt='$amn' and date='$date'"));
if(mysqli_num_rows($t)==0){
$y=mysqli_query($dbc,("insert into expens(ex_id,amnt,date,descr)
values('$name','$amn','$date','$descri')"));
 echo '<span class="green">Expense Succesfully Recorded</span></br>';

}
else{
 echo '<span class="red">Sorry Expense Already Exists</span></br>';}

}
function mpesaR($code,$amount,$receipt,$type){
	$user=$_SESSION['user'];
ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from sales where number='$receipt'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               <tr><td ><h3><b><center>LIKI TWO WINES & SPIRITS</center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Tel:0797326979/0797326363</center></td></tr>
				 <tr><td colspan="2"><center>Dealers In: All types of spirits & Wines</center></td></tr>
								 <tr><td colspan="2"><center>Located at Parkroad near Up-Rumy\'s the club</center></td></tr>				
	<tr><td><center>RECEIPT NO:# '.$receipt.'</tD></tr>									 
				 <tr><td ><center>Till: ' . $tiller . '</center></td></tr> 		
				 <tr><td ><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th><th align="right" width="200"  style="font-size: 26px">AMOUNT</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td  style="font-size: 26px">' . (strtoupper($row['p_name'])) . '
		</td><td></td><td  style="font-size: 26px" align="right">' . ($row['qty']) . ' X ' . number_format($row['price'],2) . '</td><tr></tr>
		<tr><td  style="font-size: 26px"></td>
		<td  style="font-size: 26px">
		</td><td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];

}
echo '</table><br><br>';
	echo '<table style="font-family:Bell Gothic,Verdana,Arial;">
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>

	
	 <tr><th style="font-size: 20px"><u>QTY</th></tr>
 	 <tr><td  style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td>
	</tr>
   <tr><td></td><th  style="font-size: 26px">TOTAL </th><td  style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">'.$type.' AMOUNT</th><td  style="font-size: 26px"> ' . number_format($amount, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">TRANSACTION CODE</th><td  style="font-size: 26px"> ' .($code) . '</td></tr>

	 	 <tr><td></td><th  style="font-size: 26px">CHANGE</th><td  style="font-size: 26px"> ' . number_format($amount-$total, 2) . '</td></tr>
	<tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td   style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$time.'</center></b></td></tr>
				<tr><th  style="font-size: 18px"><center>Inclusive Of VAT Where Applicable</center></th></tr>
				<tr><th  style="font-size: 24px"><center>MPESA TILL NUMBER:894972</center></th></tr>
				<tr><th   style="font-size: 26px"><center>Thankyou For Shopping With Us</center></th></tr>
			
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				<tr><td style="font-size: 17px"> <div align="center"><b>GOODS ONCE SOLD CANNOT BE RE-ACCEPTED<td></div></tr>
				
				
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();
 }
 function swap($code,$amount,$receipt){
	$user=$_SESSION['user'];
ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from sales where number='$receipt'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
									 <tr><td colspan="2"><center>#:' . receiptCount()  . '</center></td></tr>			 		
				 <tr><td colspan="2"><center>Till: ' . $tiller . '</center></td></tr> 		
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th><th align="right" width="200"  style="font-size: 26px">AMOUNT</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td  style="font-size: 26px">
		' . ($row['system']) . '</td><td></td><td  style="font-size: 26px" align="right">' . ($row['qty']) . ' X ' . number_format($row['price'],2) . '</td><tr></tr>
		<tr><td  style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td  style="font-size: 26px">
		</td><td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];

}
echo '</table><br><br>';
	echo '<table style="font-family:Bell Gothic,Verdana,Arial;">
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>

	
	 <tr><th style="font-size: 20px"><u>TOTAL QTY</th><th width="300" 
	 style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;<u>VATABLE AMT </th><th style="font-size: 20px"><u>
VAT </th></tr>
 	 <tr><td  style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td>
	 <td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 '.number_format(($total*84/100)) .'</td><td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;'.number_format($vat) .'</th></tr>
   <tr><td></td><th  style="font-size: 26px">TOTAL </th><td  style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">SWAP AMOUNT</th><td  style="font-size: 26px"> ' . number_format($amount, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">SWAP NUMBER</th><td  style="font-size: 26px"> ' .($code) . '</td></tr>

	 	 <tr><td></td><th  style="font-size: 26px">CHANGE</th><td  style="font-size: 26px"> ' . number_format($amount-$total, 2) . '</td></tr>
	<tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td   style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$time.'</center></td></tr>
				<tr><th   style="font-size: 26px"><center>Thankyou for shopping with us</center></th></tr>
				<tr><th>RECEIPT NO:# '.$receipt.'</th></tr>
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				<tr><td style="font-size: 17px"> <div align="center"><b>GOODS ONCE SOLD CANNOT BE RE-ACCEPTED<td></div></tr>
				
				
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();
 }
 function multiple($credit,$cheque,$cash,$receipt,$tt){
	$user=$_SESSION['user_name'];
ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from sales where number='$receipt'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
									 <tr><td colspan="2"><center>RECEIPT #:' . $receipt  . '</center></td></tr>			 		
				 <tr><td colspan="2"><center>Till: ' . $tiller . '</center></td></tr> 		
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"  style="font-size: 26px">ITEM</th><th width="30"  style="font-size: 26px">QTY
				</th><th align="right" width="200"  style="font-size: 26px">AMOUNT</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td  style="font-size: 26px">
		' . ($row['system']) . '</td><td></td><td  style="font-size: 26px" align="right">' . ($row['qty']) . ' X ' . number_format($row['price'],2) . '</td><tr></tr>
		<tr><td  style="font-size: 26px">' . (strtoupper($row['p_name'])) . '</td>
		<td  style="font-size: 26px">
		</td><td align="right"  style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];

}
echo '</table><br><br>';
	echo '<table style="font-family:Bell Gothic,Verdana,Arial;">
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>

	
	 <tr><th style="font-size: 20px"><u>TOTAL QTY</th><th width="300" 
	 style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;<u>VATABLE AMT </th><th style="font-size: 20px"><u>
VAT </th></tr>
 	 <tr><td  style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td>
	 <td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 '.number_format(($total*84/100)) .'</td><td  style="font-size: 26px">&nbsp;&nbsp;&nbsp;'.number_format($vat) .'</th></tr>
   <tr><td></td><th  style="font-size: 26px">TOTAL </th><td  style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">CASH </th><td  style="font-size: 26px"> ' . number_format($cash, 2) . '</td></tr>
   <tr><td></td><th  style="font-size: 26px">CREDIT NOTE</th><td  style="font-size: 26px"> ' . number_format($credit, 2) . '</td></tr>
    <tr><td></td><th  style="font-size: 26px">CHEQUE</th><td  style="font-size: 26px"> ' . number_format($cheque, 2) . '</td></tr>
	 <tr><td></td><th  style="font-size: 26px">TOTAL TENDERED</th><td  style="font-size: 26px"> ' . number_format($tt, 2) . '</td></tr>
	 	 <tr><td></td><th  style="font-size: 26px">CHANGE</th><td  style="font-size: 26px"> ' . number_format($tt-$total, 2) . '</td></tr>
	<tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td   style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$time.'</center></td></tr>
				<tr><th   style="font-size: 26px"><center>Thankyou for shopping with us</center></th></tr>
				
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				<tr><td style="font-size: 17px"> <div align="center"><b>GOODS ONCE SOLD CANNOT BE RE-ACCEPTED<td></div></tr>
				
				
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();
 }
 function Invoice($receipt,$cash,$change,$session,$user,$customer){
	$now = new DateTime();
    $now->setTimezone(new DateTimeZone('Africa/Nairobi'));
     //$now->format('H:i:s');
    $dates = date("Y/m/d");
      $vff=$now->format('Y-m-d H:i:s');
      $vgx=$now->format('H:i:s');

ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from sales where number='$receipt' and type='INVOICE' and session='$session'";
$sql=mysqli_query($dbc,$r);
	$l=mysqli_query($dbc,("select sum(debit)-sum(credit) as balance from ledgers where account_id='$customer'"));
	$rew=mysqli_fetch_array($l);
	$balance=$rew['balance'];
	
$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
                <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
				  
				  <tr><td colspan="2"   style="font-size: 26px"><center>CUSTOMER: '.Mode($customer).'</center></td></tr>	
									
									
<tr><td colspan="2"  style="font-size: 26px"><center>INVOICE NO:# '.$receipt.'</center></td></tr>									 
					
			
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				           <center> <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><center><th width="250"   style="font-size: 26px">ITEM</th><th width="20"  style="font-size: 26px">QTY
				</th><th align="right" width="150"   style="font-size: 26px">AMNT</th><th align="right" width="150"   style="font-size: 26px">TOTAL</th></center></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td   style="font-size: 26px">
		' . (strtoupper($row['p_name'])) . '</td><td   style="font-size: 26px" align="right">' . ($row['qty']) . ' <td   style="font-size: 26px" align="right">' . number_format($row['price'],2) . '</td><td align="right"   style="font-size: 26px">' . number_format($row['total_p'],2) . '</td><tr></tr>
		<tr><td   style="font-size: 26px"></td>
		<td   style="font-size: 26px">
		</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];
		$tot=$total+$balance;
    }echo '</table><br><br>';
	echo '<table style="font-family:Bell Gothic,Verdana,Arial;">
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>

	

   <tr><td></td><th   style="font-size: 26px">TOTAL </th><td   style="font-size: 26px"> ' . round($total) . '</td></tr>
    <tr><td></td><th   style="font-size: 26px">TOTAL OUTSTANDING BALANCE</th><td   style="font-size: 26px"> ' . $balance . '</td></tr>

   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td    style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$vgx.'</center></td></tr>
				<tr><th    style="font-size: 26px"><center>Thank you for shopping with us</center></th></tr>
				
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
					 <tr><th    style="font-size: 26px"><center></center></th></tr>
				 <tr><th    style="font-size: 26px"> <div align="left">Received By:.......................</div></th></tr>

				<tr><td   style="font-size: 26px"> <div align="left"><b>Signed By:.....................<td></div></tr>
				
				
        </div></table>  
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/>';

 session_regenerate_id();

}
function DepositReceipt($receipt,$cash,$change,$session,$user,$discount,$deposit){
	ini_set('display_errors', 0);

ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from sales where number='$receipt' and session='$session'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
            
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN:</center></td></tr>
								
<tr><td colspan="2"><center>RECEIPT NO:# '.$receipt.'</center></td></tr>									 
				 <tr><td colspan="2"><center>Till: ' . $tiller . '</center></td></tr> 		
				<tr><td  colspan="2"><center>' . gmdate("d-m-y h:i:s G") .
								'</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"   style="font-size: 26px">ITEM</th><th width="30"   style="font-size: 26px">QTY
				</th><th align="right" width="200"   style="font-size: 26px">AMOUNT</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td   style="font-size: 26px">
		' . (strtoupper($row['p_name'])) . '</td><td></td><td   style="font-size: 26px" align="right">' . ($row['qty']) . ' X ' . number_format($row['price'],2) . '</td><tr></tr>
		<tr><td   style="font-size: 26px"></td>
		<td   style="font-size: 26px">
		</td><td align="right"   style="font-size: 26px">' . number_format($row['total_p'],2) . '</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];
    }echo '</table><br><br>';
	echo ' <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
   <tr><td>
				<hr size="10"  noshade>  </td>
				</tr></table>';
				echo '<table  width="550" style="font-family:Bell Gothic,Verdana,Arial;">
 

	
	 <tr><th style="font-size: 20px"><u> QTY</th><th width="300" 
	 style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;<u>VATABLE AMT </th><th style="font-size: 20px"><u>
VAT </th></tr>
 	 <tr><td   style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td>
	 <td   style="font-size: 26px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	 '.number_format(($total-$vat)) .'</td><td   style="font-size: 26px">&nbsp;&nbsp;&nbsp;'.number_format($vat) .'</th></tr>
   <tr><td></td><th   style="font-size: 26px">TOTAL </th><td   style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">DISCOUNT </th><td   style="font-size: 26px"> ' . number_format($discount, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">LESS DISCOUNT</th><td   style="font-size: 26px"> ' . number_format($total-$discount, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">CASH DEPOSIT</th><td   style="font-size: 26px"> ' . number_format($deposit, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">CASH TOP UP</th><td   style="font-size: 26px"> ' . number_format($cash, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">TOTAL TENDERED</th><td   style="font-size: 26px"> ' . number_format($cash+$deposit, 2) . '</td></tr>
   <tr><td></td><th   style="font-size: 26px">CHANGE </th><td   style="font-size: 26px"> ' . number_format($change, 2) . '</td></tr>
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td    style="font-size: 26px"><center>You were served by:' . ($user) . ' at '.$time.'</center></td></tr>
				<tr><th    style="font-size: 26px"><center>Thank you for shopping with us</center></th></tr>
				
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				<tr><td style="font-size: 17px"> <div align="center"><b>GOODS ONCE SOLD CANNOT BE RE-ACCEPTED<td></div></tr>
				
				
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/>';

 session_regenerate_id();

}


function Recei($receipt,$cash,$change,$session,$user,$discount,$mpesa,$bank,$cust){
	//ini_set('display_errors', 0);
$now = new DateTime();
    $now->setTimezone(new DateTimeZone('Africa/Nairobi'));
     //$now->format('H:i:s');
    $dates = date("Y/m/d");
      $vff=$now->format('Y-m-d H:i:s');
      $vgx=$now->format('H:i:s');
ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
		$gy="select time(NOW())as tym from sales where number='$receipt' and session='$session'";
		$nj="select sum(total_p)as vr from sales where number='$receipt' and session='$session' and vat>0";
		    $bw=mysqli_query($dbc,$nj);
			       $ber=mysqli_fetch_array($bw);
				       $vb=$ber['vr'];
		  $lop=mysqli_query($dbc,$gy);
		    $oy=mysqli_fetch_array($lop);
			   $tym=$oy['tym'];
$r="select *,time(NOW()) as t from sales where number='$receipt' and session='$session'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                 
                
            
				     

				  <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
 <tr><td ><center>CUSTOMER NAME: '.$cust.'</center></td></tr>	
<tr><td ><center>RECEIPT NO:# '.$receipt.'</center></td></tr>	
	 <tr><th  style="font-size: 18px"><center></center></th></tr>
			
<tr><td ><center>' . date('m/d/Y', strtotime($tym)).
								'</center></td></tr>				 								 
				 
				<tr><td ><center>' . date('h:i:s', strtotime($vgx)).
								'</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                 <tr><th width="250" style="font-size: 18px">ITEM</th><th width="20" style="font-size: 18px">QTY
				</th><th align="right" width="150" style="font-size: 18px">PRICE</th><th align="right" width="150" style="font-size: 18px">TOTAL</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td style="font-size: 18px">
		' . (strtoupper($row['p_name'])) . '</td><td style="font-size: 18px" align="right">' . ($row['qty']) . ' <td style="font-size: 18px" align="right">' . round($row['price']) . '</td><td align="right" style="font-size: 18px">' . round($row['total_p']) . '</td><tr></tr>
		<tr><td style="font-size: 18px"></td>
		<td style="font-size: 18px">
		</td></tr></tr>';
        $total+=($row['total_p']);
		$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];
    }echo '</table><br><br>';
	echo ' <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
   <tr><td>
				<hr size="10"  noshade>  </td>
				</tr></table>';
				echo '<table  width="550" style="font-family:Bell Gothic,Verdana,Arial;">
 

	 <tr><th style="font-size: 20px"><u> QTY</th></tr>
 	 <tr><td style="font-size: 20px">&nbsp;&nbsp;'.$qty.'</td>
	 </tr>
   <tr><td></td><th style="font-size: 20px">TOTAL </th><td style="font-size: 20px"> ' . round($total) . '</td></tr>
  <tr><td></td><th style="font-size: 20px">MPESA </th><td style="font-size: 20px"> ' . ($mpesa) . '</td></tr>
  <tr><td></td><th style="font-size: 20px">PDQ</th><td style="font-size: 20px"> ' .($bank) . '</td></tr>
   <tr><td></td><th style="font-size: 20px">CASH </th><td style="font-size: 20px"> ' . ($cash) . '</td></tr>
   <tr><td></td><th style="font-size: 20px">CHANGE </th><td style="font-size: 20px"> ' . ($change) . '</td></tr>
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td  style="font-size: 22px"><center>You were served by:' . ($user) . ' at ' . date('h:i:s', strtotime($vgx)).
								'</center></td></tr>
				
				
				
				
        </div></table> <p style="page-break-after:always;"></p>
		

               
   
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/>';
}
function invoo($code,$client){
	ini_set('display_errors', 0);

ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select * from trans where cod='$code'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                 
                
          <tr><td ><h3><b><center>RESTON BEAUTY </center></b></h3></td></tr>
                 <tr><td colspan="2"><center>Shop A1</center></td></tr>
				 <tr><td colspan="2"><center>Africana Beauty Centre</center></td></tr>
				 <tr><td colspan="2"><center> Africana Hotel Building</center></td></tr>
				 <tr><td colspan="2"><center>Tel:07411466522</center></td></tr>
				 <tr><td colspan="2"><center>Paybill:400200</center></td></tr>
				  <tr><td colspan="2"><center>Acc:24887</center></td></tr>
									 <tr><td colspan="2"><center></center></td></tr>		
				  
<tr><td ><center>DELIVERY NO:# '.$code.'</center></td></tr>	
<tr><td ><center>DESTINATION:# '.$client.'</center></td></tr>									 
				 <tr><td ><center>Till: ' . $tiller . '</center></td></tr> 		
				<tr><td ><center>' . gmdate("d-m-y") .
								'</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
               <tr><th width="250"   style="font-size: 26px">ITEM</th><th width="20"   style="font-size: 26px">QTY
				</th><th align="right" width="150"   style="font-size: 26px">AMNT</th><th align="right" width="150"   style="font-size: 26px">TOTAL</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td   style="font-size: 26px">
		' . (strtoupper($row['p_name'])) . '</td><td style="font-size: 18px" align="right">' . ($row['qty']) . ' <td   style="font-size: 26px" align="right">' . number_format($row['price'],2) . '</td><td align="right"   style="font-size: 26px">' . number_format($row['tot'],2) . '</td><tr></tr>
		<tr><td style="font-size: 20px"></td>
		<td style="font-size: 20px">
		</td></tr></tr>';
        $total+=($row['tot']);
		//$vat+=$row['vat'];
		$qty+=$row['qty'];
		$time=$row['t'];
    }echo '</table><br><br>';
	echo ' <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
   <tr><td>
				<hr size="10"  noshade>  </td>
				</tr></table>';
				echo ' <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
   <tr><td>
				<hr size="10"  noshade>  </td>
				</tr></table>';
				echo '<table  width="550" style="font-family:Bell Gothic,Verdana,Arial;">
 

	
	 <tr><th style="font-size: 20px"><u> QTY</th></tr>
 	 <tr><td   style="font-size: 26px">&nbsp;&nbsp;'.$qty.'</td>
	</tr>
   <tr><td></td><th   style="font-size: 26px">TOTAL </th><td   style="font-size: 26px"> ' . number_format($total, 2) . '</td></tr>

	
	
 	
</tr>
   
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
              
				
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				  <tr><td    style="font-size: 26px"><center>You were served by:' . ($user) .'</center></td></tr>
				
				
        </div></table>  
				
				
        </div></table> 
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/>';
unset($_SESSION['invc']);

	//session_regenerate_id();

 session_regenerate_id();

}
function DamageP($receipt,$session,$user){
	ini_set('display_errors', 0);

ini_set('display_errors', 0);
include('mysql.php');
$ip=get_client_ip() ;
		$tiller=teller($ip);
$r="select *,time(NOW()) as t from damages
inner join products on products.product_id=damages.product_id where number='$receipt' and session='$session'";
$sql=mysqli_query($dbc,$r);

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
	
            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
            
				   <tr><td ><h3><b><center></b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
								
<tr><td colspan="2"><center> NO:# '.$receipt.'</center></td></tr>									 
				 <tr><td colspan="2"><center>Till: ' . $tiller . '</center></td></tr> 		
				<tr><td  colspan="2"><center>' . gmdate("d-m-y h:i:s G") .
								'</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">
							 
                <tr><th width="420"   style="font-size: 26px">ITEM</th><th width="30"   style="font-size: 26px">QTY
				</th></tr>
				';
    $total = 0;
	$vat=0;
      while ($row = mysqli_fetch_array($sql)) {
        echo '<tr><td   style="font-size: 26px">
		' . (strtoupper($row['p_name'])) . '</td><td></td><td   style="font-size: 26px" align="right">' . ($row['qty']) . '</td><tr></tr>
		<tr><td   style="font-size: 26px"></td>
		<td   style="font-size: 26px">
		</td></tr></tr>';
     
	
		$qty+=$row['qty'];
		$time=$row['t'];
    }echo '</table><br><br>';
	echo '<table style="font-family:Bell Gothic,Verdana,Arial;">
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>

	
	
   <tr><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade>  </th><th>
				<hr size="10"  noshade> </th><th>
				</tr>
    </table>
				 <table width="550" style="font-family:Bell Gothic,Verdana,Arial;">
                <tr><td    style="font-size: 26px"><center>Generated by:' . ($user) . ' at '.$time.'</center></td></tr>
				
				
				 <tr><th>
				<hr size="10"  noshade>  </th><th>
				
				</tr>
				 <tr><td><h4></h4></td></tr>
				
				
				
        </div></table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/>';

 session_regenerate_id();

}

function CashPick($session,$credit,$till,$user,$no){
include('mysql.php');

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
                <tr><td ><h3><b><center>ABC SHOP</b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX ,NAIROBI TEL:</center></td></tr>
				 <tr><td colspan="2"><center>PIN: PO51449945S</center></td></tr>
				  <tr><td colspan="2"><center><b><u>CASH PICK REPORT<b></u></center></td></tr>
									 <tr><td colspan="2"><center>NUMBER #:: '.$no.'</center></td></tr>			 		
						<tr><td colspan="2"><center>TILL #:: '.$till.'</center></td></tr>
						<tr><td colspan="2"><center>USER #:: '.$user.'</center></td></tr>
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">';
							 
                
 echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
	 echo '<tr>
		<td   style="font-size: 26px"><b>TOTAL</td>
		<td   style="font-size: 26px"><b>' . (number_format($credit)) . '</td>
		</tr>';
		echo '<tr>
		<td   style="font-size: 26px"><b>Cashier Sign</td>
		<td   style="font-size: 26px"><b>.........................................</td>
		</tr>';
		echo '<tr>
		<td   style="font-size: 26px"><b>Supervisor Sign</td>
		<td   style="font-size: 26px"><b>.........................................</td>
		</tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();
 }
 	function petty($session,$cash,$invoice,$supplier,$till,$user,$no){
include('mysql.php');

$id = "javascript:Print('stylized')";
    echo '<body onload="' . $id . '"><div id="stylized">
             <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;font-size: 25px">
               
                   <tr><td><center><img src="logo.jpg" width="550" height="100"/></center></td></tr>   
                   
                
                <tr><td ><h3><b><center>ABC SHOP</b></h3></td></tr>
                 <tr><td colspan="2"><center>P.O BOX 7109-00300,NAIROBI TEL:0723 224 270</center></td></tr>
				 <tr><td colspan="2"><center>PIN: </center></td></tr>
				  <tr><td colspan="2"><center><b><u>PETTY CASH REPORT<b></u></center></td></tr>
									 <tr><td colspan="2"><center>NUMBER #:: '.$no.'</center></td></tr>			 		
						<tr><td colspan="2"><center>TILL #:: '.$till.'</center></td></tr>
						<tr><td colspan="2"><center>USER #:: '.$user.'</center></td></tr>
				 <tr><td colspan="2"><center>Date:' . (date("Y/m/d")) . '</center></td></tr>
				 <tr><th>
				<hr size="10" noshade>  </th>
				</tr></table>
				            <table width="550" style="font-family:Bell Gothic ,Verdana,Arial;">';
							 
                
 echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
				 echo '<tr>
		<td   style="font-size: 26px"><b>SUPPLIER</td>
		<td   style="font-size: 26px"><b>' . ($supplier) . '</td>
		</tr>';
		 echo '<tr>
		<td   style="font-size: 26px"><b>INVOICE NO</td>
		<td   style="font-size: 26px"><b>' . (($invoice)) . '</td>
		</tr>';
	 echo '<tr>
		<td   style="font-size: 26px"><b>TOTAL</td>
		<td   style="font-size: 26px"><b>' . (number_format($cash)) . '</td>
		</tr>';
		echo '<tr>
		<td   style="font-size: 26px"><b>Cashier Sign</td>
		<td   style="font-size: 26px"><b>.........................................</td>
		</tr>';
		echo '<tr>
		<td   style="font-size: 26px"><b>Supervisor Sign</td>
		<td   style="font-size: 26px"><b>.........................................</td>
		</tr>';
		echo'<tr><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th><th>
				<hr size="10" noshade>  </th>
				</tr>';
		echo '</table>            
</div><input type="submit" name="btnPrint" value="print" asp:Button ID="btnPrint" runat="server" onclick="' . $id . '"/></body>';
 session_regenerate_id();
 }
function addpermission($user, $ugroup, $status, $permission) {
        $adperm = mysqli_query('insert into groupperm values("","' . 
		usergroupid($ugroup) . '","' . $permission. '","' . ($status) . '")') or die(mysql_error());
		}
		
		function AccountAdd($name, $code, $id)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$name = mysqli_real_escape_string($dbc,($name));
				$t = mysqli_query($dbc,("select * from accounts where name='$name'"));
				if (mysqli_num_rows($t) == 0)
				{
								$y = mysqli_query($dbc,("insert into accounts(name)
values('$name')"));
								$idd = mysqli_insert_id($dbc);
								$d = mysqli_query($dbc,("insert into group_accounts(group_id,account_id)values
('$id','$idd')"));
UserAudit($_SESSION['user_id'],"Added Ledger $name",date("Y/m/d"),$ip);
								echo '<span class="green">Account Succesfully Added</span></br>';
				} else
				{
								echo '<span class="red">Sorry Account Already Exists</span></br>';
				}
mysqli_close($dbc);
}
function IncomeRecordd($income, $customer, $date, $amount, $invoice, $desc, $department,
				$sub,$from)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$session = session_id();
				$desc = mysqli_real_escape_string($dbc,($desc));
				$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session,description,sub)values
				('$amount','$income','$date','$invoice',
'$customer','$session','$desc','$sub')"));
				//$f=mysqli_query("insert into customer_ledgers(customer_id,date,credit,receipt,session)values('$income','$date','$amount','$invoice','$session')");
				$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,description,sub)values
('$amount','$customer','$date','$invoice','$income','$session','$desc','$sub')"));
				$group = GetGroup($customer);
				if ($group == 10)
				{
								BankDebit($customer, $amount, $date, $invoice, $session);
				}
				echo "<span class='green'>Income Recording Succesfull </span></br>";
//$user = new Users();
				incomereceipt($user,$customer,$amount,$income,$from,$date,$session);
mysqli_close($dbc);
}
function billRecord($bill, $supplier, $date, $amount, $invoice, $maturity,$vat)
{
				$session = session_id();
				$account = $bill;
				    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
					TaxRecord(GetKra(),GetVat(),date("Y/m/d"),$vat,$invoice,$session);
				$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,
				session,mat_date,invoice)values('$amount','$supplier','$date','$invoice','$account','$session','$maturity','$invoice')"));

				$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,mat_date,invoice)values
				('$amount','$account','$date','$invoice','$supplier','$session','$maturity','$invoice')"));
				echo "<span class='green'>Bill Recording Succesfull </span></br>";
				//$f=mysqli_query("insert into supplier_ledgers(debit,supplier_id,date,code)values('$amount','$supplier','$date','$invoice')");
				session_regenerate_id();
				mysqli_close($dbc);
}
function TaxRecord($account, $supplier, $date, $amount,$invoice,$session)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
			
				$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,froms,session,mat_date,receipt,description)
				values('$amount','$supplier','$date','$account','$session','$maturity','$invoice','TAX ON GOODS')"));

				$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,froms,session,mat_date,
				receipt,description)values('$amount','$account','$date','$supplier','$session','$maturity','$invoice','TAX ON GOODS')"));
				
				//$f=mysqli_query("insert into supplier_ledgers(debit,supplier_id,date,code)values('$amount','$supplier','$date','$invoice')");
		mysqli_close($dbc);		
}

function TransferFunds($form, $to, $date, $cheque, $amount, $desc)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$session = session_id();
				$desc ='FUNDS TRANSFER';
				$q = mysqli_query($dbc,("INSERT into ledgers(date,credit,description,account_id,
				froms,session)VALUES('$date','$amount','$desc','$form','$to','$session')"));

				$r = mysqli_query($dbc,("INSERT into ledgers(date,debit,
  description,account_id,froms,session)VALUES('$date','$amount','$desc','$to','$form','$session')"));

				echo "<span class='green'>Funds Transfer Succesfull </span></br>";
				session_regenerate_id();
				mysqli_close($dbc);
}
function DoubleEntry($credit, $debit, $amount, $date, $desc,$sub)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$session = session_id();
				$desc = mysqli_real_escape_string($dbc,($desc));
				$q = mysqli_query($dbc,("INSERT into ledgers(date,credit,description,account_id,froms,session,sub)
				VALUES('$date','$amount','$desc','$credit','$debit','$session','$sub')"));

				$r = mysqli_query($dbc,("INSERT into ledgers(date,debit,
  description,account_id,froms,session,sub)VALUES('$date','$amount','$desc','$debit','$credit','$session','$sub')"));
				echo "<span class='green'>Double Entry Succesfull </span></br>";
				session_regenerate_id();
				mysqli_close($dbc);
}
function DoubleEntryM($credit, $debit, $amount, $date, $desc,$sub,$session,$cheque)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				
				$desc = mysqli_real_escape_string($dbc,($desc));
				$q = mysqli_query($dbc,("INSERT into ledgers(date,credit,description,account_id,froms,session,sub,receipt)
				VALUES('$date','$amount','$desc','$credit','$debit','$session','$sub','$cheque')"));

				$r = mysqli_query($dbc,("INSERT into ledgers(date,debit,
  description,account_id,froms,session,sub,receipt)VALUES('$date','$amount','$desc','$debit','$credit','$session','$sub','$cheque')"));
				echo "<span class='green'>Double Entry Transaction Succesfull </span></br>";
				//session_regenerate_id();
}
function RecordLedgerExp($credit, $payment, $date, $cheque, $amount, $desc, $department,
				$sub,$receipt,$vat)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$session = session_id();
				UserAudit($_SESSION['user_id'],"Recorded expenses via session $session",date("Y/m/d"),$ip);
				$q = mysqli_query($dbc,("INSERT into ledgers(date,debit,description,account_id,froms,session,receipt,sub)VALUES
 ('$date','$amount','$desc','$credit','$payment','$session','$cheque','$sub')"));

				$r = mysqli_query($dbc,("INSERT into ledgers(date,credit,
  description,account_id,froms,session,receipt,sub)VALUES('$date','$amount','$desc','$payment','$credit','$session','$cheque','$sub')"));
    $y=mysqli_query($dbc,("insert into expens(ex_id,amnt,date,descr)
values('$credit','$amount','$date','$desc')"));
  	if($vat==16){$totalv=0.16*($amount)/1.16;
	TaxRecord(GetKra(),GetVat(),date("Y/m/d"),$totalv,$receipt,$session);
	}
				$group = GetGroup($payment);
				if ($group == 10)
				{
								BankCredit($payment, $amount, $date, $cheque, $session);
				}
				echo "<span class='green'>Expenses Have Been Recorded  Succesfully </span></br>";
				session_regenerate_id();
				mysqli_close($dbc);
}
function BankCredit($bank, $amount, $date, $cheque, $session)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$r = mysqli_query($dbc,("insert into cash_book(date,credit,account_id,receipt,session)
values('$date','$amount','$bank','$cheque','$session')
"));
mysqli_close($dbc);
}
function BankDebit($bank, $amount, $date, $cheque, $session)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$r = mysqli_query($dbc,("insert into cash_book(date,debit,account_id,receipt,session)
values('$date','$amount','$bank','$cheque','$session')
"));

}
function CreateReconciliation($bank)
{
    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$v = mysqli_query($dbc,("update bank_statement set status='NOT ACTIVE' where account_id='$bank'"));

				$r = mysqli_query($dbc,("insert into bank_statement(debit,credit,receipt,date,account_id,t_date,type)( SELECT debit, credit,receipt,date,account_id,t_date,type
FROM (
SELECT  credit, debit,receipt,date,account_id,date(NOW()) as t_date,type
FROM bank_account
where account_id='$bank'
UNION ALL
SELECT credit, debit,receipt,date,account_id,date(NOW()) as t_date,type
FROM cash_book
where account_id='$bank'
)data
GROUP BY debit, credit,receipt
HAVING count( * ) !=2)"));

				$f = mysqli_query($dbc,("insert into cash_book(date,type,debit,receipt,account_id,t_date)(SELECT bank_account.date,bank_account.type,
 bank_account.debit, bank_account.receipt,bank_account.account_id,date(NOW())
FROM bank_statement, bank_account
WHERE bank_account.debit = bank_statement.debit
AND bank_statement.debit >0
AND bank_statement.account_id ='$bank'
AND bank_account.receipt=bank_statement.receipt)"));

				$f = mysqli_query($dbc,("insert into cash_book(date,type,credit,receipt,account_id,t_date)(SELECT bank_account.date,bank_account.type
 bank_account.credit, bank_account.receipt,bank_account.account_id,date(NOW())
FROM bank_statement, bank_account
WHERE bank_account.credit = bank_statement.credit
AND bank_statement.credit >0
AND bank_statement.account_id ='$bank'
AND bank_account.receipt=bank_statement.receipt)"));

				echo '<span class="green">Banking And Reconciliation Succesfully Done</span></br>';
mysqli_close($dbc);
}
function InputBank($date, $credit, $debit, $no, $session, $bank)

{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$h = mysqli_query($dbc,("select * from bank_account where number='$no' and date='$date' and credit='$credit' and debit='$debit'"));
				if (mysqli_num_rows($h) == 0)
				{
								$t = mysqli_query($dbc,("insert into bank_account(credit,debit,date,account_id,session,receipt)
values('$credit','$debit','$date','$bank','$session','$no')"));
								$id = mysqli_insert_id($dbc);
								if ($id)
								{
												echo '<span class="green">Data Entry Succesful</span></br>';
								} else
								{

												echo '<span class="red">Data Entry Not Succesful</span></br>';
								}

				} else
				{
								echo '<span class="red">Entry Already Exists !</span></br>';

				}
				mysqli_close($dbc);
}
function IncomePayment($customer, $payment, $date, $amount)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$session = session_id();
				$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session)values
				('$amount','$customer','$date','$invoice','$payment','$session')"));

				$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session)values
				('$amount','$payment','$date','$invoice','$customer','$session')"));
				echo "<span class='green'>Income Payment Succesfull </span></br>";
				$group = GetGroup($customer);
				if ($group == 10)
				{
								BankDebit($income, $amount, $date, $cheque, $session,$desc);
				}
				session_regenerate_id();
				mysqli_close($dbc);
}
function SPayment($id, $payment, $date, $amount, $session, $rec,$desc)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$ro = mysqli_query($dbc,("select id,sum(debit)as debit,sum(credit)as credit,sum(credit)-sum(debit)as balance,invoice from ledgers 
where account_id='$id'
group by invoice
having balance>0 order by invoice"));
				$customer = $id;
				$session = session_id();
				echo "<span class='green'>Supplier Payment Succesfull </span></br>";
				while ($row = mysqli_fetch_array($ro))
				{

								$credit = $row['credit'];

								$debit = $row['debit'];
								$balance = $row['balance'];
								$invoice = $row['invoice'];
								if ($amount > 0)
								{
												if ($balance > $amount)
												{
																$r = mysqli_query($dbc,("insert into
																ledgers(credit,account_id,date,receipt,froms,session,invoice,description)values
('$amount','$payment','$date','$rec','$customer','$session','$invoice','$desc')"));
																//$r=mysqli_query("insert into supplier_ledgers(credit,supplier_id,date,code,session)values('$amount','$payment','$date','$invoice','$session')");

																$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,invoice,description)values
																('$amount','$customer','$date','$rec','$payment','$session','$invoice','$desc')"));
																$amount = 0;
												}else
												if ($amount > $balance)
												{
											$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session,
																invoice,description)
																values('$balance','$payment','$date','$rec','$customer','$session','$invoice','$desc')"));
																//$r=mysqli_query("insert into supplier_ledgers(credit,supplier_id,date,code,session)values('$amount','$payment','$date','$invoice','$session')");

																$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,invoice,
																description)
																values('$balance','$customer','$date','$rec','$payment','$session','$invoice','$desc')"));
																$amount = $amount - $balance;
												}else
												if ($amount == $balance)
												{
																$r = mysqli_query($dbc,("insert into ledgers(credit,account_id,date,receipt,froms,session,invoice,
																description)values
																('$amount','$payment','$date','$rec','$customer','$session','$invoice','$desc')"));
																//$r=mysqli_query("insert into supplier_ledgers(credit,supplier_id,date,code,session)values('$amount','$payment','$date','$invoice','$session')");

																$r = mysqli_query($dbc,("insert into ledgers(debit,account_id,date,receipt,froms,session,invoice
																,description)values
																('$amount','$customer','$date','$rec','$payment','$session','$invoice','$desc')"));

																$amount = 0;
												}

								}

				}
				$group = GetGroup($payment);
				if ($group == 10)
				{
								BankCredit($payment, $amount, $date, $cheque, $session);
				}
				mysqli_close($dbc);
}
function Mode($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from accounts where id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['name'];
return $name;
mysqli_close($dbc);
}
function LoanDisbursement($account,$froms,$sub,$date,$amount,$session,$cheque){
$transaction=$_SESSION['vid'];
$l=mysqli_query("select * from voucher");
	$row=mysqli_fetch_array($l);
	$number=$row['number'];
	$tu=mysqli_query("update voucher set number=number+1");
$k=mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description,receipt,transaction)values
('$account','$froms','$sub','$date','$amount','$session','LOANS DISBURSEMENT','$cheque','$transaction')");
$r=mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description,receipt,transaction)values
('$froms','$account','$sub','$date','$amount','$session','LOANS DISBURSEMENT','$cheque','$transaction')");
$f=mysqli_query("insert into payments(date,member,amount,number,session)values('$date','$sub','$amount','$number','$session')");
BankCredit($froms, $amount, $date, $cheque, $session);
		echo "<span class='green'>Loan Disbursement Succesfull </span></br>";
		unset($_SESSION['t']);
		unset($_SESSION['vid']);
		session_regenerate_id();
		
		$url="payment.php?s=$session&d=$froms&t=LOAN DISBURSEMENT&c=$cheque&tr=$transaction";
		
ob_end_clean();
header("Location: $url");
}
function WithDisbursement($account,$froms,$sub,$date,$amount,$session,$cheque){
$transaction=gmdate("dmyhisG");


$r=mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description,receipt,transaction)values
('$account','$froms','$sub','$date','$amount','$session','MEMBER WITHDRAWAL','$cheque','$transaction')");
$r=mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description,receipt,transaction)values
('$froms','$account','$sub','$date','$amount','$session','MEMBER WITHDRAWAL','$cheque','$transaction')");

BankCredit($froms, $amount, $date, $cheque, $session);
		echo "<span class='green'>Payment Voucher  Succesfully Created </span></br>";
		unset($_SESSION['PROCESS']);
		
		session_regenerate_id();
		$url="payment.php?s=$session&d=$froms&t=MEMBER DEPOSITS REFUNDS&c=$cheque&tr=$transaction";
		
ob_end_clean();
header("Location: $url");
}
function GetLoan($id){
$r=mysqli_query("select * from initial_l where transid='$id'");
$row=mysqli_fetch_array($r);
$amount=$row['initial'];
return $amount;

}
function PrincipalPayment($account, $froms, $sub, $amount, $date, $session)
{
$date=date("Y/m/d");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','PRINCIPAL PAYMENT')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','PRINCIPAL  PAYMENT')");

}
function shutdown(){
$output=SHELL_EXEC('shutdown /s /t 00');
echo $output;
}
function RecoveryPayment($account, $froms, $sub, $amount, $date, $session)
{
$date=date("Y/m/d");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','LOANS RECOVERY')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','LOANS RECOVERY')");

}
function InvestmentPayment($account, $froms, $sub, $amount, $date, $session)
{
$date=date("Y/m/d");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','INVESTMENT PAYMENT')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','INVESTMENT  PAYMENT')");

}
//Credit Income Account ($froms)
//Debit Current Asset Account ($account)
function InterestPayment($account, $froms, $sub, $amount, $date, $session)
{
$date=date("Y/m/d");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','INTEREST PAYMENT')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','INTEREST PAYMENT')");

}
//Credit Income Account ($froms)
//Debit Current Asset Account ($account)
function InsurancePayment($account, $froms, $sub, $amount, $date, $session)
{
$date=date("Y/m/d");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','INSURANCE PAYMENT')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','INSURANCE PAYMENT')");

}
function GetSharesType($name){
$n=strtoupper($name);
$r=mysqli_query("select * from accounts where name='$n'");
if(mysqli_num_rows($r)==0){
$t=mysqli_query("insert into accounts(name,descc)values('$n','1')");
$id=mysqli_insert_id($dbc);
$f=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','2')");

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;



}
function InvestmentType($name){
$n=strtoupper($name);
$r=mysqli_query("select * from accounts where name='$n'");
if(mysqli_num_rows($r)==0){
$t=mysqli_query("insert into accounts(name,descc)values('$n','1')");
$id=mysqli_insert_id($dbc);
$f=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','13')");

}else{
$row=mysqli_fetch_array($r);
$id=$row['id'];


}
return $id;



}
//Credit Liability Account ($froms)
//Debit Current Asset Account ($account)
function SharesPayment($account, $froms, $sub, $amount, $date, $session,$name)
{
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','$name')");
				$r = mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','$name')");

}
function Acc($id)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$r = mysqli_query($dbc,("select * from accounts where id='$id'"));
				$row = mysqli_fetch_array($r);
				$cost = $row['name'];
				return $cost;

}
function Nosee($no)
{
				if ($no == 0)
				{
								$no = '-';
				}
				if ($no != 0)
				{
								$no = $no;
				}
				return $no;
}
function SubName($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from sub_accounts where id='$id'"));
$row=mysqli_fetch_array($r);
$name=$row['name'];
if($name==''){$name='-';}
return $name;
mysqli_close($dbc);
}
function GetPrincipal(){
$r=mysqli_query("select * from accounts where name='PRINCIPAL'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name,descc)values('PRINCIPAL','1')");
$id=mysqli_insert_id($dbc);
$e=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','5')");
$interest=$id;
}else{
$row=mysqli_fetch_array($r);
$interest=$row['id'];

}
return $interest;}
function GetInterest(){
$r=mysqli_query("select * from accounts where name='INTEREST'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name,descc)values('INTEREST','1')");
$id=mysqli_insert_id($dbc);
$e=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','3')");
$interest=$id;
}else{
$row=mysqli_fetch_array($r);
$interest=$row['id'];

}
return $interest;}
function GetOutstanding(){
$r=mysqli_query("select * from accounts where name='OUTSTANDING LOANS'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name,descc)values('OUTSTANDING LOANS','1')");
$id=mysqli_insert_id($dbc);
$e=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','4')");
$interest=$id;
}else{
$row=mysqli_fetch_array($r);
$interest=$row['id'];

}
return $interest;}
function GetInsurance(){
$r=mysqli_query("select * from accounts where name='RISK MANAGEMENT LOAN'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name,descc)values('RISK MANAGEMENT LOAN','1')");
$id=mysqli_insert_id($dbc);
$e=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','3')");
$interest=$id;
}else{
$row=mysqli_fetch_array($r);
$interest=$row['id'];

}
return $interest;}
function RiskPayment($account,$froms,$sub,$amount,$date,$session){
$r=mysqli_query("insert into ledgers(account_id,froms,sub,date,debit,session,description)values
('$account','$froms','$sub','$date','$amount','$session','INSURANCE PAYMENT')");
$r=mysqli_query("insert into ledgers(account_id,froms,sub,date,credit,session,description)values
('$froms','$account','$sub','$date','$amount','$session','INSURANCE PAYMENT')");

}
function GetInsurancee(){
$r=mysqli_query("select * from accounts where name='RISK MANAGEMENT  SHARES & BBF'");
if(mysqli_num_rows($r)==0){
$f=mysqli_query("insert into accounts(name,descc)values('RISK MANAGEMENT  SHARES & BBF','1')");
$id=mysqli_insert_id($dbc);
$e=mysqli_query("insert into group_accounts(account_id,group_id)values('$id','3')");
$interest=$id;
}else{
$row=mysqli_fetch_array($r);
$interest=$row['id'];

}
return $interest;}

function GetGroup($id)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$r = mysqli_query($dbc,("select * from group_accounts where account_id='$id'"));
				$row = mysqli_fetch_array($r);
				$id = $row['group_id'];
				return $id;
				mysqli_close($dbc);
}
function Legers($id)
{
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
				$r = mysqli_query($dbc,("select * from accounts where id='$id'"));
				$row = mysqli_fetch_array($r);
				$idd=$row['name'];
				return $idd;
				mysqli_close($dbc);
}
function InsertWith($mno,$date,$amount,$no){
$id = base64_encode($mno);
	$r=mysqli_query("update newmember set status='Tk9UIEFDVElWRQ==' where primarykey='$id'");
	$r=mysqli_query("update monthly_con set status='Tk9UIEFDVElWRQ==' where membernumber='$id'");
	
	$session=session_id();
	$_SESSION['PROCESS']=$session;
	$l=mysqli_query("select * from voucher");
	$row=mysqli_fetch_array($l);
	$number=$row['number'];
		$tu=mysqli_query("update voucher set number=number+1");
	$f=mysqli_query("insert into payments(date,member,amount,number,session)values('$date','$mno','$amount','$number','$session')");
	
	echo "<span class='green'>Withdrawal Succefully Done Please Proceed And Print Payment Voucher To Complete Transaction</span></br>";
}
function AddSub($name,$id,$idd){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$name=mysqli_real_escape_string($dbc,($name));

$r=mysqli_query($dbc,("select * from sub_accounts where name='$name' "));

if(mysqli_num_rows($r)==0){

$t=mysqli_query($dbc,("insert into sub_accounts(name,account)values('$name','$id')"));






 echo '<span class="green">Sub Account Succesfully Added</span></br>';}else{
 echo '<span class="red">Sub Account Exists</span></br>';}
}
function Permission($department,$type){
if(($type=='SALES')&&($department=='3'||$department=='1')){
$url='login.php';
                                        ob_end_clean();
										$_SESSION['msgg']='You Do Not Have Enough Priviliges To Access Those Resources';
header("Location: $url");
}
if($type=='BACK OFFICE'&&($department=='1'||$department=='2')){
$url='login.php';
                                        ob_end_clean();
										$_SESSION['msgg']='You Do Not Have Enough Priviliges To Access Those Resources';
header("Location: $url");
}
if($type=='ACCOUNTS'&&($department=='3'||$department=='2')){
$url='login.php';
                                        ob_end_clean();
										$_SESSION['msgg']='You Do Not Have Enough Priviliges To Access Those Resources';
header("Location: $url");
}
if($type=='ADMIN'&&($department=='3'||$department=='2'||$department=='1')){
$url='login.php';
$_SESSION['msgg']='You Do Not Have Enough Priviliges To Access Those Resources';
                                        ob_end_clean();
header("Location: $url");
}

}


function CloseYear($id){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from financial_year where id='$id' and status='ACTIVE'"));
$row=mysqli_fetch_array($r);
$account=GetEquity();
$dep_a=GetDep();
$date1=$row['start'];
$date2=$row['end'];
if($date1&&$date2){

 $de=mysqli_query($dbc,("select ledgers.account_id as account_id,sub,(datediff('$date2',ledgers.date)/365)*(dep/100*(sum(debit)-sum(credit))) as current from ledgers
inner join group_accounts on group_accounts.account_id=ledgers.account_id
inner join accounts on accounts.id=ledgers.account_id
where group_id='6'

group by ledgers.sub"));
while($row=mysqli_fetch_array($de)){

$dep=$row['current'];
$depid=$row['account_id'];
$sub=$row['sub'];
$namee=SubName($sub);
$l=mysqli_query($dbc,("insert into ledgers(account_id,froms,debit,description,date,session,sub)
values('$dep_a','$depid','$dep','ASSETS DEPRECIATION OF $namee',
'$date2',concat('$date1','$date2'),'$sub')"));
$n=mysqli_query($dbc,("insert into ledgers(account_id,froms,credit,description,date,session,sub)values
('$depid','$dep_a','$dep','ASSETS DEPRECIATION OF $namee','$date2',concat('$date1','$date2'),'$sub')"));
 }

$expenses=mysqli_query($dbc,("select  sum(debit)-sum(credit)as credit,ledgers.account_id as account_id from ledgers
inner join group_accounts on group_accounts.account_id=ledgers.account_id
where group_id='1'

and ledgers.date between '$date1' and '$date2'
group by ledgers.account_id
having credit>0
"));
while($rew=mysqli_fetch_array($expenses)){
$amount=$rew['credit'];
$account_id=$rew['account_id'];
$l=mysqli_query($dbc,("insert into ledgers(account_id,froms,debit,description,date,session)values('$account','$account_id','$amount','P & L',
'$date2',concat('$date1','$date2'))"));
$n=mysqli_query($dbc,("insert into ledgers(account_id,froms,credit,description,date,session)values
('$account_id','$account','$amount','P & L','$date2',concat('$date1','$date2'))"));
}
$cost=mysqli_query($dbc,("select  sum(debit)-sum(credit)as credit,ledgers.account_id as account_id from ledgers
inner join group_accounts on group_accounts.account_id=ledgers.account_id
where group_id='7'

and ledgers.date between '$date1' and '$date2'
group by ledgers.account_id
having credit>0"));
while($rew=mysqli_fetch_array($cost)){
$amount=$rew['credit'];
$account_id=$rew['account_id'];
$l=mysqli_query($dbc,("insert into ledgers(account_id,froms,debit,description,date,session)values
('$account','$account_id','$amount','P & L','$date2',concat('$date1','$date2'))"));
$n=mysqli_query($dbc,("insert into ledgers(account_id,froms,credit,description,date,session)values
('$account_id','$account','$amount','P & L','$date2',concat('$date1','$date2'))"));
}
$income=mysqli_query($dbc,("select  sum(credit)-sum(debit)as credit,ledgers.account_id as account_id from ledgers
inner join group_accounts on group_accounts.account_id=ledgers.account_id
where group_id='3'

and ledgers.date between '$date1' and '$date2'
group by ledgers.account_id
having credit>0"));
while($raw=mysqli_fetch_array($income)){
$amount=$raw['credit'];
$account_id=$raw['account_id'];
$li=mysqli_query($dbc,("insert into ledgers(account_id,froms,debit,description,date,session)values
('$account_id','$account','$amount','P & L','$date2',concat('$date1','$date2'))"));
$ni=mysqli_query($dbc,("insert into ledgers(account_id,froms,credit,description,date,session)values
('$account','$account_id','$amount','P & L','$date2',concat('$date1','$date2'))"));
}
$_SESSION['msg']='FINANCIAL YEAR CLOSURE SUCCESFUL';
$f=mysqli_query($dbc,("update financial_year set status='CLOSED' where id='$id'"));

}else{
$_SESSION['msg']='FINANCIAL YEAR CLOSURE NOT POSSIBLE';

}
mysqli_close($dbc);
}
function DateStatus($date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select * from financial_year where '$date' between start and end"));
$row=mysqli_fetch_array($r);
$status=$row['status'];
return $status;
mysqli_close($dbc);
}
function DebitP($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(debit)-sum(credit)as debit from ledgers where date<='$date'
and account_id='$id'"));
$row=mysqli_fetch_array($r);
$credit=$row['debit'];
return $credit;
mysqli_close($dbc);
}
function CreditP($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
$r=mysqli_query($dbc,("select sum(credit)-sum(debit)as debit from ledgers where date<='$date'
and account_id='$id'"));
$row=mysqli_fetch_array($r);
$credit=$row['debit'];
return $credit;
mysqli_close($dbc);
}
function FixedAssetsP($id,$date){
	    $dbc= mysqli_connect("localhost","vosmcyqy_af","2@RhUH7JY78@@@9823@KLULasd","vosmcyqy_afri");
  $c=mysqli_query($dbc,("select SUM(debit)-(datediff(NOW(),ledgers.date)/365)*(dep/100*SUM(debit)) as credit from ledgers



where account_id='$id'
and date<='$date'
group by ledgers.account_id
"));
$row=mysqli_fetch_array($c);
$credit=$row['credit'];
return $credit;
mysqli_close($dbc);
}