<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{

	$saudaId       = isset($_REQUEST['saudaId']) && $_REQUEST['saudaId'] != '' ? $_REQUEST['saudaId'] : 0;
	$partyId       = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	$particular    = isset($_REQUEST['particular']) && $_REQUEST['particular'] != '' ? $_REQUEST['particular'] : "";
	$fine          = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0;
	$rojTransType  = isset($_REQUEST['rojTransType']) && $_REQUEST['rojTransType'] != '' ? $_REQUEST['rojTransType'] : 0;
	$amount        = isset($_REQUEST['amount']) && $_REQUEST['amount'] != '' ? $_REQUEST['amount'] : 0;
	$cash_type     = isset($_REQUEST['cash_type']) && $_REQUEST['cash_type'] != '' ? $_REQUEST['cash_type'] : 0;
	$rate    	   = isset($_REQUEST['rate']) && $_REQUEST['rate'] != '' ? $_REQUEST['rate'] : 0;
	$acc_type  	   = isset($_REQUEST['acc_type']) && $_REQUEST['acc_type'] != '' ? $_REQUEST['acc_type'] : '';
	$saudaDate     = $_REQUEST['rdtYear']."-".$_REQUEST['rdtMonth']."-".$_REQUEST['rdtDay'];  
	
	
	
	$select = "SELECT partyType 
				FROM party
			WHERE partyId = ".$partyId;
	$res = mysql_query($select);
	if($row = mysql_fetch_array($res))
	{
	  $partyType = $row['partyType'];
	  
	  if($partyType == 'C')
	  {
		$a = 1;
	  }
	  else
	  {
		$a = 0;
	  }
	}

    if(isset($_POST['submit']) && $saudaId == 0)
	{
	  if($acc_type == 'K')
	  {
		$mcxcheckStatus = '1';
	  }
	  else
	  {
		$mcxcheckStatus = '0';  
	  }
	  
	  $insertRojmel = "INSERT INTO sauda (partyId,saudaDate,particular,fine,rojTransType,amount,cash_type,rate,acc_type,mcxcheckStatus)
	                    VALUES (".$partyId.",'".$saudaDate."','".$particular."','".$fine."','".$rojTransType."',".$amount.",'".$cash_type."',
								".$rate.",'".$acc_type."','".$mcxcheckStatus."')";
	  $insertRojmelRes = mysql_query($insertRojmel);
	  $id = mysql_insert_id();
	  if(!$insertRojmelRes)
	  {
		  echo "Sauda Insert Fail";
	  }
	  else
	  {
	    header("Location:sauda.php");
	  }
	  
		$select = "SELECT partyType 
					FROM party
				  WHERE partyId = ".$partyId;
		$res = mysql_query($select);
		if($row = mysql_fetch_array($res))
		{
		  $partyType = $row['partyType'];
		  if($partyType == 'C')
		  {
		    $a = 1;
		  }
		  else
		  {
		    $a = 0;
		  }
		}
		
		if($a == 1)
		{
	  
			if($acc_type == 'K')
			{
			  $insertClient = "INSERT INTO client (partyId,cDate,rate,particular,kg,totalAmt,saudaId,cliTransType,sell_type)
								   VALUES(".$partyId.",'".$saudaDate."',".$rate.",'".$particular."','".$fine."',".$amount.",
										  ".$id.",'".$rojTransType."','".$acc_type."')";
			  $insertClientRes = mysql_query($insertClient);
			  if(!$insertClientRes)
			  {
				echo "Sauda Client Insert Fail";
			  }
			  else
			  {
				header("Location:sauda.php");
			  } 
			}
		}
		
		if($a == 0)
		{
			if($acc_type == 'K')
			{
			  $insertVepari = "INSERT INTO vepari(partyId,vDate,veTransType,particular,rate,kg,totalAmt,saudaId,sell_type)
								  VALUES(".$partyId.",'".$saudaDate."','".$rojTransType."','".$particular."',".$rate.",'".$fine."',
										 ".$amount.",".$id.",'".$acc_type."')";
			  $insertVepariRes = mysql_query($insertVepari);
			    
			  if(!$insertVepariRes)
			  {
				echo "Sauda Vepari Insert Fail";
			  }
			  else
			  {
				header("Location:sauda.php");
			  } 
			}
			else
			{
				header("Location:sauda.php");
			} 
		}
	}
	
	if(isset($_POST['submit']) && $saudaId > 0)
	{
		$select = "SELECT * FROM sauda
		             WHERE saudaId = ".$saudaId;
		$res = mysql_query($select);
		if($row = mysql_fetch_array($res))
		{
			$IdRequested       = $row['saudaId'];
			$acc_typeReq       = $row['acc_type'];
		}
		if($acc_typeReq == 'D')
		{
			$deleteSauda = "DELETE FROM sauda
							 WHERE saudaId = ".$IdRequested;
			$deleteSaudaRes = mysql_query($deleteSauda) or die(mysql_error());
			if(!$deleteSaudaRes)
			{
				echo "sauda Delete Fail";
			}
			else
			{
				header("Location:sauda.php");
			}
		}
		else
		{
			if($a == 1)
			{
				$deleteSaudaClient = "DELETE FROM client
									  WHERE saudaId = ".$IdRequested;
				$deleteSaudaClientRes = mysql_query($deleteSaudaClient) or die(mysql_error());
				if($deleteSaudaClientRes)
				{
					$deleteSauda = "DELETE FROM sauda
								 WHERE saudaId = ".$IdRequested;
					$deleteSaudaRes = mysql_query($deleteSauda)or die(mysql_error());
					if(!$deleteSaudaRes)
					{
						echo "sauda Delete Fail";
					}
					else
					{
						header("Location:sauda.php");
					}
				} 
			}
			
			if($a == 0)
			{
				$deleteSaudaClient = "DELETE FROM vepari
									  WHERE saudaId = ".$IdRequested;
				$deleteSaudaClientRes = mysql_query($deleteSaudaClient)or die(mysql_error());
				if($deleteSaudaClientRes)
				{
					$deleteSauda = "DELETE FROM sauda
								 WHERE saudaId = ".$IdRequested;
					$deleteSaudaRes = mysql_query($deleteSauda);
					if(!$deleteSaudaRes)
					{
						echo "sauda Delete Fail";
					}
					else
					{
						header("Location:sauda.php");
					}
				} 
			}
		}
		if($acc_type == 'K')
		{
			$mcxcheckStatus = '1';
		}
		else
		{
			$mcxcheckStatus = '0';  
		}
		 
		$insertRojmel = "INSERT INTO sauda (partyId,saudaDate,particular,fine,rojTransType,amount,cash_type,rate,acc_type,mcxcheckStatus)
							VALUES (".$partyId.",'".$saudaDate."','".$particular."','".$fine."','".$rojTransType."'
								,".$amount.",'".$_REQUEST['cash_type']."',".$rate.",'".$acc_type."','".$mcxcheckStatus."')";
		$insertRojmelRes = mysql_query($insertRojmel)or die(mysql_error());
		$id = mysql_insert_id();
		if(!$insertRojmelRes)
		{
			echo "Sauda Insert Fail";
		}
		else
		{
			header("Location:sauda.php");
		}
			
		 
		if($a == 1)
		{
	  
			if($acc_type == 'K')
			{
			  $insertClient = "INSERT INTO client (partyId,cDate,rate,particular,kg,totalAmt,saudaId,cliTransType,sell_type)
								   VALUES(".$partyId.",'".$saudaDate."',".$rate.",'".$particular."','".$fine."',".$amount.",
										  ".$id.",'".$rojTransType."','".$acc_type."')";
			  $insertClientRes = mysql_query($insertClient)or die(mysql_error());
			   
			  if(!$insertClientRes)
			  {
				echo "Sauda Client Insert Fail";
			  }
			  else
			  {
				header("Location:sauda.php");
			  } 
			}
		}
		
		if($a == 0)
		{
			if($acc_type == 'K')
			{
			  $insertVepari = "INSERT INTO vepari(partyId,vDate,veTransType,particular,rate,kg,totalAmt,saudaId,sell_type)
								  VALUES(".$partyId.",'".$saudaDate."','".$rojTransType."','".$particular."',".$rate.",'".$fine."',
										 ".$amount.",".$id.",'".$acc_type."')";
			  $insertVepariRes = mysql_query($insertVepari)or die(mysql_error());
				
			  if(!$insertVepariRes)
			  {
				echo "Sauda Vepari Insert Fail";
			  }
			  else
			  {
				header("Location:sauda.php");
			  } 
			}
			else
			{
				header("Location:sauda.php");
			} 
		}
		 
	}
	
	
	if($saudaId > 0)
	{
	  $select = "SELECT * FROM sauda
		             WHERE saudaId = ".$saudaId;
	  $res = mysql_query($select);
	  if($row = mysql_fetch_array($res))
	  {
	    $saudaId       = $row['saudaId'];
	    $partyId       = $row['partyId'];
	    $saudaDate     = $row['saudaDate'];
	    $rojTransType  = $row['rojTransType'];
	    $cash_type     = $row['cash_type'];
	    $particular    = $row['particular'];
	    $rate          = $row['rate'];
	    $fine     	   = $row['fine'];
	    $acc_type      = $row['acc_type'];
	    $amount        = $row['amount'];
	  }
	}
	
	$k=0;
	$clientArray = array();
	$selectClient = "SELECT partyId,partyName
	                   FROM party
					  WHERE partyType = 'C'
						OR partyType = 'V'
					  ORDER BY partyName ASC";
	$selectClientRes = mysql_query($selectClient);
	while($clientRow = mysql_fetch_array($selectClientRes))
	{
	  $clientArray['partyId'][$k]   = $clientRow['partyId'];
	  $clientArray['partyName'][$k] = $clientRow['partyName'];
	  $k++;
	}
	
	$typeArrayValue[0] = 'A';
	$typeArrayOutput[0] = 'Sell';
	$typeArrayValue[1] = 'J';
	$typeArrayOutput[1] = 'Buy';
	
 
	
	
	$sellTypeValue[0] = 'K';
	$sellTypOutput[0] = 'Khata';
	$sellTypeValue[1] = 'D';
	$sellTypOutput[1] = 'Direct';
	
	$cashValue[0] = 'R';
	$cashOutput[0] = 'Rtgs';
	$cashValue[1] = 'C';
	$cashOutput[1] = 'Cash';
	
	
	include("./bottom.php");
	$smarty->assign("acc_type",$acc_type);
	$smarty->assign("rate",$rate);
	$smarty->assign("cash_type",$cash_type);
	$smarty->assign("cashValue",$cashValue);
	$smarty->assign("cashOutput",$cashOutput);
	$smarty->assign("sellTypeValue",$sellTypeValue);
	$smarty->assign("sellTypOutput",$sellTypOutput);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("saudaDate",$saudaDate);
	$smarty->assign("rojTransType",$rojTransType);
	$smarty->assign("particular",$particular);
	$smarty->assign("fine",$fine);
	$smarty->assign("amount",$amount);
	$smarty->assign("saudaId",$saudaId);
	$smarty->assign("typeArrayValue",$typeArrayValue);
	$smarty->assign("typeArrayOutput",$typeArrayOutput);
	$smarty->assign("clientArray",$clientArray);
	$smarty->display("sauda.tpl");
}
?>