Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Paginator To All Reports #826

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 50 additions & 49 deletions src/CoreShop/Bundle/CoreBundle/Report/CategoriesReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class CategoriesReport implements ReportInterface
*/
private $orderRepository;

/**
* @var PimcoreRepositoryInterface
*/
private $categoryRepository;

/**
* @var PimcoreRepositoryInterface
*/
Expand All @@ -68,7 +73,8 @@ class CategoriesReport implements ReportInterface
* @param Connection $db
* @param MoneyFormatterInterface $moneyFormatter
* @param LocaleContextInterface $localeService
* @param PimcoreRepositoryInterface $orderRepository,
* @param PimcoreRepositoryInterface $orderRepository ,
* @param PimcoreRepositoryInterface $categoryRepository ,
* @param PimcoreRepositoryInterface $orderItemRepository
*/
public function __construct(
Expand All @@ -77,13 +83,15 @@ public function __construct(
MoneyFormatterInterface $moneyFormatter,
LocaleContextInterface $localeService,
PimcoreRepositoryInterface $orderRepository,
PimcoreRepositoryInterface $categoryRepository,
PimcoreRepositoryInterface $orderItemRepository
) {
$this->storeRepository = $storeRepository;
$this->db = $db;
$this->moneyFormatter = $moneyFormatter;
$this->localeService = $localeService;
$this->orderRepository = $orderRepository;
$this->categoryRepository = $categoryRepository;
$this->orderItemRepository = $orderItemRepository;
}

Expand All @@ -95,12 +103,19 @@ public function getReportData(ParameterBag $parameterBag)
$fromFilter = $parameterBag->get('from', strtotime(date('01-m-Y')));
$toFilter = $parameterBag->get('to', strtotime(date('t-m-Y')));
$storeId = $parameterBag->get('store', null);
$orderCompleteState = OrderStates::STATE_COMPLETE;

$from = Carbon::createFromTimestamp($fromFilter);
$to = Carbon::createFromTimestamp($toFilter);

$page = $parameterBag->get('page', 1);
$limit = $parameterBag->get('limit', 25);
$offset = $parameterBag->get('offset', $page === 1 ? 0 : ($page - 1) * $limit);

$orderClassId = $this->orderRepository->getClassId();
$categoryClassId = $this->categoryRepository->getClassId();
$orderItemClassId = $this->orderItemRepository->getClassId();
$orderCompleteState = OrderStates::STATE_COMPLETE;
$locale = $this->localeService->getLocaleCode();

if (is_null($storeId)) {
return [];
Expand All @@ -112,59 +127,45 @@ public function getReportData(ParameterBag $parameterBag)
}

$query = "
SELECT
orderItems.product__id,
SUM(orderItems.totalGross) AS sales,
SELECT SQL_CALC_FOUND_ROWS
`categories`.oo_id as categoryId,
`categories`.o_key as categoryKey,
`localizedCategories`.name as categoryName,
`orders`.store,
SUM(orderItems.totalGross) AS sales,
SUM((orderItems.itemRetailPriceNet - orderItems.itemWholesalePrice) * orderItems.quantity) AS profit,
SUM(orderItems.quantity) AS `quantityCount`,
COUNT(orderItems.product__id) AS `orderCount`
FROM object_query_$orderClassId AS orders
INNER JOIN object_relations_$orderClassId AS orderRelations ON orderRelations.src_id = orders.oo_id AND orderRelations.fieldname = \"items\"
INNER JOIN object_query_$orderItemClassId AS orderItems ON orderRelations.dest_id = orderItems.oo_id
FROM object_$categoryClassId AS categories
INNER JOIN object_localized_query_" . $categoryClassId . "_" . $locale . " AS localizedCategories ON localizedCategories.ooo_id = categories.oo_id
INNER JOIN dependencies AS catProductDependencies ON catProductDependencies.targetId = categories.oo_id AND catProductDependencies.targettype = \"object\"
INNER JOIN object_query_$orderItemClassId AS orderItems ON orderItems.product__id = catProductDependencies.sourceid
INNER JOIN object_relations_$orderClassId AS orderRelations ON orderRelations.dest_id = orderItems.oo_id AND orderRelations.fieldname = \"items\"
INNER JOIN object_query_$orderClassId AS `orders` ON `orders`.oo_id = orderRelations.src_id
WHERE orders.store = $storeId AND orders.orderState = '$orderCompleteState' AND orders.orderDate > ? AND orders.orderDate < ? AND orderItems.product__id IS NOT NULL
GROUP BY orderItems.product__id
ORDER BY COUNT(orderItems.product__id) DESC
";

$productSales = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);

$catSales = InheritanceHelper::useInheritedValues(function () use ($productSales) {
$catSales = [];
foreach ($productSales as $productSale) {
$product = DataObject::getById($productSale['product__id']);
if ($product instanceof ProductInterface) {
$categories = $product->getCategories();
if (!empty($categories)) {
foreach ($categories as $category) {
$catId = $category->getId();
if (!isset($catSales[$catId])) {
$name = !empty($category->getName()) ? $category->getName() : $category->getKey();
$catSales[$catId] = $productSale;
$catSales[$catId]['name'] = sprintf('%s (Id: %d)', $name, $category->getId());
} else {
$catSales[$catId]['sales'] += $productSale['sales'];
$catSales[$catId]['profit'] += $productSale['profit'];
$catSales[$catId]['quantityCount'] += $productSale['quantityCount'];
$catSales[$catId]['orderCount'] += $productSale['orderCount'];
}
}
}
}
}

return $catSales;
});

usort($catSales, function ($a, $b) {
return $b['orderCount'] <=> $a['orderCount'];
});

foreach ($catSales as &$sale) {
$sale['salesFormatted'] = $this->moneyFormatter->format($sale['sales'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode());
$sale['profitFormatted'] = $this->moneyFormatter->format($sale['profit'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode());
GROUP BY categories.oo_id
ORDER BY quantityCount DESC
LIMIT $offset,$limit";

$results = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);

$this->totalRecords = (int) $this->db->fetchColumn('SELECT FOUND_ROWS()');

$data = [];
foreach ($results as $result) {
$name = !empty($result['categoryName']) ? $result['categoryName'] : $result['categoryKey'];
$data[] = [
'name' => sprintf('%s (Id: %d)', $name, $result['categoryId']),
'sales' => $result['sales'],
'profit' => $result['profit'],
'quantityCount' => $result['quantityCount'],
'orderCount' => $result['orderCount'],
'salesFormatted' => $this->moneyFormatter->format($result['sales'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode()),
'profitFormatted' => $this->moneyFormatter->format($result['profit'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode())
];
}

return array_values($catSales);
return array_values($data);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/CoreShop/Bundle/CoreBundle/Report/CustomersReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ public function getReportData(ParameterBag $parameterBag)
$from = Carbon::createFromTimestamp($fromFilter);
$to = Carbon::createFromTimestamp($toFilter);

$page = $parameterBag->get('page', 1);
$limit = $parameterBag->get('limit', 25);
$offset = $parameterBag->get('offset', $page === 1 ? 0 : ($page - 1) * $limit);

$orderClassId = $this->orderRepository->getClassId();
$customerClassId = $this->customerRepository->getClassId();
$orderCompleteState = OrderStates::STATE_COMPLETE;

$query = "
SELECT
SELECT SQL_CALC_FOUND_ROWS
customer.oo_id,
customer.email as `name`,
SUM(orders.totalNet) as sales,
Expand All @@ -99,18 +103,20 @@ public function getReportData(ParameterBag $parameterBag)
WHERE orders.orderState = '$orderCompleteState' AND orders.orderDate > ? AND orders.orderDate < ? AND customer.oo_id IS NOT NULL
GROUP BY customer.oo_id
ORDER BY COUNT(customer.oo_id) DESC
";
LIMIT $offset,$limit";

$results = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);
$this->totalRecords = (int) $this->db->fetchColumn('SELECT FOUND_ROWS()');

$customerSales = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);
foreach ($customerSales as &$sale) {
foreach ($results as &$result) {
$sale['salesFormatted'] = $this->moneyFormatter->format(
$sale['sales'],
$result['sales'],
'EUR',
$this->localeContext->getLocaleCode()
);
}

return array_values($customerSales);
return array_values($results);
}

/**
Expand Down
95 changes: 46 additions & 49 deletions src/CoreShop/Bundle/CoreBundle/Report/ManufacturerReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class ManufacturerReport implements ReportInterface
*/
private $orderRepository;

/**
* @var PimcoreRepositoryInterface
*/
private $manufacturerRepository;

/**
* @var PimcoreRepositoryInterface
*/
Expand All @@ -69,6 +74,7 @@ class ManufacturerReport implements ReportInterface
* @param Connection $db
* @param MoneyFormatterInterface $moneyFormatter
* @param LocaleContextInterface $localeService
* @param PimcoreRepositoryInterface $manufacturerRepository
* @param PimcoreRepositoryInterface $orderRepository
* @param PimcoreRepositoryInterface $orderItemRepository
*/
Expand All @@ -77,13 +83,15 @@ public function __construct(
Connection $db,
MoneyFormatterInterface $moneyFormatter,
LocaleContextInterface $localeService,
PimcoreRepositoryInterface $manufacturerRepository,
PimcoreRepositoryInterface $orderRepository,
PimcoreRepositoryInterface $orderItemRepository
) {
$this->storeRepository = $storeRepository;
$this->db = $db;
$this->moneyFormatter = $moneyFormatter;
$this->localeService = $localeService;
$this->manufacturerRepository = $manufacturerRepository;
$this->orderRepository = $orderRepository;
$this->orderItemRepository = $orderItemRepository;
}
Expand All @@ -99,7 +107,12 @@ public function getReportData(ParameterBag $parameterBag)
$from = Carbon::createFromTimestamp($fromFilter);
$to = Carbon::createFromTimestamp($toFilter);

$page = $parameterBag->get('page', 1);
$limit = $parameterBag->get('limit', 25);
$offset = $parameterBag->get('offset', $page === 1 ? 0 : ($page - 1) * $limit);

$orderClassId = $this->orderRepository->getClassId();
$manufacturerClassId = $this->manufacturerRepository->getClassId();
$orderItemClassId = $this->orderItemRepository->getClassId();
$orderCompleteState = OrderStates::STATE_COMPLETE;

Expand All @@ -113,61 +126,45 @@ public function getReportData(ParameterBag $parameterBag)
}

$query = "
SELECT
orderItems.product__id,
SUM(orderItems.totalGross) AS sales,
SELECT SQL_CALC_FOUND_ROWS
`manufacturers`.oo_id as manufacturerId,
`manufacturers`.name as manufacturerName,
`manufacturers`.o_key as manufacturerKey,
`orders`.store,
SUM(orderItems.totalGross) AS sales,
SUM((orderItems.itemRetailPriceNet - orderItems.itemWholesalePrice) * orderItems.quantity) AS profit,
SUM(orderItems.quantity) AS `quantityCount`,
COUNT(orderItems.product__id) AS `orderCount`
FROM object_query_$orderClassId AS orders
INNER JOIN object_relations_$orderClassId AS orderRelations ON orderRelations.src_id = orders.oo_id AND orderRelations.fieldname = \"items\"
INNER JOIN object_query_$orderItemClassId AS orderItems ON orderRelations.dest_id = orderItems.oo_id
FROM object_$manufacturerClassId AS manufacturers
INNER JOIN dependencies AS manProductDependencies ON manProductDependencies.targetId = manufacturers.oo_id AND manProductDependencies.targettype = \"object\"
INNER JOIN object_query_$orderItemClassId AS orderItems ON orderItems.product__id = manProductDependencies.sourceid
INNER JOIN object_relations_$orderClassId AS orderRelations ON orderRelations.dest_id = orderItems.oo_id AND orderRelations.fieldname = \"items\"
INNER JOIN object_query_$orderClassId AS `orders` ON `orders`.oo_id = orderRelations.src_id
WHERE orders.store = $storeId AND orders.orderState = '$orderCompleteState' AND orders.orderDate > ? AND orders.orderDate < ? AND orderItems.product__id IS NOT NULL
GROUP BY orderItems.product__id
ORDER BY COUNT(orderItems.product__id) DESC
";

$productSales = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);

$manufacturerSales = InheritanceHelper::useInheritedValues(function () use ($productSales) {
$manufacturerSales = [];
foreach ($productSales as $productSale) {
$product = DataObject::getById($productSale['product__id']);
if (!$product instanceof ProductInterface) {
continue;
}

$manufacturer = $product->getManufacturer();
if (!$manufacturer instanceof ManufacturerInterface) {
continue;
}

$manufacturerId = $manufacturer->getId();
if (!isset($manufacturerSales[$manufacturerId])) {
$name = !empty($manufacturer->getName()) ? $manufacturer->getName() : $manufacturer->getKey();
$manufacturerSales[$manufacturerId] = $productSale;
$manufacturerSales[$manufacturerId]['name'] = sprintf('%s (Id: %d)', $name, $manufacturer->getId());
} else {
$manufacturerSales[$manufacturerId]['sales'] += $productSale['sales'];
$manufacturerSales[$manufacturerId]['profit'] += $productSale['profit'];
$manufacturerSales[$manufacturerId]['quantityCount'] += $productSale['quantityCount'];
$manufacturerSales[$manufacturerId]['orderCount'] += $productSale['orderCount'];
}
}

return $manufacturerSales;
});

usort($manufacturerSales, function ($a, $b) {
return $b['orderCount'] <=> $a['orderCount'];
});

foreach ($manufacturerSales as &$sale) {
$sale['salesFormatted'] = $this->moneyFormatter->format($sale['sales'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode());
$sale['profitFormatted'] = $this->moneyFormatter->format($sale['profit'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode());
GROUP BY manufacturers.oo_id
ORDER BY quantityCount DESC
LIMIT $offset,$limit";

$results = $this->db->fetchAll($query, [$from->getTimestamp(), $to->getTimestamp()]);

$this->totalRecords = (int) $this->db->fetchColumn('SELECT FOUND_ROWS()');

$data = [];
foreach ($results as $result) {
$name = !empty($result['manufacturerName']) ? $result['manufacturerName'] : $result['manufacturerKey'];
$data[] = [
'name' => sprintf('%s (Id: %d)', $name, $result['manufacturerId']),
'sales' => $result['sales'],
'profit' => $result['profit'],
'quantityCount' => $result['quantityCount'],
'orderCount' => $result['orderCount'],
'salesFormatted' => $this->moneyFormatter->format($result['sales'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode()),
'profitFormatted' => $this->moneyFormatter->format($result['profit'], $store->getCurrency()->getIsoCode(), $this->localeService->getLocaleCode())
];
}

return array_values($manufacturerSales);
return array_values($data);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ services:
- '@coreshop.money_formatter'
- '@coreshop.context.locale'
- '@coreshop.repository.order'
- '@coreshop.repository.category'
- '@coreshop.repository.order_item'
tags:
- { name: coreshop.report, type: categories }
Expand Down Expand Up @@ -79,6 +80,7 @@ services:
- '@doctrine.dbal.default_connection'
- '@coreshop.money_formatter'
- '@coreshop.context.locale'
- '@coreshop.repository.manufacturer'
- '@coreshop.repository.order'
- '@coreshop.repository.order_item'
tags:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ coreshop.report.reports.categories = Class.create(coreshop.report.abstractStore,
];
},

showPaginator: function () {
return true;
},

getGrid: function () {
return new Ext.Panel({
layout: 'fit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ coreshop.report.reports.customers = Class.create(coreshop.report.abstract, {
];
},

showPaginator: function () {
return true;
},

getGrid: function () {
return new Ext.Panel({
layout: 'fit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ coreshop.report.reports.manufacturer = Class.create(coreshop.report.abstractStor
];
},

showPaginator: function () {
return true;
},

getGrid: function () {
return new Ext.Panel({
layout: 'fit',
Expand Down