Pass revenues in stats response

[MAILPOET-3069]
This commit is contained in:
Pavel Dohnal
2020-08-10 10:58:57 +02:00
committed by Veljko V
parent 24c5ff8cb1
commit 58eb27457c
4 changed files with 72 additions and 4 deletions

View File

@ -6,6 +6,7 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
use MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository;
use MailPoet\Subscribers\SubscribersRepository;
use MailPoet\WP\Functions as WPFunctions;
@ -39,11 +40,16 @@ class SubscriberStats extends APIEndpoint {
]);
}
$statistics = $this->subscribersStatisticsRepository->getStatistics($subscriber);
return $this->successResponse([
$response = [
'email' => $subscriber->getEmail(),
'total_sent' => $statistics->getTotalSentCount(),
'open' => $statistics->getOpenCount(),
'click' => $statistics->getClickCount(),
]);
];
$woocommerce = $statistics->getWooCommerceRevenue();
if ($woocommerce instanceof WooCommerceRevenue) {
$response['woocommerce'] = $woocommerce->asArray();
}
return $this->successResponse($response);
}
}

View File

@ -45,6 +45,15 @@ class WooCommerceRevenue {
return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->currency]);
}
/** @return string */
public function getFormattedAverageValue(): string {
$average = 0;
if ($this->ordersCount > 0) {
$average = $this->value / $this->ordersCount;
}
return $this->wooCommerceHelper->getRawPrice($average, ['currency' => $this->currency]);
}
/**
* @return array
*/
@ -54,6 +63,7 @@ class WooCommerceRevenue {
'value' => (float)$this->value,
'count' => (int)$this->ordersCount,
'formatted' => $this->getFormattedValue(),
'formatted_average' => $this->getFormattedAverageValue(),
];
}
}

View File

@ -2,6 +2,8 @@
namespace MailPoet\Subscribers\Statistics;
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
class SubscriberStatistics {
/** @var int */
@ -13,10 +15,14 @@ class SubscriberStatistics {
/** @var int */
private $totalSentCount;
public function __construct($clickCount, $openCount, $totalSentCount) {
/** @var WooCommerceRevenue|null */
private $wooCommerceRevenue;
public function __construct($clickCount, $openCount, $totalSentCount, $wooCommerceRevenue = null) {
$this->clickCount = $clickCount;
$this->openCount = $openCount;
$this->totalSentCount = $totalSentCount;
$this->wooCommerceRevenue = $wooCommerceRevenue;
}
/**
@ -39,4 +45,11 @@ class SubscriberStatistics {
public function getTotalSentCount(): int {
return $this->totalSentCount;
}
/**
* @return WooCommerceRevenue|null
*/
public function getWooCommerceRevenue() {
return $this->wooCommerceRevenue;
}
}

View File

@ -6,12 +6,25 @@ use MailPoet\Doctrine\Repository;
use MailPoet\Entities\StatisticsClickEntity;
use MailPoet\Entities\StatisticsNewsletterEntity;
use MailPoet\Entities\StatisticsOpenEntity;
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
use MailPoet\WooCommerce\Helper as WCHelper;
use MailPoetVendor\Doctrine\ORM\EntityManager;
/**
* @extends Repository<SubscriberEntity>
*/
class SubscriberStatisticsRepository extends Repository {
/** @var WCHelper */
private $wcHelper;
public function __construct(EntityManager $entityManager, WCHelper $wcHelper) {
parent::__construct($entityManager);
$this->wcHelper = $wcHelper;
}
protected function getEntityClassName() {
return SubscriberEntity::class;
}
@ -20,7 +33,8 @@ class SubscriberStatisticsRepository extends Repository {
return new SubscriberStatistics(
$this->getStatisticsClickCount($subscriber),
$this->getStatisticsOpenCount($subscriber),
$this->getTotalSentCount($subscriber)
$this->getTotalSentCount($subscriber),
$this->getWooCommerceRevenue($subscriber)
);
}
@ -45,4 +59,29 @@ class SubscriberStatisticsRepository extends Repository {
->getQuery()
->getSingleScalarResult();
}
private function getWooCommerceRevenue(SubscriberEntity $subscriber) {
if (!$this->wcHelper->isWooCommerceActive()) {
return null;
}
$currency = $this->wcHelper->getWoocommerceCurrency();
$result = $this->entityManager
->createQueryBuilder()
->select('SUM(stats.orderPriceTotal) AS total, COUNT(stats.id) AS cnt')
->from(StatisticsWooCommercePurchaseEntity::class, 'stats')
->where('stats.subscriber = :subscriber')
->andWhere('stats.orderCurrency = :currency')
->setParameter('subscriber', $subscriber)
->setParameter('currency', $currency)
->getQuery()
->getSingleResult();
return new WooCommerceRevenue(
$currency,
(float)$result['total'],
(int)$result['cnt'],
$this->wcHelper
);
}
}