-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUGFIX] Proper storage of taxes for an order #521
Changes from all commits
806ed47
20c8209
ec3a830
2c7b095
e827cfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,71 @@ | ||||||||
<?php | ||||||||
|
||||||||
declare(strict_types=1); | ||||||||
|
||||||||
namespace Extcode\Cart\EventListener\Order\Create\PersistOrder; | ||||||||
|
||||||||
/* | ||||||||
* This file is part of the package extcode/cart. | ||||||||
* | ||||||||
* For the full copyright and license information, please read the | ||||||||
* LICENSE file that was distributed with this source code. | ||||||||
*/ | ||||||||
|
||||||||
use Extcode\Cart\Domain\Model\Order\Item as OrderItem; | ||||||||
use Extcode\Cart\Domain\Model\Order\Tax as OrderTax; | ||||||||
use Extcode\Cart\Domain\Repository\Order\ItemRepository; | ||||||||
use Extcode\Cart\Domain\Repository\Order\TaxRepository; | ||||||||
use Extcode\Cart\Event\Order\PersistOrderEventInterface; | ||||||||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||||||||
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; | ||||||||
|
||||||||
class Taxes | ||||||||
{ | ||||||||
public function __construct( | ||||||||
private PersistenceManager $persistenceManager, | ||||||||
private ItemRepository $itemRepository, | ||||||||
private TaxRepository $taxRepository | ||||||||
) {} | ||||||||
|
||||||||
public function __invoke(PersistOrderEventInterface $event): void | ||||||||
{ | ||||||||
$orderItem = $event->getOrderItem(); | ||||||||
|
||||||||
$orderItem = $this->addTaxToRepositoryAndItem($event, $orderItem, 'tax'); | ||||||||
$orderItem = $this->addTaxToRepositoryAndItem($event, $orderItem, 'totalTax'); | ||||||||
|
||||||||
$this->itemRepository->update($orderItem); | ||||||||
$this->persistenceManager->persistAll(); | ||||||||
} | ||||||||
|
||||||||
protected function addTaxToRepositoryAndItem( | ||||||||
PersistOrderEventInterface $event, | ||||||||
OrderItem $orderItem, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the extra function parameter $orderItem? We can get it from
Suggested change
|
||||||||
string $typeOfTax | ||||||||
): OrderItem { | ||||||||
$cart = $event->getCart(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above.
Suggested change
|
||||||||
$storagePid = $event->getStoragePid(); | ||||||||
$taxClasses = $event->getTaxClasses(); | ||||||||
|
||||||||
$taxes = $typeOfTax === 'tax' ? $cart->getTaxes() : $cart->getTotalTaxes(); | ||||||||
|
||||||||
/** @var int $taxClassId */ | ||||||||
/** @var float $tax */ | ||||||||
foreach ($taxes as $taxClassId => $tax) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you change:
Suggested change
you can remove the new variable in line 55. |
||||||||
$taxValue = $tax; | ||||||||
$taxClass = $taxClasses[$taxClassId]; | ||||||||
$orderTax = GeneralUtility::makeInstance(OrderTax::class, $taxValue, $taxClass); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is also readable:
Suggested change
to we do not need the variable assignment. |
||||||||
$orderTax->setPid($storagePid); | ||||||||
|
||||||||
$this->taxRepository->add($orderTax); | ||||||||
|
||||||||
if ($typeOfTax === 'tax') { | ||||||||
$orderItem->addTax($orderTax); | ||||||||
} else { | ||||||||
$orderItem->addTotalTax($orderTax); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return $orderItem; | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -49,6 +49,13 @@ services: | |||||
event: Extcode\Cart\Event\Order\PersistOrderEvent | ||||||
after: 'cart--order--create--persist-order--item' | ||||||
|
||||||
Extcode\Cart\EventListener\Order\Create\PersistOrder\Taxes: | ||||||
tags: | ||||||
- name: event.listener | ||||||
identifier: 'cart--order--create--persist-order--tax' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My syntax for the identifier is related to the class name. Can you change this?
Suggested change
|
||||||
event: Extcode\Cart\Event\Order\PersistOrderEvent | ||||||
after: 'cart--order--create--persist-order--tax-classes' | ||||||
|
||||||
Extcode\Cart\EventListener\Order\Create\PersistOrder\Products: | ||||||
tags: | ||||||
- name: event.listener | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think the
$this->taxRepository
isn't needed anymore in this class. Could the property and inject-method can also be removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@extcode I have unfortunately no laptop with me, just a smartphone with the GitHub app. If you want to merge it quick you need to adapt the code yourself. All the proposals look good to me.