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/shop.komma.nl/app/Orders/Kms/OrderSection.php
<?php


namespace App\Orders\Kms;

use App\Checkout\CheckoutService;
use App\Shipments\Shipment;
use App\Users\SiteUser;
use Illuminate\Support\Collection;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Link;
use Komma\KMS\Core\Attributes\Models\SelectOption;
use Komma\KMS\Core\Attributes\Select;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\View;
use Komma\KMS\Core\Sections\Section;
use Komma\KMS\Core\Attributes\Title;
use App\Orders\Models\Order;
use App\Users\Genders;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\MessageBag;
use Komma\KMS\Globalization\RegionInfo;
use Komma\KMS\Globalization\RegionInfoInterface;
use Komma\KMS\Helpers\KommaHelpers;

class OrderSection extends Section
{
    protected string $slug = "orders";

    /**
     * Define the attributes and tabs for this section.
     *
     * @param Model|Order|null $currentModel
     */
    public function defineAttributesAndTabs(Model $currentModel = null): void
    {
        /** @var SiteUser $customer */
        $customer = $currentModel->customer;

        /** @var Order $order */
        $order = $currentModel;

        $checkoutService = new CheckoutService();
        $orderVatRateTotals = $checkoutService->getVatRateTotalsFromOrder($order);

        /** @var $regular Collection */
        /** @var $hotlist Collection */
        [$regular, $hotlist] = RegionInfo::getSpecificCultures()->sortBy(function(RegionInfo $regionInfo) {
            return $regionInfo->getNativeName();
        })->partition(function(RegionInfo $regionInfo) {
            return (!in_array($regionInfo->getThreeLetterISORegionName(), config('languages.country_hotlist', []))); //Region infos that are in the hotlist will be put in the hotlist collection, the rest in the regular one.
        });

        $regionInfoOptions = $hotlist->merge($regular)->map(function(RegionInfo $regionInfo) {
            return (new SelectOption())->setContent($regionInfo->getNativeName())->setHtmlContent($regionInfo->getNativeName())->setValue($regionInfo->getThreeLetterISORegionName());
        })->toArray();

        //*****************************************************************************************\\
        //*** Determine and define gender dropdown data                                           ***\\
        //*****************************************************************************************\\
        $genderOptions = [];
        collect(Genders::getAsArray())->each(function ($gender) use (&$genderOptions) {
            $genderOptions[] = (new SelectOption())->setContent(__('auth.genders.'.$gender))->setHtmlContent(__('auth.genders.'.$gender))->setValue($gender);
        });


        //*****************************************************************************************\\
        //*** Determine and define culture dropdown data                                           ***\\
        //*****************************************************************************************\\
        $cultureOptions = [];
        RegionInfo::getSpecificCultures()->each(function (RegionInfo $cultureInfo) use (&$cultureOptions) {
            $cultureOptions[] = (new SelectOption())->setContent($cultureInfo->getNativeName())->setHtmlContent($cultureInfo->getNativeName())->setValue($cultureInfo->getName());
        });

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        $attributes = [];

        $order->load(
            'orderedProducts.product.translations',
            'orderedGroups.productGroup.translations',
            'orderedProductComposites.orderedGroups.productGroup.translations',
            'orderedProductComposites.productComposite.translations'
        );

        $orderOverviewData = [
            'order'         => $order,
            'vatRateTotals' => $orderVatRateTotals,
            'showTotal'     => true,
            'regionInfo'    => app(RegionInfoInterface::class),
        ];

        $transactionViewData = [
            'order'          => $order,
            'shopRegionInfo' => app(RegionInfoInterface::class),
        ];

        $this->tabs->makeTab()->addItem(
            (new Title())
                ->setLabelText(__('KMS::orders.status_name'))
        );

        if ($currentModel) {
            $this->tabs->getTab()->addItem(
                (new TextField())
                    ->setReadOnly(true)
                    ->setReference('updated_at')
                    ->setLabelText(__('KMS::global.updated_at'))
            );
        }

        $this->tabs->getTab()->addItems([
            (new TextField())
                ->setReadOnly(true)
                ->setReference('order_number')
                ->setLabelText(__('KMS::orders.order_number')),

            (new Select())
                ->setItems($this->getOrderStatusItems())
                ->setLabelText(__('KMS::orders.status_name'))
                ->setReference('status'),

            (new Seperator()),

            (new Title())
                ->setLabelText(__('KMS::orders.customer_info')),

            (new Link())
                ->setLabelText(__('KMS::orders.customer'))
                ->setLink(route('site_users.show', ['site_user' => $customer]))
                ->setLinkText($customer->first_name.' '.$customer->last_name_prefix.' '.$customer->last_name.' ('.$customer->email.')'),

            (new Seperator()),

            (new Title())
                ->setLabelText(__('KMS::customers.telephone')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('telephone')
                ->setLabelText(__('KMS::customers.telephone')),

            (new Seperator()),

            (new Title())
                ->setLabelText(__('KMS::orders.invoice_address')),

            (new Select())
                ->setReference('invoice_country_iso3')
                ->setLabelText(__('KMS::kms_users.culture'))
                ->setItems($regionInfoOptions),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('invoice_postal_code')
                ->setLabelText(__('KMS::customers.postal')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('invoice_city')
                ->setLabelText(__('KMS::customers.city')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('invoice_street')
                ->setLabelText(__('KMS::customers.street')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('invoice_house_number')
                ->setLabelText(__('KMS::customers.house_nr')),

            (new Seperator()),

            (new Title())
                ->setLabelText(__('KMS::orders.shipping_address')),

            (new Select())
                ->setReference('shipping_country_iso3')
                ->setLabelText(__('KMS::kms_users.culture'))
                ->setItems($regionInfoOptions),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('shipping_postal_code')
                ->setLabelText(__('KMS::customers.postal')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('shipping_city')
                ->setLabelText(__('KMS::customers.city')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('shipping_street')
                ->setLabelText(__('KMS::customers.street')),

            (new TextField())
                ->setReadOnly(false)
                ->setReference('shipping_house_number')
                ->setLabelText(__('KMS::customers.house_nr')),

            (new Seperator()),

            (new Title())
                ->setLabelText(__('KMS::transactions.transactions')),
            (new View())
                ->setViewName('KMS::partials.orders.transactions')
                ->setViewData($transactionViewData),

        ]);

//        if ($currentModel->status) {
//            $this->tabs->getTab()->addItems([
//                (new Seperator()),
//                (new Title())
//                    ->setLabelText(__('KMS::shipments.shipments')),
//                (new View())
//                    ->setViewName('KMS::partials.orders.shipments')
//                    ->setViewData($shipmentsData),
//            ]);
//        }

        $attributes = [];
        $attributes[] = (new Seperator());

        $attributes[] = (new Title())
            ->setLabelText(__('KMS::orders.order'));
        $attributes[] = (new View())
            ->setViewName('KMS::partials.orders.orderDetails')
            ->setViewData($orderOverviewData);

        $attributes[] = (new Seperator());

        $attributes[] = (new Title())
            ->setLabelText(__('KMS::shipments.shipments'));

        $shipments = $currentModel->shipments()->get();
        if($shipments->count() == 0) {
            $attributes[] = (new TextField)
                ->setReadOnly(true)
                ->setValue(__('KMS::orders.no_shipments'))
                ->setReference(__('KMS::orders.no_shipments'))
                ->setEntity(Attribute::ValueFromItself);
        } else if ($shipments->count() > 0) {
            foreach($shipments as $shipment) {
                $attributes[] = (new Link())
                    ->setLink(route('shipments.show', ['shipment' => $shipment]))
                    ->setLinkText($shipment->getDisplayName());
            }
        }

        $attributes[] = (new View())->setViewName('KMS::partials.orders.createShipmentButton');

        $attributes[] = (new Seperator());

        $attributes[] = (new Title())
            ->setLabelText(__('KMS::global.documents'));

        $attributes[] = (new Link())
            ->setLinkText(__('KMS::orders.invoice'))
            ->setLink(route('orders.invoice.view', ['order' => $order, 'vatRateTotals' => $orderVatRateTotals]))
            ->openInNewWindowOrTab();

        $attributes[] = (new Link())
            ->setLinkText(__('KMS::global.download').' '.__('KMS::orders.invoice'))
            ->setLink(route('orders.invoice.download',
                ['order' => $order, 'vatRateTotals' => $orderVatRateTotals]));

        $this->tabs->getTab()->addItems($attributes);


        if ($order->credit_invoice_number) {
            $this->tabs->getTab()->addItems([
                (new Link())
                    ->setLinkText(__('KMS::orders.credit_invoice'))
                    ->setLink(route('orders.credit_invoice.view',
                        ['order' => $order, 'vatRateTotals' => $orderVatRateTotals])),

                (new Link())
                    ->setLinkText(__('KMS::global.download').' '.__('KMS::orders.credit_invoice'))
                    ->setLink(route('orders.credit_invoice.download',
                        ['order' => $order, 'vatRateTotals' => $orderVatRateTotals])),
            ]);
        } else {
            $this->tabs->getTab()->addItem(
                (new Link())
                    ->setLinkText(__('KMS::global.create').' '.__('KMS::orders.credit_invoice'))
                    ->setLink(route('orders.credit_invoice.create',
                        ['order' => $order, 'vatRateTotals' => $orderVatRateTotals]))
            );
        }
    }

    public function getOrderStatusItems()
    {
        $items = [];
        foreach (__('KMS::orders.status') as $value => $translation) {
            $items[] = (new SelectOption())
                ->setValue($value)
                ->setContent($translation)
                ->setHtmlContent($translation);
        }

        return $items;
    }

    /**
     * For the index controller action
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function renderOrderIndex()
    {
        $saveRoute = $this->modelService->getSaveRoute($this->slug, $this->getModelId());

        $successes = (\Session::has('successes')) ? \Session::get('successes') : new MessageBag();

        return view('kms/shop/orders/index', [
            'siteSlug'                     => !$this->siteService->getCurrentSite()->exists ? null : $this->siteService->getCurrentSite()->slug,
            'section'                      => $this,
            'saveRoute'                    => $saveRoute,
            'successes'                    => $successes,
            'maxUploadSize'                => KommaHelpers::fileUploadMaxSize(),
            'maxPostSize'                  => KommaHelpers::maxPostSize(),
            'preventNavigationTranslation' => json_encode(__('kms/prevent-navigation')),
        ]);
    }

    /**
     * Makes a full width view. The sidebar is not needed
     *
     * @return \Illuminate\Contracts\View\View
     */
    protected function makeView(): \Illuminate\Contracts\View\View
    {
        $saveRoute = $this->modelService->getSaveRoute($this->slug, $this->getModelId());

        $successes = (\Session::has('successes')) ? \Session::get('successes') : new MessageBag();

        $shopRegionInfo = app(RegionInfoInterface::class);

        return view('kms/section.fullwidth', [
            'siteSlug'                     => !$this->siteService->getCurrentSite()->exists ? null : $this->siteService->getCurrentSite()->slug,
            'section'                      => $this,
            'saveRoute'                    => $saveRoute,
            'successes'                    => $successes,
            'maxUploadSize'                => KommaHelpers::fileUploadMaxSize(),
            'maxPostSize'                  => KommaHelpers::maxPostSize(),
            'preventNavigationTranslation' => json_encode(__('kms/prevent-navigation')),
        ]);
    }
}