Skip to content

Commit

Permalink
Feature to send emails with kkm reports (#32)
Browse files Browse the repository at this point in the history
* [NN-702] Added feature to send emails with kkm reports

* [NN-702] Added translation; Added unknown transactions to report; Increased version
  • Loading branch information
Ruslan Mavlyanov authored and luckyraul committed Jan 28, 2019
1 parent 4b66961 commit 8252f46
Show file tree
Hide file tree
Showing 8 changed files with 537 additions and 3 deletions.
173 changes: 173 additions & 0 deletions app/code/community/Mygento/Kkm/Model/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

/**
*
*
* @category Mygento
* @package Mygento_Kkm
* @copyright 2019 NKS LLC. (https://www.mygento.ru)
*/
class Mygento_Kkm_Model_Report
{
public function sendReport()
{
$helper = Mage::helper('kkm');
$period = $helper->getConfig('general/report_period');

switch ($period) {
case Mygento_Kkm_Model_Source_Periods::WEEKLY_PERIOD:
$statistics = $this->getWeekStatistics();
break;
case Mygento_Kkm_Model_Source_Periods::DAILY_PERIOD:
$statistics = $this->getTodayStatistics();
break;
case Mygento_Kkm_Model_Source_Periods::DAILY_PREV_PERIOD:
default:
$statistics = $this->getYesterdayStatistics();
break;
}

return $this->send($statistics);
}

/**
* @return \Mygento_Kkm_Model_Statistics
* @throws \Zend_Date_Exception
*/
public function getYesterdayStatistics()
{
$currentTime = Mage::getModel('core/date')->timestamp(time());
$date = new Zend_Date($currentTime);
$date->subDay(1);

$fromDate = $date->toString('Y-MM-dd 00:00:00');
$toDate = $date->toString('Y-MM-dd 23:59:59');

$statuses = $this->getStatusesByPeriod($fromDate, $toDate);
$statistics = $this->collectStatistics($statuses);
$statistics->setPeriod($fromDate, $toDate);

return $statistics;
}

/**
* @return \Mygento_Kkm_Model_Statistics
* @throws \Zend_Date_Exception
*/
public function getWeekStatistics()
{
$currentTime = Mage::getModel('core/date')->timestamp(time());
$monday = strtotime('monday this week', $currentTime);
$date = new Zend_Date($monday);

$fromDate = $date->toString('Y-MM-dd 00:00:00');

$statuses = $this->getStatusesByPeriod($fromDate);
$statistics = $this->collectStatistics($statuses);
$statistics->setPeriod($fromDate);

return $statistics;
}

/**
* @return \Mygento_Kkm_Model_Statistics
* @throws \Zend_Date_Exception
*/
public function getTodayStatistics()
{
$currentTime = Mage::getModel('core/date')->timestamp(time());
$date = new Zend_Date($currentTime);

$fromDate = $date->toString('Y-MM-dd 00:00:00');

$statuses = $this->getStatusesByPeriod($fromDate);
$statistics = $this->collectStatistics($statuses);
$statistics->setPeriod($fromDate);

return $statistics;
}

/**
* @param \Mygento_Kkm_Model_Resource_Status_Collection $collection
* @return \Mygento_Kkm_Model_Statistics
*/
protected function collectStatistics($collection)
{
$statistics = Mage::getModel('kkm/statistics');

foreach ($collection as $item) {
if ($item->getShortStatus() == 'fail') {
$statistics->addFail($item);
continue;
}
if ($item->getShortStatus() == 'wait') {
$statistics->addWait($item);
continue;
}
if ($item->getShortStatus() == 'done') {
$statistics->addDone($item);
continue;
}
$statistics->addUnknown($item);
}

return $statistics;
}

/**
* @param \Mygento_Kkm_Model_Statistics $statistics
* @return bool
*/
protected function send($statistics)
{
$helper = Mage::helper('kkm');
if (!$helper->getConfig('general/enabled') || !$helper->getConfig('general/send_report_enabled')) {
return false;
}

$params = [
'statistics' => $statistics,
];

$emails = $helper->getConfig('report_emails');

// Loading of template
$templateId = $helper->getConfig('send_report_template');

if (is_numeric($templateId)) {
$emailTemplate = Mage::getModel('core/email_template')->load($templateId);
} else {
$emailTemplate = Mage::getModel('core/email_template')->loadDefault($templateId);
}

$senderEmail = $helper->getConfig('report_sender_email');
$senderName = $helper->getConfig('report_sender_name');

$emailTemplate->setSenderName($senderName);
$emailTemplate->setSenderEmail($senderEmail);

try {
return $emailTemplate->send($emails, null, $params);
} catch (Exception $error) {
$helper->addLog('Send report error: ' . $error->getMessage(), Zend_Log::WARN);

return false;
}
}

/**
* @param string $from date
* @param string|null $to date
* @return Mygento_Kkm_Model_Resource_Status_Collection
*/
public function getStatusesByPeriod($from, $to = null)
{
$statuses = Mage::getModel('kkm/status')->getCollection()
->addFieldToFilter('created_at', ['gteq' => $from]);
if ($to) {
$statuses->addFieldToFilter('created_at', ['lteq' => $to]);
}

return $statuses;
}
}
39 changes: 39 additions & 0 deletions app/code/community/Mygento/Kkm/Model/Source/Periods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
*
*
* @category Mygento
* @package Mygento_Kkm
* @copyright 2019 NKS LLC. (https://www.mygento.ru)
*/
class Mygento_Kkm_Model_Source_Periods
{

const WEEKLY_PERIOD = 'week';
const DAILY_PREV_PERIOD = 'dailyprev';
const DAILY_PERIOD = 'daily';

/**
* Options source
*
* @return array
*/
public function toOptionArray()
{
return [
[
'value' => self::WEEKLY_PERIOD,
'label' => Mage::helper('kkm')->__('Weekly (This week)'),
],
[
'value' => self::DAILY_PREV_PERIOD,
'label' => Mage::helper('kkm')->__('Daily (Yesterday)'),
],
[
'value' => self::DAILY_PERIOD,
'label' => Mage::helper('kkm')->__('Daily (Today)'),
],
];
}
}
101 changes: 101 additions & 0 deletions app/code/community/Mygento/Kkm/Model/Statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
*
*
* @category Mygento
* @package Mygento_Kkm
* @copyright 2019 NKS LLC. (https://www.mygento.ru)
*/
class Mygento_Kkm_Model_Statistics extends Mage_Core_Model_Abstract
{
protected $fails = [];
protected $unknown = [];
protected $waitCount = 0;
protected $doneCount = 0;
protected $fromDate;
protected $toDate;

/**
* @param Mygento_Kkm_Model_Status $status
*/
public function addFail($status)
{
$this->fails[] = $status;
}

/**
* @param Mygento_Kkm_Model_Status $status
*/
public function addUnknown($status)
{
$this->unknown[] = $status;
}

public function addWait()
{
$this->waitCount++;
}

public function addDone()
{
$this->doneCount++;
}

public function setPeriod($from, $to = null)
{
if (!$to) {
$currentTime = Mage::getModel('core/date')->timestamp(time());
$date = new Zend_Date($currentTime);
$to = $date->toString('Y-MM-dd H:mm:s');
}

$this->fromDate = $from;
$this->toDate = $to;
}

public function getFails()
{
return $this->fails;
}

public function getFailsCount()
{
return count($this->fails);
}

public function getUnknowns()
{
return $this->unknown;
}

public function getUnknownsCount()
{
return count($this->unknown);
}

public function getNotSentCount()
{
return count($this->unknown) + count($this->fails);
}

public function getWaitsCount()
{
return $this->waitCount;
}

public function getDonesCount()
{
return $this->doneCount;
}

public function getFromDate()
{
return $this->fromDate;
}

public function getToDate()
{
return $this->toDate;
}
}
22 changes: 20 additions & 2 deletions app/code/community/Mygento/Kkm/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Mygento_Kkm>
<version>1.2.0</version>
<version>1.2.1</version>
</Mygento_Kkm>
</modules>
<frontend>
Expand Down Expand Up @@ -96,10 +96,15 @@
<template>
<email>
<kkm_error_notification>
<label>KKM Error Notification</label>
<label>[KKM] Error Email</label>
<file>kkm_error_notification.html</file>
<type>html</type>
</kkm_error_notification>
<kkm_general_send_report_template translate="label" module="kkm">
<label>[KKM] Report Email</label>
<file>kkm_report.html</file>
<type>html</type>
</kkm_general_send_report_template>
</email>
</template>
</global>
Expand Down Expand Up @@ -230,6 +235,11 @@
<apply_algorithm>1</apply_algorithm>
<spread_discount>0</spread_discount>
<split_allowed>0</split_allowed>
<send_report_template>kkm_general_send_report_template</send_report_template>
<report_sender_name>KKM Reporter</report_sender_name>
<report_sender_email>[email protected]</report_sender_email>
<report_period>dailyprev</report_period>
<cron_report_expr>1 0 * * *</cron_report_expr>
<show_notif_in_admin>1</show_notif_in_admin>
<debug_level>3</debug_level>
</general>
Expand All @@ -245,6 +255,14 @@
<model>kkm/observer::updateTransactions</model>
</run>
</kkm_update_transactions>
<kkm_send_report>
<schedule>
<config_path>kkm/general/cron_report_expr</config_path>
</schedule>
<run>
<model>kkm/report::sendReport</model>
</run>
</kkm_send_report>
</jobs>
</crontab>
</config>
Loading

0 comments on commit 8252f46

Please sign in to comment.