<?php 
use WHMCS\Database\Capsule;
require __DIR__ . '/init.php';	
	$rowsData = Capsule::select("SELECT d.userid,d.domain FROM tbldomains d INNER JOIN expiredomain_temporary dt on dt.domain=d.domain group by d.domain");
  $results = array();
	foreach ($rowsData as $rowData)
  {
      $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 ='".$rowData->userid."' ");
      if(!empty($rsData))
      {
          $results[$rsData[0]->client_id][] = $rowData->domain;
      }else{
          $results[$rowData->userid][] = $rowData->domain;
      }
  }
  //echo count($results); // die();
  foreach ($results as $userid => $domains){
    $chk = Capsule::table("emails_logs_type2")->where('domainid',$userid)->where('type','type7')->count();
    if(empty($chk))
    {
        $command = 'SendEmail';
        $postData = array(
            'messagename' => 'Expired Domain Notice for Customer/Reseller Type 4',
            'id' => $userid ,     
            'customtype' => 'general',
            'customsubject' => 'Expired Domain Notice',            
            'custommessage' => '<p>Greetings!<br /><br />Your domain name/names have expired and is due for Deletion . Due to this your site will stop working. We recommend renewing this immediately .<br /><br />In case this is not done, the domain name will be deleted in the registry and will stop working permanently.This is the Final reminder !<br />In case there is a discrepancy pls do get back to us! <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 <br />kindly get in touch with us in the email address support@dfordomains.com .<br /><br /><strong>Domain Names</strong><br />'.implode('<br>', $domains).'<br /><br />Best regards,<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);
        Capsule::table("emails_logs_type2")->insert(array('domainid'=>$userid,'type'=>'type7'));
        print_r($postData);
    }    
  }

  //echo '<pre>'; print_r($results); 
  die('JMD');
?>
