Current File : /home/inlingua/www/sensoriumpsychologists.com/backup/franchise_between/payment_funcs.php
<?php
require_once("../../franchise_between/e_wallet_funcs.php");
require_once("../../franchise_back/database_func.php");
class payment_funcs
{
	private $franchise_id,$student_id,$amount;
	private $created_by,$created_browser,$created_ip,$updated_by,$updated_browser,$updated_ip,$updated_date;
	
	private function get_validity($date_str, $months)
	{
    $date = new DateTime($date_str);
    $start_day = $date->format('j');

    $date->modify("+{$months} month");
    $end_day = $date->format('j');

    if ($start_day != $end_day)
        $date->modify('last day of last month');
		
    $date->modify("-1 day");

    return $date;
	}
	
	public function __construct()
	{
		$this->franchise_id="";
		$this->student_id="";
		$this->amount="";
		$this->created_by="";
		$this->created_browser="";
		$this->created_ip="";
		$this->updated_by="";
		$this->updated_browser="";
		$this->updated_ip="";
		$this->updated_date="0000-00-00 00:00:00";
	}
	public function set_value($variable_name,$new_value)
	{
		switch($variable_name)
		{
			case 'franchise_id':$this->franchise_id=$new_value;break;
			case 'student_id':$this->student_id=$new_value;break;
			case 'amount':$this->amount=$new_value;break;
			case 'created_by':$this->created_by=$new_value;break;
			case 'created_browser':$this->created_browser=$new_value;break;
			case 'created_ip':$this->created_ip=$new_value;break;
		}
	}
	public function get_value($variable_name)
	{
		$to_return="";
		switch($variable_name)
		{
			case 'student_id':$to_return=$this->student_id;break;
			case 'amount':$to_return=$this->amount;break;
		}
		return $to_return;
	}
	public function view_initial_payment_unpaid($payment_no)
	{
		$franchise_id=$this->franchise_id;
		$columns_array=array("student_id","credit");
		$table_name="shristi_students_payments";
		
		$mysql_obj=new mysql_functions_class;
		
		$mysql_obj->open_connection();
		$payment_no=$mysql_obj->real_escape($payment_no);
		$conditions=" WHERE table_id='$payment_no' AND franchise_id='$franchise_id' AND status='unpaid' AND payment_mode='' AND type='initial' ";
		$res=$mysql_obj->select_simple($table_name,$columns_array,$conditions);
		$mysql_obj->close_connection();
		if($res->num_rows==1)
		{
			$detail=$res->fetch_assoc();
			$this->student_id=$detail['student_id'];
			$this->amount=$detail['credit'];
		}
		else
		{
			echo("Error : Invalid request...");
			exit();
		}
	}
	public function view_initial_payment_head_office($payment_no)
	{
		$franchise_id=$this->franchise_id;
		$columns_array=array("student_id","credit");
		$table_name="shristi_students_payments";
		
		$mysql_obj=new mysql_functions_class;
		
		$mysql_obj->open_connection();
		$payment_no=$mysql_obj->real_escape($payment_no);
		$conditions=" WHERE table_id='$payment_no' AND franchise_id='$franchise_id' AND status='unpaid' AND payment_mode='head_office' AND type='initial' ";
		$res=$mysql_obj->select_simple($table_name,$columns_array,$conditions);
		$mysql_obj->close_connection();
		if($res->num_rows==1)
		{
			$detail=$res->fetch_assoc();
			$this->student_id=$detail['student_id'];
			$this->amount=$detail['credit'];
		}
		else
		{
			echo("Error : Invalid request...");
			exit();
		}
	}
	public function view_initial_payment_ewallet($payment_no)
	{
		$franchise_id=$this->franchise_id;
		$columns_array=array("student_id","credit");
		$table_name="shristi_students_payments";
		
		$mysql_obj=new mysql_functions_class;
		
		$mysql_obj->open_connection();
		$payment_no=$mysql_obj->real_escape($payment_no);
		$conditions=" WHERE table_id='$payment_no' AND franchise_id='$franchise_id' AND status='paid' AND payment_mode='ewallet' AND type='initial' ";
		$res=$mysql_obj->select_simple($table_name,$columns_array,$conditions);
		$mysql_obj->close_connection();
		if($res->num_rows==1)
		{
			$detail=$res->fetch_assoc();
			$this->student_id=$detail['student_id'];
			$this->amount=$detail['credit'];
		}
		else
		{
			echo("Error : Invalid request...");
			exit();
		}
	}
	public function pay_student_to_head_office($student_id,$payment_no)
	{
		$franchise_id=$this->franchise_id;
		$ip=$_SERVER['REMOTE_ADDR'];
		$browser=$_SERVER['HTTP_USER_AGENT'];
		$datetime=date("Y-m-d H:i:s");
		
		$mysql_obj=new mysql_functions_class;
		
		$mysql_obj->open_connection();
		$mysql_obj->start_transaction();
		$payment_no=$mysql_obj->real_escape($payment_no);
		$student_id=$mysql_obj->real_escape($student_id);
		
		$prepared_statement="UPDATE shristi_students_payments SET payment_mode='head_office',updated_by=updated_by+'!@!@$franchise_id',updated_browser=updated_browser+'!@!@$browser',updated_ip=updated_ip+'!@!@$ip',updated_date=updated_date+'!@!@$datetime' WHERE table_id='$payment_no'";
		$res=$mysql_obj->simply_run_statement_passed($prepared_statement);
		$aff_rows=$mysql_obj->affected_rows();
		if($aff_rows!=1)
		{
			$mysql_obj->rollback();
			$mysql_obj->close_connection();
			echo("Error : Couldn't update payment status now...Please try again later and if problem persists, please contact your administrator");
			exit();
		}
		
		$prepared_statement="UPDATE shristi_child_details SET created_date='$datetime',status='pending',updated_by='$franchise_id',updated_browser='$browser',updated_ip='$ip',updated_date='$datetime' WHERE table_id='$student_id'";
		$res=$mysql_obj->simply_run_statement_passed($prepared_statement);
		$aff_rows=$mysql_obj->affected_rows();
		if($aff_rows!=1)
		{
			$mysql_obj->rollback();
			$mysql_obj->close_connection();
			echo("Error : Couldn't activate student now...Please try again later and if problem persists, please contact your administrator");
			exit();
		}
		$mysql_obj->commit();
		$mysql_obj->close_connection();
	}
	public function pay_student_to_ewallet($student_id,$payment_no)
	{
		$franchise_id=$this->franchise_id;
		
		$balance=0;
		$e_wallet_obj=new e_wallet();
		$e_wallet_obj->set_value("franchise_id",$franchise_id);
		$balance=$e_wallet_obj->e_wallet_balance();
		
		$ip=$_SERVER['REMOTE_ADDR'];
		$browser=$_SERVER['HTTP_USER_AGENT'];
		$datetime=date("Y-m-d H:i:s");
		$today_date=date("Y-m-d");
		
		$mysql_obj=new mysql_functions_class;
		
		$mysql_obj->open_connection();
		$mysql_obj->start_transaction();
		$payment_no=$mysql_obj->real_escape($payment_no);
		$student_id=$mysql_obj->real_escape($student_id);
		
		$validity=1;
		$temp_2=$this->get_validity($today_date,$validity);
		$end_date = $temp_2->format('Y-m-d');
		if($end_date=="0000-00-00")
		{
			echo("Error : Cannot calculate date...");
			exit();
		}
	
		
		$prepared_statement="UPDATE shristi_students_payments SET status='paid',payment_mode='ewallet',updated_by=updated_by+'!@!@$franchise_id',updated_browser=updated_browser+'!@!@$browser',updated_ip=updated_ip+'!@!@$ip',updated_date=updated_date+'!@!@$datetime' WHERE table_id='$payment_no' AND credit<=$balance";
		$res=$mysql_obj->simply_run_statement_passed($prepared_statement);
		$aff_rows=$mysql_obj->affected_rows();
		if($aff_rows!=1)
		{
			$mysql_obj->rollback();
			$mysql_obj->close_connection();
			echo("Error : Couldn't update payment status now...Please try again later and if problem persists, please contact your administrator");
			exit();
		}
		
		$prepared_statement="SELECT * FROM shristi_child_details WHERE table_id='$student_id'";
		$res=$mysql_obj->simply_run_statement_passed($prepared_statement);
		$aff_rows=$mysql_obj->affected_rows();
		if($aff_rows!=1)
		{
			$mysql_obj->rollback();
			$mysql_obj->close_connection();
			echo("Error : Couldn't get student details now...Please try again later and if problem persists, please contact your administrator");
			exit();
		}
		$detail=$res->fetch_assoc();
		$p_city=$detail['p_city'];
		$reg_no=strtoupper(substr($p_city, 0, 3));
		$reg_no.=date("y").date("m").$student_id;
		
		
		$prepared_statement="UPDATE shristi_child_details SET registration_date='$today_date',next_end_date='$end_date',registration_no='$reg_no',status='active',updated_by='$franchise_id',updated_browser='$browser',updated_ip='$ip',updated_date='$datetime' WHERE table_id='$student_id'";
		$res=$mysql_obj->simply_run_statement_passed($prepared_statement);
		$aff_rows=$mysql_obj->affected_rows();
		if($aff_rows!=1)
		{
			$mysql_obj->rollback();
			$mysql_obj->close_connection();
			echo("Error : Couldn't activate student now...Please try again later and if problem persists, please contact your administrator");
			exit();
		}
		$mysql_obj->commit();
		$mysql_obj->close_connection();
	}
}
?>