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/farmfun/reserveren.farmfun.be/app/Komma/Shop/Orders/Kms/OrderService.php
<?php

namespace App\Komma\Shop\Orders\Kms;

use App\Komma\Kms\Core\ModelService;
use App\Komma\Shop\Categories\Kms\CategorizableInterface;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Shop\Orders\Product\OrderedProduct;
use App\Komma\Shop\Orders\ProductComposite\OrderedProductComposite;
use App\Komma\Shop\Orders\ProductGroup\OrderedProductGroup;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Facades\URL;

class OrderService extends ModelService
{
    protected $sortable = false;

    /** @var SiteServiceInterface */
    protected $siteService;

    public function __construct()
    {
        parent::__construct();
        $this->setModelClassName(Order::class);
    }

    /**
     * Return a query builder to retrieve order orders by a certain status.
     *
     * @param string $status
     * @return mixed
     */
    public function ordersByStatus(string $status)
    {
        if (! OrderStatus::isValidItem($status)) {
            throw new \InvalidArgumentException('The order status "'.$status.'" is invalid.');
        }

        return $this->modelClassName::where('status', '=', $status);
    }

    /**
     * Returns the order ids as a comma separated string for the CategorizableInterface implementation
     *
     * @param CategorizableInterface $model
     * @return string Order ids, comma separated
     */
    public function getOrderIdsForModel(CategorizableInterface $model): ?string
    {
        if (! $model->id) {
            return null;
        }

        $idsCollection = $model->orders()->get(['order_id'])->map(function (Order $order) {
            return $order->order_id;
        });
        $idString = implode(',', $idsCollection->toArray());

        if ($idString == '') {
            return null;
        }

        return $idString;
    }

    /**
     * Calculates the total order price and updates the orders total price.
     * Then returns the order. Notice that this method does not save the order.
     * Also notice that it does nothing with VAT. Use a vat rate service for that.
     *
     * The total price is in cents
     *
     * @param Order $order
     * @return Order
     */
    public function calculateTotalOrderPrice(Order $order): Order
    {
        $order->load([
            'orderedProducts',
            'orderedGroups.orderedProducts',
            'orderedProductComposites.orderedGroups.orderedProducts',
        ]);

        $productsPrice = $order->orderedProducts->map(function (OrderedProduct $orderedProduct) {
            return $orderedProduct->price * $orderedProduct->quantity;
        })->sum();
        $productGroupsPrice = $order->orderedGroups->map(function (OrderedProductGroup $orderedProductGroup) {
            return $orderedProductGroup->price * $orderedProductGroup->quantity;
        })->sum();
        $productCompositesPrice = $order->orderedProductComposites->map(function (OrderedProductComposite $orderedProductComposite) {
            return $orderedProductComposite->price * $orderedProductComposite->quantity;
        })->sum();

        //Prices are with discounts taken into account. So we can simply sum them and we are good to go
        $totalInCents = round($productsPrice + $productGroupsPrice + $productCompositesPrice, 0, config('financial.cent_rounding.total_ex', PHP_ROUND_HALF_UP));
        $order->total_price_ex = $totalInCents;

        return $order;
    }

    /**
     * Retrieves an array where the key names are field names, and value are the search values.
     * And uses that array to search for orders. The found orders are returned as a QueryBuilder
     * that holds the query to return all the found orders.
     *
     * @param array $input $input
     * @return Builder
     */
    public function search(array $input): Builder
    {
        /** @var Builder $ordersQueryBuilder */
        $ordersQueryBuilder = $this->modelClassName::query();

        foreach ($input as $fieldName => $searchValue) {
            if ($searchValue == '') {
                continue;
            }
            switch ($fieldName) {
                case 'order_number':
                    $ordersQueryBuilder->where('order_number', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'first_name':
                    $ordersQueryBuilder->whereHas('customer', function (Builder $builder) use ($searchValue) {
                        $builder->where('first_name', 'LIKE', '%'.$searchValue.'%');
                    });
                    break;
                case 'last_name':
                    $ordersQueryBuilder->whereHas('customer', function (Builder $builder) use ($searchValue) {
                        $builder->where('last_name', 'LIKE', '%'.$searchValue.'%');
                    });
                    break;
                case 'email':
                    $ordersQueryBuilder->whereHas('invoiceAddress', function (Builder $builder) use ($searchValue) {
                        $builder->where('email', 'LIKE', '%'.$searchValue.'%');
                    })
                        ->orWhereHas('shippingAddress', function (Builder $builder) use ($searchValue) {
                            $builder->where('email', 'LIKE', '%'.$searchValue.'%');
                        });
                    break;
                case 'street':
                    $ordersQueryBuilder->whereHas('invoiceAddress', function (Builder $builder) use ($searchValue) {
                        $builder->where('street', 'LIKE', '%'.$searchValue.'%');
                    })
                        ->orWhereHas('shippingAddress', function (Builder $builder) use ($searchValue) {
                            $builder->where('street', 'LIKE', '%'.$searchValue.'%');
                        });
                    break;
                case 'house_number':
                    $ordersQueryBuilder->whereHas('invoiceAddress', function (Builder $builder) use ($searchValue) {
                        $builder->where('houseNumber', 'LIKE', '%'.$searchValue.'%');
                    })
                        ->orWhereHas('shippingAddress', function (Builder $builder) use ($searchValue) {
                            $builder->where('houseNumber', 'LIKE', '%'.$searchValue.'%');
                        });
                    break;
                case 'postal_code':
                    $ordersQueryBuilder->whereHas('invoiceAddress', function (Builder $builder) use ($searchValue) {
                        $builder->where('postal_code', 'LIKE', '%'.$searchValue.'%');
                    })
                        ->orWhereHas('shippingAddress', function (Builder $builder) use ($searchValue) {
                            $builder->where('postal_code', 'LIKE', '%'.$searchValue.'%');
                        });
                    break;
                case 'status':
                    if ($searchValue == 'each') {
                        continue;
                    }
                    $ordersQueryBuilder = $ordersQueryBuilder->where('status', '=', $searchValue);
                    break;
            }
        }

        return $ordersQueryBuilder;
    }

    /**
     * Change the status for a collection of orders,
     * save them, return them.
     *
     * @param iterable $orders
     * @param string $status
     * @return BaseCollection
     */
    public function changeOrdersStatus(iterable $orders, string $status)
    {
        if (! self::iterableContainsOrders($orders)) {
            throw new \InvalidArgumentException('The iterable does not exclusively contain orders.');
        }

        $collection = new BaseCollection();
        foreach ($orders as $order) {
            /** @var Order $order */
            $collection->push($this->changeOrderStatus($order, $status));
        }

        return $collection;
    }

    /**
     * Change the status of an order
     *
     * @param Order $order
     * @param string $status
     * @return Order
     */
    public function changeOrderStatus(Order $order, string $status)
    {
        if (! OrderStatus::isValidItem($status)) {
            throw new \InvalidArgumentException('The given status is invalid. Use the OrderStatus enum for valid statuses');
        }

        //TODO. Maybe add some constraints to allow or disallow certain status updates
        $order->status = $status;
        $order->save();

        return $order;
    }

    /**
     * Returns true if the iteratable contains orders only. false if not.
     *
     * @param iterable $iterable
     * @return bool
     */
    public static function iterableContainsOrders(iterable $iterable):bool
    {
        foreach ($iterable as $item) {
            if (! is_a($item, Order::class)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Returns the latest x orders. Where x is a random number.
     *
     * @param $int
     * @return Builder
     */
    public function getLatest($int): Builder
    {
        /** @var Builder $ordersQueryBuilder */
        $ordersQueryBuilder = $this->modelClassName::query();

        return $ordersQueryBuilder->latest('created_at')->limit($int);
    }

    /**
     * Generates an url that must lead to a page in which a user can view IF he still needs to
     * pay for an order or not. If he needs to pay for it, this page must have a way to
     * redirect the user to the PSP payment endpoint.
     *
     * @param Order $order
     * @return string
     */
    public function getManualPaymentUrl(Order $order)
    {
        return URL::signedRoute('orders.manual_payment', ['order' => $order]);
    }
}