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/blije-gasten.komma.pro/app/Komma/Zipcodes/ZipcodeController.php
<?php

namespace App\Komma\Zipcodes;

use App\Komma\Globalization\RegionInfo;
use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;

final class ZipcodeController extends BaseController
{
    /** @var ZipcodeService $zipcodeService */
    private $zipcodeService;

    public function __construct()
    {
        $this->zipcodeService = app()->make(ZipcodeService::class);
    }

    /**
     * Get the shipping cost by the given request.
     * Will also update the shopping cart if success.
     *
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function getShippingCostsByZipcode(Request $request)
    {
        // Only ajax request should be allowed
        if (!$request->ajax()) {
            abort(400, 'Only AJAX requests please');
        }

        $zipcode = $request->get('zip');
        if (!$zipcode) {
            abort(422, __('shop/cart.no_zipcode_error'));
        }

        $country = $request->get('country');
        if (!$country) {
            abort(422, 'Please specify country');
        }

        /**
         * Also store the filled zipCode and country for session
         * @var ShoppingCartServiceInterface $shoppingCartService
         */
        $shoppingCartService = app()->make(ShoppingCartServiceInterface::class);
        $shoppingCartService->setDeliveryLocation(null, $zipcode, $country); 

        if(strlen($zipcode) < 4) {
            abort(422, __('shop/cart.incomplete_zipcode'));
        }
//        else if (strlen($zipcode) > 4) {
//            $zipcode = substr($zipcode, 0, 4);
//        }


        $zipcodes = $this->zipcodeService->getZipcodeByCountryAndZipcodePart($country, $zipcode);

        if (!isset($zipcodes) || $zipcodes->count() == 0) {

            $countryInfo = RegionInfo::getInstance($country);

            return response()->json([
                'message' => __('shop/cart.region_not_found', ['postal' => $zipcode, 'country' => $countryInfo->getNativeName()]),
                'price'   => __('shop/cart.shipping_undefined'),
            ], 206);
        }

        if ($zipcodes->count() >= 2) {
            return response()->json([
                'message' => __('shop/cart.multiple_zipcode_error'),
            ], 422);
        }

        if ($zipcodes->count() == 1) {
            return $zipcodes->first();
        }


        return response()->json([
            'message' => 'Unknown zipcode error',
        ], 500);
    }

    /**
     * Update the shopping cart to free
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function setShippingCostToFree()
    {
        /** @var ShoppingCartServiceInterface $shoppingCartService */
        $shoppingCartService = app()->make(ShoppingCartServiceInterface::class);
        $shoppingCartService->removeDeliveryLocation();

    }

    /**
     *
     * @return mixed
     */
    public function findZipcode()
    {
        return $this->zipcodeService->getZipcodeByCountryAndZipcodePart('BEL', '360');
    }

}