<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Registration extends CI_Controller {

	public function __construct()
	{

		parent::__construct();
		session_start();
		
		$this->load->model('main_m');

	}
	 
	public function index($session='',$company_error='')
	{
		
		$current_event = $this->main_m->get_current_event();
		
		if($session != '')
		{
			$timeFirst  = strtotime($_SESSION['registration_number_date']);
			$timeSecond = strtotime(date('Y-m-d H:i:s'));
			$differenceInSeconds = $timeSecond - $timeFirst;

			if($differenceInSeconds > 3600)
			{
				session_destroy();
				redirect('registration/index');
			}
		}
		
		$data['error'] = 0;
		$data['imas_company'] = $this->main_m->get_imas_company();
		
		
		if($session != '')
		{
			$data = $_SESSION['registration_data']['step1'];
		}
		
		$submit_m = $this->input->post('submit_m');

		if($submit_m == 1)
		{
			$data['main_contact_firstname'] = $this->input->post('main_contact_firstname');
			$data['main_contact_lastname'] = $this->input->post('main_contact_lastname');
			$data['main_contact_email'] = $main_contact_email = $this->input->post('main_contact_email');
			$data['main_contact_contact'] = $this->input->post('main_contact_contact');
			$data['main_contact_jobtitle'] = $this->input->post('main_contact_jobtitle');
			$data['main_contact_imas_membership'] = $this->input->post('main_contact_imas_membership');
			
			
			$data['funding_agree'] = $this->input->post('funding_agree');
			$data['beneficiary_name'] = $this->input->post('beneficiary_name');
			$data['bank_name'] = $this->input->post('bank_name');
			$data['bank_account'] = $this->input->post('bank_account');
			
			
			$_SESSION['domain'] = '';
			
			if($data['main_contact_imas_membership'] == 1)
			{			
				$data['main_contact_companyid'] = $main_contact_companyid = $this->input->post('main_contact_companyid');	
				
				$company_info = $this->main_m->get_company_info($main_contact_companyid);
				
				$domain = $company_info['domain'];
				
				$_SESSION['domain'] = $domain;				
				
			
				$domain_error = 1;
				$domains = explode(',',$domain);
				
				foreach($domains as $dd)
				{
					if (strpos(strtolower($main_contact_email), strtolower($dd)) !== false)
					{
						$domain_error = 0;
					} 
				}
				
			}
			
			if($data['main_contact_imas_membership'] == 2)
			{			
				$data['main_contact_companyname'] = $this->input->post('main_contact_companyname');
			}
			
			$data['promo_code'] = $code = $this->input->post('promo_code');
			
			$data['promo_code_error'] = 0;
			$_SESSION['code'] = '';
				
			unset($_SESSION['participants_info'][1][$_SESSION['registration_number']]['master1']);
			unset($_SESSION['participants_info'][1][$_SESSION['registration_number']]['master2']);
			unset($_SESSION['number_of_pass']);
			
							
			$_SESSION['step3_previous'] = 'step2';
			$_SESSION['member_pricing'] = 0;
			$_SESSION['custom_price'] = '';
			$_SESSION['early_bird_price'] = 0;
						
			if($code != '')
			{
				$_SESSION['member_pricing'] = 0;
				$_SESSION['early_bird_price'] = 0;
				$current_event = $this->main_m->get_current_event();
				$event_id = $current_event['id'];
				$promo_is_active = $this->main_m->check_promo_code($code,$event_id);
				
				if(empty($promo_is_active))
				{
					$data['promo_code_error'] = 1;
				} else {
							
					if($promo_is_active['restrict_to_email'] != '' && !endsWith(strtolower($data['main_contact_email']), strtolower(trim($promo_is_active['restrict_to_email']))))
					{						
						$data['promo_code_error'] = 1;
						
					} else {
						
						$data['promo_code_error'] = 0;
					}
							
					$_SESSION['step3_previous'] = 'step2';
					
				}
				
				//check if promo has been used
				
				$promo_been_used = $this->main_m->check_promo_been_used($code,$event_id);
				
				$complimentary = $promo_is_active['complimentary'];
				$custom_price = $promo_is_active['custom_price'];
			
				
				$used_code = $this->main_m->get_used_code_listing($code);
				$used_code = sizeof($used_code);

				if($promo_is_active['member_price'] == 1)
				{
					$data['promo_code_error'] = 0;
				}

							
				if($used_code >= $complimentary && $promo_is_active['allow_early_bird'] == 0 && $complimentary > 0)
				{
					$data['promo_code_error'] = 2;
				}
				
				
				$max_passes = $promo_is_active['complimentary'];			
				
				$used_code = $this->main_m->get_used_code_listing($code);
				$used_code = sizeof($used_code);
				
				if($max_passes > 0)
				{
						$max_passes = $max_passes - $used_code;
						
						if($max_passes <= 0)
							$data['promo_code_error'] = 3;
				}
			}
				
		
			if($data['promo_code_error'] == 0)
			{
				
				if($_SESSION['registration_number'] == '')
				{
					$_SESSION['registration_number'] = date('Y-m-dHis').'_'.generateRandomString(12);
				}
				
				if($_SESSION['registration_number_date'] == '')
				{
					$_SESSION['registration_number_date'] = date('Y-m-d H:i:s');
				}
				
				unset($_SESSION['registration_data']['step1']);
				$_SESSION['registration_data']['step1'] = $data;
				
				if($data['promo_code_error'] == 0)
				{
					$_SESSION['code'] = $code;	
				}
				
				if($domain_error == 1)
				{
					
					redirect('registration/index/'.$_SESSION['registration_number'].'/1');
					
				} else {
				
					if($promo_is_active['member_price'] == 1)
					{
						$_SESSION['member_pricing'] = 1;
					}
					
					if($promo_is_active['custom_price'] != '')
					{
						$_SESSION['custom_price'] = $custom_price;
					}
						
				
					
					if($promo_is_active['allow_early_bird'] == 1)
					{
						$_SESSION['early_bird_price'] = 1;
					}
					
					redirect('registration/step2a/'.$_SESSION['registration_number']);
					
				}
				
			} else {
				$data['error'] = 1;
				
				if($domain_error == 1)
				{					
					$data['company_error'] = 1;
				}
			}
						
		}
		
		$data['company_error'] = $company_error;
		
		if($domain_error == 1)
		{					
			$data['company_error'] = 1;
		}
		
		
		$this->load->view('frontend/index',$data);
	}

	 
	public function step2a($session='')
	{		
		
		if($_SESSION['registration_number'] == '')
		{
			redirect('registration/index');
		}

		$data['error'] = 0;
		
		$submit_m = $this->input->post('submit_m');

		if($submit_m == 1)
		{		
					
			$number_of_pass = $this->input->post('number_of_pass');			
			
			$_SESSION['number_of_pass'] = $number_of_pass;		
			
			$step = $_SESSION['step'];
			redirect('registration/'.$step);	
		}
		
		$_SESSION['step'] = 'step2';
		
		if($_SESSION['code'] != '')
		{
			$_SESSION['promo_type'] = '';
			
			$current_event = $this->main_m->get_current_event();
			$event_id = $current_event['id'];
			
			if($event_id > 0)
			{
				$promo_is_active = $this->main_m->check_promo_code($_SESSION['code'],$event_id);
				$data['promo'] = $promo_is_active;
				$data['complimentary'] = $data['promo']['complimentary'];
				$_SESSION['promo_type'] = $data['promo']['promo_type'];
				
				if($_SESSION['promo_type'] == 1)					
					$_SESSION['step'] = 'step2';
				
				if($_SESSION['promo_type'] == 2)					
					$_SESSION['step'] = 'step2b';
				
				if($_SESSION['promo_type'] == 3)					
					$_SESSION['step'] = 'step2';
				
			}
		}
		
		
		if($session != '')
		{
			$data['participants'] = $_SESSION['participants'][$session];
			$data['participants_info'] = $_SESSION['participants_info'][$session];
			
		}
		
		
		//determine the max pass
		$max_passes = $data['complimentary'];		
		
		$used_code = $this->main_m->get_used_code_listing($_SESSION['code']);
		$used_code = sizeof($used_code);
		
		if($max_passes > 0)
		{
			$max_passes = $max_passes - $used_code;
			$data['min_passes'] = 1;
			$data['max_passes'] = $max_passes;
			
		} else {
			$data['min_passes'] = 1;
			$data['max_passes'] = 30;
			
		}
		
		$data['session2'] = $session;
		$data['session'] = $session;
		
		
		$this->load->view('frontend/apply2a',$data);
	}

	
	 
	public function step2($session='')
	{		
		
		
		if($_SESSION['registration_number'] == '')
		{
			redirect('registration/index');
		}

		$data['error'] = 0;
		
		$submit_m = $this->input->post('submit_m');

		
		if($submit_m == 1)
		{	
					
			$participants = $_SESSION['number_of_pass'];
			$nric = $this->input->post('nric');				
			$firstname = $this->input->post('fullname');		
			$CFA = $this->input->post('CFA');
			$email = $this->input->post('email');
			$contact = $this->input->post('contact');
			$jobtitle = $this->input->post('jobtitle');
			$company = $this->input->post('company');
			$attending = $this->input->post('attending');
			$in_person = $this->input->post('in_person');
			$vaccinated = $this->input->post('vaccinated');
			$dob = $this->input->post('dob');
			$master1 = $this->input->post('master1');
			$master2 = $this->input->post('master2');
			$meal = $this->input->post('meal');
			$singapore_citizen_pr = $this->input->post('singapore_citizen_pr');		
			
		
			
			for($i=1;$i<=$participants;$i++)
			{
				$firstname_listing[$i] = $firstname[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$CFA_listing[$i] = $CFA[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$lastname_listing[$i] = $lastname[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$email_listing[$i] = $submitted_email = $email[$i];
				
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$contact_listing[$i] = $contact[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$jobtitle_listing[$i] = $jobtitle[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$company_listing[$i] = $company[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$in_person_listing[$i] = $in_person[$i];
			}
			for($i=1;$i<=$participants;$i++)
			{
				$vaccinated_listing[$i] = $vaccinated[$i];
			}
			for($i=1;$i<=$participants;$i++)
			{
				$nric_listing[$i] = $nric[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$singapore_citizen_pr_listing[$i] = $singapore_citizen_pr[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$dob_listing[$i] = $dob[$i];
			}
			
			$master1_listing = array();
			$master2_listing = array();
			
			for($i=1;$i<=$participants;$i++)
			{
					$master1_listing[$i] = $master1[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
					$master2_listing[$i] = $master2[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$meal_listing[$i] = $meal[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				if($_SESSION['promo_type'] == 1 && $_SESSION['promo_type'] == 2)
				{
					$attending_list[$i] = 0;
					
				} else {
					$attending_list[$i] = $attending[$i];
					
				}
			}
			
			unset($_SESSION['participants_info'][1][$_SESSION['registration_number']]['master1']);
			unset($_SESSION['participants_info'][1][$_SESSION['registration_number']]['master2']);
			
			$_SESSION['participants_info'] = '';
			$_SESSION['participants'][1][$_SESSION['registration_number']] = '';
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['fullname'] = $firstname_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['CFA'] = $CFA_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['email'] = $email_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['contact'] = $contact_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['jobtitle'] = $jobtitle_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['company'] = $company_listing;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['attending'] = $attending_list;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['in_person'] = $in_person_listing;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['vaccinated'] = $vaccinated_listing;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['nric'] = $nric_listing;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['singapore_citizen_pr'] = $singapore_citizen_pr_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['dob'] = $dob_listing;	
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['master1'] = $master1_listing;
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['master2'] = $master2_listing;		
			$_SESSION['participants_info'][1][$_SESSION['registration_number']]['meal'] = $meal_listing;		
		
	
				redirect('registration/step3');	
		}
		
		if($_SESSION['code'] != '')
		{
			$current_event = $this->main_m->get_current_event();
			$event_id = $current_event['id'];
			
			if($event_id > 0)
			{
				$promo_is_active = $this->main_m->check_promo_code($_SESSION['code'],$event_id);
				$data['promo'] = $promo_is_active;
				$data['complimentary'] = $data['promo']['complimentary'];
				$data['request_in_person'] = $data['promo']['request_in_person'];
				$data['hide_masterclass'] = $data['promo']['hide_masterclass'];
				$data['hide_funding'] = $data['promo']['hide_funding'];
				$data['pre_booking'] = $data['promo']['pre_booking'];
				$_SESSION['pre_booking'] = $data['pre_booking'];
			}
		}
		
		
		if($session != '')
		{
			$data['participants'] = $_SESSION['participants'][1][$session];
			$data['participants_info'] = $_SESSION['participants_info'][1][$session];
			
		}
		
		$data['session'] = $_SESSION['registration_number'];
		$data['session2'] = $session;
		$num_of_of_pass = $data['number_of_pass'] = $_SESSION['number_of_pass'];
		
		$class_capacity = $this->main_m->get_class_capacity();
		
		foreach($class_capacity as $cc)
		{
			$full_class_capacity[$cc['name']] = $cc['amount'];
		}
	
	 	$list1 = array('ESG – Part 1 & 2 (2pm to 5pm)','Leadership & Human Capital','Risk Management');
	 	$list2 = array('Private Assets','Digitalisation');
		
		foreach($list1 as $i)
		{
			$listing[$i] = $this->main_m->registration_class_count('master1',$i);
		}
		foreach($list2 as $i)
		{
			$listing[$i] = $this->main_m->registration_class_count('master1',$i);
		}
		
		
		
		$data['full_class_capacity'] = $full_class_capacity;
		$data['listing'] = $listing;
		
		$this->load->view('frontend/apply2',$data);
	}
	
	
	public function step2b($session='')
	{		
		
		if($_SESSION['registration_number'] == '')
		{
			redirect('registration/index');
		}

		$data['error'] = 0;
		
		$submit_m = $this->input->post('submit_m');

		if($submit_m == 1)
		{	
					
			$participants = $_SESSION['number_of_pass'];
			$badge = $this->input->post('badge');		
			$nric = $this->input->post('nric');			
			$firstname = $this->input->post('fullname');	
			$CFA = $this->input->post('CFA');
			$email = $this->input->post('email');
			$contact = $this->input->post('contact');
			$jobtitle = $this->input->post('jobtitle');
			$company = $this->input->post('company');
					
			
			for($i=1;$i<=$participants;$i++)
			{
				$badge_listing[$i] = $badge[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$nric_listing[$i] = $nric[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$firstname_listing[$i] = $firstname[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$CFA_listing[$i] = $CFA[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$lastname_listing[$i] = $lastname[$i];
			}
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$email_listing[$i] = $submitted_email = $email[$i];
				
			}
			
			
			
			for($i=1;$i<=$participants;$i++)
			{
				$contact_listing[$i] = $contact[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$jobtitle_listing[$i] = $jobtitle[$i];
			}
			
			for($i=1;$i<=$participants;$i++)
			{
				$company_listing[$i] = $company[$i];
			}
			
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['badge'] = $badge_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['nric'] = $nric_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['fullname'] = $firstname_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['CFA'] = $CFA_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['email'] = $email_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['contact'] = $contact_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['jobtitle'] = $jobtitle_listing;
			$_SESSION['participants_info'][2][$_SESSION['registration_number']]['company'] = $company_listing;			
			
			redirect('registration/step3');	
			
		}
		
		if($_SESSION['code'] != '')
		{
			$current_event = $this->main_m->get_current_event();
			$event_id = $current_event['id'];
			
			if($event_id > 0)
			{
				$promo_is_active = $this->main_m->check_promo_code($_SESSION['code'],$event_id);
				$data['promo'] = $promo_is_active;
				$data['complimentary'] = $data['promo']['complimentary'];
			}
		}
		
		if($session != '')
		{
			$data['participants'] = $_SESSION['participants'][2][$session];
			$data['participants_info'] = $_SESSION['participants_info'][2][$session];
			
			
		}
		
		//determine people whom attending dap
			
		$attending = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['attending'];
		

		foreach($attending as $key=>$a)
		{
			if($a == 1)
				$attending_list[] = $key;
		}
		
		$remaining_people = $_SESSION['number_of_pass'];
		$remaining_people = $remaining_people - sizeof($attending_list);
		
			
		if($_SESSION['code'] != '' && $_SESSION['promo_type'] == 2)
		{
			$previous = 'step2a';
		} else {
			$previous = 'step2';
		}
		
		if($_SESSION['code'] != '' && $_SESSION['promo_type'] == 3)
		{
			$previous = 'step2';
		} 
		
		$data['session'] = $_SESSION['registration_number'];
		$data['session2'] = $session;
		$data['number_of_pass'] = $_SESSION['number_of_pass'];
		$data['attending_list'] = $attending_list;
		$data['remaining_people'] = $remaining_people;
		$data['previous'] = $previous;		
		
		
		$this->load->view('frontend/apply2b',$data);
	}

	public function step3()
	{
		
		if($_SESSION['registration_number'] == '')
		{
			redirect('registration/index');
		}

		$data['error'] = 0;
		
		$submit_m = $this->input->post('submit_m');

		if($submit_m == 1)
		{
			$_SESSION['agree3'] = 0;
			$_SESSION['agree4'] = 0;
			$agree1 = $this->input->post('agree1');
			$agree2 = $this->input->post('agree2');
			$_SESSION['agree3'] = $this->input->post('agree3');
			$_SESSION['agree4'] = $this->input->post('agree4');
			
			if($agree1 && $agree2)			
				redirect('registration/step4');	
		}		
		
		$data['session'] = $_SESSION['registration_number'];
		$data['step3_previous'] = $_SESSION['step3_previous'];
		
		$this->load->view('frontend/apply3',$data);
	}

	public function step4()
	{
		
		if($_SESSION['registration_number'] == '')
		{
			redirect('registration/index');
		}

		$data['error'] = 0;
		
		$submit_m = $this->input->post('submit_m');

		if($submit_m == 1)
		{		
			
			$datar['main_contact_firstname'] = $_SESSION['registration_data']['step1']['main_contact_firstname'];
			$datar['main_contact_lastname'] = $_SESSION['registration_data']['step1']['main_contact_lastname'];
			$datar['main_contact_email'] = $_SESSION['registration_data']['step1']['main_contact_email'];
			$datar['main_contact_contact'] = $_SESSION['registration_data']['step1']['main_contact_contact'];
			$datar['main_contact_jobtitle'] = $_SESSION['registration_data']['step1']['main_contact_jobtitle'];
			$datar['main_contact_imas_membership'] = $_SESSION['registration_data']['step1']['main_contact_imas_membership'];
			$datar['main_contact_companyname'] = $_SESSION['registration_data']['step1']['main_contact_companyname'];
			$datar['main_contact_companyid'] = $_SESSION['registration_data']['step1']['main_contact_companyid'];
			
			
			
			$datar['funding_agree'] = $_SESSION['registration_data']['step1']['funding_agree'];
			$datar['beneficiary_name'] = $_SESSION['registration_data']['step1']['beneficiary_name'];
			$datar['bank_name'] = $_SESSION['registration_data']['step1']['bank_name'];
			$datar['bank_account'] = $_SESSION['registration_data']['step1']['bank_account'];
			
			$datar['option1'] = $_SESSION['agree3'];			
			$datar['option2'] = $_SESSION['agree4'];
			
			if($datar['main_contact_companyid'] == '')
				$datar['main_contact_companyid'] = 0;
			
			$current_event = $this->main_m->get_current_event();
			$event_id = $current_event['id'];

			$complimentary = 0;
			
			$item_name = 'IMAS Investment Conference & Masterclass 2024 ';
			
			$ticket_custom_price = '';
			$prebooking = 0;
			
			if($_SESSION['code'] != '')
			{
				$code = $_SESSION['code'];
				$promo_is_active = $this->main_m->check_promo_code($code,$event_id);
				
				if(!empty($promo_is_active))
				{
					$datar['promo_code'] = $code;
					
					if($promo_is_active['custom_price'] == '')
						$datar['complimentary'] = $complimentary = $promo_is_active['complimentary'];
					
					$datar['restrict_to_email'] = $promo_is_active['restrict_to_email'];
					$prebooking = $promo_is_active['pre_booking'];
					$promo_type = $promo_is_active['promo_type'];
					$ticket_custom_price = $promo_is_active['custom_price'];
				
				}
					
			}
			
			
			$number_of_pass = $_SESSION['number_of_pass'];
		
			if($number_of_pass >= 5)
			{
				$group = 0;
			} else {
				$group = 0;
			}
			
			if($_SESSION['registration_data']['step1']['main_contact_imas_membership'] != '')
				$main_contact_imas_membership = $_SESSION['registration_data']['step1']['main_contact_imas_membership'];

			if($main_contact_imas_membership == 1 || $_SESSION['member_pricing'] == 1)
			{
				if($group == 0)
				{
					$invoice_type = 6;
					$ticket_price = $current_event['imas_member_price'];
				} else {
					$invoice_type = 5;
					$ticket_price = $current_event['group_imas_member_discount_price'];

				}
				
				if($_SESSION['member_pricing'] == 1)
				{
					if($group == 0)
					{
						$invoice_type = 8;
					} else {
						$invoice_type = 7;

					}
				}
			}

			if($main_contact_imas_membership == 2 && $_SESSION['member_pricing'] == 0)
			{
				if($group == 0)
				{
					$invoice_type = 8;
					$ticket_price = $current_event['regular_price'];
				} else {
					$invoice_type = 7;
					$ticket_price = $current_event['group_discount_price'];

				}

			}
			
			
			if($_SESSION['custom_price'] != '')
			{
				$invoice_type = 9;
				$ticket_price = $_SESSION['custom_price'];

			}
				
			$today = date('Y-m-d');

			$early_bird_amount = 0;
			if(($today >= $current_event['earlybird_start'] && $today <= $current_event['earlybird_end']) || $_SESSION['early_bird_price'] == 1)	
			{
				if($main_contact_imas_membership == 1 || $_SESSION['member_pricing'] == 1)
				{
					if($group == 0)
					{
						$invoice_type = 2;
						$ticket_price = $current_event['early_bird_imas_member_price'];
					} else {
						$invoice_type = 1;
						$ticket_price = $current_event['early_bird_group_imas_member_discount_price'];

					}
					
					if($_SESSION['member_pricing'] == 1)
					{
						if($group == 0)
						{
							$invoice_type = 4;
						} else {
							$invoice_type = 3;

						}
					}
					
					$early_bird_amount = $ticket_price;
				}
				if($main_contact_imas_membership == 2 && $_SESSION['member_pricing'] == 0)
				{
					if($group == 0)
					{
						$invoice_type = 4;
						$ticket_price = $current_event['early_bird_price'];
					} else {
						$invoice_type = 3;
						$ticket_price = $current_event['early_bird_group_discount_price'];

					}
					
					$early_bird_amount = $ticket_price;

				}

			}
			
			$invoice_name = 'IMAS Investment Conference & Masterclass 2024  (TGS-2024041950)';
			
			$datar['prebooking'] = $prebooking;
			$datar['item_name'] = $item_name;
			$datar['ticket_price'] = $ticket_price;
			$datar['invoice_type'] = $invoice_type;
			$datar['invoice_name'] = $invoice_name;
					
			$full_total = 0;
			$total = 0;
			$gst_amount = 0;
			$grand_total = 0;						
							
			$datar['num_of_participants'] = $num_of_participants = $_SESSION['number_of_pass'];
		
			$qty = $num_of_participants;
			$full_total = $qty*$ticket_price;
	
			
			if($complimentary > 0 && $_SESSION['custom_price'])
			{
				$full_total = $full_total - ($num_of_participants*$ticket_price);
				$complimentary_amount = $qty*$ticket_price;
			}
			
			if($ticket_custom_price > 0)
			{				
				$full_total = $qty*$ticket_custom_price;
				$ticket_price = $ticket_custom_price;
			}
			
			$total = $full_total;
			
			$gst_amount = $total*0.09;
			$grand_total = $total*1.09;
			
			$datar['complimentary_amount'] = $complimentary_amount;
			$datar['early_bird_amount'] = number_format($early_bird_amount);		
			$datar['full_total'] = $full_total;
			$datar['total'] = $total;
			$datar['gst_amount'] = number_format($gst_amount,2);
			$datar['grand_total'] = number_format($grand_total,2);
		
			$datar['payment_method'] = $this->input->post('payment_method');
			$datar['company_uen'] = $this->input->post('company_uen');
			$payment_method = $datar['payment_method'];
			$datar['status'] = 0;
			$datar['paid'] = 0;
			
			if($complimentary > 0)
				$datar['paid'] = 1;				
			
			$datar['member_price'] = $_SESSION['member_pricing'];
			$datar['custom_price'] = $_SESSION['custom_price'];
			$datar['early_bird_price'] = $_SESSION['early_bird_price'];
			$datar['datetime_submission'] = date('Y-m-d H:i:s');	
			
				
			if($prebooking == 0)
			{
				$trans_num = $this->main_m->get_trans_num();
				$trans_num = intval($trans_num);
				$transaction_num  = '2024-IMAS-'.str_pad($trans_num, 7, "0", STR_PAD_LEFT);				
			} else {
				$trans_num = $this->main_m->get_trans_num2();
				$trans_num = intval($trans_num);
				$transaction_num  = '2024-IMAS-PB-'.str_pad($trans_num, 7, "0", STR_PAD_LEFT);	
				
			}
			
			
			$current_event = $this->main_m->get_current_event();
			
			
			
			$datar['registration_id'] = $current_event['id'];
			
			$primary_id = $id = $this->main_m->add_registration($datar);
			
			if($id > 0)
			{
				$datar2['transaction_num'] = $transaction_num;
				$datar2['invoice_no'] = $transaction_num;
				$this->main_m->update_registration($id,$datar2);
				
				$trans_no = $datar2['transaction_num'];
				
				$current_event = $this->main_m->get_current_event();
				$event_id = $current_event['id'];
				
				//upload participants
				$complimentary_num = 0;
								
				
				for($i=1;$i<=$num_of_participants;$i++)
				{
					if($complimentary > 0)
					{
						if($i <= $complimentary)
						{
							$complimentary_num = 1;
							$code = $_SESSION['code'];

						} else {						
							$complimentary_num = 0;
							$code = '';
						}
						
					} else {
						$complimentary_num = 0;;
						$code = '';
						
					}
					
					if($_SESSION['member_pricing'] == 1)
					{
						$code = $_SESSION['code'];
					}					
					
					if($_SESSION['early_bird_price'] == 1)
					{
						$code = $_SESSION['code'];
					}
					
					
					if($_SESSION['custom_price'] != '')
					{
						$code = $_SESSION['code'];
					}
					
					
					$attending = 0;
					$datar3 = array();
					$datar3['event_id'] = $event_id;
					$datar3['registration_id'] = $id;
					$datar3['reg_type'] = 1;
					$datar3['menu_order'] = $i;
					$datar3['complimentary'] = $complimentary_num;
					
					if($complimentary_num == 1)
						$datar3['confirmed'] = 1;		
					
					$dob = '';
					
					$datar3['custom_price'] = $_SESSION['custom_price'];
					$datar3['code'] = $code;
					$datar3['badge'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['badge'][$i];
					$datar3['fullname'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['fullname'][$i];
					$datar3['CFA'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['CFA'][$i];
					$datar3['badge'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['badge'][$i];
					$datar3['email'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['email'][$i];
					$datar3['contact'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['contact'][$i];
					$datar3['jobtitle'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['jobtitle'][$i];
					$datar3['company'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['company'][$i];
					$datar3['in_person'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['in_person'][$i];	
					$datar3['vaccinated'] = $singapore_pr = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['vaccinated'][$i];
					$datar3['nric'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['nric'][$i];
					$datar3['singapore_citizen_pr'] = $singapore_citizen_pr = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['singapore_citizen_pr'][$i];	
					$datar3['dob'] = $dob = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['dob'][$i];	
					$datar3['master1'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['master1'][$i];
					$datar3['master2'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['master2'][$i];
					$datar3['meal'] = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['meal'][$i];		
					
					$funding_amount = 0;
					
					if($singapore_pr == 1)
						$funding_amount = $this->calculate_individual_funding($ticket_price,$dob,$singapore_citizen_pr);
					
					
					$datar3['full_amount'] = $ticket_price;
					$datar3['nett_amount'] = $ticket_price - $funding_amount;
					$datar3['funding_amount'] = $funding_amount;
					
					$total_funding += $funding_amount;
					
					$attending = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['attending'][$i];							
					
					if($payment_method == 3)
						$datar3['confirmed'] = 1;
					
					if($datar3['fullname'] == '')
						continue;
					
					$p_id = $this->main_m->add_registration_participants($datar3);
					
					$p_ida = str_pad($p_id, 7, "0", STR_PAD_LEFT);
					
					$pdata['qr_number'] = $p_ida;
					$this->main_m->update_registration_participants($p_id,$pdata);
					
					if($attending == 1)
					{
						$datar3['reg_type'] = 2;
						$datar3['nric'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['nric'][$i];;
						
						$p_id = $this->main_m->add_registration_participants($datar3);
					
						$p_ida = str_pad($p_id, 7, "0", STR_PAD_LEFT);

						$pdata['qr_number'] = $p_ida;
						$this->main_m->update_registration_participants($p_id,$pdata);
					}
				}				
				
			}
			
				for($i=1;$i<=$num_of_participants;$i++)
				{
					if($complimentary > 0)
					{
						if($i <= $complimentary)
						{
							$complimentary_num = 1;
							$code = $_SESSION['code'];

						} else {						
							$complimentary_num = 0;
							$code = '';
						}
						
					} else {
						$complimentary_num = 0;;
						$code = '';
						
					}
					
					if($_SESSION['member_pricing'] == 1)
					{
						$code = $_SESSION['code'];
					}
					
					if($_SESSION['early_bird_price'] == 1)
					{
						$code = $_SESSION['code'];
					}
										
					if($_SESSION['custom_price'] != '')
					{
						$code = $_SESSION['code'];
					}
					
					if($_SESSION['custom_price'] != '')						
						$complimentary_num = 0;
					
					$attending = 0;
					$datar3 = array();
					$datar3['event_id'] = $event_id;
					$datar3['registration_id'] = $id;
					$datar3['reg_type'] = 2;
					$datar3['menu_order'] = $i;
					$datar3['complimentary'] = $complimentary_num;
										
					if($complimentary_num == 1)
						$datar3['confirmed'] = 1;						
					
					$datar3['custom_price'] = $_SESSION['custom_price'];
					$datar3['code'] = $code;
					$datar3['badge'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['badge'][$i];
					$datar3['fullname'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['fullname'][$i];
					$datar3['CFA'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['CFA'][$i];
					$datar3['badge'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['badge'][$i];
					$datar3['nric'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['nric'][$i];
					$datar3['email'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['email'][$i];
					$datar3['contact'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['contact'][$i];
					$datar3['jobtitle'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['jobtitle'][$i];
					$datar3['company'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['company'][$i];
					$datar3['in_person'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['in_person'][$i];
					$datar3['vaccinated'] = $singapore_pr = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['vaccinated'][$i];	
					$datar3['nric'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['nric'][$i];
					$datar3['singapore_citizen_pr'] = $singapore_citizen_pr = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['singapore_citizen_pr'][$i];		
					$datar3['dob'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['dob'][$i];	
					$datar3['master1'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['master1'][$i];
					$datar3['master2'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['master2'][$i];
					$datar3['meal'] = $_SESSION['participants_info'][2][$_SESSION['registration_number']]['meal'][$i];
					$funding_amount = 0;
					
					if($singapore_pr == 1)
						$funding_amount = $this->calculate_individual_funding($ticket_price,$dob,$singapore_citizen_pr);
					
					$total_funding += $funding_amount;
					
					$datar3['full_amount'] = $ticket_price;
					$datar3['nett_amount'] = $ticket_price - $funding_amount;
					$datar3['funding_amount'] = $funding_amount;
					
					
					

					if($payment_method == 3)
						$datar3['confirmed'] = 1;
					
					if($datar3['fullname'] == '')
						continue;
					
					$p_id = $this->main_m->add_registration_participants($datar3);
					
					$p_ida = str_pad($p_id, 7, "0", STR_PAD_LEFT);
					
					$pdata['qr_number'] = $p_ida;
					$this->main_m->update_registration_participants($p_id,$pdata);
					
				}	
			
			
			$datar_t['total_net'] = $full_total-$total_funding;
			$datar_t['deposit'] = $total_funding;
			$datar_t['funding'] = $total_funding;
			$this->main_m->update_registration($id,$datar_t);
				
			
			$invoice_firstname = $this->input->post('invoice_firstname');
			$invoice_lastname = $this->input->post('invoice_lastname');
			$invoice_email = $this->input->post('invoice_email');
			$invoice_contact = $this->input->post('invoice_contact');
			$invoice_address1 = $this->input->post('invoice_address1');
			$invoice_address2 = $this->input->post('invoice_address2');
			$invoice_postalcode = $this->input->post('invoice_postalcode');
			$invoice_attention_to = $this->input->post('invoice_attention_to');
		
			
			if($payment_method == 4)
			{		
				
				$datar_1['status'] = 2;
				$datar_1['paid'] = 1;
				$datar_1['invoice_firstname'] = $invoice_firstname;
				$datar_1['invoice_lastname'] = $invoice_lastname;
				$datar_1['invoice_email'] = $invoice_email;
				$datar_1['invoice_contact'] = $invoice_contact;
				$datar_1['invoice_address1'] = $invoice_address1;
				$datar_1['invoice_address2'] = $invoice_address2;
				$datar_1['invoice_postalcode'] = $invoice_postalcode;
				$datar_1['invoice_attention_to'] = $invoice_attention_to;
				$datar_1['datetime_submission2'] = date('Y-m-d H:i:s');
				$datar_1['datetime_paid'] = date('Y-m-d H:i:s');
				$datar_1['datetime_confirmed'] = date('Y-m-d H:i:s');
			
				$this->main_m->update_registration($id,$datar_1);
				
				
				redirect('registration/thankyou');	
			}
			
			
			if($payment_method == 3)
			{		
				
				$datar_1['status'] = 2;
				$datar_1['paid'] = 1;
				$datar_1['invoice_firstname'] = $invoice_firstname;
				$datar_1['invoice_lastname'] = $invoice_lastname;
				$datar_1['invoice_email'] = $invoice_email;
				$datar_1['invoice_contact'] = $invoice_contact;
				$datar_1['invoice_address1'] = $invoice_address1;
				$datar_1['invoice_address2'] = $invoice_address2;
				$datar_1['invoice_postalcode'] = $invoice_postalcode;
				$datar_1['invoice_attention_to'] = $invoice_attention_to;
				$datar_1['datetime_submission2'] = date('Y-m-d H:i:s');
				$datar_1['datetime_paid'] = date('Y-m-d H:i:s');
				$datar_1['datetime_confirmed'] = date('Y-m-d H:i:s');
			
				$this->main_m->update_registration($id,$datar_1);
				
				
				redirect('registration/thankyou');	
			}
			
			
			if($payment_method == 1)
			{	
						
				$datar_1['status'] = 1;
				$datar_1['invoice_firstname'] = $invoice_firstname;
				$datar_1['invoice_lastname'] = $invoice_lastname;
				$datar_1['invoice_email'] = $invoice_email;
				$datar_1['invoice_contact'] = $invoice_contact;
				$datar_1['invoice_address1'] = $invoice_address1;
				$datar_1['invoice_address2'] = $invoice_address2;
				$datar_1['invoice_postalcode'] = $invoice_postalcode;
				$datar_1['invoice_attention_to'] = $invoice_attention_to;
				$datar_1['datetime_submission2'] = date('Y-m-d H:i:s');
				$this->main_m->update_registration($id,$datar_1);
				
				$this->send_acknowledgement($primary_id);
				
				redirect('registration/thankyou');	
			}
			
			
			if($payment_method == 2)
			{					
							
				$datar_2['invoice_firstname'] = $invoice_firstname;
				$datar_2['invoice_lastname'] = $invoice_lastname;
				$datar_2['invoice_email'] = $invoice_email;
				$datar_2['invoice_contact'] = $invoice_contact;
				$datar_2['invoice_address1'] = $invoice_address1;
				$datar_2['invoice_address2'] = $invoice_address2;
				$datar_2['invoice_postalcode'] = $invoice_postalcode;
				$datar_2['invoice_attention_to'] = $invoice_attention_to;
				$datar_2['status_to_payment'] = 1;	
				$datar_2['datetime_redirect_to_payment'] = date('Y-m-d H:i:s');
										

				if($complimentary_num > 0)
				{ 		
					$datar_2['status'] = 2;
					$datar_2['paid'] = 1;
					$datar_2['datetime_paid'] = date('Y-m-d H:i:s');
					$datar_2['datetime_confirmed'] = date('Y-m-d H:i:s');
				
					$this->main_m->update_registration($id,$datar_2);		 
					redirect('registration/thankyou');
				} else {
					
					$this->main_m->update_registration($id,$datar_2);					
					
					$this->invoice($id);
				
					header('Location: '.$this->config->item('base_url').'/paypal-express-checkout/process.php?trans_no='.$trans_no);	
				}
				
				
				
			}
			
		}
				
		$current_event = $this->main_m->get_current_event();
		
		$today = date('Y-m-d');
		
		$number_of_pass = $_SESSION['number_of_pass'];
	
		
		if($number_of_pass >= 5)
		{
			$group = 0;
		} else {
			$group = 0;
		}
		
		
		if($_SESSION['registration_data']['step1']['main_contact_imas_membership'] != '')
			$main_contact_imas_membership = $_SESSION['registration_data']['step1']['main_contact_imas_membership'];
		
		
		if($main_contact_imas_membership == 1 || $_SESSION['member_pricing'] == 1)
		{
			if($group == 0)
			{
				$ticket_price = $current_event['imas_member_price'];
			} else {
				$ticket_price = $current_event['group_imas_member_discount_price'];
				
			}
		}
		if($main_contact_imas_membership == 2 && $_SESSION['member_pricing'] == 0)
		{
			if($group == 0)
			{
				$ticket_price = $current_event['regular_price'];
			} else {
				$ticket_price = $current_event['group_discount_price'];
				
			}
			
		}
			
		$today = date('Y-m-d');
		if(($today >= $current_event['earlybird_start'] && $today <= $current_event['earlybird_end']) || $_SESSION['early_bird_price'] == 1)	
		{
			if($main_contact_imas_membership == 1 || $_SESSION['member_pricing'] == 1)
			{
				if($group == 0)
				{
					$ticket_price = $current_event['early_bird_imas_member_price'];
				} else {
					$ticket_price = $current_event['early_bird_group_imas_member_discount_price'];

				}
			}
			if($main_contact_imas_membership == 2 && $_SESSION['member_pricing'] == 0)
			{
				if($group == 0)
				{
					$ticket_price = $current_event['early_bird_price'];
				} else {
					$ticket_price = $current_event['early_bird_group_discount_price'];

				}

			}

		}
		
	
		if($_SESSION['custom_price'] != '')
		{
			$invoice_type = 9;
			$ticket_price = $_SESSION['custom_price'];

		}

		$complimentary = 0;
		$item_name = 'IMAS Investment Conference & Masterclass 2024 ';
		
		if($_SESSION['code'] != '')
		{
			$code = $_SESSION['code'];
			$current_event = $this->main_m->get_current_event();
			$event_id = $current_event['id'];
			$promo_is_active = $this->main_m->check_promo_code($code,$event_id);

			if(!empty($promo_is_active))
			{
				if($promo_is_active['custom_price'] == '')
					$complimentary = $promo_is_active['complimentary'];
				
				$promo_type = $promo_is_active['promo_type'];
				
				if($promo_type == 1)					
					$item_name = 'Conference Pass';
				
				if($promo_type == 2)
					$item_name = 'Showcase Pass';
			}
			
				$data['promo'] = $promo_is_active;
		}	
		

	
		$data['session'] = $_SESSION['registration_number'];
		$data['item_name'] = $item_name;		
		$data['ticket_price'] = $ticket_price;
		$data['complimentary'] = $complimentary;
		$data['qty'] = $_SESSION['number_of_pass'];
		
		
		$num_of_participants = $_SESSION['number_of_pass'];
	
		$funding_amount = 0;
		$claim_funding = 0;
		
		for($i=1;$i<=$num_of_participants;$i++)
		{


			$dob = '';
			$is_funding = '';


			$is_funding = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['vaccinated'][$i];
			$singapore_citizen_pr = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['singapore_citizen_pr'][$i];	
			$dob = $_SESSION['participants_info'][1][$_SESSION['registration_number']]['dob'][$i];	

			if($is_funding == 1)
			{
				$funding_amount += $this->calculate_individual_funding($ticket_price,$dob,$singapore_citizen_pr);
				$claim_funding = 1;	
			}
		}
		
	
		$data['funding'] = $funding_amount;
		$data['claim_funding'] = $claim_funding;
		
		$this->load->view('frontend/apply4',$data);
	}
	

	public function thankyou()
	{
		
		$data['error'] = 0;
		session_destroy();
		
		$this->load->view('frontend/thankyou',$data);
	}
	
	public function payment_failure()
	{
		$data['error'] = 0;
		session_destroy();
		
		$this->load->view('frontend/payment_failure',$data);
	}
	
	

	public function send_acknowledgement($primary_id='')
	{
		
		if($primary_id == '')
			return false;
		
		$registration_info = $this->main_m->get_registration_info($primary_id);
		
		if($registration_info['payment_method'] != 1 && $registration_info['payment_method'] != 3)
			return false;
		
		$event_info = $this->main_m->get_event_info($registration_info['registration_id']);
		$year = date('Y',strtotime($event_info['event_date']));
		
		if($registration_info['main_contact_imas_membership'] == 1)
		{
			$company_info = $this->main_m->get_imas_company_info($registration_info['main_contact_companyid']);
			$companyname = $company_info['company'];
		} else {
			$companyname = $registration_info['main_contact_companyname'];
		}
		
		$to = $registration_info['main_contact_email'];
		$data['year'] = $year;
		$data['event_info'] = $event_info;
		$data['companyname'] = $companyname;
		$data['registration_info'] = $registration_info;
		$data['registration_id'] = $registration_id = $registration_info['transaction_num'];
		$data['name'] = $registration_info['main_contact_firstname'].' '.$registration_info['main_contact_lastname'];
		$data['tnc_link'] = site_url('registration/tnc');
		
		$html = $this->load->view('emails/acknowledgement',$data,true);
		
		$from = $this->config->item('from');
		$username = $this->config->item('username');
		$password = $this->config->item('password');
		$host = $this->config->item('host');
		$port = $this->config->item('port');
		$from_name = $this->config->item('from_name');
		
		$subject = 'Confirmation for IMAS Investment Conference & Masterclass 2024 - '.$registration_id;
		
		$invoice_pdf = $this->invoice($primary_id);
		
		if($invoice_pdf != '')
		{
			$file = 'uploads/'.$invoice_pdf.'.pdf';
			$filename = $invoice_pdf.'.pdf';	
		}
		
		
		send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$html,$file,$filename);
		
	}
	
	private function calculate_funding($primary_id)
	{
		$total_funding = 0;
		
		$registration_info = $this->main_m->get_registration_info($primary_id);	
		$people_listing = $this->main_m->get_registration_participants_list($primary_id);
		
		$unit_price = $registration_info['ticket_price'];
		$custom_price = $registration_info['custom_price'];
		
		if($custom_price > 0)
			$unit_price = $custom_price;
		
		$total_funding = 0;
			
		foreach($people_listing as $p)
		{			
			$funding_amount = 0;
			$age = '';
			$singapore_citizen_pr = '';
			
			$dob2 = explode('-',$p['dob']);
			$singapore_citizen_pr = $p['vaccinated'];
			$year = $dob2[0];
			$age = date('Y')-$year;
			
			if($singapore_citizen_pr == 1)
			{
				if($age >= 40)
				{
					$funding_amount = $unit_price*0.7;
				} else {
					$funding_amount = $unit_price*0.3;
					
				}
			}

			if($funding_amount > 500)
				$funding_amount = 500;
			
			
			$total_funding += $funding_amount;
		}
			
		return $total_funding;
	}
	
	
	private function calculate_individual_funding($unit_price,$dob,$singapore_citizen_pr)
	{
		
		$total_funding = 0;
		
		$funding_amount = 0;
		$age = '';

		$dob2 = explode('-',$dob);
		$year = $dob2[0];
		$age = date('Y')-$year;
		
		
		if($singapore_citizen_pr == 1)
		{

			if($age >= 40)
			{

				$funding_amount = $unit_price*0.7;


			} else {
				
				$unit_price = 900;

				$funding_amount = $unit_price*0.3;

			}

		}

		if($singapore_citizen_pr == 2)
		{

			$funding_amount = $unit_price*0.3;

		}
		
		if($funding_amount > 500)
			$funding_amount = 500;

		return $funding_amount;
	}
	
	
	public function invoice($primary_id='')
	{		
		if($primary_id == '')
			return false;
		
		$registration_info = $this->main_m->get_registration_info($primary_id);		
		
		if($registration_info['payment_method'] != 1 && $registration_info['payment_method'] != 2 && $registration_info['payment_method'] != 3)
			return false;
		
		$event_info = $this->main_m->get_event_info($registration_info['registration_id']);
		
		if($registration_info['main_contact_imas_membership'] == 1)
		{
			$company_info = $this->main_m->get_imas_company_info($registration_info['main_contact_companyid']);
			$companyname = $company_info['company'];
			$address = $company_info['address'];
		} else {
			$companyname = $registration_info['main_contact_companyname'];
		}
			
		$people_listing = $this->main_m->get_registration_participants_list($primary_id);
		
		foreach($people_listing as $p)
		{
			$people[] = $p['fullname'];
		}
		
		$data['people'] = $people;
		$data['event_info'] = $event_info;
		$data['companyname'] = $companyname;
		$data['address'] = $address;
		$data['registration_info'] = $registration_info;
		$data['people_listing'] = $people_listing;
		$data['qty'] = $registration_info['num_of_participants'];		
		$data['invoice_no'] = $registration_info['invoice_no'];
		$data['date'] = date('d F Y');
		$data['due_date'] = date('d F Y',strtotime('+14 days'));
		$data['name'] = $registration_info['main_contact_firstname'].' '.$registration_info['main_contact_lastname'];
		$data['registration_id'] = $registration_info['transaction_num'];
		
		$all_courses = array('2pm - 3.30pm Masterclass','3.30pm - 5pm Masterclass');
		$data['all_courses'] = $all_courses;
		
		$html = $this->load->view('emails/invoice_pdf',$data,true);
		
		
		pdf_create($html,'Invoice-'.$data['invoice_no'],false);		
		
		if($data['invoice_no'] != '')
		{
			return 'Invoice-'.$data['invoice_no'];
		} else {
			return '';
		}
	}
	
	public function tnc()
	{
				
		$this->load->view('frontend/tnc',$data);
	}
}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */