Skip to content

Commit

Permalink
Merge pull request #29 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4-develop
  • Loading branch information
Shinichi69 authored May 26, 2021
2 parents eeaa9ee + 6e51581 commit 1b1fd3b
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 208 deletions.
23 changes: 14 additions & 9 deletions Block/Dashboard/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ class Tax extends AbstractClass
*/
public function getTotal($includeContainer = true)
{
$total = 0;
$date = $this->_helperData->getDateRange();
$totals = $this->_helperData->getTotalsByDateRange($date[0], $date[1]);

return $this->getBaseCurrency()->format($totals->getTax() ?: 0, [], $includeContainer);
foreach ($totals->getItems() as $item) {
$total += $item->getTaxBaseAmountSum();
}

return $this->getBaseCurrency()->format($total, [], $includeContainer);
}

/**
Expand All @@ -62,19 +67,19 @@ public function getTotal($includeContainer = true)
public function getRate()
{
$dates = $this->_helperData->getDateRange();
$totals = $this->_helperData->getTotalsByDateRange($dates[0], $dates[1]);
$compareTotals = $this->_helperData->getTotalsByDateRange($dates[2], $dates[3]);
if ((int) $totals->getTax() === 0 && (int) $compareTotals->getTax() === 0) {
$totals = $this->_helperData->getTotalsByDateRange($dates[0], $dates[1])->getFirstItem();
$compareTotals = $this->_helperData->getTotalsByDateRange($dates[2], $dates[3])->getFirstItem();
if ((int) $totals->getTaxBaseAmountSum() === 0 && (int) $compareTotals->getTaxBaseAmountSum() === 0) {
return 0;
}
if ((int) $compareTotals->getTax() === 0) {
if ((int) $compareTotals->getTaxBaseAmountSum() === 0) {
return 100;
}
if ((int) $totals->getTax() === 0) {
if ((int) $totals->getTaxBaseAmountSum() === 0) {
return -100;
}

return round((($totals->getTax() - $compareTotals->getTax()) / $compareTotals->getTax()) * 100, 2);
return round((($totals->getTaxBaseAmountSum() - $compareTotals->getTaxBaseAmountSum()) / $compareTotals->getTaxBaseAmountSum()) * 100, 2);
}

/**
Expand All @@ -86,9 +91,9 @@ public function getRate()
*/
protected function getDataByDate($date, $endDate = null)
{
$totals = $this->_helperData->getTotalsByDateRange($date, $endDate);
$totals = $this->_helperData->getTotalsByDateRange($date, $endDate)->getFirstItem();

return round($totals->getTax() ?: 0, 2);
return round($totals->getTaxBaseAmountSum() ?: 0, 2);
}

/**
Expand Down
27 changes: 22 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Tax\Model\ResourceModel\Report\Collection as TaxCollection;
use Mageplaza\Core\Helper\AbstractData;

/**
Expand Down Expand Up @@ -252,6 +253,12 @@ public function addTimeFilter($collection, $startDate, $endDate = null)
{
[$startDate, $endDate] = $this->getDateTimeRangeFormat($startDate, $endDate, 1);

if ($this->_request->getParam('card_id') === 'tax') {
return $collection
->addFieldToFilter('period', ['gteq' => $startDate])
->addFieldToFilter('period', ['lteq' => $endDate]);
}

return $collection
->addFieldToFilter('created_at', ['gteq' => $startDate])
->addFieldToFilter('created_at', ['lteq' => $endDate]);
Expand Down Expand Up @@ -401,10 +408,16 @@ public function getTotalsByDateRange($startDate, $endDate)
|| $this->_request->getParam('website')
|| $this->_request->getParam('group');

/* @var $collection Collection */
$collection = $this->_orderCollectionFactory->create();
$collection = $this->addTimeFilter($collection, $startDate, $endDate);
$collection->checkIsLive('')->calculateTotals($isFilter);
if ($this->_request->getParam('card_id') === 'tax') {
/** @var TaxCollection $collection */
$collection = $this->createObject(TaxCollection::class);
$collection = $this->addTimeFilter($collection, $startDate, $endDate);
} else {
/* @var $collection Collection */
$collection = $this->_orderCollectionFactory->create();
$collection = $this->addTimeFilter($collection, $startDate, $endDate);
$collection->checkIsLive('')->calculateTotals($isFilter);
}

if ($this->_request->getParam('store')) {
$collection->addFieldToFilter('store_id', $this->_request->getParam('store'));
Expand All @@ -414,7 +427,7 @@ public function getTotalsByDateRange($startDate, $endDate)
} elseif ($this->_request->getParam('group')) {
$storeIds = $this->storeManager->getGroup($this->_request->getParam('group'))->getStoreIds();
$collection->addFieldToFilter('store_id', ['in' => $storeIds]);
} elseif (!$collection->isLive()) {
} elseif (method_exists($collection, 'isLive') && !$collection->isLive()) {
$collection->addFieldToFilter(
'store_id',
['eq' => $this->storeManager->getStore(Store::ADMIN_CODE)->getId()]
Expand All @@ -423,6 +436,10 @@ public function getTotalsByDateRange($startDate, $endDate)

$collection->load();

if ($this->_request->getParam('card_id') === 'tax') {
return $collection;
}

return $collection->getFirstItem();
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"mageplaza/module-core": "^1.4.5"
},
"type": "magento2-module",
"version": "4.0.0",
"version": "4.1.0",
"license": "proprietary",
"authors": [
{
Expand Down
Loading

0 comments on commit 1b1fd3b

Please sign in to comment.