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

if(isset($_REQUEST['addjournel']) && $_REQUEST['addjournel']!='')
{

$comments=$_POST['comments'];
$ref_no=$_POST['ref_no'];
$status=$_POST['status'];
$date=date('Y-m-d',strtotime($_POST['adddate']));
$cnt=$_POST['cnt'];
$current_date=date('Y-m-d');
	$jour_ref="select max(journal_reference) as journal_reference  from cf_journal";
   $jour_refqry=mysql_query($jour_ref);
   while($jour_refrow=mysql_fetch_array($jour_refqry))
   {
   $journal_reference=$jour_refrow['journal_reference'];
  
   }
   $journal_reference=$journal_reference+1;

for($i=1;$i<=$cnt;$i++)
{
$to=$_POST['to'.$i];
if($to=='To')
{
$acc_type = '1';
$credit=$_POST['received_to'.$i];
$debit='';
}
elseif($to=='By')
{
$acc_type = '2';
$credit='';
$debit=$_POST['received_to'.$i];
}
$amount=$_POST['ledamount'.$i];

if($_POST['received_to'.$i] != 0){
$crd_dbt=$_POST['received_to'.$i];
}

mysql_query("insert into cf_journal (`credit`,`debit`,`crd_dbt`,`amount`,`account_type`,`pay_date`,`add_date`,`purpose`,`ref_no`,`journal_reference`,`status`) values ('$credit','$debit','$crd_dbt','$amount','$acc_type','$date','$current_date','$comments','$ref_no','$journal_reference','$status') ");


// nithya	
//----------------------accounts receivable-----------------------//
if($acc_type == 1){
mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`-$amount where ledgerid=$crd_dbt"); 
}else{
mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`+$amount where ledgerid=$crd_dbt"); 
}
//----------------------accounts payable-----------------------//
if($acc_type == 1){
mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`+$amount where ledgerid= '$crd_dbt'");
}else{
mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`-$amount where ledgerid= '$crd_dbt'");
} 
//end
}


}
?>

<!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 src="js/trippopup.js" type="text/javascript"></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(){
$('#adddate').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 addmore_journel(numval){

	if (!$("input[name='to"+numval+"']:checked").val()) {
        alert("Select Ledger Type !");
        return false;
    }else if($("#ledjournel"+numval).val() == ""){
		alert("Select Ledger Name !");
        return false;
	}else if($("#ledamount"+numval).val() == ""){
		alert("Enter Ledger Amount !");
        return false;
	}else{
	//alert(numval);document.getElementById('img1').style.display="none";
	$('.imagenme').hide();
	
	var num=parseInt(numval)+parseInt(1);
	fields="<tr><td width='83' valign='middle'><p class='input1'><input type='radio' name='to"+num+"' id='to"+num+"' value='To' >Credit&nbsp;&nbsp;</td><td width='81' valign='middle'><input type='radio' name='to"+num+"' id='too"+num+"' value='By' >Debit</p> </td><td width='81' valign='middle'><p class='input1'>Ledger Name</p></td><td width='163' valign='middle'><span id='seljournel"+num+"'></span></td><td width='40' valign='middle'><p class='input1'>Amount</p></td><td valign='middle' align='right'><input type='text' class='datecls input1' name='ledamount"+num+"' id='ledamount"+num+"'  ></td><td width='15' align='left' valign='middle'><a id='img"+num+"' class='imagenme' style='cursor:hand;cursor:pointer;' onclick=\"addmore_journel('"+num+"')\"><img  src='images/addmore.jpg' border='0' title='Add More'/></a></td></tr><tr><td colspan='5'>&nbsp;</td></tr>";
	selectledgerfn(num);
	$( "#tabid" ).append(fields);
		document.getElementById('cnt').value=num; 
		
	}
	}
	
function selectledgerfn(num){
	$.ajax({
			method: 'POST',
			url: 'ajax/ajax_journal_view.php?number='+num,
			success: function(msg){
			
				document.getElementById('seljournel'+num).innerHTML=msg;
			}
		});
	}
	
		function amountvalidation(){
	var ledamount;
	var toSUM=0;var fromSUM=0;
	var to;
	var cnt=document.getElementById('cnt').value;
	for(i=1;i<=cnt;i++)
		{
	  		 if(document.getElementById('to'+i).checked==true){
			  to=document.getElementById('to'+i).value;
			  }
			   if(document.getElementById('too'+i).checked==true){
			  to=document.getElementById('too'+i).value;
			  }
			  ledamount=document.getElementById('ledamount'+i).value;
			  if(to=="To"){
			   toSUM=parseInt(toSUM)+parseInt(ledamount);
			  }else{
			  if(to=="By"){
			   fromSUM=parseInt(fromSUM)+parseInt(ledamount);
			  }
			  }
		} 
		document.getElementById('trcredit').innerHTML="Total Credit: "+toSUM;
		document.getElementById('trdebit').innerHTML="Total Debit: "+fromSUM;
		if($("#adddate").val() == ""){
			alert('Select The Date');
			$("#adddate").focus();
			return false;
		}
		for(i=1;i<=cnt;i++)
		{
	  		 if (!$("input[name='to"+i+"']:checked").val()) {
				alert("Select Ledger Type !");
				return false;
			}else if($("#ledjournel"+i).val() == ""){
				alert("Select Ledger Name !");
				return false;
			}else if($("#ledamount"+i).val() == ""){
				alert("Enter Ledger Amount !");
				return false;
			}
		} 
		/*if($("#comments").val() == ""){
				alert("Enter Comments !");
				$("#comments").focus();
				return false;
		}*/
		if(confirm('Have You Submit this page Now?')){
		
			if(toSUM!=fromSUM){
			alert('The Dr. Amount And Cr. Amount Not Equal !');
			return false;
			}
		}else{
		return false;
		}
	}
</script>
</head>

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



<!---------------popup area ends------------------>
<div class="viewEmployeePopup login-popup"  id="login-box" style="height: 150px;width:500px;
overflow-x: scroll;">
    <a href="#" class="close">
    <img src="" 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>Add Journal</h1>
   <div class="detailDiv"  style="width:750px;margin-left:250px; margin-bottom:20px; border:1px dotted #999; padding:25px;">
	<form action="" method="post">
	<table width="744" cellpadding="0" >
	<tr><td colspan="4"><table width="100%" id="tabid">
	<tr>
		<td colspan="5" align="left" valign="top" ><p class="input1" style="color:#FF0000"><strong>You should enter the total amounts equal in both to and by</strong></p> </td>
		<td width="197" align="right" valign="top" >
		<p class="input1">Date
		  <input type="text" name="adddate" id="adddate" class="datecls input1"/>
		</p>			  </td><td colspan="2" align="right" valign="top">&nbsp;</td>
	</tr>
	<tr><td colspan="6"><input type='hidden' name='cnt' id='cnt' value='1'>&nbsp;</td></tr>
	
	<tr><td width="83" valign="middle"><p class='input1'><input type='radio' name='to1' id='to1' value='To' >Credit&nbsp;&nbsp;</p> </td>
	  <td width="81" valign="middle"><span class="input1">
	    <input type='radio' name='to1' id='too1' value='By' >
Debit</span></td>
	  <td width="81" valign="middle"><p class='input1'>Ledger Name</p></td><td width="163" valign="middle">
	<select class="chzn-select restselect input1 received_to" name="received_to1" required="required">
     <option value="">Select Ledger</option>
       <?php 
         $sq_tripno="select * from cf_ledger where ledgertype != 'bankledger' and ledgerid != '195'";
   $arrtripno1=mysql_query($sq_tripno);
   while($restripno=mysql_fetch_array($arrtripno1))
   {
  ?>
   <option  value="<?=$restripno['ledgerid']?>" ><?=$restripno['ledgername']?></option>
        <?php
    }
  ?>
     </select>
		  
	
		  
		
	<!--<input type='text' class='datecls' name='ledjournel1' id='ledjournel1'  > --></td>
	<td width="40" valign="middle"><p class='input1'>Amount</p></td><td valign='middle' align="right"><input type='text' class='datecls input1' name='ledamount1' id='ledamount1'   ></td>
	<td width="15" align="left" valign='middle'>
	<a id='img1' style='cursor:hand;cursor:pointer;' onClick="addmore_journel('1')"></a></td>
	<td width="41" align="left" valign='middle'><a id='img1' class="imagenme" style='cursor:hand;cursor:pointer;' onClick="addmore_journel('1')"><img src="images/addmore.jpg" /></a></td>
	</tr>
	<tr><td colspan="6">&nbsp;</td></tr>
	</table></td></tr>
	<tr><td width="101"><p class="input1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status
	</p></td>
	  <td  align="left" style="width:250px;"><p class="input1">
	    <select name="status" class="input1">
	      <option value="1">Active</option>
	      <option value="0">Not Active</option>
	      </select>
	  </td>
	  <td  align="right" style="width:250px;"><span class="input1">Reference No:</span></td>
	  <td  align="left" style="width:250px;"><input type="text" name="ref_no" id="ref_no" class="input1" value="" /></td>
	  <td width="61" align="left"><p class="input1">&nbsp;</p></td>
	  </tr>
	<tr><td width="101" style="vertical-align:top;"><p class="input1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Comments
	</p></td>
	  <td width="633" colspan="3" align="left"><p class="input1">
	   <textarea class="input1 input3" name="comments" id="comments"></textarea>
	  </p></td>
	</tr>
	<tr><td colspan="4"><table width="100%" border="0">
  <tr><td width="68%"><p class='input1' id="trcredit"></p></td><td width="32%"><p class='input1' id="trdebit"></p></td></tr>
</table>
</td></tr>
	<tr><td colspan="4"><input type="submit" name="addjournel" value="Submit" class="input1" onClick="return amountvalidation();"/>
	<a href="journal-form.php"><input type="button" id="cancelnew" name="addjournel" value="Cancel" class="input1" /></a></td></tr>
	</table>
	</form>
	</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>
