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

class Add_newcard_cron extends CI_Controller
{
	public function __construct()
	{ 
		parent:: __construct();
		$this->load->model('common_model');
	}
	public function index(){
	  	$currentDate = date('Y-m-d',strtotime('+3 day',time()));
        $time = strtotime($currentDate);

        $qry = $this->db->query('select * from (select *, case when time = 365 then DATE(FROM_UNIXTIME(created + (365 * 86400))) when time = 182 then DATE(FROM_UNIXTIME(created + (182 * 86400))) end as required_date  from (SELECT shop_orders.payment_method_id,shop_orders.time,shop_articles_carts.article_id,shop_orders.created,shop_orders.cart_id,`usr_users`.`id` as `user_id`,`usr_users`.`email` as `email`, `usr_users`.`cc_ref_id`, `shop_articles`.`userstatus`,usr_users.stop_renewal,usr_users.card_alias FROM `shop_articles` INNER JOIN `shop_articles_carts` ON `shop_articles_carts`.`article_id` = `shop_articles`.`id` INNER JOIN `shop_carts` ON `shop_carts`.`id` = `shop_articles_carts`.`cart_id` INNER JOIN `usr_users` ON `usr_users`.`id` = `shop_carts`.`user_id` INNER JOIN `shop_orders` ON `shop_orders`.`cart_id` = `shop_carts`.`id` WHERE ( `shop_articles_carts`.`cancelled` != 1 OR `shop_articles_carts`.`cancelled` IS NULL ) AND `shop_orders`.`payed` = 1 ORDER BY `usr_users`.`id` DESC, `shop_carts`.`created` DESC) as a GROUP BY a.user_id ORDER BY a.user_id DESC) as b where case when time = 365 then b.created <('.$time.' - (364 * 86400)) when time = 182 then b.created <('.$time.' - (181 * 86400)) end AND article_id in (5,4) AND required_date = "'.$currentDate.'" AND stop_renewal=0 AND card_alias is NULL order by b.created DESC limit 0,10');
        //$qry = $this->db->query('select * from (select *, case when time = 365 then DATE(FROM_UNIXTIME(created + (365 * 86400))) when time = 182 then DATE(FROM_UNIXTIME(created + (182 * 86400))) end as required_date from (SELECT shop_orders.time,shop_articles_carts.article_id,shop_orders.created,shop_orders.cart_id,`usr_users`.`id` as `user_id`,`usr_users`.`email` as `email`, `usr_users`.`cc_ref_id`, `shop_articles`.`userstatus` FROM `shop_articles` INNER JOIN `shop_articles_carts` ON `shop_articles_carts`.`article_id` = `shop_articles`.`id` INNER JOIN `shop_carts` ON `shop_carts`.`id` = `shop_articles_carts`.`cart_id` INNER JOIN `usr_users` ON `usr_users`.`id` = `shop_carts`.`user_id` INNER JOIN `shop_orders` ON `shop_orders`.`cart_id` = `shop_carts`.`id` WHERE ( `shop_articles_carts`.`cancelled` != 1 OR `shop_articles_carts`.`cancelled` IS NULL ) AND `shop_orders`.`payed` = 1 ORDER BY `usr_users`.`id` DESC, `shop_carts`.`created` DESC) as a GROUP BY a.user_id ORDER BY a.user_id DESC) as b where case when time = 365 then b.created <('.time().' - (365 * 86400)) when time = 182 then b.created <('.time().' - (182 * 86400)) end AND article_id in (5,4) AND required_date = "'.date('Y-m-d').'" order by required_date DESC limit 0,10');
        $getUserListData = $qry->result_array();
	    
	    if($getUserListData)
	    {
	    	foreach ($getUserListData as $key => $value) {
	    		$userdetails = $this->common_model->getAllRow('feu_userdetails',array('user_id'=>$value['user_id']));
	    		/**********************MAIL**************************/
	    		$to = $value['time'];
				$sub = getLang('Please_update_your_card_details');
				$body= '<p>Dear '.$userData['username'].',</p>';
				$body= '<p>'.getLang('Your_membership_will_be_end_at').' '.date('m-d-Y',$time).'.</p>';
				$body.= '<p>'.getLang('due_to_some_reason').'</p>';
                $body.= '<p><a href="'.base_url().'add_card">'.getLang('Please_find_the_link_below_to_renew').'</a></p>';
                $body.= '<p>'.getLang('Thank_you_very_much_for').'.</p>';
                $body.= '<p>'.getLang('Please_let_us_know_if').'.</p>';
				
				$bodys='<table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
                <tr>
                   	<td>
                      	<table cellspacing="0" cellpadding="0" border="0" width="100%">
                         	<tr>
	                            <td align="center" style="padding: 40px; font-family: sans-serif; font-size: 15px; mso-height-rule: exactly; line-height: 20px; color: #b3975b;">
	                               <h2>'.getLang('Hello').' <span style="text-transform:capitalize">'.$userdetails['username'].'</span> </h2>
	                            </td>
                         	</tr>
                         	<tr>
	                            <td style="font-family: sans-serif; color: #fff; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
	                               '.$body.'
	                            </td>
                         	</tr>
                         	<tr>
	                         	<td align="center" style="font-family: sans-serif; color: rgb(179, 151, 91); padding: 20px 40px; border-top: 1px solid rgb(79, 79, 79); background: rgb(19, 18, 24) none repeat scroll 0px 0px;">
	                         		'.getLang('Best_wishes').' <br>
	                         		<p style="margin:5px 0 0"><a href="'.base_url().'" style="text-decoration:none; color: #b3975b;">'.getLang('Your_Castleeventscom_Team').'</a></p> 
	                         	</td>
                         	</tr>
                      	</table>
                   	</td>
                </tr>
            	</table>';
            	echo $bodys; die;
            	//$this->utility->sendCIMail($to,$sub,$bodys);
		    	/**********************MAIL**************************/
	    	}
	    }
	}
}