Skip to content

Commit c3fa1a5

Browse files
author
Maxime Huran
authored
Merge pull request #29 from delyriand/fix/avoid-division-zero
Avoid division by zero and allow order and payment status to be modified
2 parents be42b50 + f7c6575 commit c3fa1a5

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ monsieurbiz_sales_reports_plugin:
5454
5555
All reports columns are sortable by clicking on it.
5656
57+
If you want to change order or payments states used in the reports, you can configure it in the `config/packages/monsieurbiz_sylius_sales_reports_plugin.yaml` file:
58+
59+
```yaml
60+
parameters:
61+
monsieurbiz.sales_reports.order_states:
62+
- …
63+
monsieurbiz.sales_reports.payment_states:
64+
- …
65+
```
66+
67+
(FYI, is it possible to use `!php/const` to use constants in the config file)
68+
5769
### Global sales report
5870

5971
![Global sales report](screenshots/global.png)

src/Repository/AbstractReportRepository.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ abstract class AbstractReportRepository
7676
*/
7777
public function __construct(
7878
OrderRepositoryInterface $orderRepository,
79-
ProductVariantRepositoryInterface $productVariantRepository
79+
ProductVariantRepositoryInterface $productVariantRepository,
80+
protected ?array $orderStates,
81+
protected ?array $paymentStates,
8082
) {
8183
$this->orderRepository = $orderRepository;
8284
$this->productVariantRepository = $productVariantRepository;
@@ -186,6 +188,8 @@ protected function appendAdjustmentsAndParameters(
186188
bool $isOrder = false
187189
) {
188190
$elementAlias = $isOrder ? 'o' : 'element';
191+
$orderStates = $this->orderStates ?? [OrderInterface::STATE_FULFILLED, OrderInterface::STATE_NEW];
192+
$paymentStates = $this->paymentStates ?? [OrderPaymentStates::STATE_PAID];
189193

190194
return $queryBuilder
191195
// Adjustments joins
@@ -237,8 +241,8 @@ protected function appendAdjustmentsAndParameters(
237241
->andWhere('o.checkoutCompletedAt BETWEEN :from AND :to')
238242
// Filters parameters
239243
->setParameter('channel', $channel)
240-
->setParameter('states', [OrderInterface::STATE_FULFILLED, OrderInterface::STATE_NEW])
241-
->setParameter('payment_states', [OrderPaymentStates::STATE_PAID]) // @TODO Take care of OrderPaymentStates::STATE_PARTIALLY_PAID
244+
->setParameter('states', $orderStates)
245+
->setParameter('payment_states', $paymentStates)
242246
->setParameter('from', $fromDate)
243247
->setParameter('to', $toDate)
244248
;

src/Resources/config/services.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
parameters:
2+
monsieurbiz.sales_reports.order_states:
3+
- !php/const Sylius\Component\Order\Model\OrderInterface::STATE_FULFILLED
4+
- !php/const Sylius\Component\Order\Model\OrderInterface::STATE_NEW
5+
monsieurbiz.sales_reports.payment_states:
6+
- !php/const Sylius\Component\Core\OrderPaymentStates::STATE_PAID
7+
18
services:
29
# Default configuration for services in *this* file
310
_defaults:
@@ -26,3 +33,8 @@ services:
2633
class: MonsieurBiz\SyliusSalesReportsPlugin\Menu\AdminMenuListener
2734
tags:
2835
- { name: kernel.event_listener, event: sylius.menu.admin.main, method: addAdminMenuItem }
36+
37+
MonsieurBiz\SyliusSalesReportsPlugin\Repository\ReportRepository:
38+
arguments:
39+
$orderStates: '%monsieurbiz.sales_reports.order_states%'
40+
$paymentStates: '%monsieurbiz.sales_reports.payment_states%'

src/Resources/views/Admin/Report/_average.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</thead>
2222
<tbody>
2323
<tr>
24-
<td>{{ (average_sales_result.item_qty_total / average_sales_result.number_of_elements)|round(2, 'floor') }}</td>
24+
<td>{{ average_sales_result.number_of_elements ? (average_sales_result.item_qty_total / average_sales_result.number_of_elements)|round(2, 'floor') : 0 }}</td>
2525
<td>{{ money.format(average_sales_result.without_tax_total, channel.baseCurrency.code) }}</td>
2626
<td>{{ money.format(average_sales_result.without_tax_promo_total, channel.baseCurrency.code) }}</td>
2727
<td>{{ money.format(average_sales_result.without_tax_shipping_total, channel.baseCurrency.code) }}</td>

0 commit comments

Comments
 (0)