<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Checkout extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		checkDashboard();
		$this->load->model('checkoutmain', 'checkout');
		$this->lang->load("checkout", $this->session->userdata('language'));
		
	}

	public function index()
	{
		$this->projects($this->session->userdata('projectID'));
	}

	function projects($projectID=null)
	{
		$user = $this->session->userdata('logged_in');
		

		$data['user'] 			= $this->checkout->getUser($user['userID']);
		$data['userBilling'] 	= $this->checkout->getUserBillingInfo($user['userID']);


		if($projectID){
			$this->session->set_userdata('projectID', $projectID);

			$data['current'] = $this->checkout->getProject($projectID);

			
			$data['current']['startTime'] = date('d/m/Y h:i A', strtotime($data['current']['startDT']));
			$data['current']['endTime'] = date('d/m/Y h:i A', strtotime($data['current']['endDT']));

			if($data['current']['user'] != $user['userID']) redirect(base_url().'project');

			$data['current']['detail'] = $this->getCost($projectID);

			$this->layouts->view('checkout/index', array(), $data);

		} 
		// else {
		// 	$projects = $this->checkout->getProjectsbyUser($user['userID']);
		
		// 	$data['projects'] = array();

		// 	foreach($projects as $project){
		// 		$project['voters'] = $this->checkout->getVotersbyProject($project['ID']);

		// 		if($project['voters'] != null){
		// 			$project['paidVoters'] 	 = (count($this->checkout->getVotersbyPaymentStatus($project['ID'], 1))>0)?$this->checkout->getVotersbyPaymentStatus($project['ID'], 1):null;
		// 			$project['unpaidVoters'] = (count($this->checkout->getVotersbyPaymentStatus($project['ID'], 0))>0)?$this->checkout->getVotersbyPaymentStatus($project['ID'], 0):null;

		// 			if 		(count($project['paidVoters']) > 0 && $project['unpaidVoters'] == null) 			{ $project['status'] = 'Fully Paid'; }
		// 			else if (count($project['paidVoters']) > 0 && count($project['unpaidVoters']) > 0) 			{ $project['status'] = 'Partially Paid'; } 
		// 			else 																						{ $project['status'] = 'Not Paid'; }
					
		// 		}else{
		// 			$project['status'] = 'No Voters';
		// 		}

		// 		$totalVoters = count($project['voters']);
		// 		$project['cost'] = $this->countProjectCost($totalVoters);

				
		// 		array_push($data['projects'], $project);
		// 	}
		// 	$this->layouts->view('checkout/list', array(), $data);
		// }
	}

	function bills($projectID=null){
		$user = $this->session->userdata('logged_in');

		if($projectID != null){
			$data['bills'] = $this->checkout->getProjectBills($projectID);
			
			$this->layouts->view('checkout/bills', array(), $data);
		}else{
			// $data['bills'] = $this->checkout->getUserBills($user['userID']);
			redirect('projects', 'refresh');
		}
	}

	function getProjectCost($projectID)
	{
		$data['voters'] = $this->checkout->getVotersbyProject($projectID);

		$totalVoters = count($data['voters']);
		
		//Check for status
		if($totalVoters != null){
			$data['paidVoters'] 	 = (count($this->checkout->getVotersbyPaymentStatus($projectID, 1))>0)?$this->checkout->getVotersbyPaymentStatus($projectID, 1):null;
			$data['unpaidVoters'] 	 = (count($this->checkout->getVotersbyPaymentStatus($projectID, 0))>0)?$this->checkout->getVotersbyPaymentStatus($projectID, 0):null;
			
			$data['bills'] 			 = $this->checkout->getProjectBills($projectID);
			
			//check if project base price is paid
			$basePaid = 0;
			if (count($data['bills']) >= 1) 					 	{ $basePaid = 1; }

			if ($basePaid == 0) 								 	{ $data['status'] = 'Not Paid'; }
			
			if ($basePaid == 1 && count($totalVoters) <= 200) 		{ $data['status'] = 'Fully Paid'; }

			if ($basePaid == 1 && count($totalVoters) > 200) {
				if 		(count($data['paidVoters']) > 0 && $data['unpaidVoters'] == null) 		{ $data['status'] = 'Fully Paid'; }
				else if (count($data['paidVoters']) > 0 && count($data['unpaidVoters']) > 0) 	{ $data['status'] = 'Partially Paid'; } 
				else 																			{ $data['status'] = 'Not Paid'; }
			}

		}else{
			$data['status'] = 'No Voters';
		}

		$status 	 		= $data['status'];

		$baseUnit 			= 200;
		$basePrice 			= 500.00;
		$additionalCharges 	= 2.00;

		$data['baseUnit'] 			= $baseUnit;
		$data['basePrice'] 			= $basePrice;
		$data['additionalCharges'] 	= $additionalCharges;

		//rate is based on per customer (RM)
		if ($totalVoters >= 201 && $totalVoters <= 500){

			$data['tier1']['voters'] 		= $totalVoters - $baseUnit;
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['unpaidRate'] 			= $data['tier1']['rate'];

			$data['totalTierCost'] 			= number_format($data['tier1']['cost']);

		} else if ($totalVoters >= 501 && $totalVoters <= 1000) {

			$data['tier1']['voters'] 		= 300; //max 300 for this tier
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['tier2']['voters']  		= ($totalVoters-500);
			$data['tier2']['rate'] 			= 0.4;
			$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

			$data['unpaidRate'] 			= $data['tier2']['rate'];

			$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost']), 2);

		} else if ($totalVoters >= 1001) {

			$data['tier1']['voters'] 		= 300; //max 300 for this tier
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['tier2']['voters'] 		= 500; //max 500 for this tier
			$data['tier2']['rate'] 			= 0.4;
			$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

			$data['tier3']['voters'] 		= ($totalVoters-1000);
			$data['tier3']['rate'] 			= 0.3;
			$data['tier3']['cost'] 			= $data['tier3']['voters']*$data['tier3']['rate'];


			$data['unpaidRate'] 			= $data['tier3']['rate'];
			
			$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost'] + $data['tier3']['cost']), 2);

		} else { // no voters or voters is below 200

			$data['totalTierCost'] 	= 0;
			$data['unpaidRate'] = null;

		}

		$data['projectSize'] = ($this->checkout->getPaidProjectSize($projectID)) ? $this->checkout->getPaidProjectSize($projectID) : 0;

		if($status == "Fully Paid") { 
			$data['numBills'] 			= count($this->checkout->getProjectBills($projectID));
			$data['totalCharges'] 		= $data['numBills'] * $additionalCharges;
			$data['totalPaidCost']		= ($data['projectSize'] <= 200) ? $basePrice + $additionalCharges : $basePrice + $additionalCharges + $data['totalTierCost'];
			$data['totalUnpaidCost'] 	= 0;
		 }

		if($status == "Partially Paid") {
			$data['numBills'] 			= count($this->checkout->getProjectBills($projectID));
			$data['totalCharges'] 		= $data['numBills'] * $additionalCharges;
			$data['totalPaidCost'] 		= ($data['projectSize'] <= 200) ? $basePrice + $additionalCharges : $basePrice + $data['totalCharges'];
			$data['totalUnpaidCost'] 	= $data['totalTierCost'] + $additionalCharges;
		}

		if($status == "Not Paid" || $status == "No Voters") {
			$data['totalPaidCost'] 	= 0;
			$data['totalUnpaidCost'] 	= $basePrice + $data['totalTierCost'] + $additionalCharges;
		}

		$data['totalCost'] = $data['totalPaidCost'] + $data['totalUnpaidCost'];

		return $data;
	}

	function getCost($projectID)
	//updated function
	{
		$data['voters'] = $this->checkout->getVotersbyProject($projectID);

		$totalVoters = count($data['voters']);

		if 		($totalVoters == 0)  { $data['status'] = 'No Voters'; }
		// else if ($totalVoters <= 50) { $data['status'] = 'Free Trial'; }
		else 						 { $data['status'] = ($this->checkout->getPaymentStatus($projectID) == true) ? 'Paid' : 'Not Paid'; }

		$tiers = $this->checkout->getPaymentConfig();

		// $data['tier'] = null;

		// foreach($tiers as $t){
			
		// 	if($totalVoters >= $t['unit_min']){
		// 		$data['tier'] = $t['tier_name'];
		// 		// $data['tier'.$t['level']]['voters'] = ;		
		// 		// $data['tier'.$t['level']]['rate'] 			= 0.5;
		// 	}

		// 	if($totalVoters >= 1000){
		// 		$data['tier'] = $t['tier_name'];
		// 	}

			
		// }

		// var_dump($data['tier']);

		// exit();

		$status 	 		= $data['status'];
		$baseUnit 			= 200;
		$basePrice 			= 500.00;

		$data['baseUnit'] 	= $baseUnit;
		$data['basePrice'] 	= $basePrice;

		//rate is based on per customer (RM)
		if ($totalVoters >= 201 && $totalVoters <= 500){

			$data['tier1']['voters'] 		= $totalVoters - $baseUnit;
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['totalTierCost'] 			= number_format($data['tier1']['cost']);

		} else if ($totalVoters >= 501 && $totalVoters <= 1000) {

			$data['tier1']['voters'] 		= 300; //max 300 for this tier
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['tier2']['voters']  		= ($totalVoters-500);
			$data['tier2']['rate'] 			= 0.4;
			$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

			$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost']), 2);

		} else if ($totalVoters >= 1001) {

			$data['tier1']['voters'] 		= 300; //max 300 for this tier
			$data['tier1']['rate'] 			= 0.5;
			$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

			$data['tier2']['voters'] 		= 500; //max 500 for this tier
			$data['tier2']['rate'] 			= 0.4;
			$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

			$data['tier3']['voters'] 		= ($totalVoters-1000);
			$data['tier3']['rate'] 			= 0.3;
			$data['tier3']['cost'] 			= $data['tier3']['voters']*$data['tier3']['rate'];

			$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost'] + $data['tier3']['cost']), 2);

		} else { // no voters or voters is below 200
			$data['totalTierCost'] 	= 0;
		}

		if($status == "Paid") { 
			$data['bills'] 			 = $this->checkout->getProjectBills($projectID);
			$data['projectSize'] 	 = $this->checkout->getPaidProjectSize($projectID) ? $this->checkout->getPaidProjectSize($projectID) : 0;
			$data['totalPaidCost']	 = $data['projectSize'] <= 200 ? $basePrice : $basePrice + $data['totalTierCost'];
			$data['totalUnpaidCost'] = 0;
		 }

		if($status == "Not Paid") {
			$data['bills'] 			 = null;
			$data['totalPaidCost'] 	 = 0;
			$data['totalUnpaidCost'] = $basePrice + $data['totalTierCost'];
		}

		// if($status == "Free Trial" || $status == "No Voters") {
		// 	$data['bills'] 			 = null;
		// 	$data['totalPaidCost'] 	 = 0;
		// 	$data['totalUnpaidCost'] = 0;
		// }

		if($status == "No Voters") {
			$data['bills'] 			 = null;
			$data['totalPaidCost'] 	 = 0;
			$data['totalUnpaidCost'] = 0;
		}

		$data['totalCost'] = $data['totalPaidCost'] + $data['totalUnpaidCost'];

		return $data;
	}

	function saveBill(){
		$userID 		= $this->session->userdata('logged_in')['userID'];
		$projectID 		= $this->session->userdata('projectID');

		$projectVoters 	= $this->checkout->getVotersbyProject($projectID);
		$totalVoters 	= (count($projectVoters) <= 200) ? 200 : count($projectVoters);

		$billData = array(
			'userID' 		=> $userID,
			'billID' 		=> $_REQUEST['billplz']['id'],
			'totalVoters' 	=> $totalVoters,
			'projectID' 	=> $projectID,
			'createdBy' 	=> $userID
		);

		$this->db->insert('user_billing_detail', $billData);
		
		if($this->db->insert_id() > 0){
			$this->db->set('paid', 1)->where('ID', $projectID)->update('project');
			redirect(base_url().'checkout/projects/'.$projectID);
		}
	}

	function getBills($projectID=null){
		if($projectID == null){
			$projectID = $this->input->post('projectID');
		}
		
		$billingDet = $this->checkout->getProjectBills($this->session->userdata('projectID'));

		return $billingDet;
	}

	function mybill($billID) {
		$data['projectID'] = $this->session->userdata('projectID');
		if($data['detail'] = $this->checkout->getBillDetail($billID)) {
			
			$baseUnit 			= 200;
			$basePrice 			= 500.00;
			$totalVoters = $data['detail']['totalVoter'];
			$projectID = $data['projectID'];
			$data['baseUnit'] 	= $baseUnit;
			$data['basePrice'] 	= $basePrice;

			//rate is based on per customer (RM)
			if ($totalVoters >= 201 && $totalVoters <= 500){

				$data['tier1']['voters'] 		= $totalVoters - $baseUnit;
				$data['tier1']['rate'] 			= 0.5;
				$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

				$data['totalTierCost'] 			= number_format($data['tier1']['cost']);

			} else if ($totalVoters >= 501 && $totalVoters <= 1000) {

				$data['tier1']['voters'] 		= 300; //max 300 for this tier
				$data['tier1']['rate'] 			= 0.5;
				$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

				$data['tier2']['voters']  		= ($totalVoters-500);
				$data['tier2']['rate'] 			= 0.4;
				$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

				$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost']), 2);

			} else if ($totalVoters >= 1001) {

				$data['tier1']['voters'] 		= 300; //max 300 for this tier
				$data['tier1']['rate'] 			= 0.5;
				$data['tier1']['cost'] 			= $data['tier1']['voters']*$data['tier1']['rate'];

				$data['tier2']['voters'] 		= 500; //max 500 for this tier
				$data['tier2']['rate'] 			= 0.4;
				$data['tier2']['cost'] 			= $data['tier2']['voters']*$data['tier2']['rate'];

				$data['tier3']['voters'] 		= ($totalVoters-1000);
				$data['tier3']['rate'] 			= 0.3;
				$data['tier3']['cost'] 			= $data['tier3']['voters']*$data['tier3']['rate'];

				$data['totalTierCost'] 	= number_format(($data['tier1']['cost'] + $data['tier2']['cost'] + $data['tier3']['cost']), 2);

			} else { // no voters or voters is below 200
				$data['totalTierCost'] 	= 0;
			}
			
			$data['projectSize'] 	 = ($size = $this->checkout->getPaidProjectSize($projectID)) ? $size : 0;
			$data['totalPaidCost']	 = $data['projectSize'] <= 200 ? $basePrice : $basePrice + $data['totalTierCost'];

			
			//debug($data);
			//exit();
			
			// GET CONTENT AND BUFFER INTO A STRING
			$html = $this->load->view('checkout/mybill', $data, TRUE);	
			// WRITING PDF
			$this->load->library('PDF','pdf');			
			$pdf = new PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			$pdf->setTitle('INVOICE');
			$pdf->setSubTitle('SPS E-Voting');
			
			$pdf->SetFont('helvetica', '', 10);	
			$pdf->SetTextColor(150, 150, 150);
			$pdf->AddPage();	
			$pdf->writeHTMLCell(0, 0, '', 40, $html, 0, 1, 0, true, '', true);
			$pdf->Output('my_bill_'.(date('Ymdhis')).'.pdf', 'I');
			exit();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>