<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Attendees extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		session_start();
		
		if(!isset($_SESSION['admin']) && $_SESSION['admin'] == '')
		{
			redirect('backend/main/index');
		}

		$this->load->model('admin_m');		
		$this->load->model('audits_m');	
		$this->load->model('events_m');

	}
	
	function num_to_letters($n)
	{
		$n -= 1;
		for ($r = ""; $n >= 0; $n = intval($n / 26) - 1)
			$r = chr($n % 26 + 0x41) . $r;
		return $r;
	}
	
	function export_in_person($registration_id='',$reg_type='')
	{
		
		$current_event = $this->events_m->is_current_event();
		$event_id = $current_event['id'];
		
		
		if($registration_id != '' && $registration_id > 0)
		{			
			$attendees = $this->events_m->get_attendees_all($registration_id,1,$reg_type);
			
		} else {
			
			$attendees = $this->events_m->get_registration_participants_by_event_in_person($event_id,1);
		}		
		
		//$object->getActiveSheet()->getStyle($this->num_to_letters(3).$excel_row)->getFont()->setBold(true);
		//$object->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		
		if($reg_type == 1)
		{
		
			foreach($attendees as $a)
			{
				if($a['join_type'] == 1)
				{
					$join_type1 = 'Yes';
					$join_type2 = 'Yes';
				}

				if($a['join_type'] == 2)
				{
					$join_type1 = 'Yes';
					$join_type2 = 'No';
				}

				if($a['join_type'] == 3)
				{
					$join_type1 = 'No';
					$join_type2 = 'Yes';
				}
				
				$field1 = 'No';
				$field2 = 'No';
				
				if($a['in_person'] == 1)
				{
					$field1 = 'Yes';
				}
				
				if($a['vaccinated'] == 1)
				{
					$field2 = 'Yes';
				}
				
				$listing[] = array(
					$a['qr_number'],
					$a['firstname'],
					$a['lastname'],
					$a['email'],
					$a['contact'],
					$a['company'],
					$a['jobtitle'],
					$a['code'],
					$field1,
					$field2,
				);
			}
			
		}
		
		
		
		$this->load->library("excel");
		$object = new PHPExcel();
		$object->getActiveSheet()->setTitle("week2");
		$object->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
		$object->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

		$object->setActiveSheetIndex(0);
		
	
		if($reg_type == 1)
		{			
			$table_columns = array(
				'[B]ID',
				'[B]First Name',
				'[B]Last Name',
				'[B]Email',
				'[B]Contact',
				'[B]Company',
				'[B]Title',
				'[B]Code',
				'[B]Request an In-Person Pass',
				'[B]I declare I am fully vaccinated',
			);
		}
		
		

		$column = 0;

		foreach($table_columns as $field)
		{
			
			if (strpos($field, '[B]') !== false)
			{
				$object->getActiveSheet()->getStyle($this->num_to_letters($column+1).'1')->getFont()->setBold(true);
				$field = str_replace('[B]','',$field);
			} 
				
			$object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
			$column++;
		}

		$excel_row = 2;

		foreach($listing as $row)
		{
			foreach($row as $col=>$r)
			{				
				if (strpos($r, '[B]') !== false)
				{
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFont()->setBold(true);
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');

					$r = str_replace('[B]','',$r);
				} 
				$object->getActiveSheet()->setCellValueByColumnAndRow($col, $excel_row, $r);
			}
		
			$excel_row++;
		}

		
		
		
		$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Attendees.xls"');
		$object_writer->save('php://output');
	}
	 
	function export($registration_id='',$reg_type='')
	{
		
		$current_event = $this->events_m->is_current_event();
		$event_id = $current_event['id'];
		
		
		if($registration_id != '' && $registration_id > 0)
		{			
			$attendees = $this->events_m->get_attendees_all($registration_id,1,$reg_type);
			
		} else {
			
			$attendees = $this->events_m->get_registration_participants_by_event($event_id,1);
		}		
		
		//$object->getActiveSheet()->getStyle($this->num_to_letters(3).$excel_row)->getFont()->setBold(true);
		//$object->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		
		if($reg_type == 1)
		{
		
			foreach($attendees as $a)
			{
				if($a['join_type'] == 1)
				{
					$join_type1 = 'Yes';
					$join_type2 = 'Yes';
				}

				if($a['join_type'] == 2)
				{
					$join_type1 = 'Yes';
					$join_type2 = 'No';
				}

				if($a['join_type'] == 3)
				{
					$join_type1 = 'No';
					$join_type2 = 'Yes';
				}

				$item1 = 'No';
				
				if($a['in_person'] == 1)
				{
					$item1 = 'Yes';
				}
				
				$item2 = 'No';
				
				if($a['vaccinated'] == 1)
				{
					$item2 = 'Yes';
				}
				
				$listing[] = array(
					$a['qr_number'],
					$a['firstname'],
					$a['lastname'],
					$a['email'],
					$a['contact'],
					$a['company'],
					$a['jobtitle'],
					$a['code'],
					$item1,
					$item2,
				);
			}
			
		}
		
		
		
		$this->load->library("excel");
		$object = new PHPExcel();
		$object->getActiveSheet()->setTitle("week2");
		$object->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
		$object->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

		$object->setActiveSheetIndex(0);
		
	
		if($reg_type == 1)
		{			
			$table_columns = array(
				'[B]ID',
				'[B]First Name',
				'[B]Last Name',
				'[B]Email',
				'[B]Contact',
				'[B]Company',
				'[B]Title',
				'[B]Code',
				'[B]Request an In-Person Pass',
				'[B]I declare I am fully vaccinated',
			);
		}
		
		

		$column = 0;

		foreach($table_columns as $field)
		{
			
			if (strpos($field, '[B]') !== false)
			{
				$object->getActiveSheet()->getStyle($this->num_to_letters($column+1).'1')->getFont()->setBold(true);
				$field = str_replace('[B]','',$field);
			} 
				
			$object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
			$column++;
		}

		$excel_row = 2;

		foreach($listing as $row)
		{
			foreach($row as $col=>$r)
			{				
				if (strpos($r, '[B]') !== false)
				{
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFont()->setBold(true);
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');

					$r = str_replace('[B]','',$r);
				} 
				$object->getActiveSheet()->setCellValueByColumnAndRow($col, $excel_row, $r);
			}
		
			$excel_row++;
		}

		
		
		
		$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Attendees.xls"');
		$object_writer->save('php://output');
	}
	
	
	 
	
	
	function allexport($registration_id='')
	{
		
		$current_event = $this->events_m->is_current_event();
		$event_id = $current_event['id'];
		
		if($registration_id != '')
		{						
			$attendees = $this->events_m->get_registration_participants2($registration_id);
		}
		
		
		foreach($attendees as $count=>$a)
		{
			$details = $this->events_m->get_contact_person_info($a['registration_id']);
			
			$total = '-';
			$payment_date = '';
			$total = '$'.number_format($details['ticket_price'],2);
			$grand_total = '-';
			$grand_total = '$'.number_format($details['ticket_price']*1.07);
			$gst = '$'.number_format($details['ticket_price']*0.07,2);
			
			if($details['datetime_paid'] != '' && $details['datetime_paid'] != '0000-00-00 00:00:00')
				$payment_date = $details['datetime_paid'];
			
			if($details['payment_method'] == 1)
			{
				$payment_method = 'Bank Transfer';
				$payment_method2 = 'Bank Transfer';				
			}
			
			if($details['payment_method'] == 2)
			{
				$payment_method = 'PayPal';
				$payment_method2 = 'PayPal';
			}
			
			if($details['complimentary'] > 0)
			{
				$payment_method = 'Complimentary';
				$payment_method2 = 'Complimentary';
			}

			
			if($details['payment_method'] == 3)
			{
				$payment_method = 'Complimentary';
				$payment_method2 = '-';
				$total = '-';
				$grand_total = '-';
				$gst = '-';
				$payment_date  = '-';
			}
			
			$imas_member = 'N';
			
			if($details['main_contact_imas_membership'] == 1)
				$imas_member = 'Y';
			
			if($details['paid'] == 1)
				$paid = 'Paid';
			
			if($details['paid'] == 0)
				$paid = 'Unpaid';
			
			if($details['payment_method'] == 3 && $details['complimentary'] > 0)
				$paid = 'Complimentary';
			
			if($a['reg_type'] == 2)
			{
				$total = '-';
				$gst = '-';
				$grand_total = '-';
				$payment_method2 = '-';
				$payment_date  = '-';
			}		
			
			if($a['join_type'] == 1)
			{
				$join_type1 = 'Yes';
				$join_type2 = 'Yes';
			}
			
			if($a['join_type'] == 2)
			{
				$join_type1 = 'Yes';
				$join_type2 = 'No';
			}
			
			if($a['join_type'] == 3)
			{
				$join_type1 = 'No';
				$join_type2 = 'Yes';
			}
			
			$listing[] = array(
				$count+1,
				$a['qr_number'],
				($details['option1'] == 1) ? 'Yes': 'No',
				($details['option2'] == 1) ? 'Yes': 'No',
				$join_type1,
				$join_type2,
				$a['firstname'],
				$a['lastname'],
				$a['company'],
				$a['jobtitle'],
				$a['contact'],
				$a['email'],
				$details['main_contact_firstname'],
				$details['main_contact_lastname'],
				$details['main_contact_email'],
				$imas_member,
				$details['promo_code'],
				$payment_method,
				$total,
				$gst,
				$grand_total,
				$payment_method2,
				$details['invoice_no'],
				$payment_date,
				$paid,
			);
		}
		
	
		
		$this->load->library("excel");
		$object = new PHPExcel();
		$object->getActiveSheet()->setTitle("week2");
		$object->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
		$object->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);

		$object->setActiveSheetIndex(0);
		
		$table_columns = array(
			'[B]No.',
			'[B]Reg ID',
			'[B]I would like to receive marketing and promotional materials from IMAS and IMAS Singapore',
			'[B]I agree to share my business contact information with the event co-host, IMAS Singapore',
			'[B]IMAS Investment Conference',
			'[B]IMAS Digital Summit',
			'[B]First Name',
			'[B]Last Name',
			'[B]Company Name',
			'[B]Designation',
			'[B]Contact No',
			'[B]Email',
			'[B]Contact Person\'s First Name',
			'[B]Contact Person\'s Last Name',
			'[B]Email',
			'[B]IMAS Member',
			'[B]Promo Code',
			'[B]Category',
			'[B]Amount b/GST',
			'[B]GST',
			'[B]Total Amount',
			'[B]Payment Mode',
			'[B]Invoice Number',
			'[B]Payment Date',
			'[B]Payment Status',
		);

		$column = 0;

		foreach($table_columns as $field)
		{
			
			if (strpos($field, '[B]') !== false)
			{
				$object->getActiveSheet()->getStyle($this->num_to_letters($column+1).'1')->getFont()->setBold(true);
				$field = str_replace('[B]','',$field);
			} 
				
			$object->getActiveSheet()->setCellValueByColumnAndRow($column, 1, $field);
			$column++;
		}

		$excel_row = 2;

		foreach($listing as $row)
		{
			foreach($row as $col=>$r)
			{				
				if (strpos($r, '[B]') !== false)
				{
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFont()->setBold(true);
					$object->getActiveSheet()->getStyle($this->num_to_letters($col+1).$excel_row)->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');

					$r = str_replace('[B]','',$r);
				} 
				$object->getActiveSheet()->setCellValueByColumnAndRow($col, $excel_row, $r);
			}
		
			$excel_row++;
		}

		
		
		
		$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Registrations.xls"');
		$object_writer->save('php://output');
	}

}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */