HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Cart/Resources/ShoppingCartItemResource.php
<?php

namespace App\Cart\Resources;

use App\Base\FormatsValuesTrait;
use App\Cart\ShoppingCartItem;
use App\Products\Product\Product;
use App\Products\Product\ProductResource;
use App\Products\ProductComposite\ProductComposite;
use App\Products\ProductComposite\ProductCompositeResource;
use App\Products\ProductGroup\ProductGroup;
use App\Products\ProductGroup\ProductGroupResource;
use App\Vat\Models\FinancialProperties;
use Illuminate\Http\Resources\Json\JsonResource;
use Komma\KMS\Globalization\RegionInfoInterface;

/**
 * Class ShoppingCartItemResource
 *
 * @mixin ShoppingCartItem The $this variable in a JsonResource is proxied to the ShoppingCartItem
 *
 * @package App\Cart\Resources
 */
class ShoppingCartItemResource extends JsonResource
{
    use FormatsValuesTrait;

    private array $fieldsToFormat = ['price', 'priceInc', 'priceWithDiscountsInc', 'priceEx', 'priceWithDiscountsEx', 'vatAmount', 'vatAmountWithDiscounts'];
    private ?RegionInfoInterface $regionInfo = null;

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return array
     */
    public function toArray($request)
    {
        if(!$this->regionInfo) $this->regionInfo = app(RegionInfoInterface::class);

        $productableResource = null;
        switch (get_class($this->getProductable())) {
            case Product::class:
                $productableResource = new ProductResource($this->getProductable());
                break;
            case ProductGroup::class:
                $productableResource = new ProductGroupResource($this->getProductable());
                break;
            case ProductComposite::class:
                $productableResource = new ProductCompositeResource($this->getProductable());
                break;
        }

        //These prices did take discounts into account. Because of the shoppingcart items getPrice method.
        //That method is called in the vatService to calculate the inc, ex, vat amount prices.
        $price = $this->getPrice();
        $priceInc = $this->getPriceInc();
        $priceEx = $this->getPriceEx();
        $vatAmount = $this->getVatAmount();

        //Lets calculate the prices without the discounts. The calculation done here is fine, because it only is for showing it on the screen.
        //And will not and must not be used to do further maths on. Because of rounding errors.
        foreach($this->getDiscountPriceMutations() as $discountPriceMutation) {
            /** @var FinancialProperties $discountPriceMutation */
            $price -= $discountPriceMutation->getPrice();
            $priceInc -= $discountPriceMutation->getPriceInc();
            $priceEx -= $discountPriceMutation->getPriceEx();
            $vatAmount -= $discountPriceMutation->getVatAmount();
        }

        $baseData = [
            'id' => $this->getId(),
            'quantity' => $this->getQuantity(),
            'productable' => $productableResource,
            'price' => $price,
            'priceInc' => $priceInc,
            'priceEx' => $priceEx,
            'priceWithDiscounts' => $this->getPrice(),
            'priceWithDiscountsInc' => $this->getPriceInc(),
            'priceWithDiscountsEx' => $this->getPriceEx(),
            'vatAmount' => $vatAmount,
            'vatAmountWithDiscounts' => $this->getVatAmount(),
            'currencySymbol' => $this->regionInfo->getCurrencySymbol(),
            'discountPriceMutations' => $this->getDiscountPriceMutations(),
        ];

        return array_merge($baseData, $this->formattedValues($baseData));
    }
}