<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$tyrenum_veh='';

include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();

//$tablename='cf_client';
$todaydate = date('Y-m-d H:i:s');
		if(isset($_POST['purchasesubmit'])){
			
			
			if(!empty($_POST['clientid']))
			{
				$clientid=addslashes($_POST['clientid']);
			}
			else
			{
				$clientid=addslashes($_POST['vendorid']);
			}
			
			if(isset($_POST['num_jar'])){
			$numjar=addslashes($_POST['num_jar']);
			}	
			
			if(isset($_POST['amount'])){
			$amount=addslashes($_POST['amount']);
			}
			
			if(isset($_POST['jar_purchase'])){
			$jar_purchase=addslashes($_POST['jar_purchase']);
			}
			
			if(isset($_POST['purchase_date'])){
			$purchase_date = date(('Y-m-d'),strtotime($_POST['purchase_date']));
			}
			
		
			$sql2 = mysql_query("insert into cf_jar_purchase (jar_purchase_type,jar_clientid,jar_no,jar_amount,date) values ('$jar_purchase','$clientid','$numjar','$amount','$purchase_date')");
			
			$purid = mysql_insert_id();
			
			if($jar_purchase == 'purchase' || $jar_purchase == 'purchasereturn'){
			mysql_query("update cf_stock set `stock_jar`=`stock_jar`+$numjar");
				
			
			}else if ($jar_purchase == 'jarpurchasereturn'){
			mysql_query("update cf_stock set `stock_jar`=`stock_jar`-$numjar");
			
			}
			
			 $acc_ref=mysql_query("select max(account_reference) as account_reference  from cf_purchase_account");
			 $acc_refrow=mysql_fetch_array($acc_ref);
			 if(mysql_num_rows($acc_ref) >0)
			 {
			 	$acc_reference=$acc_refrow['account_reference'];
			 }
			 else
			 {
			 	$acc_reference=0;
			 }
			
			if($jar_purchase == 'purchase'){
				mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`+$amount where vendorid= '$clientid'"); 
				
				//----Jar Purchase------------------- 
				$reselleres = mysql_query("select * from cf_ledger where ledgertype = 'vendorledger' and empid = '$clientid'");
		    	$resellrw = mysql_fetch_array($reselleres);
		    	$ledgerids = $resellrw['ledgerid'];
				
				 $acc_reference=$acc_reference+1;
				
				 $sql31 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','2','999','$purchase_date','$todaydate','$acc_reference')");
		   
		   
		   $sql32 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','1','$ledgerids','$purchase_date','$todaydate','$acc_reference')");	
		   
		  		 //----End Jar Purchase------------------- 
			}
			else if($jar_purchase == 'purchasereturn'){
				 mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`-$amount where clientid=$clientid");  
				
				//----Jar Sales Return------------------- 
				 
				$reclientres = mysql_query("select * from cf_ledger where ledgertype = 'clientledger' and empid = '$clientid'");
		    	$clientrw = mysql_fetch_array($reclientres);
		    	$ledgerids = $clientrw['ledgerid'];
				
				$acc_reference=$acc_reference+1;
				$sql31 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','2','1004','$purchase_date','$todaydate','$acc_reference')");
			
			$sql32 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','1','$ledgerids','$purchase_date','$todaydate','$acc_reference')");
			
			  //----End Jar Sales Return------------------- 
			
			}
			else if($jar_purchase == 'jarpurchasereturn'){
				mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`-$amount where vendorid= '$clientid'"); 
				
				//----Jar Purchase Return------------------- 
				
				$reselleres = mysql_query("select * from cf_ledger where ledgertype = 'vendorledger' and empid = '$clientid'");
		    	$resellrw = mysql_fetch_array($reselleres);
		    	$ledgerids = $resellrw['ledgerid'];
				
				$acc_reference=$acc_reference+1;
				$sql31 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','1','1002','$purchase_date','$todaydate','$acc_reference')");
			
				$sql32 = mysql_query("insert into cf_purchase_account (purchaseid,acc_amount,account_type,ven_client_id,pay_date,add_date,account_reference) values ('$purid','$amount','2','$ledgerids','$purchase_date','$todaydate','$acc_reference')");
			 
			  //----End Jar Purchase Return------------------- 	
				
			}
			
			//mysql_query("update cf_stock set `stock_jar`=`stock_jar`+$numjar");
			/// nithya
			/*if(!empty($_POST['clientid']))
			{
				mysql_query("update  cf_accountsreceivable set `receiveamount`=`receiveamount`-$amount where clientid=$clientid"); 
			}
			else
			{
				mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`+$amount where vendorid= '$clientid'"); 
			}*/
			
			 
			
			// end//
			header("location:jar_purchase_list.php");	
					
			}
			
				$clientids='';
				$jar_no='';
				$jar_amount='';
			
			if(isset($_REQUEST['jar_mainid'])){
			
				$id=$_REQUEST['jar_mainid'];
				$tablename='cf_jar_purchase';
				$sqlObj->tablename="cf_jar_purchase";
				$condition=" where jar_id='$id'";
				$sqlObj->condition=$condition;
				$sqlObj->fieldvalue="*";
				$fieldvalue=$sqlObj->fieldvalue;
				$sql=$sqlObj->SelectTable();
				$clientids = $sql['jar_clientid'];
				$jar_no = $sql['jar_no'];
				$jar_amount = $sql['jar_amount'];
				$date = $sql['date'];
				}
				
				if(isset($_POST['clientedit'])){
				
					if(isset($_POST['clientid'])){
					$clientid=addslashes($_POST['clientid']);
					}
					
					if(isset($_POST['num_jar'])){
					$numjar=addslashes($_POST['num_jar']);
					}	
					
					if(isset($_POST['amount'])){
					$amount=addslashes($_POST['amount']);
					}			
					
					$sql2 = mysql_query("update cf_jar_purchase set jar_clientid='$clientid',jar_no='$numjar',jar_amount='$amount' where jar_id='$id'");
				
					header("location:jar_purchase_list.php");	
				
				}	
			

?>
<!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 type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<!--<script src="js/driverdrop.js"></script>-->
<script src="js/addmoreclient.js"></script>
<script type="text/javascript" src="scripts_date/jquery.js"></script>
<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script>$(document).ready(function(){
$('#purchase_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});
function change_dropdown(val){
   if (val == "purchase") {
    document.getElementById('clientid').style.display = 'none';
            document.getElementById('vendorid').style.display = 'block';          
        } 
        else if (val == "jarreturn") {
		  document.getElementById('clientid').style.display = 'block';
			 document.getElementById('vendorid').style.display = 'none';
        }
		 else if (val == "jarpurchasereturn") {
		   document.getElementById('clientid').style.display = 'none';
            document.getElementById('vendorid').style.display = 'block';
        }  
		
		   

//alert(val);
}
</script>

<script type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

</script>



<style>
.lableentrytext{
font-size: 12px;
color: #505050;
clear: both;
margin-bottom: 5px;
text-transform: capitalize;
}

body {
	padding:0px;
	margin:0px;
	font-family:Arial, Helvetica, sans-serif;
	color:#000000;
}
.div01 {
	width:80%;
		margin:0 70px;
	padding-bottom:15px;
}
.ptForm {
	/*border:1px solid #000000;*/
	width:100%;
	margin:0 auto;
	padding:4px;
}
.ptText01 {
	font-size:20px;
	font-weight:bold;
}
.ptText02 {
	font-size:12px;
}
.ptText03 {
	font-size:16px;
	font-weight:bold;
}
.ptText04 {
	font-size:16px;
	font-weight:bold;
	border-left:solid 1px #000000;
	border-right:solid 1px #000000;
	border-top:solid 1px #000000;
}
.ptText05 {
	font-size:14px;
	font-weight:bold;
}
.field01 {
	border-bottom:#333333 dotted 1px;
	border-left:none;
	border-top:none;
	border-right:none;
	color:#000000;
	font-size:12px;
	font-weight:bold;
	background-color:#f1f1f1;
}
.field02 {
	border:none;
	color:#000000;
	font-size:12px;
	font-weight:bold;
	background-color:#f1f1f1;
}
.ptText06 {
	font-size:12px;
	font-weight:bold;
}
.rightTd {
	border-bottom:solid 1px #000000;
}
.leftTd {
	border-bottom:solid 1px #000000;
	border-right:solid 1px #000000;
	padding:4px;
}
.tdItem {
	border-bottom:solid 1px #000000;
	border-right:solid 1px #000000;
	padding:4px;
}
.rightTd {
	border-bottom:solid 1px #000000;
	padding:4px;
}
/*.rightTdBtm {
	border:none;
	padding:4px;
}
.leftTdBtm {
	border-right:solid 1px #000000;
	padding:4px;
}*/
.tdItemBtm {
	
	padding:4px;
}

#adv{

	display:none;
	
}

</style>



</head>

<body onLoad="">
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 
<div class="tabbedArea">
<?php
/*if(!empty($_REQUEST['tripid']))
{ 
	$tripid1 = $_REQUEST['tripid'];
	$arrtrip1=mysql_query("select * from cf_tripsheetsub where trip_id = '$tripid1'");
	$cnttrip1=mysql_num_rows($arrtrip1);
	if($cnttrip1>0){
		$div = $cnttrip1;
	}else{
		$div=1;
	}
}else{
		$div=1;
}*/
	$div=1;	
	?>
    <input type="hidden" id="divcnt" name="divcnt" value="<?=$div?>"/>
  <div class="tabBody">
  <?php 
		if(empty($_REQUEST['clientsubid']))
		{ ?>
		  <h1>Add Jar Purchase</h1>
		  <?php } else { ?>
		  <h1>Edit Purchase jar <?php }
  ?>
   
   </h1>
		  <div class="div01">
  <div class="ptForm">
  <form name="clientform" id="clientform" method="post" >
    <table width="100%" border="0" cellspacing="5" cellpadding="2"  style="border:1px solid #000000; padding:4px;">
      <input type="hidden" name="totcnt" id="totcntw" value="<?=$div?>">
      <tr>
        <td  colspan="5"  class="ptText02" style="width:70%;"><div id="list">
          <table width="100%"  cellspacing="0" cellpadding="0"   class="List" align="center" style="border-top:solid 1px #000000; border-bottom:none; border-left:solid 1px #000000; border-right:solid 1px #000000;" >
            
			<tr>
              <td class="tdItem"></td>
              <td class="tdItem" >
			 	<input type="radio" name="jar_purchase" value="purchase" onChange="change_dropdown('purchase')">
			 	Jar Purchase
				<input type="radio" name="jar_purchase" value="purchasereturn" onChange="change_dropdown('jarreturn')">
				Jar Sales Return  
				<input type="radio" name="jar_purchase" value="jarpurchasereturn" onChange="change_dropdown('jarpurchasereturn')">
				Jar Purchase Return        
                </td>
            </tr>
            <tr>
              <td class="tdItem">Client</td>
              <td class="tdItem">
			  <select class="input1" name="clientid" id="clientid"  style="display:none;">
                  <option value="">Select Client</option>
                  <?php 
					$sq_client="select * from cf_client";
					$arrclient=mysql_query($sq_client);
					while($resclt=mysql_fetch_array($arrclient))
					{
					?>
                  <option value="<?=$resclt['client_id']?>" <? if($resclt['client_id']==$clientids) {?>selected<? }?>   >
                    <?=$resclt['client_name']?>
                    </option>
                  <?php
					}
	 				?>
                </select>   
				  
				<select class="input1" name="vendorid" id="vendorid" >
                  <option value="">Select Vendor</option>
                  <?php 
					$sq_vendor="select * from cf_reseller";
					$arrvendor=mysql_query($sq_vendor);
					while($resven=mysql_fetch_array($arrvendor))
					{
					?>
                  <option value="<?=$resven['reseller_id']?>" >
                    <?=$resven['res_name']?>
                    </option>
                  <?php
					}
	 				?>
                </select>         
				    
                </td>
            </tr>
            
 			<tr>
              <td class="tdItem">No of Jar </td>
              <td class="tdItem">
              <input type="text" name="num_jar" id="num_jar" class="input1" style="width:75px;" value="<?=$jar_no?>" ></td>
            </tr>
            
            <tr>
              <td class="tdItem">Amount</td>
              <td class="tdItem">
                <input type="text" name="amount" id="amount" class="input1" style="width:75px;" value="<?=$jar_amount?>" >
                </td>
            </tr>
			
			 <tr>
              <td class="tdItem">Date</td>
              <td class="tdItem">
                <input type="text" name="purchase_date" id="purchase_date" style="width:100px;" maxlength="100" class="input1" value="" />
                </td>
            </tr>
            	
          </table>
        </div></td>
      </tr>
        
            
            <tr>
            	<td width="825">
                <?php 
                 	if(empty($_REQUEST['jar_mainid']))
					{ ?>
					  <input type="submit" name="purchasesubmit" value="submit" class="submit-btn" onClick="return validation()"  >
                <?php } else { ?>
                	  <input type="submit" name="clientedit" value="edit" class="submit-btn" onClick="return validation()"  >
                <?php } ?>                </td>
            </tr>    
    </table>
    
    </form>
    
  </div>
  <div style="text-align:center;">
    
  </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>


		






