Skip to content

Commit

Permalink
fix: customer group discount tax
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominikrt committed Sep 8, 2024
1 parent 81879a1 commit 8b09ee7
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion engine/Shopware/Core/sBasket.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public function sInsertDiscount()
$tax = $this->config->get('sDISCOUNTTAX');
}

if (!$tax) {
if (!$tax && $tax != 0) {
$tax = 19;
}

Expand Down
40 changes: 40 additions & 0 deletions tests/Functional/Bundle/StoreFrontBundle/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Enlight_Components_Db_Adapter_Pdo_Mysql;
use Shopware\Bundle\StoreFrontBundle\Service\ListProductServiceInterface;
use Shopware\Bundle\StoreFrontBundle\Struct\Customer\Group as CustomerGroupStruct;
Expand All @@ -45,11 +47,15 @@
use Shopware\Models\Article\Configurator\Option;
use Shopware\Models\Article\Supplier;
use Shopware\Models\Category\Category;
use Shopware\Models\Customer\Customer;
use Shopware\Models\Customer\Discount as CustomerDiscount;
use Shopware\Models\Customer\Group;
use Shopware\Models\Customer\Group as CustomerGroup;
use Shopware\Models\Price\Discount;
use Shopware\Models\Price\Group as PriceGroup;
use Shopware\Models\Shop\Currency;
use Shopware\Models\Shop\Shop as ShopModel;
use Shopware\Models\Tax\Rule as TaxRule;
use Shopware\Models\Tax\Tax as TaxModel;
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper\ProgressHelper;
use Shopware_Components_Config;
Expand Down Expand Up @@ -361,6 +367,40 @@ public function createTax(array $data = []): TaxModel
return $tax;
}

/**
* @param array<string, string|int|Customer|Group|bool> $data
*
* @throws ORMException
* @throws OptimisticLockException
*/
public function createTaxRule(array $data): TaxRule
{
$taxRule = new TaxRule();
$taxRule->fromArray($data);

$this->entityManager->persist($taxRule);
$this->entityManager->flush();

return $taxRule;
}

/**
* @param array<string, string|int|Customer|Group|bool> $data
*
* @throws OptimisticLockException
* @throws ORMException
*/
public function createCustomerGroupDiscount(array $data): CustomerDiscount
{
$customerDiscount = new CustomerDiscount();
$customerDiscount->fromArray($data);

$this->entityManager->persist($customerDiscount);
$this->entityManager->flush();

return $customerDiscount;
}

public function createCurrency(array $data = []): Currency
{
$currency = new Currency();
Expand Down
64 changes: 64 additions & 0 deletions tests/Functional/Core/BasketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
use Shopware\Components\Random;
use Shopware\Models\Article\Detail;
use Shopware\Models\Customer\Customer;
use Shopware\Models\Customer\Discount as CustomerGroupDiscount;
use Shopware\Models\Customer\Group as CustomerGroup;
use Shopware\Models\Tax\Rule as TaxRule;
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper;
use Shopware\Tests\Functional\Helper\Utils;
use Shopware\Tests\Functional\Traits\ContainerTrait;
Expand Down Expand Up @@ -156,6 +159,44 @@ public function testsCheckBasketQuantitiesWithLowerQuantityThanAvailable(): void
static::assertFalse($result['articles'][$inStockProduct['ordernumber']]['OutOfStock']);
}

public function testInsertDiscountWithTaxFreeTaxRule(): void
{
$this->generateBasketSession();

$customerGroupRepo = $this->getContainer()->get('models')->getRepository(CustomerGroup::class);
$customerGroup = $customerGroupRepo->findOneBy(['key' => 'EK']);
static::assertInstanceOf(CustomerGroup::class, $customerGroup);

$taxRule = $this->createTaxFreeTaxRule($customerGroup);
$customerGroupDiscount = $this->createCustomerGroupDiscount($customerGroup);

$this->module->sAddArticle('SW10003');
$this->module->sInsertDiscount();

$discount = $this->connection->fetchAssociative(
'SELECT * FROM s_order_basket WHERE sessionID = :sessionId AND ordernumber = :ordernumber',
[
'sessionId' => $this->getSessionId(),
'ordernumber' => 'sw-discount',
]
);

static::assertIsArray($discount);
static::assertSame(0, (int) $discount['tax_rate']);

// Housekeeping
$this->connection->delete(
's_order_basket',
['sessionID' => $this->getSessionId()]
);

$modelManager = $this->getContainer()->get('models');
$modelManager->remove($taxRule);
$modelManager->remove($customerGroupDiscount);

$modelManager->flush();
}

public function testsCheckBasketQuantitiesWithHigherQuantityThanAvailable(): void
{
$this->generateBasketSession();
Expand Down Expand Up @@ -2677,4 +2718,27 @@ private function getSessionId(): string
{
return $this->session->get('sessionId');
}

private function createTaxFreeTaxRule(CustomerGroup $group): TaxRule
{
$resourceHelper = new Helper($this->getContainer());

return $resourceHelper->createTaxRule([
'name' => 'PHPUNIT-TAX-FREE',
'active' => true,
'customerGroup' => $group,
'groupId' => 1,
]);
}

private function createCustomerGroupDiscount(CustomerGroup $group): CustomerGroupDiscount
{
$resourceHelper = new Helper($this->getContainer());

return $resourceHelper->createCustomerGroupDiscount([
'group' => $group,
'discount' => 1,
'value' => 2,
]);
}
}

0 comments on commit 8b09ee7

Please sign in to comment.