| Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/ |
| Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/SyncDataIssuesEmail.php |
<?php
namespace FlipsDigital\HappyMind\Cron;
use Magento\Store\Model\ScopeInterface;
class SyncDataIssuesEmail
{
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface,
\FlipsDigital\TextbookList\Model\TextbookListFactory $textbookListFactory,
\FlipsDigital\TextbookListBook\Model\TextbookListBookFactory $textbookListBookFactory
) {
$this->_scopeConfig = $scopeConfig;
$this->transportBuilder = $transportBuilder;
$this->timezoneInterface = $timezoneInterface;
$this->_tlFactory = $textbookListFactory;
$this->_tlbFactory = $textbookListBookFactory;
}
public function execute()
{
$objTL = $this->_tlFactory->create();
$collectionTL = $objTL->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('mers_stock_source_g_gl_magento_end_date', ['gteq' => $this->timezoneInterface->date()->format('Y-m-d H:i:s')])
->addAttributeToFilter('sync_data_start_date', ['notnull' => true])
->setOrder('entity_id','DESC');
$emailToAdmin = false;
if ($collectionTL->count()){
$emailContent = '';
foreach ($collectionTL as $key => $textbook) {
$tbEntityID = $textbook->getId();
$syncDataStartDate = $textbook->getData('sync_data_start_date');
$syncDataEndDate = $textbook->getData('sync_data_end_date');
$syncDataIssue = false;
$errorMsg = '';
if(!$syncDataEndDate){
$emailToAdmin = true;
$syncDataIssue = true;
$errorMsg = 'Books data synchronization failed.';
}else{
if($syncDataStartDate > $syncDataEndDate){
$emailToAdmin = true;
$syncDataIssue = true;
$errorMsg = 'Books data synchronization failed.';
}else{
$objTLB = $this->_tlbFactory->create();
$collectionTLB = $objTLB->getCollection();
$collectionTLB->addAttributeToSelect('*');
$collectionTLB->addFieldToFilter('booklist_id', $tbEntityID);
$collectionTLB->addFieldToFilter('status', 1);
if(!$collectionTLB->count()){
$emailToAdmin = true;
$syncDataIssue = true;
$errorMsg = 'Cannot find any books data when synchronization is completed.';
}
}
}
if($syncDataIssue){
$emailContent .= '<tr>';
$emailContent .= '<td>'.$tbEntityID.'</td>';
$emailContent .= '<td>'.$textbook->getData('identifier').'</td>';
$emailContent .= '<td>'.$errorMsg.'</td>';
$emailContent .= '</tr>';
}
}
$table = '<table border="1" cellpadding="5" cellspacing="0" style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">';
$table .= '<tr>';
$table .= '<th>ID</th>';
$table .= '<th>Identifier</th>';
$table .= '<th>Error Code</th>';
$table .= '</tr>';
$table .= $emailContent;
$table .= '</table>';
}
if($emailToAdmin){
$templateVars = [
'current_date' => $this->timezoneInterface->date()->format('Y-m-d'),
'email_content' => $table
];
$senderInfo = [
'name' => $this->_scopeConfig->getValue('trans_email/ident_custom1/name', ScopeInterface::SCOPE_STORE),
'email' => $this->_scopeConfig->getValue('trans_email/ident_custom1/email', ScopeInterface::SCOPE_STORE)
];
$emails = [
'singo.fung@flipsdigital.com',
'support@store.happymind.com.hk'
];
$transport = $this->transportBuilder
->setTemplateIdentifier('sync_data_issues_template')
->setTemplateOptions(
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)->setTemplateVars($templateVars)
->setFrom($senderInfo)
->addTo($emails)
->getTransport();
$transport->sendMessage();
}
}
}