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/Orders/Kms/OrderSection.php
<?php
/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\Komma\Shop\Orders\Kms;

//The new object oriented attributes
use App\Helpers\KommaHelpers;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Globalization\RegionInfoInterface;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Link;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\Seperator;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Attributes\View;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\NoLanguageTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Users\Genders;
use App\Komma\Users\Models\SiteUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;

class OrderSection extends Section
{
    protected $title = 'Orders';

    protected $subtitle = 'All product orders';

    protected $slug = 'orders';

    protected $hasRoutes = false;

    public $showSave = 'all';

    public $showDelete = [1];

    public $showCreate = '';

    /**
     * OrderSection constructor.
     * @param string $slug
     */
//    function __construct(Kms $kms, PageRepository $repository)
    public function __construct(string $slug)
    {
        $this->title = ucfirst(__('shop/orders.orders'));
        $this->subtitle = __('shop/orders.all_orders');

        $sectionTabDirector = new NoLanguageTabs();
        parent::__construct($sectionTabDirector, $slug);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     * This is the place where you need to setup your sections appearance. Just make sure you build an array of attributes
     * and put each attribute in a AbstractSectionTabItem with a SectionTabGroups constant to link them to a tab.
     *
     * @see PageRepository::saveModel()
     * @param Model $currentModel
     * @return Collection A collection of SectionTabItems
     */
    protected function generateAttributes(Model $currentModel = null): Collection
    {
        /** @var SiteUser $customer */
        $customer = $currentModel->customer;

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\
        $emailValidationSet = (new ValidationSet())
            ->setRules([
                'required',
                'email',
            ])
            ->setMessages([
                'required' => __('validation.required'),
                'email' => __('kms/kms_users.enterValidEmailAddress'),
            ]);

        //*****************************************************************************************\\
        //*** 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 = [];

        /** @var Order $order */
        $order = $currentModel;
        $order->load(
            'orderedProducts.product.translations',
            'orderedGroups.productGroup.translations',
            'orderedProductComposites.orderedGroups.productGroup.translations',
            'orderedProductComposites.productComposite.translations'
        );

        $shipmentsData = [
            'order' => $order,
            'shipments' => $order->shipments()->with('shipmentGroup')->get(),
        ];

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

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

        //Build the general attributes and put them in the attributes array
        $attributes[] = (new Title(__('shop/orders.status_name')));

        $attributes[] = (new TextField(__('shop/orders.order_number')))
            ->setReadOnly(true)
            ->mapValueFrom(Attribute::ValueFromModel, 'order_number');

        $attributes[] = (new Select())
            ->setItems($this->getOrderStatusItems())
            ->setLabelText(__('shop/orders.status_name'))
            ->mapValueFrom(Attribute::ValueFromModel, 'status');

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

        $attributes[] = (new Title(__('shop/orders.customer_info')));

        $attributes[] = (new Link(__('shop/orders.customer')))->setLink(route('site_users.show', ['site_user' => $customer]))->setLinkText($customer->first_name.' '.$customer->last_name.' ('.$customer->email.')');

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

        $attributes[] = (new Title(__('shop/orders.invoice_address')));

        $attributes[] = (new TextField(__('kms/customers.postal')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_postal_code');

        $attributes[] = (new TextField(__('kms/customers.city')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_city');

        $attributes[] = (new TextField(__('kms/customers.street')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_street');

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

        $attributes[] = (new Title(__('shop/orders.shipping_address')));

        $attributes[] = (new TextField(__('kms/customers.postal')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_postal_code');

        $attributes[] = (new TextField(__('kms/customers.city')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_city');

        $attributes[] = (new TextField(__('kms/customers.street')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_street');

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

        $attributes[] = (new Title(__('shop/transactions.transactions')));
        $attributes[] = (new View('shop.partials.orders.transactions'))->setViewData($transactionViewData);

        if ($currentModel->status) {
            $attributes[] = (new Seperator());
            $attributes[] = (new Title(__('shop/shipments.shipments')));
            $attributes[] = (new View('shop.partials.orders.shipments'))->setViewData($shipmentsData);
        }

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

        $attributes[] = (new Title(__('shop/orders.order')));
        $attributes[] = (new View('shop.partials.orders.orderDetails'))->setViewData($orderOverviewData);

        $attributes[] = (new Seperator());
        $attributes[] = (new Title(__('kms/global.documents')));
        if ($order) {
            $attributes[] = (new Link(''))->setLink(route('orders.invoice.view', ['order' => $order]))->setLinkText(__('shop/orders.invoice'))->openInNewWindowOrTab();
            $attributes[] = (new Link(''))->setLink(route('orders.invoice.download', ['order' => $order]))->setLinkText(__('kms/global.download').' '.__('shop/orders.invoice'));
            if ($order->credit_invoice_number) {
                $attributes[] = (new Link(''))->setLink(route('orders.credit_invoice.view', ['order' => $order]))->setLinkText(__('shop/orders.credit_invoice'));
                $attributes[] = (new Link(''))->setLink(route('orders.credit_invoice.download', ['order' => $order]))->setLinkText(__('kms/global.download').' '.__('shop/orders.credit_invoice'));
            } else {
                $attributes[] = (new Link(''))->setLink(route('orders.credit_invoice.create', ['order' => $order]))->setLinkText(__('kms/global.create').' '.__('shop/orders.credit_invoice'));
            }
        }

        //Return all attributes as a collection
        return collect($attributes);
    }

    public function getOrderStatusItems()
    {
        $items = [];
        foreach (__('shop/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->routeService->getSaveRoute($this->getSlug(), $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->routeService->getSaveRoute($this->getSlug(), $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')),
        ]);
    }
}