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/ShippingCosts/ShippingCostsService.php
<?php
namespace App\ShippingCosts;

use App\Addresses\Models\Address;
use App\Cart\ShoppingCartInterface;
use App\Finance\RoundingService;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\SelectOptionInterface;
use Komma\KMS\Core\ModelService;
use App\Cart\ShoppingCart;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Komma\KMS\Globalization\RegionInfo;

/**
 * Class ShippingCostservice
 * @package App\ShippingCosts
 */
class ShippingCostsService extends ModelService
{
    /**
     * ShippingCostsService constructor.
     */
    public function __construct()
    {
        $this->setModelClassName(ShippingCost::class);
        parent::__construct();
    }

    /**
     * Puts the values of attributes in an Eloquent model. And then saves that model.
     *
     * @param Model $model
     * @param \Illuminate\Support\Collection $attributes
     * @return Model
     */
    public function save(Model $model, Collection $attributes = null): Model
    {
        $model = parent::save($model, $attributes);

        $attributes->each(function(Attribute $attribute) use(&$model, &$keys) {
            $valueReference = $attribute->getsValueFromReference();
            $value = $attribute->getValue();

            if($valueReference == 'is_default' && $value == '1') {
                ShippingCost::query()->update(['is_default' => 0]); //Make all shippingCosts non default. Parent will make current one default
                $model->is_default = true; //Note that a number of 1 will not work in here. Only boolean true
            } elseif ($valueReference == 'price') {
                [$price, $vat_scenario_enum] = explode('|', $value);
                $model->cost = RoundingService::Round($price); //Rounds fractions of cents. Price already is in cents.
                $model->vat_scenario_enum = $vat_scenario_enum;
            }
            return null;
        });

        $model->save();
        return $model;
    }

    public function load(Model $model, Collection $attributes = null): Collection
    {
        $attributes = parent::load($model, $attributes);

        $attributes->each(function(Attribute $attribute) use(&$model, &$keys) {
            if($attribute->getsValueFromReference() == 'price') {
                $attribute->setValue($model->cost.'|'.$model->vat_scenario_enum);
            }
        });
        return $attributes;
    }

    /**
     * @return Collection
     */
    public function getCountryOptionsForSelect()
    {
        $selectOptions = RegionInfo::getNeutralCultures()->sortBy(function(RegionInfo $regionInfo) {
            return $regionInfo->getDisplayName();
        })->map(function(RegionInfo $regionInfo) {
            $selectOption = (App::make(SelectOptionInterface::class))
                ->setContent($regionInfo->getDisplayName())
                ->setHtmlContent($regionInfo->getDisplayName())
                ->setValue($regionInfo->getThreeLetterISORegionName());

            return $selectOption;
        });

        return $selectOptions;
    }

    /**
     * Return then shipping costs
     *
     * @param ShoppingCartInterface $shoppingCart
     * @param Address               $shippingAddress
     *
     * @return ShippingCost
     */
    public function get(ShoppingCartInterface $shoppingCart, Address $shippingAddress = null): ShippingCost
    {
        //Notice that following techniques assume that shipping costs are country / region dependent only.

        //Use the shipping address to find the region info. Currently, the country of the shipping address is being used to find it.
        /** @var RegionInfo|null $regionInfo */
        $regionInfo = null;
        if($shippingAddress) {
            $regionInfo = RegionInfo::getNeutralCultures()->filter(function (RegionInfo $regionInfo) use ($shippingAddress) {
                return ($regionInfo->getThreeLetterISORegionName() === $shippingAddress->country_iso3);
            })->first();
        }

        //Get the default shipping cost model
        /** @var ShippingCost $defaultShippingCosts */
        $defaultShippingCosts = $this->models()->where('is_default', '=', 1)->first();
        if(!$defaultShippingCosts) throw new \RuntimeException('ShippingCostsService: Could not calculate shipping costs. Default shipping costs are needed but not available.');

        //Use the region info to find the custom shipping cost model or not.
        $customShippingCosts = null;
        if($regionInfo) {
            /** @var ShippingCost $customShippingCosts */
            $customShippingCosts = $this->models()->where('code', '=', strtoupper($regionInfo->getThreeLetterISORegionName()))->first();
        }

        return $customShippingCosts ?? $defaultShippingCosts;
    }
}