<?php

App::import('Core');
App::import('Component', 'Email');
class ReportShell extends Shell {

	public $uses = array('Order', 'Evaluation');
		
	public function startup() 
	{
		$this->Email = new EmailComponent();
		$this->Email->delivery = 'smtp';
		$this->Email->smtpOptions = array(
			'host' => 'mail.e-testmetrix.ca',
			'username' => 'admin.service+e-testmetrix.ca',
			'password' => '20metrix11-'
		);
	}
	
	function main() {
		
		//Gets the auto-cancelled after 3rd reminder files from the past week
		// AARon//

		$today = '2020-04-26';
		$aWeekAgo = "";		
		$previous_week = '2020-04-19';
		$listTestsStatus14 = $this->Evaluation->listTestsStatus14($today, $previous_week);
		$cancelledTests = count($listTestsStatus14);
		//echo nl2br($today. "\n");
		//echo nl2br($previous_week);
		//echo('HERE:');
		//echo($cancelledTests);
		//print_r($listTestsStatus14);


		$i = 0;
		$evalEmps = $listTestsStatus14;

		if (empty($evalEmps)) 
		{
			$this->out('There are no cancelled tests for the selected period');
			$this->_stop();
		}


		$this->out('List of cancelled Evaluations for the period of '. $previous_week. " to ". $today . "\n");

		//Print out cancelled orders
		foreach($evalEmps as $evalEmp) {
			$i++;
			$this->out("Record# ". $i. "\n");
			$this->out('Requester First Name: ' . $evalEmp['Requester']['first_name'] . "\n");
			$this->out('Requester Last Name: ' . $evalEmp['Requester']['last_name'] . "\n");
			$this->out('Order Code#: ' . $evalEmp['Order']['order_code'] . "\n");
			$this->out('Evaluation Id#: ' . $evalEmp['Evaluation']['id'] . "\n");
			$this->out('Status Date: ' . $evalEmp['Evaluation']['status_date'] . "\n");
			$this->out('Employee Id#: ' . $evalEmp['Employee']['id'] . "\n");		
			$this->out('Employee First Name: ' . $evalEmp['Employee']['first_name'] . "\n");
			$this->out('Employee Last Name: ' . $evalEmp['Employee']['last_name'] . "\n");

			$this->out('----------------------------------------' . "\n");
		}

		$this->out('These are the cancelled evaluations for the last 7 days. Thank you!');
		$n = count($evalEmps);
		$this->out('Number of cancelled tests "'.$n.'"');
					
		//$passw = Security::hash('ano77mous-ny', null, true);
		$passw = '00c383efdccfff3c654bf5f91b4a12bd5ee2fe56';
		

		// !!!!!!!!!!!!!!!!! Correct this. !!!!!!!!!!!!!!!!!!!!!!!!!!!!
		// Eliminate loop for sending multiple individual 
		// emails fro all cases in the orders mpodel retrieving function
		// foreach($evalEmps as $evalEmp) {
		// 	$currentEmail = 'tech@testmetrix.ca';
		// 	$finalMsg ='';

		// 	$this->out('Sending to '.$currentEmail.'... ', false);			
			
		// 	$this->Email->sendAs = 'html';
			

		// 	// EMAIL MESSAGE
		// 	if  ($evalEmp['Employee']['customer_id'] == 7)
		// 	{ 
		// 			$this->Email->subject = 'List of cancelled tests';


		// // !!!!!!!!!!!!!!!!! Correct this. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
		// // USE the ".=" syntax to add lines to email's $finalMsg variable for email body

		// 			$finalMsg .= 'List of cancelled tests from'
		// 					     . ' ' .  $previous_week . ' ' .  'to' . ' ' .  $today;			
		// 	}	

		// 			//REPLACE STRINGS FOR CUSTOMIZED MESSAGES WITH INSERTED FIELDS
		// 			$finalMsg = str_replace('#', '<b>'.$evalEmp['Customer']['name'].'</b>', $finalMsg);
		// 			$finalMsg = str_replace('?', '<b>'.$evalEmp['Employee']['first_name'].'</b>', $finalMsg);
					
		// 	$this->Email->from = 'Testmetrix Admin Service <admin.service@e-testmetrix.ca>';
		// 	$this->Email->to = $currentEmail;

		// 	###ADDED A BLIND CARBON COPY OF EACH ORDER EMAIL###
		// 	###CREATED FEBRUARY 12, 2017###
		// 	$this->Email->bcc = array('aaronlevi75@hotmail.com');
		// 	$sent = $this->Email->send($finalMsg);

			
		// 	if ($sent) {
		// 		$this->out('DONE');
		// 	} else {
		// 		$error = !empty($this->Email->smtpError) ? $this->Email->smtpError : '';
		// 		$this->out('ERROR' . (!empty($error) ? ': '.$error : ''));
		// 	}	
			
		// 	$this->Email->reset();
		// }
		



	}
}
?>
