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/farmfun.komma.pro/app/Komma/Shop/Shipments/ShipmentService.php
<?php

namespace App\Komma\Shop\Shipments;

use App\Komma\Kms\Core\ModelService;
use App\Komma\Shop\Orders\Kms\OrderService;
use App\Komma\Shop\Orders\Models\Order;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class ShipmentService extends ModelService
{
    protected $modelClassName = Shipment::class;

    /**
     * Makes a shipment using an Order and optionally a tracking code and comment.
     * Notice that since this methods only makes the shipment. You need to save it yourself.
     *
     * @param Order $order
     * @param string|null $trackingCode
     * @param string $comment
     * @return Shipment
     */
    public function makeForOrder(Order $order, string $trackingCode = null, string $comment = null): Shipment
    {
        return $this->make($order, $trackingCode, $comment);
    }

    /**
     * Give this method an array, collection or any other iterable that contains Orders, and it will
     * return you a collection of Shipments for those orders
     *
     * @param Collection $orders
     * @return Collection
     */
    public function makeForOrders(Collection $orders): Collection
    {
        if (! OrderService::iterableContainsOrders($orders)) {
            throw new \InvalidArgumentException('The iterable does not exclusively contain orders.');
        }

        $shipments = new Collection();
        $orders->each(function (Order $order) use (&$shipments) {
            $shipments->push($this->makeForOrder($order));
        });

        return $shipments;
    }

    /**
     * Makes a shipment using an order
     *
     * @param Order $order
     * @param string|null $trackingCode
     * @param string|null $comment
     * @return Shipment
     */
    private function make(Order $order, string $trackingCode = null, string $comment = null): Shipment
    {
        $shipment = new Shipment();
        $shipment->order()->associate($order);
        $shipment->tracking_code = $trackingCode ?: '';
        $shipment->comment = $comment ?: '';
        $shipment->status = ShipmentStatus::NEW;

        return $shipment;
    }

    /**
     * Returns true if the iteratable contains shipments only. false if not.
     *
     * @param iterable $iterable
     * @return bool
     */
    public static function iterableContainsShipments(Collection $iterable):bool
    {
        foreach ($iterable as $item) {
            if (! is_a($item, Shipment::class)) {
                dd($item);

                return false;
            }
        }

        return true;
    }

    /**
     * Returns the latest x shipment groups. 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);
    }

    /**
     * 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 'id':
                    $ordersQueryBuilder->where('id', '=', $searchValue);
                    break;
                case 'tracking_code':
                    $ordersQueryBuilder->where('tracking_code', '=', $searchValue);
                    break;
                case 'order_number':
                    $ordersQueryBuilder->whereHas('order', function (Builder $builder) use ($searchValue) {
                        $builder->where('order_number', 'LIKE', '%'.$searchValue.'%');
                    });
                    break;
                case 'status':
                    if ($searchValue == 'each') {
                        continue;
                    }
                    $ordersQueryBuilder = $ordersQueryBuilder->where('status', '=', $searchValue);
                    break;
            }
        }

        return $ordersQueryBuilder;
    }
}