Skip to content

Commit e92dd08

Browse files
committed
fix: customer group discount tax
1 parent 81879a1 commit e92dd08

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

engine/Shopware/Core/sBasket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public function sInsertDiscount()
421421
$tax = $this->config->get('sDISCOUNTTAX');
422422
}
423423

424-
if (!$tax) {
424+
if (!$tax && $tax != 0) {
425425
$tax = 19;
426426
}
427427

tests/Functional/Bundle/StoreFrontBundle/Helper.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
use Doctrine\Common\Collections\ArrayCollection;
2929
use Doctrine\DBAL\Connection;
30+
use Doctrine\ORM\Exception\ORMException;
31+
use Doctrine\ORM\OptimisticLockException;
3032
use Enlight_Components_Db_Adapter_Pdo_Mysql;
3133
use Shopware\Bundle\StoreFrontBundle\Service\ListProductServiceInterface;
3234
use Shopware\Bundle\StoreFrontBundle\Struct\Customer\Group as CustomerGroupStruct;
@@ -45,11 +47,15 @@
4547
use Shopware\Models\Article\Configurator\Option;
4648
use Shopware\Models\Article\Supplier;
4749
use Shopware\Models\Category\Category;
50+
use Shopware\Models\Customer\Customer;
51+
use Shopware\Models\Customer\Discount as CustomerDiscount;
52+
use Shopware\Models\Customer\Group;
4853
use Shopware\Models\Customer\Group as CustomerGroup;
4954
use Shopware\Models\Price\Discount;
5055
use Shopware\Models\Price\Group as PriceGroup;
5156
use Shopware\Models\Shop\Currency;
5257
use Shopware\Models\Shop\Shop as ShopModel;
58+
use Shopware\Models\Tax\Rule as TaxRule;
5359
use Shopware\Models\Tax\Tax as TaxModel;
5460
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper\ProgressHelper;
5561
use Shopware_Components_Config;
@@ -361,6 +367,41 @@ public function createTax(array $data = []): TaxModel
361367
return $tax;
362368
}
363369

370+
/**
371+
* @param array<string, string|int|Customer|Group|bool> $data
372+
* @return TaxRule
373+
* @throws ORMException
374+
* @throws OptimisticLockException
375+
*/
376+
public function createTaxRule(array $data): TaxRule
377+
{
378+
$taxRule = new TaxRule();
379+
$taxRule->fromArray($data);
380+
381+
$this->entityManager->persist($taxRule);
382+
$this->entityManager->flush();
383+
384+
return $taxRule;
385+
}
386+
387+
388+
/**
389+
* @param array<string, string|int|Customer|Group|bool> $data
390+
* @return CustomerDiscount
391+
* @throws OptimisticLockException
392+
* @throws ORMException
393+
*/
394+
public function createCustomerGroupDiscount(array $data): CustomerDiscount
395+
{
396+
$customerDiscount = new CustomerDiscount();
397+
$customerDiscount->fromArray($data);
398+
399+
$this->entityManager->persist($customerDiscount);
400+
$this->entityManager->flush();
401+
402+
return $customerDiscount;
403+
}
404+
364405
public function createCurrency(array $data = []): Currency
365406
{
366407
$currency = new Currency();

tests/Functional/Core/BasketTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
use Shopware\Components\Random;
4141
use Shopware\Models\Article\Detail;
4242
use Shopware\Models\Customer\Customer;
43+
use Shopware\Models\Customer\Discount as CustomerGroupDiscount;
44+
use Shopware\Models\Customer\Group as CustomerGroup;
45+
use Shopware\Models\Tax\Rule as TaxRule;
4346
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper;
4447
use Shopware\Tests\Functional\Helper\Utils;
4548
use Shopware\Tests\Functional\Traits\ContainerTrait;
@@ -156,6 +159,44 @@ public function testsCheckBasketQuantitiesWithLowerQuantityThanAvailable(): void
156159
static::assertFalse($result['articles'][$inStockProduct['ordernumber']]['OutOfStock']);
157160
}
158161

162+
public function testInsertDiscountWithTaxFreeTaxRule(): void
163+
{
164+
$this->generateBasketSession();
165+
166+
$customerGroupRepo = $this->getContainer()->get('models')->getRepository(CustomerGroup::class);
167+
$customerGroup = $customerGroupRepo->findOneBy(['key' => 'EK']);
168+
static::assertInstanceOf(CustomerGroup::class, $customerGroup);
169+
170+
$taxRule = $this->createTaxFreeTaxRule($customerGroup);
171+
$customerGroupDiscount = $this->createCustomerGroupDiscount($customerGroup);
172+
173+
$this->module->sAddArticle('SW10003');
174+
$this->module->sInsertDiscount();
175+
176+
$discount = $this->connection->fetchAssociative(
177+
'SELECT * FROM s_order_basket WHERE sessionID = :sessionId AND ordernumber = :ordernumber',
178+
[
179+
'sessionId' => $this->getSessionId(),
180+
'ordernumber' => 'sw-discount',
181+
]
182+
);
183+
184+
static::assertIsArray($discount);
185+
static::assertSame(0, (int) $discount['tax_rate']);
186+
187+
// Housekeeping
188+
$this->connection->delete(
189+
's_order_basket',
190+
['sessionID' => $this->getSessionId()]
191+
);
192+
193+
$modelManager = $this->getContainer()->get('models');
194+
$modelManager->remove($taxRule);
195+
$modelManager->remove($customerGroupDiscount);
196+
197+
$modelManager->flush();
198+
}
199+
159200
public function testsCheckBasketQuantitiesWithHigherQuantityThanAvailable(): void
160201
{
161202
$this->generateBasketSession();
@@ -2677,4 +2718,27 @@ private function getSessionId(): string
26772718
{
26782719
return $this->session->get('sessionId');
26792720
}
2721+
2722+
private function createTaxFreeTaxRule(CustomerGroup $group): TaxRule
2723+
{
2724+
$resourceHelper = new Helper($this->getContainer());
2725+
2726+
return $resourceHelper->createTaxRule([
2727+
'name' => 'PHPUNIT-TAX-FREE',
2728+
'active' => true,
2729+
'customerGroup' => $group,
2730+
'groupId' => 1,
2731+
]);
2732+
}
2733+
2734+
private function createCustomerGroupDiscount(CustomerGroup $group): CustomerGroupDiscount
2735+
{
2736+
$resourceHelper = new Helper($this->getContainer());
2737+
2738+
return $resourceHelper->createCustomerGroupDiscount([
2739+
'group' => $group,
2740+
'discount' => 1,
2741+
'value' => 2,
2742+
]);
2743+
}
26802744
}

0 commit comments

Comments
 (0)