<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$parent_name='';
$ledger_name='';
$veh_num='';
$sdate_12 ='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$msg = '';

if(isset($_REQUEST['del']) && $_REQUEST['del']=='delete')
{
	$id = $_REQUEST['id'];
	$sql=mysql_query("delete from cf_ledger where ledgerid='$id'");
	header("location:account-ledger.php?msg=You Have Successfully Deleted");
}

if(isset($_REQUEST['addledger']))
{
if($_POST['addledger_name'])
{
$ledger_name=$_POST['addledger_name'];
}
else
{
$ledger_name='';
}
if($_POST['addparent_name'])
{
 $parent=$_POST['addparent_name'];
 }
else
{
$parent=0;
}
if($_POST['addopeningbal'])
{
 $openingbal=$_POST['addopeningbal'];
 }
else
{
$openingbal=0;
}

if($_POST['crdt'])
{
 $crdt=$_POST['crdt'];
}

if(!empty($_POST['pay_date']))
{
$paydate=date(('Y-m-d'),strtotime($_POST['pay_date'])); 
}
else
{
 $paydate=date('Y-m-d');	
}


mysql_query("insert into cf_ledger (ledgername,cr_dt,groupid,opening_balance,ledger_start_date) values ('$ledger_name','$crdt','$parent','$openingbal','$paydate')");

}
if(isset($_REQUEST['editledger']))
{
$edit_id=$_POST['hid_id'];
if($_POST['addledger_name'])
{
$ledger_name=$_POST['addledger_name'];
}
else
{
$ledger_name='';
}
if($_POST['addparent_name'])
{
 $parent=$_POST['addparent_name'];
 }
else
{
$parent=0;
}
if($_POST['addopeningbal'])
{
 $openingbal=$_POST['addopeningbal'];
 }
else
{
$openingbal=0;
}

if($_POST['oldopeningbal'])
{
 $oldopeningbal=$_POST['oldopeningbal'];
 }
else
{
$oldopeningbal=0;
}

if($_POST['crdt'])
{
 $crdt=$_POST['crdt'];
}

if(!empty($_POST['pay_date']))
{
$paydate=date(('Y-m-d'),strtotime($_POST['pay_date']));
}
else
{
 $paydate=date('Y-m-d');	
}


mysql_query("update  cf_ledger set ledgername='$ledger_name',cr_dt='$crdt',groupid='$parent',opening_balance='$openingbal',ledger_start_date='$paydate' where ledgerid=$edit_id");

// nithya // for vender
mysql_query("update  cf_acountspayable set `pendingamount`=`pendingamount`-$oldopeningbal where ledgerid=$edit_id");
mysql_query("update  cf_acountspayable set `pendingamount`=`pendingamount`+$openingbal where ledgerid=$edit_id");  
// nithya // fpr client
mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`-$oldopeningbal  where ledgerid=$edit_id");
mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+$openingbal  where ledgerid=$edit_id");  

}
if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
}
if(isset($_REQUEST['parent_name']) && $_REQUEST['parent_name']!=''){
$parent_name=$_REQUEST['parent_name'];
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OIL FILL</title>
<link href="css/styles.css" rel="stylesheet"  />
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<style>
.newcategoryProfile{ background:#FFFFFF; position:absolute; width:400px; height:250px; z-index:5;
					-webkit-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					-moz-box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);
					box-shadow: 0px 0px 35px 1px rgba(0,0,0,0.75);}


</style>
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>



<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />

<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script language="javascript">

$(document).ready(function(){
$('#fromdate').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd'
});
});

$(document).ready(function(){
$('#todate').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd'
});
});

$(document).ready(function(){
$('#pay_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});
function getXMLHTTP()
	 { //fuction to return the xml http object
	var xmlhttp=false;	
		try
		{
		xmlhttp=new XMLHttpRequest();
		}
		catch(e)	
		{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e)
			{
				try
				{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1)
				{
				xmlhttp=false;
				}
			}
		}
	return xmlhttp;
	}   


function Searchdate(val)
{ 
 	
	 var page=val;	
	 var ledger_name='';
	 var parent_name='';
	
	 	
	 if(document.ledg_frm.ledger_name){
	 ledger_name= document.ledg_frm.ledger_name.value;
	 }
	 
	 if(document.ledg_frm.parent_name){
	 parent_name= document.ledg_frm.parent_name.value;
	 }
	 
	
	
	var strURL="ajax/account_ledger_data.php?&page="+page;
	if(ledger_name!=''){
	strURL+="&ledger_name="+ledger_name;
	}
	
	if(parent_name!=''){
	strURL+="&parent_name="+parent_name;
	}
	
	
	
	//alert(strURL);
	var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
		 
			if ((req.readyState == 0)||(req.readyState == 1)||(req.readyState == 2)||(req.readyState == 3))
      		{
			//document.getElementById('order').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			//document.getElementById('Cakeorder').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			}
		 
		 
      if (req.readyState == 4)
      {
	 // only if "OK"
	 	if (req.status == 200)
	 	{ 
	   document.getElementById('list').innerHTML=req.responseText;
	//	document.getElementById('Cakeorder').innerHTML=req.responseText;
	 	}
	  else 
	 	{
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);

	 }
     }
     }
   req.open("GET", strURL, true);
   req.send(null);
   }
 
   
}
$(document).ready(function() {
			   
	$('a.login-window').click(function() {
		
		//Getting the variable's value from a link 
		var loginBox = $(this).attr('href');

		//Fade in the Popup
		$(loginBox).fadeIn(300);
		var id = $(this).find('input[type="hidden"]').val();
		
		add_account(id);
		//Set the center alignment padding + border see css style
		//var popMargTop = ($(loginBox).height() + 24) / 2; 
		//var popMargLeft = ($(loginBox).width() + 24) / 2; 
		var popMargTop = '202'; 
		var popMargLeft = '302'; 
		$(loginBox).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		// Add the mask to body
		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300);
		
		return false;
	});
	
	// When clicking on the button close or the mask layer the popup closed
	$('a.close, #mask').live('click', function() { 
	  $('#mask , .login-popup').fadeOut(300 , function() {
		$('#mask').remove();  
	}); 
	return false;
	});
});

function add_account(){
	
		$.ajax({
		method: 'POST',
		<!--url: 'http://192.168.1.43/FrameWorks/TravancoWebsite/TravancoAdmin/TravancoFinance/viewreceipt/'+empid,-->
		url:'http://www.aadhocc.com/oil_fill/ajax/addledger.php',
		success: function(msg){
			//alert(msg);
			if(msg){
				$("#view_emp").html(msg).show();
				$('#pay_date').datepicker({
					showOn: 'both',
					buttonImage: 'icons_date/calendar.gif',
					buttonImageOnly: true,
					changeMonth: true,
					changeYear: true,
					dateFormat: 'dd-mm-yy'
					});
			}
		}
		});
	}
	
// JavaScript Document
$(document).ready(function() {
						   
					   
	$(document).on('click','a.login-update',function() {
		
		//Getting the variable's value from a link 
		var loginBox = $(this).attr('href');

		//Fade in the Popup
		$(loginBox).fadeIn(300);
		var id = $(this).find('input[type="hidden"]').val();
		
		edit_account(id);
		//Set the center alignment padding + border see css style
		//var popMargTop = ($(loginBox).height() + 24) / 2; 
		//var popMargLeft = ($(loginBox).width() + 24) / 2; 
		var popMargTop = '202'; 
		var popMargLeft = '302'; 
		$(loginBox).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		// Add the mask to body
		$('body').append('<div id="mask"></div>');
		$('#mask').fadeIn(300);
		
		return false;
	});
	
	// When clicking on the button close or the mask layer the popup closed
	$('a.close, #mask').live('click', function() { 
	  $('#mask , .login-popup').fadeOut(300 , function() {
		$('#mask').remove();  
	}); 
	return false;
	});
});

function edit_account(id){

		$.ajax({
		method: 'POST',
		<!--url: 'http://192.168.1.43/FrameWorks/TravancoWebsite/TravancoAdmin/TravancoFinance/viewreceipt/'+empid,-->
		url:'http://www.aadhocc.com/oil_fill/ajax/addledger.php?editid='+id,
		success: function(msg){
			//alert(msg);
			if(msg){
				$("#view_emp").html(msg).show();
				$('#pay_date').datepicker({
					showOn: 'both',
					buttonImage: 'icons_date/calendar.gif',
					buttonImageOnly: true,
					changeMonth: true,
					changeYear: true,
					dateFormat: 'dd-mm-yy'
					});
			}
		}
		});
	}


</script>
</head>

<body>
<!------------------popup area (view, edit)--------------->



<!---------------popup area ends------------------>
<div class="viewEmployeePopup login-popup"  id="login-box" style="height: 220px;width:500px;
overflow-x: scroll;">
    <a href="#" class="close">
    <img src="images/close.png" class="btn_close" title="Close Window" alt="Close" />
    </a>
    <div id="view_emp"></div>
</div>
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 <div class="tabbedArea">
   
  <div class="tabBody">
   <h1>Ledger List</h1>
   <form name="ledg_frm" id="ledg_frm" method="post"  >
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px; ">
   <div align="left">
   <div style="padding:20px;">
    <div class="searchField">
     <p class="input1">Ledger Name</p>
     <input type="text" name="ledger_name" id="ledger_name" class="input1" value="<?=$ledger_name?>" />
    </div>
    
    
    <div class="searchField">
     <p class="input1">Parent group</p>
     <select name="parent_name" id="parent_name" class="input1" />
	 <option value="">Select one</option>
	 <?php
	 	$cf_drop="select * from cf_account_groups where status=1 ";
$cf_array=mysql_query($cf_drop);
if($cf_array && mysql_num_rows($cf_array)>0){

while($cf_res=mysql_fetch_array($cf_array))
{

?>
<option <?php if($cf_res['id']==$parent_name){?> selected="selected" <?php } ?>	value="<?=$cf_res['id']?>"><?=$cf_res['category']?></option>

<?php
}
	} 
	 ?>
	 </select>
    </div>
   
    
    <!--<div class="searchField">
     <p class="input1">Concerned Person</p>
     <input type="text" name="con_person" id="con_person" class="input1" />
    </div>
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">From Date</p>
     <input type="text" name="fromdate" id="fromdate" class="input1" />
    </div>
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">To Date</p>
     <input type="text" name="todate" id="todate" class="input1" />
    </div>-->
       
    <div class="searchField">
     <input name="button" type="button" class="input1" onClick="Searchdate(1);" value="search" />
    </div>
  </div>
   </div><div class="addnewgroup" align="right" style="cursor:pointer;"> <a href="#login-box" class="login-window" style="font-weight:bold;" data-type='id'>Add New Ledger</a></div>
   </div>
   </form>
   <div id="popup" align="center"  style="margin-left: 400px;"></div>
   <div id="list">
   <table  border="0" cellspacing="0" cellpadding="0" class="tableList">
  <tr class="heading">
    <td>No</td>    
    <td>Ledger Name</td>    
    <td>Group Name</td>
	<td>Opening Balance</td>
    <td>Edit</td>
   <!-- <td>Delete</td>-->
  </tr>
  
  <?php


/*
if(isset($_REQUEST['typename'])){
$typenames=$_REQUEST['typename'];
}*/

$i=1;

	
	   $sq_arr="SELECT a.*,b.category as parent
    FROM cf_ledger a
        LEFT JOIN cf_account_groups b
            ON b.id = a.groupid
    WHERE a.status = '1'";





$query=$sq_arr;

if(isset($_REQUEST['ledger_name']) && $_REQUEST['ledger_name']!=''){
$ledger_name=$_REQUEST['ledger_name'];
$sq_arr.="and   a.ledgername like '%".$ledger_name."%'";
}
if(isset($_REQUEST['parent_name']) && $_REQUEST['parent_name']!=''){
$parent_name=$_REQUEST['parent_name'];
$sq_arr.="and   a.groupid='$parent_name'";
}
$sq_arr.=" order by a.ledgerid asc ";

$function="Searchdate";
include("pagingserch.php");
 $sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;
while($res=mysql_fetch_array($array))
{

$pagecnt++;


  ?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
    <td><?php echo $res['ledgername'];?></td>
    <td><?php echo $res['parent'];?></td>
     <td><?php echo $res['opening_balance'];?></td>
   <td class="tr_td_b"> <a href="#login-box" class="login-update" style="font-weight:bold;" data-type=<?php echo $res['ledgerid'];?>><img src="images/edit.png" title="edit"><input type="hidden" name="acc_id" id="acc_id<?php echo $res['ledgerid'];?>" value="<?php echo $res['ledgerid'];?>" /></a></td>
   
   <!--<td class="tr_td_r" ><a href="account-ledger.php?del=delete&id=<?php// echo $res['ledgerid']; ?>" onClick="return confirm('Do you really want to delete this field?');"><img src="images/delete.png" title="delete"></a></td>-->
    
   
  </tr>
  <?php 
    }}
  ?>
 
  <!--<tr>
    <td><input type="checkbox">2</td>
    <td>2323d</td>
    <td>jinesh</td>
    <td>jose</td>
    <td>9443274636</td>
    <td>email@gmail.com</td>
    <td>design</td>
    <td>designer</td>
    <td>23/4/11</td>
    <td><div class="deletedStat"></div></td>
    <td><a href="#">
      <div class="actEdit"></div><div class="actDelete"></div>
    </a></td>
  </tr>-->

   
</table>

  <div align="right" style="padding-top:10px;"><?=$pagination?></div> 
  </div>
  </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<div class="quicLInksCover">
 <?php 
	include("leftlink.php");
?>

</div>
</body>
</html>
