Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Jun 26, 2024
1 parent 8a4ebef commit 19bc7b8
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 100 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,6 @@ jobs:
name: Run PHPStan
run: vendor/bin/phpstan analyse

# -
# name: Run PHPSpec
# run: vendor/bin/phpspec run --ansi -f progress --no-interaction

-
name: Run PHPUnit
run: vendor/bin/phpunit --colors=always

-
name: Upload Behat logs
uses: actions/upload-artifact@v2
if: failure()
with:
name: Behat logs
path: etc/build/
if-no-files-found: ignore
4 changes: 1 addition & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-->

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.2/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="all">
<directory>tests</directory>
Expand All @@ -22,5 +22,3 @@
<server name="KERNEL_CLASS" value="Tests\Sylius\GmvBundle\Application\TestKernel" />
</php>
</phpunit>


4 changes: 2 additions & 2 deletions src/Command/GmvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::SUCCESS;
}

foreach ($gmvs as $key => $gmv) {
$output->writeln(sprintf('<comment>GMV in %s:</comment> %s', $key, $gmv));
foreach ($gmvs as $currencyCode => $gmv) {
$output->writeln(sprintf('<comment>GMV in %s:</comment> %s', $currencyCode, $gmv));
}

return Command::SUCCESS;
Expand Down
9 changes: 4 additions & 5 deletions src/Parser/DateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function parseStartOfMonth(string $date): \DateTime
throw new \InvalidArgumentException('Invalid date format. Expected format: m/Y.');
}

return $dateTime->modify('first day of this month')->setTime(0, 0, 0);
return $dateTime->modify('first day of this month 00:00:00');
}

public function parseEndOfMonth(string $date): \DateTime
Expand All @@ -34,23 +34,22 @@ public function parseEndOfMonth(string $date): \DateTime
throw new \InvalidArgumentException('Invalid date format. Expected format: m/Y.');
}

return $dateTime->modify('last day of this month')
->setTime(23, 59, 59);
return $dateTime->modify('last day of this month 23:59:59');
}

public function getDefaultStartDate(): \DateTime
{
$now = new \DateTime();

return (clone $now)
return $now
->modify('first day of -12 months');
}

public function getDefaultEndDate(): \DateTime
{
$now = new \DateTime();

return (clone $now)
return $now
->modify('last day of last month');
}
}
63 changes: 24 additions & 39 deletions src/Provider/GmvProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

namespace Sylius\GmvBundle\Provider;

use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\OrderPaymentStates;
Expand Down Expand Up @@ -48,22 +50,11 @@ public function getGmvForPeriod(\DateTimeInterface $periodStart, \DateTimeInterf
/** @return array<string> */
private function findCurrenciesInOrders(\DateTimeInterface $periodStart, \DateTimeInterface $periodEnd): array
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o');
$queryBuilder = $this->createCommonQueryBuilder($periodStart, $periodEnd);
$queryBuilder->select('o.currencyCode')
->groupBy('o.currencyCode');

$query = $queryBuilder
->select('o.currencyCode')
->andWhere('o.checkoutCompletedAt >= :periodStart')
->andWhere('o.checkoutCompletedAt <= :periodEnd')
->andWhere('o.checkoutState = :completedState')
->andWhere('o.paymentState != :cancelledState')
->setParameter('periodStart', $periodStart)
->setParameter('periodEnd', $periodEnd)
->setParameter('completedState', OrderCheckoutStates::STATE_COMPLETED)
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED)
->groupBy('o.currencyCode')
->getQuery();

$currencies = $query->getScalarResult();
$currencies = $queryBuilder->getQuery()->getScalarResult();

Assert::isArray($currencies);

Expand All @@ -72,45 +63,39 @@ private function findCurrenciesInOrders(\DateTimeInterface $periodStart, \DateTi

private function calculateGmvForPeriodAndCurrency(\DateTimeInterface $periodStart, \DateTimeInterface $periodEnd, string $currencyCode): int
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o');
$queryBuilder = $this->createCommonQueryBuilder($periodStart, $periodEnd);
$queryBuilder->andWhere('o.currencyCode = :currencyCode')
->setParameter('currencyCode', $currencyCode);

$totalItemsQuery = $queryBuilder
$totalItemsQuery = (int) $queryBuilder
->select('SUM(o.itemsTotal) as totalItems')
->andWhere('o.checkoutCompletedAt >= :periodStart')
->andWhere('o.checkoutCompletedAt <= :periodEnd')
->andWhere('o.checkoutState = :completedState')
->andWhere('o.paymentState != :cancelledState')
->andWhere('o.currencyCode = :currencyCode')
->setParameter('periodStart', $periodStart)
->setParameter('periodEnd', $periodEnd)
->setParameter('completedState', OrderCheckoutStates::STATE_COMPLETED)
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED)
->setParameter('currencyCode', $currencyCode)
->getQuery()
->getSingleScalarResult();

$totalTaxQuery = $queryBuilder
$totalTaxQuery = (int) $queryBuilder
->select('SUM(adjustment.amount) as totalTaxes')
->leftJoin('o.items', 'items')
->leftJoin('items.units', 'units')
->leftJoin('units.adjustments', 'adjustment', 'WITH', 'adjustment.type = :taxType AND adjustment.neutral = true')
->setParameter('taxType', AdjustmentInterface::TAX_ADJUSTMENT)
->getQuery()
->getSingleScalarResult();

return $totalItemsQuery - $totalTaxQuery;
}

private function createCommonQueryBuilder(\DateTimeInterface $periodStart, \DateTimeInterface $periodEnd): QueryBuilder
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o');

return $queryBuilder
->andWhere('o.checkoutCompletedAt >= :periodStart')
->andWhere('o.checkoutCompletedAt <= :periodEnd')
->andWhere('o.checkoutState = :completedState')
->andWhere('o.paymentState != :cancelledState')
->andWhere('o.currencyCode = :currencyCode')
->setParameter('periodStart', $periodStart)
->setParameter('periodEnd', $periodEnd)
->setParameter('completedState', OrderCheckoutStates::STATE_COMPLETED)
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED)
->setParameter('taxType', 'tax')
->setParameter('currencyCode', $currencyCode)
->getQuery()
->getSingleScalarResult();

$totalItems = (int) $totalItemsQuery;
$totalTaxes = (int) $totalTaxQuery;

return $totalItems - $totalTaxes;
->setParameter('cancelledState', OrderPaymentStates::STATE_CANCELLED);
}
}
20 changes: 10 additions & 10 deletions src/Validator/InputParametersValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ public function __construct(private readonly DateParserInterface $dateParser)
{
}

public function validateDateFormat(string $date): bool
{
return preg_match('/^(0[1-9]|1[0-2])\/\d{4}$/', $date) === 1;
}

public function validateDates(\DateTime $startDate, \DateTime $endDate): bool
{
return $startDate < $endDate;
}

public function validate(string $periodStart, string $periodEnd): bool
{
if (!$this->validateDateFormat($periodStart) || !$this->validateDateFormat($periodEnd)) {
Expand All @@ -42,4 +32,14 @@ public function validate(string $periodStart, string $periodEnd): bool

return $this->validateDates($startDate, $endDate);
}

private function validateDateFormat(string $date): bool
{
return preg_match('/^(0[1-9]|1[0-2])\/\d{4}$/', $date) === 1;
}

private function validateDates(\DateTime $startDate, \DateTime $endDate): bool
{
return $startDate < $endDate;
}
}
4 changes: 0 additions & 4 deletions src/Validator/InputParametersValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@

interface InputParametersValidatorInterface
{
public function validateDateFormat(string $date): bool;

public function validateDates(\DateTime $startDate, \DateTime $endDate): bool;

public function validate(string $periodStart, string $periodEnd): bool;
}
32 changes: 16 additions & 16 deletions tests/Application/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
imports:
- { resource: '@SyliusCoreBundle/Resources/config/app/config.yml' }
- { resource: '@SyliusGmvBundle/config/services.xml' }
- { resource: '@SyliusCoreBundle/Resources/config/app/config.yml' }
- { resource: '@SyliusGmvBundle/config/services.xml' }

parameters:
locale: en_US
locale: en_US

security:
firewalls:
main:
security: false
firewalls:
main:
security: false

framework:
test: true
session:
handler_id: null
csrf_protection: ~
router:
resource: ~
secret: 'test'
test: true
session:
handler_id: null
csrf_protection: ~
router:
resource: ~
secret: 'test'

doctrine:
dbal:
driver: pdo_sqlite
path: '%kernel.project_dir%/var/data/test.sqlite'
dbal:
driver: pdo_sqlite
path: '%kernel.project_dir%/var/data/test.sqlite'
49 changes: 49 additions & 0 deletions tests/DataFixtures/sales_with_shipping_and_taxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-01-27 00:00:01'))>"
adjustments: ["@order_001_shipping"]
order_002:
channel: "@channel"
currencyCode: USD
Expand All @@ -75,6 +76,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-03-15 06:00:01'))>"
adjustments: ["@order_002_shipping"]
order_003:
channel: "@channel"
currencyCode: USD
Expand All @@ -84,6 +86,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-03-15 15:00:01'))>"
adjustments: ["@order_003_shipping"]
order_004:
channel: "@channel"
currencyCode: USD
Expand All @@ -93,6 +96,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-03-18 06:00:01'))>"
adjustments: ["@order_004_shipping"]
order_005:
channel: "@channel"
currencyCode: USD
Expand All @@ -102,6 +106,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-02-18 08:00:01'))>"
adjustments: ["@order_005_shipping"]
order_006:
channel: "@channel"
currencyCode: USD
Expand All @@ -111,6 +116,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-02-18 09:30:01'))>"
adjustments: ["@order_006_shipping"]
expensive_order_001:
channel: "@expensive_channel"
currencyCode: EUR
Expand All @@ -120,6 +126,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: paid
checkoutCompletedAt: "<(new \\DateTime('2024-01-29 09:30:01'))>"
adjustments: ["@expensive_order_001_shipping"]
expensive_order_002:
channel: "@expensive_channel"
currencyCode: EUR
Expand All @@ -129,6 +136,7 @@ Sylius\Component\Core\Model\Order:
checkoutState: completed
paymentState: new
checkoutCompletedAt: "<(new \\DateTime('2024-03-18 09:30:01'))>"
adjustments: ["@expensive_order_002_shipping"]

Sylius\Component\Core\Model\OrderItem:
sw_mug_item_1:
Expand Down Expand Up @@ -261,6 +269,47 @@ Sylius\Component\Core\Model\Adjustment:
label: "Tax"
amount: 11168
neutral: true
order_001_shipping:
type: shipping
label: "Shipping"
amount: 1200
neutral: false
order_002_shipping:
type: shipping
label: "Shipping"
amount: 4500
neutral: false
order_003_shipping:
type: shipping
label: "Shipping"
amount: 6200
neutral: false
order_004_shipping:
type: shipping
label: "Shipping"
amount: 5634
neutral: false
order_005_shipping:
type: shipping
label: "Shipping"
amount: 3300
neutral: false
order_006_shipping:
type: shipping
label: "Shipping"
amount: 1254
neutral: false
expensive_order_001_shipping:
type: shipping
label: "Shipping"
amount: 13000
neutral: false
expensive_order_002_shipping:
type: shipping
label: "Shipping"
amount: 34500
neutral: false


Sylius\Component\Core\Model\Channel:
channel:
Expand Down
Loading

0 comments on commit 19bc7b8

Please sign in to comment.