Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Classes/EventListener/Order/Create/PersistOrder/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Extcode\Cart\Domain\Model\Cart\Product;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\ProductAdditional;
use Extcode\Cart\Domain\Model\Order\Tax;
use Extcode\Cart\Domain\Repository\Order\ProductAdditionalRepository;
use Extcode\Cart\Domain\Repository\Order\ProductRepository;
use Extcode\Cart\Domain\Repository\Order\TaxRepository;
Expand Down Expand Up @@ -185,15 +184,6 @@ protected function addVariantsOfVariant(BeVariant $variant, int $level): void
*/
protected function addBeVariant(BeVariant $variant, int $level): void
{
$orderTax = GeneralUtility::makeInstance(
Tax::class,
$variant->getTax(),
$this->taxClasses[$variant->getTaxClass()->getId()]
);
$orderTax->setPid($this->storagePid);

$this->taxRepository->add($orderTax);
Copy link
Owner

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?

Copy link
Collaborator Author

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.


$variantInner = $variant;
for ($count = $level; $count > 0; $count--) {
if ($count > 1) {
Expand Down
71 changes: 71 additions & 0 deletions Classes/EventListener/Order/Create/PersistOrder/Taxes.php
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,
Copy link
Owner

Choose a reason for hiding this comment

The 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 $event in this method again.

Suggested change
OrderItem $orderItem,

string $typeOfTax
): OrderItem {
$cart = $event->getCart();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

Suggested change
$cart = $event->getCart();
$cart = $event->getCart();
$orderItem = $event->getOrderItem();

$storagePid = $event->getStoragePid();
$taxClasses = $event->getTaxClasses();

$taxes = $typeOfTax === 'tax' ? $cart->getTaxes() : $cart->getTotalTaxes();

/** @var int $taxClassId */
/** @var float $tax */
foreach ($taxes as $taxClassId => $tax) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change:

Suggested change
foreach ($taxes as $taxClassId => $tax) {
foreach ($taxes as $taxClassId => $taxValue) {

you can remove the new variable in line 55.

$taxValue = $tax;
$taxClass = $taxClasses[$taxClassId];
$orderTax = GeneralUtility::makeInstance(OrderTax::class, $taxValue, $taxClass);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is also readable:

Suggested change
$orderTax = GeneralUtility::makeInstance(OrderTax::class, $taxValue, $taxClass);
$orderTax = GeneralUtility::makeInstance(OrderTax::class, $taxValue, $taxClasses[$taxClassId]);

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;
}
}
7 changes: 7 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The 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
identifier: 'cart--order--create--persist-order--tax'
identifier: 'cart--order--create--persist-order--taxes'

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
Expand Down
15 changes: 8 additions & 7 deletions Configuration/TCA/tx_cart_domain_model_order_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,18 @@
],
'addresses' => [
'showitem' => 'billing_address, shipping_address',
'canNotCollapse' => 0,
],
'numbers' => [
'showitem' => 'order_number, order_date, --linebreak--, invoice_number, invoice_date, --linebreak--, delivery_number, delivery_date',
'canNotCollapse' => 1,
],
'price' => [
'showitem' => 'currency, --linebreak--, currency_code, currency_sign, currency_translation, --linebreak--, gross, net, --linebreak--, order_tax',
'canNotCollapse' => 1,
'showitem' => 'currency, --linebreak--, currency_code, currency_sign, currency_translation, --linebreak--, gross, net, --linebreak--, tax',
],
'total_price' => [
'showitem' => 'total_gross, total_net, --linebreak--, order_total_tax',
'canNotCollapse' => 1,
'showitem' => 'total_gross, total_net, --linebreak--, total_tax',
],
'pdfs' => [
'showitem' => 'order_pdfs, --linebreak--, invoice_pdfs, --linebreak--, delivery_pdfs',
'canNotCollapse' => 1,
],
],
'columns' => [
Expand Down Expand Up @@ -412,6 +407,9 @@
'readOnly' => 1,
'foreign_table' => 'tx_cart_domain_model_order_tax',
'foreign_field' => 'item',
'foreign_match_fields' => [
'record_type' => 'tax',
],
'maxitems' => 9999,
'default' => 0,
],
Expand All @@ -424,6 +422,9 @@
'readOnly' => 1,
'foreign_table' => 'tx_cart_domain_model_order_tax',
'foreign_field' => 'item',
'foreign_match_fields' => [
'record_type' => 'total_tax',
],
'maxitems' => 9999,
'default' => 0,
],
Expand Down
2 changes: 0 additions & 2 deletions Configuration/TCA/tx_cart_domain_model_order_payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
'showitem' => '',
'service' => [
'showitem' => 'service_country, service_id',
'canNotCollapse' => 0,
],
],
'service' => [
'showitem' => 'service_country, service_id',
'canNotCollapse' => 0,
],
],
'columns' => [
Expand Down
2 changes: 0 additions & 2 deletions Configuration/TCA/tx_cart_domain_model_order_product.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@
],
'price' => [
'showitem' => 'price, discount, --linebreak--, gross, net, --linebreak--, tax, tax_class',
'canNotCollapse' => 1,
],
'product_type_and_id' => [
'showitem' => 'product_type, product_id',
'canNotCollapse' => 1,
],
],
'columns' => [
Expand Down
2 changes: 0 additions & 2 deletions Configuration/TCA/tx_cart_domain_model_order_shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
'showitem' => '',
'service' => [
'showitem' => 'service_country, service_id',
'canNotCollapse' => 0,
],
],
'service' => [
'showitem' => 'service_country, service_id',
'canNotCollapse' => 0,
],
],
'columns' => [
Expand Down
6 changes: 5 additions & 1 deletion Configuration/TCA/tx_cart_domain_model_order_tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
'enablecolumns' => [],
'searchFields' => '',
'iconfile' => 'EXT:cart/Resources/Public/Icons/Order/Tax.svg',
'type' => 'record_type',
],
'hideTable' => 1,
'types' => [
'1' => [
'tax' => [
'showitem' => 'tax, tax_class',
],
'total_tax' => [
'showitem' => 'tax, tax_class',
],
],
Expand Down
1 change: 1 addition & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ CREATE TABLE tx_cart_domain_model_order_tax (
pid int(11) DEFAULT '0' NOT NULL,

item int(11) unsigned DEFAULT '0' NOT NULL,
record_type varchar(255) DEFAULT '' NOT NULL,

tax double(11,2) DEFAULT '0.00' NOT NULL,
tax_class int(11) unsigned DEFAULT '0' NOT NULL,
Expand Down
Loading