diff --git a/src/CoreShop/Bundle/CoreBundle/Report/CategoriesReport.php b/src/CoreShop/Bundle/CoreBundle/Report/CategoriesReport.php index 062d226817..7e2ff7937a 100644 --- a/src/CoreShop/Bundle/CoreBundle/Report/CategoriesReport.php +++ b/src/CoreShop/Bundle/CoreBundle/Report/CategoriesReport.php @@ -58,6 +58,11 @@ class CategoriesReport implements ReportInterface */ private $orderRepository; + /** + * @var PimcoreRepositoryInterface + */ + private $categoryRepository; + /** * @var PimcoreRepositoryInterface */ @@ -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( @@ -77,6 +83,7 @@ public function __construct( MoneyFormatterInterface $moneyFormatter, LocaleContextInterface $localeService, PimcoreRepositoryInterface $orderRepository, + PimcoreRepositoryInterface $categoryRepository, PimcoreRepositoryInterface $orderItemRepository ) { $this->storeRepository = $storeRepository; @@ -84,6 +91,7 @@ public function __construct( $this->moneyFormatter = $moneyFormatter; $this->localeService = $localeService; $this->orderRepository = $orderRepository; + $this->categoryRepository = $categoryRepository; $this->orderItemRepository = $orderItemRepository; } @@ -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 []; @@ -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); } /** diff --git a/src/CoreShop/Bundle/CoreBundle/Report/CustomersReport.php b/src/CoreShop/Bundle/CoreBundle/Report/CustomersReport.php index 885c089d82..a69e4d4f8b 100644 --- a/src/CoreShop/Bundle/CoreBundle/Report/CustomersReport.php +++ b/src/CoreShop/Bundle/CoreBundle/Report/CustomersReport.php @@ -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, @@ -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); } /** diff --git a/src/CoreShop/Bundle/CoreBundle/Report/ManufacturerReport.php b/src/CoreShop/Bundle/CoreBundle/Report/ManufacturerReport.php index 45db1ad114..a1173d3056 100644 --- a/src/CoreShop/Bundle/CoreBundle/Report/ManufacturerReport.php +++ b/src/CoreShop/Bundle/CoreBundle/Report/ManufacturerReport.php @@ -59,6 +59,11 @@ class ManufacturerReport implements ReportInterface */ private $orderRepository; + /** + * @var PimcoreRepositoryInterface + */ + private $manufacturerRepository; + /** * @var PimcoreRepositoryInterface */ @@ -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 */ @@ -77,6 +83,7 @@ public function __construct( Connection $db, MoneyFormatterInterface $moneyFormatter, LocaleContextInterface $localeService, + PimcoreRepositoryInterface $manufacturerRepository, PimcoreRepositoryInterface $orderRepository, PimcoreRepositoryInterface $orderItemRepository ) { @@ -84,6 +91,7 @@ public function __construct( $this->db = $db; $this->moneyFormatter = $moneyFormatter; $this->localeService = $localeService; + $this->manufacturerRepository = $manufacturerRepository; $this->orderRepository = $orderRepository; $this->orderItemRepository = $orderItemRepository; } @@ -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; @@ -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); + } /** diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/config/services/reports.yml b/src/CoreShop/Bundle/CoreBundle/Resources/config/services/reports.yml index 3bf8400b55..373bd1ad1f 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/config/services/reports.yml +++ b/src/CoreShop/Bundle/CoreBundle/Resources/config/services/reports.yml @@ -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 } @@ -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: diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/categories.js b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/categories.js index 66ad3f6dc0..09aaa0c8e2 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/categories.js +++ b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/categories.js @@ -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', diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/customers.js b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/customers.js index 287c34079e..753ab8b452 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/customers.js +++ b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/customers.js @@ -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', diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/manufacturer.js b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/manufacturer.js index 37fce28032..65ae3e1642 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/manufacturer.js +++ b/src/CoreShop/Bundle/CoreBundle/Resources/public/pimcore/js/report/reports/manufacturer.js @@ -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',