<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test_email extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		session_start();

		if(!isset($_SESSION['admin']) && $_SESSION['admin'] == '')
		{
			redirect('backend/main/index');
		}

		
		date_default_timezone_set('Asia/Singapore');
		$this->load->model('edm_m');
		$this->load->model('invoices_m');
	}
	
	
	public function read($invoice_number='')
	{	
		$filename = 'reminder2025.csv';
		
		$data = [];

		if (($handle = fopen($filename, "r")) !== false) {
			while (($row = fgetcsv($handle, 1000, ",")) !== false) {
				$data[] = $row; // Each line is an array of values
			}
			fclose($handle);
		}
		
		foreach($data as $d)
		{
			$data_list[trim($d[2])] = $d;
		}
		
		return $data_list[$invoice_number];
		
	}
	
	public function send($id='')
	{		
		if($id == '')
			exit;
		
		$details = $this->invoices_m->info($id);
	
		$invoice_no = '';
		$invoice_no = $details['invoice_no'];
		
		$to = array();
		$to = $this->read($invoice_no);
		
		if(empty($to))
		{
			print 'ERROR';
			exit;
		}
		
		$name = '';
		$name = $to[6];
		$to_email = trim($to[7]);
		
		$cc_email = '';
		$cc_email = $details['invoice_primary_email'].','.$details['invoice_cc_email'];
		$cc_email_list = array();
		$valueToRemove = $to_email;
		
		$cc_email_list = explode(',',$cc_email);
		$cc_email_list = array_values(array_diff($cc_email_list, [$valueToRemove]));
		$cc_email = implode(',',$cc_email_list);
		
		
		$content_new = "
		<p>Dear ".$name.",</p>

<p>We hope this message finds you well.</p>
<p>This is a gentle reminder regarding your annual membership subscription fees for FY2025/26, which are still reflected as outstanding in our records. For your convenience, we have re-attached the invoice here.
</p>
<p>We would be most grateful if you could arrange for payment at your earliest convenience. If payment has already been made, please disregard this message with our sincere thanks.
</p>
<p>We greatly appreciate your continued support and look forward to another productive year of partnership with you.</p>";

		
		$data['content'] = $content_new;
		
		$html_new = $this->load->view('emails/mass-email.php',$data,TRUE);
		
		$details = $this->invoices_m->info($id);
						
		$old_content = '<p style="font-family: Verdana; font-size: 11px;">Dear members,</p>
<p style="font-family: Verdana; font-size: 11px;">On behalf of IMAS, we would like to extend our heartfelt gratitude for your continued support as a valued IMAS member. Your participation and contributions have been vital in driving our initiatives forward.&nbsp;</p>
<p style="font-family: Verdana; font-size: 11px;">Over the past year, IMAS has organised 24 member events &mdash; including our annual marquee IMAS Investment Conference &amp; Masterclass &mdash; collectively attended by close to 2,000 members, asset owners and partners. We have facilitated over 10 consultation papers with the MAS and other offshore regulators such as IOSCO, organised discussions with MAS, FCA, IRAS and also tapped our global network to facilitate exchanges with partners such as ICI, Investment Association UK, DTCC and Dubai Department of Trade and Economy.</p>
<p style="font-family: Verdana; font-size: 11px;">We also trained over 4,000 industry professionals to date through iLearn, our e-learning portal, and stepped up progressively with the launch of the Climate Handbook, a definitive roadmap for investors by investors on how to integrate climate considerations into your portfolios. This Climate Handbook is powered by a Gen AI LLM BOT that our members can use to interact with investors across the region, as it supports Chinese, Indonesian, Thai, Vietnamese in addition to English. We hope these initiatives have been very useful. To help the industry digitalise, we continue to curate fintechs that serves the asset management industry and have organised them neatly for your access through the Fintech Directory on our website.&nbsp;</p>
<p style="font-family: Verdana; font-size: 11px;">Looking ahead, we are committed to bring new initiatives to further support your business and professional growth. Some of these include:</p>
<ol style="font-family: Verdana; font-size: 11px;">
<li><strong>Core CPD Training programmes</strong> with enhanced 90/70% funding by SSG. &nbsp;This will bring training costs down substantially for our members, as eligible participants will be able to access 6hrs CPD learning starting from only SGD $50.</li>
<li><strong>Exclusive training partnership with CAIA Association</strong> through the launch of Fundamentals of Alternative Investments (FAI) in Singapore.</li>
<li><strong>Additional Membership Privileges &amp; Resources Page</strong> - Discounted rates for business-related services and products as well as help you identify government funding, grants and resources. These will also help our members manage costs in a challenging environment.&nbsp;</li>
</ol>
<p style="font-family: Verdana; font-size: 11px;">As we attach this year\'s membership fee invoice, we would like to invite you to connect with IMAS on <a href="https://www.linkedin.com/company/imassgp/posts/?feedView=all" target="_blank" rel="noopener">LinkedIn</a> and subscribe to our <a href="https://imas.org.sg/contact-us/" target="_blank" rel="noopener">mailing list</a> so that you can receive timely updates on our initiatives. If you would like to do more with IMAS, please do connect with our membership team through <a href="mailto:enquiries@imas.org.sg">enquiries@imas.org.sg</a>.</p>
<p style="font-family: Verdana; font-size: 11px;">We could not have come this far without your support and we look forward to another year where we can bring you more value and partner with you to ensure that our collective voices are heard.&nbsp;</p>';
		
		
		$data['content'] = $old_content;
		
		$old_html = $this->load->view('emails/mass-email2.php',$data,TRUE);
		
		$content = "
		<p>Dear members,</p>
<p>We hope this message finds you well.</p>
<p>As a follow-up to our previous correspondence, we are writing to remind you that the annual membership subscription fees for FY2025/26 is currently outstanding in our records.  We have attached the invoice here for your reference and would be grateful if you could arrange for payment to be made by 12 September 2025.
</p>
<p>We thank you for your continued support and look forward to another year of our valuable partnership. </p>";
		
		$data['content'] = $content;
		
		$html = $this->load->view('emails/mass-email3.php',$data,TRUE);
		
		$final_html = $html_new.$html.$old_html;
		
		$invoice_no = $details['invoice_no'];
		$invoice_no2 = str_replace("/", "-", $invoice_no);
		
		$file = 'uploads/'.$invoice_no2.'.pdf';
		$filename = $invoice_no2.'.pdf';	
		
		if($invoice_no == '')
			exit;
		
		$from = 'replies@imas.org.sg';
		$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 = '[Gentle Reminder]: IMAS Annual Membership Subscription Fees for FY2025/26 - Inv#'.$invoice_no;
		
		//$to = $to_email;	
		//$cc = $cc_email.',replies@imas.org.sg';
		
		if($to != '')
		{
			//send_email($to,$host,$port,$username,$password,$from,$from_name,$subject,$final_html,$file,$filename,$file,$filename,$cc);
			
		}
		
		print 'SENT';
	}
	 
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */