how can we get the Revenue using API or Without using API programming in Magento 2.4, like below image,
I have tried using API, and below is my code. webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">
<route url="/V1/custom/custom-api/post/" method="GET">
<service class="Rental\Totalrevenue\Api\CustomInterface" method="getTotalRevenue"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
custom.php /var/www/html/m2_new_staging/app/code/Rental/Totalrevenue/Model/Api
<?php
namespace Rental\Totalrevenue\Model\Api;
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
use Psr\Log\LoggerInterface;
class Custom
{
protected $_collectionFactory;
protected $logger;
public function __construct(
LoggerInterface $logger,
CollectionFactory $collectionFactory
)
{
$this->logger = $logger;
}
/**
- @inheritdoc
*/
public function getTotalRevenue()
{
//pass ID parameter as you need from store or website
$isFilter = $this->getRequest()->getParam(
'store'
) || $this->getRequest()->getParam(
'website'
) || $this->getRequest()->getParam(
'group'
);
$period = $this->getRequest()->getParam('period', Period::PERIOD_24_HOURS); // 1y, 2y
/ @var $collection Collection /
$collection = $this->_collectionFactory->create()->addCreateAtPeriodFilter(
$period
)->calculateTotals(
$isFilter
);
if ($this->getRequest()->getParam('store')) {
$collection->addFieldToFilter('store_id', $this->getRequest()->getParam('store'));
} else {
if ($this->getRequest()->getParam('website')) {
$storeIds = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'))->getStoreIds();
$collection->addFieldToFilter('store_id', ['in' => $storeIds]);
} else {
if ($this->getRequest()->getParam('group')) {
$storeIds = $this->_storeManager->getGroup($this->getRequest()->getParam('group'))->getStoreIds();
$collection->addFieldToFilter('store_id', ['in' => $storeIds]);
} elseif (!$collection->isLive()) {
$collection->addFieldToFilter(
'store_id',
['eq' => $this->_storeManager->getStore(Store::ADMIN_CODE)->getId()]
);
}
}
}
$collection->load();
$totals = $collection->getFirstItem();
return $totals->getRevenue();
//return 'Total Revenue $param ' . $param;
}
public function getPost($value)
{
$response = ['success' => false];
try {
// Your Code here
$response = ['success' => true, 'message' => $value];
} catch (\Exception $e) {
$response = ['success' => false, 'message' => $e->getMessage()];
$this->logger->info($e->getMessage());
}
$returnArray = json_encode($response);
return $returnArray;
}
}
CustomInterface.php
/var/www/html/m2_new_staging/app/code/Rental/Totalrevenue/Api
<?php
namespace Rental\Totalrevenue\Api;
interface CustomInterface
{
/**
- GET for Post api
- @param string $param
- @return string
*/
public function getTotalRevenue();
}
here i have code $period = $this->getRequest()->getParam('period', Period::PERIOD_24_HOURS);, can we have changes that period like, today, last 3 months?
