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/blijegasten/blijegasten.be/app/Komma/Shop/ShippingCosts/ShippingCostsService.php
<?php
namespace App\Komma\Shop\ShippingCosts;

use App\Komma\Addresses\Models\Address;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;

/**
 * Class ShippingCostservice
 * @package App\Komma\Shop\ShippingCosts
 */
class ShippingCostsService extends ModelService
{
    protected $sortable = false;

    protected $forModelName = ShippingCosts::class;

    /**
     * ShippingCostsService constructor.
     */
    public function __construct()
    {
        $this->setModelClassName(ShippingCosts::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
    {
        if($attributes === null) return $model;

        $this->checkContainsAttributes($attributes);
        $keys = array_keys($model->getAttributes());

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

            if($valueFrom !== Attribute::ValueFromModel) return $model;

            if($valueReference == 'is_default' && $attribute->getValue() == '1') {
                ShippingCosts::query()->update(['is_default' => 0]); //Make all shippingCosts non default. Parent will make current one default
            }
        });

        $model = $this->treeService->save($model, $attributes);
        $model->save();

        return $model;
    }

    /**
     * @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;
    }

    /**
     * Calculate the total amount of shipping costs for the the cart in cents.
     *
     * @param ShoppingCartServiceInterface $shoppingCart
     * @param Address $shippingAddress
     * @return mixed
     */
    public function calculate(ShoppingCartServiceInterface $shoppingCart, Address $shippingAddress = null): float
    {
        //Notice that following techniques assume that shipping costs are country / region dependant 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->getName() === $shippingAddress->country_iso2);
            })->first();
        }


        //Get the default shipping cost model
        /** @var ShippingCosts $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) {
            $customShippingCosts = $this->models()->where('code', '=', strtoupper($regionInfo->getThreeLetterISORegionName()))->first();
        }

        $shippingCosts =  $customShippingCosts ?? $defaultShippingCosts;

        return $shippingCosts->cost; //Do not round the costs. They may be modified later on
    }
}