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/SBogers95/rentman.io/app/Komma/Shop/Orders/Kms/OrderService.php
<?php

namespace App\Komma\Shop\Orders\Kms;

use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Shop\Categories\Kms\CategorizableInterface;
use App\Komma\Shop\Orders\Models\Order;
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 App\Komma\Users\Models\User;
use Illuminate\Database\Eloquent\Builder;

class OrderService extends SectionService implements OrderServiceInterface
{
    protected $sortable = false;

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

    public function __construct()
    {
        $this->forModelName = Order::class;
        $this->siteService = \App::make(SiteServiceInterface::class);

        parent::__construct();
    }

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

    /**
     * @param Order $order
     * @param User $customer
     * @return Order
     */
    public function fillOrderWithCustomerInfo(Order $order, User $customer): Order
    {
        $order->invoice_first_name = $customer->first_name;
        $order->invoice_last_name = $customer->last_name;
        $order->invoice_email = $customer->email;
        $order->invoice_company = $customer->company_name;
        $order->invoice_culture = $customer->culture;
        $order->invoice_postal_code = $customer->postal_code;
        $order->invoice_city = $customer->city;
        $order->invoice_street = $customer->street;
        $order->invoice_house_number = $customer->house_number;
        $order->invoice_telephone = $customer->telephone;

        $order->shipping_first_name = $customer->first_name;
        $order->shipping_last_name = $customer->last_name;
        $order->shipping_email = $customer->email;
        $order->shipping_company = $customer->company_name;
        $order->shipping_culture = $customer->culture;
        $order->shipping_postal_code = $customer->postal_code;
        $order->shipping_city = $customer->city;
        $order->shipping_street = $customer->street;
        $order->shipping_house_number = $customer->house_number;
        $order->shipping_telephone = $customer->telephone;

        return $order;
    }

    /**
     * Calculates the total order price and updates the orders total price.
     * Then returns the order. Notice that this method does not save the order.
     *
     * The total price is in cents
     *
     * @param Order $order
     * @return Order
     */
    public function calculateTotalOrderPrice(Order $order): Order
    {
        $order->load(['orderedProducts', 'orderedProductGroups.orderedProducts', 'orderedProductComposites.orderedGroups.orderedProducts']);

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

        //Prices are with discounts taken into account. So we can simply sum them and we are good to go
        $order->total_price = $productsPrice + $productGroupsPrice + $productCompositesPrice;

        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->forModelName::query();

        foreach ($input as $fieldName => $searchValue) {
            if ($searchValue == '') {
                continue;
            }
            switch ($fieldName) {
                case 'order_number':
                    //TODO Not implemeted yet as orders don't have order numbers. Only id
                    break;
                case 'first_name':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_first_name', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_first_name', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'last_name':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_last_name', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_last_name', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'email':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_email', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_email', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'company':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_email', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_email', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'street':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_street', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_street', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'house_number':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_house_number', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_house_number', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'postal_code':
                    $ordersQueryBuilder = $ordersQueryBuilder->where('invoice_postal_code', 'LIKE', '%'.$searchValue.'%')->orWhere('shipping_postal_code', 'LIKE', '%'.$searchValue.'%');
                    break;
                case 'status':
                    if ($searchValue == 'each') {
                        continue;
                    }
                    $ordersQueryBuilder = $ordersQueryBuilder->where('status', '=', $searchValue);
                    break;
            }
        }

        return $ordersQueryBuilder;
    }

    /**
     * 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->forModelName::query();

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