<?php
session_start();
header("Content-type: text/html; charset=windows-1256");



if($_SESSION['dbName']==""){
header("location:../index.php");
}
require('config.inc.php');
include "function.inc.php";
include("Functions_UserAccess.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536);
		mysql_select_db($_SESSION['dbName'],$connection);


$printreceipt = $_GET['printreceipt'];
$dist_receiptdt=getSmodulevalue('dist_receiptdt','string',$_SESSION['logincompany'],$_SESSION['dbLabel'],$connection);
$regreceipt_headerjournal=getSmodulevalue('regreceipt_headerjournal','string',$_SESSION['logincompany'],$_SESSION['dbLabel'],$connection);

$usereceiptjv = getSmodulevalue('receiptjv','string',$_SESSION['logincompany'],$_SESSION['dbLabel'],$connection);

if($regreceipt_headerjournal=='')	$regreceipt_headerjournal=0;

$actionmode 	= $_POST["actionmode"]; 	if ($actionmode == ""){		$actionmode 	= $_GET["actionmode"];}	
$refresh 		= $_POST["refresh"]; 		if ($refresh == ""){		$refresh 		= $_GET["refresh"];}	
$mainmode 		= $_POST["mainmode"]; 		if ($mainmode == ""){		$mainmode 		= $_GET["mainmode"];}	
$totalAmount 	= $_POST["totalAmount"]; 	if ($totalAmount == ""){	$totalAmount 	= $_GET["totalAmount"];}	
$fee_id 		= $_POST["fee_id"]; 		if ($fee_id == ""){			$fee_id 		= $_GET["fee_id"];}	
//echo "fee_id:".$fee_id;
 $result = get_currency($connection);
 $row=mysql_fetch_row($result);
 //$currency_code=$row[0];
 
	$query_schema = "SELECT accounting_schema from setup";
		//echo$query_schema;
		$result_schema=mysql_query($query_schema, $connection);		
		$row=mysql_fetch_row($result_schema);
		$schema=$row[0];
		$accounting_schema=$row[0];
		
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from ".$schema.".setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error()); 
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


if($mainmode=='calculateall'){

$vamount1 = 0;
$vamount2 = 0;
$vamount3 = 0;

$today=date('Y-m-d');

	if($curr1!='' && $amount1!='')
		$vamount1 = ConvertCurrency($curr1, $base1, $today, $amount1, $connection,$schema);
	
	if($curr2!='' && $amount2!='')
		$vamount2 = ConvertCurrency($curr2, $base1, $today, $amount2, $connection,$schema);

	if($curr3!='' && $amount3!='')
		$vamount3 = ConvertCurrency($curr3, $base1, $today, $amount3, $connection,$schema);
	
	$Total =  $vamount1 + $vamount2 + $vamount3; 
	echo $Total;
	
exit;
}
if($mainmode=='calculateall'){

$vamount1 = 0;
$vamount2 = 0;
$vamount3 = 0;

$today=date('Y-m-d');

	if($curr1!='' && $amount1!='')
		$vamount1 = ConvertCurrency($curr1, $base1, $today, $amount1, $connection,$schema);
	
	if($curr2!='' && $amount2!='')
		$vamount2 = ConvertCurrency($curr2, $base1, $today, $amount2, $connection,$schema);

	if($curr3!='' && $amount3!='')
		$vamount3 = ConvertCurrency($curr3, $base1, $today, $amount3, $connection,$schema);
	
	
	$Total =  $vamount1 + $vamount2 + $vamount3; 
	echo $Total;
	
exit;
}

if($mainmode=='calculateall1'){

$vamount1 = 0;
$vamount2 = 0;
$vamount3 = 0;
$vamount4 = 0;

$today=date('Y-m-d');

	if($curr1!='' && $amount1!='')
		$vamount1 = ConvertCurrency($curr1, $base1, $today, $amount1, $connection,$schema);
	
	if($curr2!='' && $amount2!='')
		$vamount2 = ConvertCurrency($curr2, $base1, $today, $amount2, $connection,$schema);

	if($curr3!='' && $amount3!='')
		$vamount3 = ConvertCurrency($curr3, $base1, $today, $amount3, $connection,$schema);
	
	if($curr4!='' && $amount4!='')
		$vamount4 = ConvertCurrency($curr4, $base1, $today, $amount4, $connection,$schema);
	
	$Total =  $vamount1 + $vamount2 + $vamount3 + $vamount4; 
	echo $Total;
	
exit;
}


if($dist_receiptdt == 2 && $mainmode!='delete' && $mainmode!='edit'){
   $query_schema = "SELECT ifnull(receipt_reference,0)+1 from sequence";
			$result_schema=mysql_query($query_schema, $connection);		
			$row=mysql_fetch_row($result_schema);
			$regreceiptheader_reference=$row[0];

			$query_schema = "update sequence set receipt_reference='".$regreceiptheader_reference."'";
			$result_schema=mysql_query($query_schema, $connection);		
//$totalAmount = str_replace(",","",$totalAmount);
    $queryexists="select * from regreceiptheader where period_id=".$period_id." and familydt_id=".$familydt_id." and payment_number=".$payment_number;
	$resultexists= mysql_query($queryexists, $connection) or die(mysql_error());
	$count=mysql_num_rows($resultexists);
	//echo "count:".$count;exit;
	if($count==0){
			$querydt = "insert into regreceiptheader (period_id, familydt_id, regreceiptheader_amount, currency_code,regreceiptheader_date,currency_code2, regreceiptheader_amount2, check_date, check_currency_code, check_amount, bank_name, bank_branch,check_no,regreceiptheader_reference,regreceiptheader_description,ledger_number,ledger_number2,bank_reference,payment_number) 
				values ('".$period_id."','".$familydt_id."','".$regreceiptheader_amountbase1."','".$currency_code."',".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
				,".GetSQLValueString($currency_code2,'text')."
				, ".GetSQLValueString($regreceiptheader_amount2,'double')."
				, ".GetSQLValueString(explodeword2($check_date),'date')."
				, ".GetSQLValueString($check_currency_code,'text')."
				, ".GetSQLValueString($check_amount,'double')."
				, ".GetSQLValueString($bank_name,'text')."
				, ".GetSQLValueString($bank_branch,'text')."
				, ".GetSQLValueString($check_no,'text')."
				, ".GetSQLValueString($regreceiptheader_reference,'text')."
				, ".GetSQLValueString($description,'text')."
				, ".GetSQLValueString($ledger_number,'text')."
				, ".GetSQLValueString($ledger_number2,'text')."
				, ".GetSQLValueString($bank_reference,'text')."
				, ".GetSQLValueString($payment_number,'int')."
				)";
		//	echo $querydt;
		//exit;
			$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
			$regreceiptheader_id = mysql_insert_id();
			
			$queryfee="SELECT fee_id FROM fees where family_id=".$familyid." and period_id=".$period_id;
				$resultfee=mysql_query($queryfee, $connection);
				$fee=mysql_fetch_array($resultfee);
				//echo $queryfee;exit;
				$fee_id=$fee[0];
				//echo "payment_number".$payment_number;
			
			
			
			/*$query_payment="select FeeDeferredPayment_id,feesdeferredpayments.Fee_id,date_format(FeeDeferredPayment_date,'%d-%m-%Y'),FeeDeferredPayment_number,date_format(FeeDeferredvalue_date,'%d-%m-%Y'),FeeDeferredPayment_amount,
											FeeTemplateDt_name,student_firstname,FeesTemplatename_name,feesdeferredpayments.student_id as stid,feesdeferredpayments.ledger_number,FeeDeferredPayment_amountBase1,FeeDeferredPayment_amountBase2,FeeDeferredPayment_discountper,
											FeeDeferredPayment_amount-((FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0))/100)
											,if(FeeDeferredPayment_discountamount=0,FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0)/100	,FeeDeferredPayment_discountamount),FeeDeferredPayment_number
											,(select sum(FeedeferredPayment_amount)from feesdeferredpayments where student_id=stid and feesdeferredpayments.Fee_id=".$fee_id." and Feedeferredpayment_number=".$payment_number." )
											from feesdeferredpayments
											left join feestemplatenames on feestemplatenames.FeesTemplatename_id=feesdeferredpayments.FeeTemplateDt_name
											left join students on students.student_id=feesdeferredpayments.student_id
											left join fees on fees.fee_id=feesdeferredpayments.fee_id	
											where feesdeferredpayments.Fee_id=".$fee_id." and Feedeferredpayment_number=".$payment_number."  
											order by FeeDeferredPayment_number,FeesTemplatename_name,student_firstname,FeeDeferredPayment_date";*/
											
			$query_payment="select FeeDeferredPayment_id,feesdeferredpayments.Fee_id,date_format(FeeDeferredPayment_date,'%d-%m-%Y'),FeeDeferredPayment_number,date_format(FeeDeferredvalue_date,'%d-%m-%Y'),FeeDeferredPayment_amount,
											FeeTemplateDt_name,student_firstname,FeesTemplatename_name,feesdeferredpayments.student_id as stid,feesdeferredpayments.ledger_number,FeeDeferredPayment_amountBase1,FeeDeferredPayment_amountBase2,FeeDeferredPayment_discountper,
											FeeDeferredPayment_amount-((FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0))/100)
											,if(FeeDeferredPayment_discountamount=0,FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0)/100	,FeeDeferredPayment_discountamount),FeeDeferredPayment_number
											,sum(FeedeferredPayment_amount)
											from feesdeferredpayments
											left join feestemplatenames on feestemplatenames.FeesTemplatename_id=feesdeferredpayments.FeeTemplateDt_name
											left join students on students.student_id=feesdeferredpayments.student_id
											left join fees on fees.fee_id=feesdeferredpayments.fee_id	
											where feesdeferredpayments.Fee_id=".$fee_id." and Feedeferredpayment_number=".$payment_number." 
											group by feesdeferredpayments.student_id											
											order by FeeDeferredPayment_number,FeesTemplatename_name,student_firstname,FeeDeferredPayment_date";					
				$result_payment=mysql_query($query_payment, $connection);
				while($nums=mysql_fetch_array($result_payment)){
				  if($std!=$nums[9]){
						$queryrecp = "insert into regreceipt (period_id, student_id, regreceipt_amount, currency_code,regreceiptheader_id) 
							values ('".$period_id."','".$nums[9]."','".str_replace(",","",$nums[17])."','".$currency_code."',$regreceiptheader_id)";
						$resultr=mysql_query($queryrecp, $connection);
						$regreceipt_id = mysql_insert_id();
					//$resultdt = mysql_query($querydt, $connection);
					//$regreceipt_id = mysql_insert_id();
					$std=$nums[9];
					}
															
					$query_paymentdt="select FeeDeferredPayment_id,feesdeferredpayments.Fee_id,date_format(FeeDeferredPayment_date,'%d-%m-%Y'),FeeDeferredPayment_number,date_format(FeeDeferredvalue_date,'%d-%m-%Y'),FeeDeferredPayment_amount,
											FeeTemplateDt_name,student_firstname,FeesTemplatename_name,feesdeferredpayments.student_id,feesdeferredpayments.ledger_number,FeeDeferredPayment_amountBase1,FeeDeferredPayment_amountBase2,FeeDeferredPayment_discountper,
											FeeDeferredPayment_amount-((FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0))/100)
											,if(FeeDeferredPayment_discountamount=0,FeeDeferredPayment_amount*ifnull(FeeDeferredPayment_discountper,0)/100	,FeeDeferredPayment_discountamount),FeeDeferredPayment_number
											,(select sum(FeedeferredPayment_amount)from feesdeferredpayments where student_id=".$std." and feesdeferredpayments.Fee_id=".$fee_id." and Feedeferredpayment_number=".$payment_number." )
											from feesdeferredpayments
											left join feestemplatenames on feestemplatenames.FeesTemplatename_id=feesdeferredpayments.FeeTemplateDt_name
											left join students on students.student_id=feesdeferredpayments.student_id
											left join fees on fees.fee_id=feesdeferredpayments.fee_id	
											where feesdeferredpayments.Fee_id=".$fee_id." and Feedeferredpayment_number=".$payment_number." and feesdeferredpayments.student_id=".$std."   
											order by FeeDeferredPayment_number,FeesTemplatename_name,student_firstname,FeeDeferredPayment_date";							
											
					//echo $query_paymentdt;exit;
					$result_paymentdt=mysql_query($query_paymentdt, $connection);
			
					while($numsdt=mysql_fetch_array($result_paymentdt)){
						$today=date('Y-m-d');			
						
						$student_id=$numsdt[9];
						//echo $querydt;exit;
						
						$regreceiptdt_amountBase1 = ConvertCurrency($currency_code, $base1, $today, $numsdt[14], $connection,$schema);
						$regreceiptdt_amountBase2 = ConvertCurrency($currency_code, $base2, $today, $numsdt[14], $connection,$schema);
								$ledgernumber='';
								$ledgernumber = Get_LedgerNumber($period_id,$student_id,$familyid,$numsdt[6],$connection);
								$querydt = "insert into regreceiptdt (tuitionfees_id, regreceiptdt_amount, regreceipt_id,regreceiptdt_amountbase1,regreceiptdt_amountbase2,ledger_number) 
									values ('".$numsdt[6]."','".$numsdt[14]."','".$regreceipt_id."',$regreceiptdt_amountBase1,$regreceiptdt_amountBase2,'$ledgernumber')";
									//echo$querydt."<br>";exit;
									$resultdt = mysql_query($querydt, $connection);
						
						
						
						
						
						}
								
								
			}
	
	
	}
	
	
		header ("Location: ./Form_Receipt.php?message=$message&regreceiptheader_id=$regreceiptheader_id&familydt_id=$familydt_id&currency=$currency");




 }


if($dist_receiptdt == 2 && $mainmode=='edit'){

$querydt = "update regreceiptheader   
				set period_id =  '".$period_id."'
				,regreceiptheader_amount = '".$regreceiptheader_amountbase1."'
				,currency_code = '".$currency_code."'
				,currency_code2 = ".GetSQLValueString($currency_code2,'text')."
				, regreceiptheader_amount2= ".GetSQLValueString($regreceiptheader_amount2,'double')."
				, check_date = ".GetSQLValueString(explodeword2($check_date),'date')."
				, check_currency_code = ".GetSQLValueString($check_currency_code,'text')."
				, check_amount = ".GetSQLValueString($check_amount,'double')."
				, bank_name = ".GetSQLValueString($bank_name,'text')."
				, bank_branch = ".GetSQLValueString($bank_branch,'text')."
				, check_no = ".GetSQLValueString($check_no,'text')."
				,ledger_number= ".GetSQLValueString($ledger_number,'text')."
				,ledger_number2= ".GetSQLValueString($ledger_number2,'text')."
				,regreceiptheader_date= ".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
				,bank_reference= ".GetSQLValueString($bank_reference,'text')."
				,payment_number= ".GetSQLValueString($payment_number,'int')."
				where regreceiptheader_id = $regreceiptheader_id";
	//echo $querydt;exit;		 
			$resultdt = mysql_query($querydt, $connection);

header ("Location: ./Form_Receipt.php?message=$message&regreceiptheader_id=$regreceiptheader_id&familydt_id=$familydt_id&currency=$currency&mainmode=insert");




}




if($mainmode=='insert' && $dist_receiptdt != 2){
			
		$query_schema = "SELECT ifnull(receipt_reference,0)+1 from sequence";
		$result_schema=mysql_query($query_schema, $connection);		
		$row=mysql_fetch_row($result_schema);
		$regreceiptheader_reference=$row[0];

		$query_schema = "update sequence set receipt_reference='".$regreceiptheader_reference."'";
		$result_schema=mysql_query($query_schema, $connection);		


		
		if($regreceiptheader_prestudid=='')
			$querydt = "insert into regreceiptheader (period_id, familydt_id, regreceiptheader_amount, currency_code,regreceiptheader_date,currency_code2, regreceiptheader_amount2, check_date, check_currency_code, check_amount, bank_name, bank_branch,check_no,regreceiptheader_reference,regreceiptheader_description,ledger_number,ledger_number2,bank_reference,payment_number,
					check_amount2, check_currency_code2, checktype, receipttotal_amount,additional_amount,AdditionalAmount_Account) 
					
			values ('".$period_id."','".$familydt_id."','".$regreceiptheader_amountbase1."','".$currency_code."',".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
			,".GetSQLValueString($currency_code2,'text')."
			, ".GetSQLValueString($regreceiptheader_amount2,'double')."
			, ".GetSQLValueString(explodeword2($check_date),'date')."
			, ".GetSQLValueString($check_currency_code,'text')."
			, ".GetSQLValueString($check_amount,'double')."
			, ".GetSQLValueString($bank_name,'text')."
			, ".GetSQLValueString($bank_branch,'text')."
			, ".GetSQLValueString($check_no,'text')."
			, ".GetSQLValueString($regreceiptheader_reference,'text')."
			, ".GetSQLValueString($description,'text')."
			, ".GetSQLValueString($ledger_number,'text')."
			, ".GetSQLValueString($ledger_number2,'text')."
			, ".GetSQLValueString($bank_reference,'text')."
			, ".GetSQLValueString($payment_number,'int')."
			, ".GetSQLValueString($check_amount2,'double')."
			, ".GetSQLValueString($check_currency_code2,'text')."
			, ".GetSQLValueString($checktype,'int')."
			, ".GetSQLValueString($regreceiptheader_amount,'double')." 
			, ".GetSQLValueString($additional_amount,'double')."
			,".GetSQLValueString($ledger_number,'text')."
			)";
		else
			$querydt = "insert into regreceiptheader (period_id, regreceiptheader_prestudid, regreceiptheader_amount, currency_code,regreceiptheader_date,currency_code2, regreceiptheader_amount2, check_date, check_currency_code, check_amount, bank_name, bank_branch,check_no,regreceiptheader_reference,ledger_number,ledger_number2,bank_reference,payment_number,
						check_amount2, check_currency_code2, checktype, receipttotal_amount,additional_amount,AdditionalAmount_Account) 
			values ('".$period_id."','".$regreceiptheader_prestudid."','".$regreceiptheader_amountbase1."','".$currency_code."',".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
			,".GetSQLValueString($currency_code2,'text')."
			, ".GetSQLValueString($regreceiptheader_amount2,'double')."
			, ".GetSQLValueString(explodeword2($check_date),'date')."
			, ".GetSQLValueString($check_currency_code,'text')."
			, ".GetSQLValueString($check_amount,'double')."
			, ".GetSQLValueString($bank_name,'text')."
			, ".GetSQLValueString($bank_branch,'text')."
			, ".GetSQLValueString($check_no,'text')."
			, ".GetSQLValueString($regreceiptheader_reference,'text')."
			, ".GetSQLValueString($ledger_number,'text')."
			, ".GetSQLValueString($ledger_number2,'text')."
			, ".GetSQLValueString($bank_reference,'text')."
			, ".GetSQLValueString($payment_number,'int')."
			, ".GetSQLValueString($check_amount2,'double')."
			, ".GetSQLValueString($check_currency_code2,'text')."
			, ".GetSQLValueString($checktype,'int')."
			, ".GetSQLValueString($regreceiptheader_amount,'double')."
			, ".GetSQLValueString($additional_amount,'double')."
			,".GetSQLValueString($ledger_number,'text')."
			)";		
		//	echo $querydt;
		//	exit;
			$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
			$regreceiptheader_id = mysql_insert_id();
		
		$perd_default = get_defaultperiod($connection);
		
		if($dist_receiptdt==1 || $dist_receiptdt==0){
		$query_defaultper = "select period_id from periods where period_default=1;";
		$res_defaultper = mysql_query($query_defaultper,$connection);
		$rw_defaultper = mysql_fetch_row($res_defaultper);
		if($period_id != $rw_defaultper[0]) {
			$regOnly=1;
			$nowperiod=$rw_defaultper[0];
		}
		else $regOnly=0;
		if($regreceiptheader_prestudid==''){
		if($familycode1 != '')	
		$query="select Student_FirstName,Class_code,student_code,student_id 
			from students inner join classes on students.class_id=classes.class_id 
			where (familyDt_idfather=".$familydt_id." or familyDt_idmother=".$familydt_id .") 
			and (if($regOnly=1,period_id ='$nowperiod',period_id ='$period_id') or
			if($regOnly=1,nextperiod_id='$nowperiod',nextperiod_id='$period_id'))
				and if('$period_id' = '$perd_default', 1=1,studentstatus_id=4) 
				and if('$period_id' = '$perd_default', 1=1,class_last != 1)	
				order by student_id";
		else 
		$query="select prestud_firstname,Class_code,null,prestud_id 
			from prestudent inner join classes on prestud_class=classes.class_id 
			where prestud_familyid=".$familydt_id." 
			and prestud_decision='a' and prestud_period=$period_id
				order by prestud_id";	
		/* 	" and nextperiod_id= ";
	    if($regOnly == 1) $query=$query."$nowperiod ";		  
		else $query=$query."$period_id "; */
		}	
		else
			$query="select 	prestud_firstname,null,prestud_familyname,prestud_id 
							from prestudent where prestud_id=".$regreceiptheader_prestudid;
			//echo $query;exit;
		$result=mysql_query($query,$connection);
		$i=0;
		$j=0;	
		while ($res=mysql_fetch_row($result)){
		
			$student_id = $res[3];
			$amount = 'amount'.$i;
			$class_id=$res[6];
			$feesname='feesname'.$i;
			
			$$amount = str_replace(",","",$$amount);
			
			if($regreceiptheader_prestudid=='')
				$querydt = "insert into regreceipt (period_id, student_id, regreceipt_amount, currency_code,regreceiptheader_id) 
				values ('".$period_id."','".$student_id."','".$$amount."','".$currency_code."',$regreceiptheader_id)";
			else $querydt = "insert into regreceipt (period_id, prestud_id, regreceipt_amount, currency_code,regreceiptheader_id) 
				values ('".$period_id."','".$student_id."','".$$amount."','".$currency_code."',$regreceiptheader_id)";
					
			//echo$querydt."<br>";exit;
			$resultdt = mysql_query($querydt, $connection);
			$regreceipt_id = mysql_insert_id();
		
			 
			
			
			$student_id=$res[3];
			echo"<tr height=20><td><b>&nbsp;</b></tr>";
			//echo $regreceiptheader_prestudid;
			if($regreceiptheader_prestudid==''){
					$query="SELECT FeesTemplatename_id,FeesTemplatename_name,FeesTemplatename_period,null,FeesTemplatename_name FROM feestemplatenames 
						";
					if($feesname!='-1')
						$query=$query." Where FeesTemplatename_id in ($feesname) ";
						else 	$query=$query." where transportation_included=0 or transportation_included=1 or  transportation_included=3 ";
					
					$query = "  select FeeTemplateDt_name,FeeTemplateDt_name,FeesTemplatename_period,null,FeesTemplatename_name,feedeferredpayment_date,student_id,feedeferredpayment_amount 
									,ledger_number2
									from feesdeferredpayments
								left join fees on fees.fee_id=feesdeferredpayments.fee_id
								left join feestemplatenames on feestemplatenames.FeesTemplatename_id=feesdeferredpayments.FeeTemplateDt_name
								
								where student_id = $student_id
								And FeeTemplateDt_name in (".$$feesname.")
								order by FeesTemplatename_name,feedeferredpayment_date ";	//group by feesdeferredpayments.FeeTemplateDt_name
										$resultcheck=mysql_query($query,$connection) or die(mysql_error());
							$numcheck=mysql_num_rows($resultcheck);
							
								if($numcheck==0){
									
								
										$query = " SELECT  FeesTemplatename_id,FeesTemplatename_name,FeesTemplatename_period,null,FeesTemplatename_name 
										FROM feestemplatenames							
										where FeesTemplatename_id in(".$$feesname.")";
								
								
								}				
						}
					else {
					$query="SELECT FeesTemplatename_id,FeesTemplatename_name,FeesTemplatename_period,null,FeesTemplatename_name FROM feestemplatenames ";
					//where transportation_included=2 or transportation_included=1";
					//if($$feesname!='-1')
					//	$query=$query." Where FeesTemplatename_id in (".$$feesname.") ";
					//else 	
					$query=$query." where transportation_included=2 or transportation_included=2";
					}
			 //echo $query;echo "<br>";
			$resultfees=mysql_query($query,$connection);
			$j=0;
				$jname='startj'.$i;
				$$jname=$j;
				
				$regreceipt_amount=0;
				while( $resfees = mysql_fetch_row($resultfees))
				{   //if($stdid != $student_id && $j != 0) 
					$fees = $resfees[0];
					$fees = 'fees'.$student_id."-".$j;
						
						if($resfees[2]=='Y' or $resfees[2]=='y')
							$next=1;
				   
				   
					$today=date('Y-m-d');
					//echo $resfees[0]; echo "<br>";
					//echo $fees; echo "<br>";
					$regreceiptdt_amountBase1 = ConvertCurrency($currency_code, $base1, $today, $$fees, $connection,$schema);
					$regreceiptdt_amountBase2 = ConvertCurrency($currency_code, $base2, $today, $$fees, $connection,$schema);
					$ledgernumber='';
					$ledgernumber = Get_LedgerNumber($period_id,$student_id,$familyid,$resfees[0],$connection);
						
						if($regreceiptdt_amountBase1 != 0 || $regreceiptdt_amountBase2 != 0){
					    $querydt = "insert into regreceiptdt (tuitionfees_id, regreceiptdt_amount, regreceipt_id,regreceiptdt_amountbase1,regreceiptdt_amountbase2,ledger_number) 
						values ('".$resfees[0]."','".$$fees."','".$regreceipt_id."',".round($regreceiptdt_amountBase1,5).",".round($regreceiptdt_amountBase2,5).",'$ledgernumber')";
						//echo$querydt."<br>";
						//exit;
						$resultdt = mysql_query($querydt, $connection);
						}
			
					$j+=1;
					$regreceipt_amount+=$$fees;
					$stdid = $student_id;
				} //echo "<br>";
			//exit;
			if($next==1){
			$nextperiod_id=get_nextperiod($connection);
			$query = " update regreceipt set period_id = $nextperiod_id where regreceipt_id = $regreceipt_id ";
			$resultdt = mysql_query($query, $connection);
			$query = " update regreceiptheader set period_id = $nextperiod_id where regreceiptheader_id = $regreceiptheader_id ";
			$resultdt = mysql_query($query, $connection);
			
			}	
			
			//$querydt = " update  regreceipt set regreceipt_amount =  $regreceipt_amount where regreceipt_id=$regreceipt_id ";
			//echo$querydt."<br>";
			//$resultdt = mysql_query($querydt, $connection);
			
			
			$i++;
		
		}
		//exit;
		
		$query="select ifnull(journal_id,0) from regreceiptheader where regreceiptheader_id='".$regreceiptheader_id."'";
		$result=mysql_query($query, $connection);		
		$row=mysql_fetch_row($result);
		$journal_id=$row[0];
		
		
	
	
	
	
	 }
	 if($usereceiptjv == 1){
	 if($journal_id=='')
			$journal_id=0;

		$query="delete from ".$accounting_schema.".journaldt where journal_id='".$journal_id."'";
		$result=mysql_query($query, $connection);
		//echo $query;
		$query="update ".$schema.".journal set Amnt1_db=0,Amnt1_cd=0,Amnt2_cd=0,Amnt2_db=0 where journal_id='".$journal_id."'";
		$result=mysql_query($query, $connection);
		//echo $query; exit;
		$accesslabels=$config['accesslabels'];
			
		$query_jv="call auto_receipt(".$regreceiptheader_id.",".$period_id.",'01','admin','".$accounting_schema."','".$accesslabels."','".$journal_id."','".$base1."','".$base2."')";
		
	//	echo$query_jv."<br>";
	//exit;	
		$result_jv=mysql_query($query_jv,$connection);
		//echo $printreceipt;exit;
	 }	
header ("Location: ./Form_Receipt.php?message=$message&regreceiptheader_id=$regreceiptheader_id&mainmode=edit&familydt_id=$familydt_id&currency=$currency&familycode1=$familycode1");
	
	
	
	
	
}

else if($mainmode=='edit' && $dist_receiptdt != 2){ 
$totalAmount = str_replace(",","",$totalAmount);
$regreceiptheader_amount2 = str_replace(",","",$regreceiptheader_amount2); 

if($regreceiptheader_prestudid=='')
			$querydt = "update regreceiptheader   
				set period_id =  '".$period_id."'
				,familydt_id = '".$familydt_id."'
				,regreceiptheader_amount = '".$regreceiptheader_amountbase1."'
				,currency_code = '".$currency_code."'
				,currency_code2 = ".GetSQLValueString($currency_code2,'text')."
				, regreceiptheader_amount2= ".GetSQLValueString($regreceiptheader_amount2,'double')."
				, check_date = ".GetSQLValueString(explodeword2($check_date),'date')."
				, check_currency_code = ".GetSQLValueString($check_currency_code,'text')."
				, check_amount = ".GetSQLValueString($check_amount,'double')."
				, bank_name = ".GetSQLValueString($bank_name,'text')."
				, bank_branch = ".GetSQLValueString($bank_branch,'text')."
				, check_no = ".GetSQLValueString($check_no,'text')."
				,regreceiptheader_description= ".GetSQLValueString($description,'text')."
				,ledger_number= ".GetSQLValueString($ledger_number,'text')."
				,ledger_number2= ".GetSQLValueString($ledger_number2,'text')."
				,regreceiptheader_date= ".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
				,bank_reference= ".GetSQLValueString($bank_reference,'text')."
				,payment_number= ".GetSQLValueString($payment_number,'int').",checktype='$checktype' 
				where regreceiptheader_id = $regreceiptheader_id";
		else
			$querydt = "update regreceiptheader   
				set period_id =  '".$period_id."'
				,regreceiptheader_prestudid = '".$regreceiptheader_prestudid."'
				,regreceiptheader_amount = '".$regreceiptheader_amountbase1."'
				,currency_code = '".$currency_code."'
					,currency_code2 = ".GetSQLValueString($currency_code2,'text')."
				, regreceiptheader_amount2= ".GetSQLValueString($regreceiptheader_amount2,'double')."
				, check_date = ".GetSQLValueString(explodeword2($check_date),'date')."
				, check_currency_code = ".GetSQLValueString($check_currency_code,'text')."
				, check_amount = ".GetSQLValueString($check_amount,'double')."
				, bank_name = ".GetSQLValueString($bank_name,'text')."
				, bank_branch = ".GetSQLValueString($bank_branch,'text')."
				, check_no = ".GetSQLValueString($check_no,'text')."
				,ledger_number= ".GetSQLValueString($ledger_number,'text')."
				,ledger_number2= ".GetSQLValueString($ledger_number2,'text')."
				,regreceiptheader_date= ".GetSQLValueString(explodeword2($regreceiptheader_date),'date')."
				,bank_reference= ".GetSQLValueString($bank_reference,'text')."
				,payment_number= ".GetSQLValueString($payment_number,'int').",checktype='$checktype' 
				where regreceiptheader_id = $regreceiptheader_id";
			//echo $querydt;exit;
			$resultdt = mysql_query($querydt, $connection);

		if($regreceiptheader_prestudid=='')	
			$query="select Upper(Student_FirstName),Class_code,student_code,students.student_id ,regreceipt_id ,regreceipt_amount,students.class_id
					from regreceipt,students inner join classes on students.class_id=classes.class_id 
					where (familyDt_idfather=".$familydt_id." or familyDt_idmother=".$familydt_id.") 
					and regreceipt.student_id=students.student_id and regreceiptheader_id= $regreceiptheader_id"		;
		else		
			$query="select prestud_firstname,prestud_familyname,prestudent.prestud_id,prestudent.prestud_id ,regreceipt_id ,regreceipt_amount,prestud_class
					from regreceipt,prestudent ,regreceiptheader 
					where prestudent.prestud_id=".$regreceiptheader_prestudid."
					and regreceiptheader.regreceiptheader_prestudid=prestudent.prestud_id 
					and regreceipt.regreceiptheader_id=regreceiptheader.regreceiptheader_id 
					and regreceiptheader.regreceiptheader_id= $regreceiptheader_id"
					;
	//echo $query; exit;
	$j=0;
	$i=0;
	$result=mysql_query($query,$connection);/*
	while ($res=mysql_fetch_row($result)){
	
		$amount = 'amount'.$i;
	
		if($regreceiptheader_prestudid=='')
				$querydt = "update regreceipt set period_id = '".$period_id."'
									, student_id = '".$res[3]."'
									, regreceipt_amount = '".$$amount."'
									, currency_code = '".$currency_code."' where regreceipt_id=$res[4]"; 
				else $querydt = "update regreceipt set period_id = '".$period_id."'
									, prestud_id = '".$res[2]."'
									, regreceipt_amount = '".$$amount."'
									, currency_code = '".$currency_code."' where regreceipt_id=$res[4]"; 
				//echo $querydt."<br>";exit;
				$resultdt=mysql_query($querydt,$connection);
				
				
			if($regreceiptheader_prestudid=='')
				$query="SELECT regreceiptdt_id, tuitionfees_id, regreceiptdt_amount, regreceipt_id	 ,FeesTemplatename_name, FeesTemplatename_id
				from regreceiptdt ,feestemplatenames
				where regreceipt_id=$res[4]
				and feestemplatenames.FeesTemplatename_id=regreceiptdt.tuitionfees_id				";
				
			else $query="SELECT regreceiptdt_id, tuitionfees_id, regreceiptdt_amount, regreceipt_id	 ,FeesTemplatename_name, FeesTemplatename_id
				from regreceiptdt ,feestemplatenames
				where regreceipt_id=$res[4]
				and feestemplatenames.FeesTemplatename_id=regreceiptdt.tuitionfees_id				";
				//echo $query."<br>"; exit;
				
			$resultfees=mysql_query($query,$connection) or die(mysql_error());
			
			while( $resfees = mysql_fetch_row($resultfees))
				{
				
					$fees = $resfees[0];
					$fees = 'fees'.$res[3]."-".$j;
						
					
					$today=date('Y-m-d');
			 
					$amt=$$fees;
					
					$regreceiptdt_amountBase1 = ConvertCurrency($currency_code, $base1, $today,$amt , $connection,$schema);
					$regreceiptdt_amountBase2 = ConvertCurrency($currency_code, $base2, $today,$amt, $connection,$schema);
					
					
						$querydt = "update regreceiptdt set tuitionfees_id = '".$resfees[1]."'
									, regreceiptdt_amount = ".$$fees."
									, regreceiptdt_amountbase1 = $regreceiptdt_amountBase1
									,regreceiptdt_amountbase2 = $regreceiptdt_amountBase2
									where regreceiptdt_id = $resfees[0]";
						//echo $fees." ".$$fees."<br>";
					//	echo $querydt."<br>";
						$resultdt1 = mysql_query($querydt, $connection);
			
				$j+=1;
			}
		$i+=1;	
	}
	*/
	///// regreceiptdt
  if($usereceiptjv == 1){
$query="select ifnull(journal_id,0) from regreceiptheader where regreceiptheader_id='".$regreceiptheader_id."'";
		$result=mysql_query($query, $connection);		
		$row=mysql_fetch_row($result);
		$journal_id=$row[0];
		
		if($journal_id=='')
			$journal_id=0;

		$query="delete from ".$accounting_schema.".journaldt where journal_id='".$journal_id."'";
		$result=mysql_query($query, $connection);
		
		$query="update ".$schema.".journal set Amnt1_db=0,Amnt1_cd=0,Amnt2_cd=0,Amnt2_db=0 where journal_id='".$journal_id."'";
		$result=mysql_query($query, $connection);
		
		$accesslabels=$config['accesslabels'];
			
		$query_jv="call auto_receipt(".$regreceiptheader_id.",".$period_id.",'01','admin','".$accounting_schema."','".$accesslabels."','".$journal_id."','".$base1."','".$base2."')";
		//echo $query_jv;
		$result_jv=mysql_query($query_jv,$connection);
	//echo $printreceipt;exit;
  }
		

header ("Location: ./Form_Receipt.php?message=$message&regreceiptheader_id=$regreceiptheader_id&mainmode=edit&familydt_id=$familydt_id&currency=$currency&familycode1=$familycode1");


}


else if($mainmode=='delete' and $regreceiptheader_id!=''){
fbegin();
	
		$query="select ifnull(journal_id,0) from regreceiptheader where regreceiptheader_id='".$regreceiptheader_id."'";
		$result=mysql_query($query, $connection);		
		$row=mysql_fetch_row($result);
		$journal_id=$row[0];
		//echo $query."<br>";

		$query="delete from ".$accounting_schema.".journal where journal_id='".$journal_id."'";
		//echo $query."<br>";
		$result=mysql_query($query, $connection);

	$query = "delete from  regreceiptdt where regreceipt_id in (select regreceipt_id from regreceipt where regreceiptheader_id=$regreceiptheader_id)";
	$result1 = mysql_query($query, $connection) or die(mysql_error());
	//echo $query."<br>";
	$query = "delete from  regreceipt where regreceiptheader_id=$regreceiptheader_id";
	$result2 = mysql_query($query, $connection) or die(mysql_error());
	//echo $query."<br>";
	$query = "delete from  regreceiptheader where regreceiptheader_id=$regreceiptheader_id";
	//echo $query."<br>";exit;
	$result3 = mysql_query($query, $connection) or die(mysql_error());
	if($result1 and $result2 and $result3)
		fcommit();	
	else
		rollback();
		
header ("Location: ./Form_Receipt.php?message=$message&mainmode=insert&familydt_id=$familydt_id");
	
	
}

function explodeword2($date)
	{
if($date!=''){
		$trim = explode('/',$date);
		$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";

		return $trim;
	}
}
function Get_LedgerNumber($period_id,$student_id,$family_id,$feetemplatedt_name,$connection){

$query = " select ledger_number2
										from feesdeferredpayments,fees
										where fees.fee_id = feesdeferredpayments.fee_id 
										and fees.family_id = $family_id
										and fees.period_id = $period_id
										and feesdeferredpayments.student_id = $student_id
										and feetemplatedt_name = '$feetemplatedt_name'";
							echo $query."<br>";
							$resultledger=mysql_query($query,$connection) or die(mysql_error());
							$resled = mysql_fetch_row($resultledger);
							return $resled[0];
}

?>