<?php 
use WHMCS\Database\Capsule;
require __DIR__ . '/init.php';
		
	$rowsData = Capsule::select("select * from ResellersCenter_Resellers where id not in (select reseller_id from  ResellersCenter_ResellersSettings WHERE setting='disallowMainStore')");
	foreach($rowsData as $rowData){
		Capsule::table("ResellersCenter_ResellersSettings")->insert(array('reseller_id'=>$rowData->id,'private'=>'0','setting'=>'disallowMainStore','value'=>'on'));
	}
	//echo '<pre>'; print_r($rowsData);
	die('DONE');


	$domainRenewalNotices = Capsule::table("tblconfiguration")->select('value')->where('setting','DomainRenewalNotices')->first();
	$days = explode(',', $domainRenewalNotices->value);
	//echo '<pre>'; print_r($days);
	$renewalNoticeDatas = array();
	$today = date('Y-m-d');
	foreach ($days as $day) {
		$notifData = date('Y-m-d', strtotime($today. ' '.$day.' days'));
		$renewalNoticeDatas[] = $notifData;
	}
	//echo '<pre>'; print_r($renewalNoticeDatas);
	$rowsData = Capsule::table("tbldomains")->select('id','expirydate','userid','domain')->whereIn('expirydate',$renewalNoticeDatas)->get();	
	//echo '<pre>'; print_r($rowsData); die('JMD');
    foreach ($rowsData as $rowData)
    {    	    
	    $expirydate = $rowData->expirydate;
		$domainID = $rowData->id;
		$userid = $rowData->userid;
		$domain = $rowData->domain;
		/*
		$expirydate = '2019-03-31';
		$domainID = '15717';
		$userid = '7647';
		$domain = 'footballgossips.in';
		
		$expirydate = '2019-04-02';
		$domainID = '46664';
		$userid = '19015';
		$domain = 'testshine.com';
		*/
		$expirydate44 = date('Y-m-d', strtotime($expirydate. ' + 44 days'));
		$expirydate45 = date('Y-m-d', strtotime($expirydate. ' + 45 days'));		
		$chk = Capsule::table("emails_logs_type2")->where('domainid',$domainID)->where('type','type3')->where('date',$today)->count();
		if(empty($chk))
		{	
			echo $domainID.'=='.$userid.'=='.$expirydate.'=='.$domain.'<br>';
			$rsData = Capsule::select("select r.client_id from ResellersCenter_Resellers r inner join ResellersCenter_ResellersClients rc on rc.reseller_id=r.id where rc.client_id ='".$userid."' ");
			if(!empty($rsData))
			{				
				//echo '<pre>'; print_r($rsData); die();
				$command = 'SendEmail';
				$postData = array(	    
				    'messagename' => 'Expired Domain Notice for Customer Type 2',
				    'id' => $domainID,	    
				    'type' => 'domain',
				    'customvars' => base64_encode(serialize(array("domain_expiry44"=>$expirydate44, "domain_expiry45"=>$expirydate45))),
				);
				$adminUsername = ''; // Optional for WHMCS 7.2 and later
				$results = localAPI($command, $postData, $adminUsername);
				print_r($results);

			
			
				$command = 'SendEmail';
				$postData = array(
				    'messagename' => 'Expired Domain Notice for Reseller Type 2',
				    'id' => $rsData[0]->client_id ,	    
				    'customtype' => 'general',
				    'customsubject' => 'Domain Due for renewal',		    		
				    'custommessage' => '<p>Greetings!<br /><br />The domain name '.$domain.' is going to expire on '.$expirydate.'. On '.$expirydate.' your site will stop working. We recommend renewing this immediately to avoid Redemption charges .<br /><br />You have until '.$expirydate44.' to renew this domain name. After '.$expirydate45.' the domain name will be deleted in the registry and will stop working permanently.<br /><br />Kindly login into your DforDomains account and renew the domain name by paying online. If you prefer to pay via cheque or Wire transfer kindly get in touch with us in the email address support@dfordomains.com .</p>
<p>Best regards,<br /><br />DforDomains.com<br />4th floor, The Ruby, 29, Senapati Bapat Marg, Dadar West<br />400028 Mumbai<br />India<br />Tel:- +91-22-40611144,57490850</p>',	    
				);
				$adminUsername = ''; // Optional for WHMCS 7.2 and later
				$results = localAPI($command, $postData, $adminUsername);
				print_r($results);
			}else{
				$command = 'SendEmail';
				$postData = array(	    
				    'messagename' => 'Expired Domain Notice for Direct Customer Type 2',
				    'id' => $domainID,	    
				    'type' => 'domain',
				    'customvars' => base64_encode(serialize(array("domain_expiry44"=>$expirydate44, "domain_expiry45"=>$expirydate45))),
				);
				$adminUsername = ''; // Optional for WHMCS 7.2 and later
				$results = localAPI($command, $postData, $adminUsername);
				print_r($results);
			}
			Capsule::table("emails_logs_type2")->insert(array('domainid'=>$domainID,'type'=>'type3','date'=>$today));
		}	
	}	
	die('done');
?>