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

namespace App\KommaApp\Orders\Kms;

//The new object oriented attributes
use App\KommaApp\Customers\Kms\CustomerService;
use App\KommaApp\Kms\Core\Attributes\AutocompleteInput;
use App\KommaApp\Kms\Core\Attributes\PayByLink;
use App\KommaApp\Kms\Core\Attributes\Select;
use App\KommaApp\Kms\Core\Sections\NoLanguageTabsDirector;
use App\KommaApp\Kms\Core\Sections\Section;
use App\KommaApp\Orders\Models\Order;
use App\KommaApp\Products\Kms\ProductService;
use App\KommaApp\Routes\RouteService;
use App\KommaApp\Sites\Kms\SiteService;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\OnOff;
use App\KommaApp\Kms\Core\Attributes\Seperator;
use App\KommaApp\Kms\Core\Attributes\TextField;
use App\KommaApp\Kms\Core\Attributes\Title;
use App\KommaApp\Kms\Core\Kms;
use App\KommaApp\Kms\Core\Sections\SectionTabGroups;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Kms\Core\Sections\SectionTabsBuilder;
use App\KommaApp\Kms\Core\ValidationSet;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class OrderSection extends Section
{
    protected $title = "Facturen";
    protected $subTitle = "Overzicht";
    protected $slug = "orders";

    public $showSave = 'all';
    public $showDelete = 'all';
    public $showCreate = 'all';

    private $siteService;
    private $customerService;
    private $productService;
    public $deleteButtonText = '';

    /**
     * PageSection constructor.
     * @param Kms $kms
     * @param OrderService $sectionService
     * @param RouteService $routeService
     */
//    function __construct(Kms $kms, PageRepository $repository)
    function __construct(OrderService $sectionService, RouteService $routeService, SiteService $siteService, CustomerService $customerService, ProductService $productService)
    {
        $sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct

        $this->siteService = $siteService;
        $this->customerService = $customerService;
        $this->productService = $productService;
        $this->deleteButtonText = __('kms/orders.createCreditOrder');

        parent::__construct($sectionService, $routeService, $sectionTabDirector);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\KommaApp\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 bool $forCreate
     * @return Collection A collection of SectionTabItems
     */
    protected function generateAttributes($forCreate = false): Collection
    {
//        \Log::info("PageSection::Generating attributes");
        $model = $this->getModel();

        $locked = $model->status == Order::ORDER_STATUS_REFUNDED || $model->status == Order::ORDER_STATUS_CREDIT || $model->status == Order::ORDER_STATUS_DELETED;

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\
        $requiredValidationSet = (new ValidationSet())
            ->setRules('required')
            ->setMessages(['required' => __('validation.required')]);

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        //Build the general attributes and put them in the attributes array


        $attributes = [];

        $attributes[] = new Title(__('kms/global.settings'));

        $statuses = $this->getSectionService()->getOrderStatuses();
        $attributes[] = (new Select())
            ->setLabelText(__('kms/orders.status'))
            ->setItems($statuses)
            ->setReadOnly($locked)
            ->mapValueFrom(Attribute::ValueFromModel, 'status');

        if($forCreate == false) {
            $attributes[] = (new TextField(__('kms/orders.id')))
                ->setPlaceholderText(__('kms/orders.enterOrderId'))
                ->setReadOnly(true)
                ->mapValueFrom(Attribute::ValueFromModel, 'order_id');

            $attributes[] = (new OnOff())
                ->setLabelText('Betaald')
                ->setReadOnly(true)
                ->mapValueFrom(Attribute::ValueFromModel, 'payed');

            switch($model->status) {
                case Order::ORDER_STATUS_CREDIT:
                    $attributes[] = (new TextField(__('kms/orders.original_invoice')))
                        ->setReadOnly(true)
                        ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'relatedOrder|order_id');
                    break;
                case Order::ORDER_STATUS_REFUNDED:
                    $attributes[] = (new TextField(__('kms/orders.credit_invoice')))
                        ->setReadOnly(true)
                        ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'relatedOrder|order_id');
            }

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

            $attributes[] = new Title(__('kms/orders.propertyInfo'));

            $attributes[] = (new TextField(__('kms/orders.houseForSaleStreet')))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromItself, 'houseForSaleStreet');

            $attributes[] = (new TextField(__('kms/orders.houseForSaleNumber')))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromItself, 'houseForSaleNumber');

            $attributes[] = (new TextField(__('kms/orders.houseNumberAddition')))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromItself, 'houseForSaleNumberAddition');

            $attributes[] = (new TextField(__('kms/orders.houseForSaleZip')))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromItself, 'houseForSaleZip');

            $attributes[] = (new TextField(__('kms/orders.houseForSaleLocation')))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromItself, 'houseForSaleLocation');
        }

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

        $attributes[] = new Title(__('kms/orders.customerInfo'));

        $clients = $this->customerService->getCustomersForSelect();


        if($forCreate == false) {
            $attributes[] = (new Select())
                ->setItems($clients)
                ->setReadOnly(true)
                ->setLabelText(__('kms/orders.client'))
                ->setExcludeCurrentModelItem(true)
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromModel, 'customer_id');
            $attributes[] = (new TextField(__('kms/orders.customerPhone')))
                ->setReadOnly(true)
                ->mapValueFrom(Attribute::ValueFromItself, 'customerPhone');

            $attributes[] = (new TextField(__('kms/orders.customerEmail')))
                ->setReadOnly(true)
                ->mapValueFrom(Attribute::ValueFromItself, 'customerEmail');
        } else {
            $attributes[] = (new AutocompleteInput(__('kms/orders.client')))
                ->setMaxItemsToSelect(1)
                ->setItems($clients)
                ->setLabelText(__('kms/orders.client'))
//            ->setReadOnly(true)
                ->mapValueFrom(Attribute::ValueFromModel,'customer_id');
        }

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

        $attributes[] = new Title(__('kms/orders.servicesInfo'));

        $productOptionsModels = $this->productService->getProductsForSelect();
        $attributes[] = $productSelector = (new AutocompleteInput())
        ->setItems($productOptionsModels)
        ->setLabelText(__('kms/orders.services'))
        ->setReadOnly($locked)
        ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'products|id');

        //enable autosave or not by setting an autosave url
        if ($model && $forCreate == false) {
            $productSelector->setAutoSaveUrl(route('orders.autosave.products', ['order' => $model]));
            $productSelector->setExplanation(__('kms/orders.auto_save_explanation') . '<br/>'. __('kms/orders.auto_save_explanation2') );
        }

        if($forCreate == false) {
            //Only show the payment link generator when there is a model (save, update routes)
            if ($model) {
                $attributes[] = (new PayByLink(__('icepay.paybymail.paymentlink')))
                    ->setGenerateUrl(route('icepay.pbm.generate', ['order' => $model->id]))
                    ->setExplanation(__('kms/orders.paybymail_autosave_explanation')) // Second explanation rule is in the pay by link blade
                    ->setLinkedAttribute($productSelector) //Disables the product selector when the PayByLink has a link
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'orderPayments|url');
            }

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

            $attributes[] = new Title(__('kms/orders.invoiceInfo'));

            $categories = $this->customerService->getGenderSelect();
            $attributes[] = (new Select())
                ->setItems($categories)
                ->setLabelText(__('kms/orders.gender'))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromModel, 'gender');

            $attributes[] =  (new TextField(__('kms/orders.lastName')))
                ->setPlaceholderText(__('kms/orders.enterLastName'))
                ->setValidationSet((new ValidationSet())->setRules('required')->setMessages(['required' => 'Factuur achternaam is verplicht']))
                ->setReadOnly($locked)
                ->mapValueFrom(Attribute::ValueFromModel, 'last_name');

                $attributes[] = (new TextField(__('kms/orders.zip')))
                    ->setPlaceholderText(__('kms/orders.enterZip'))
                    ->setValidationSet((new ValidationSet())->setRules('required')->setMessages(['required' => 'Factuur postcode is verplicht']))
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModel, 'zip');

                $attributes[] = (new TextField(__('kms/orders.houseNumber')))
                    ->setPlaceholderText(__('kms/orders.enterHouseNumber'))
                    ->setValidationSet((new ValidationSet())->setRules('required')->setMessages(['required' => 'Factuur huisnummer is verplicht']))
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModel, 'house_number');

                $attributes[] = (new TextField(__('kms/orders.houseNumberAddition')))
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModel, 'house_number_addition');

                $attributes[] = (new TextField(__('kms/orders.street')))
                    ->setPlaceholderText(__('kms/orders.enterStreet'))
                    ->setValidationSet((new ValidationSet())->setRules('required')->setMessages(['required' => 'Factuur straatnaam is verplicht']))
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModel, 'street');

                $attributes[] = (new TextField(__('kms/orders.city')))
                    ->setPlaceholderText(__('kms/orders.enterCity'))
                    ->setValidationSet((new ValidationSet())->setRules('required')->setMessages(['required' => 'Factuur woonplaats is verplicht']))
                    ->setReadOnly($locked)
                    ->mapValueFrom(Attribute::ValueFromModel, 'city');
        }

        //****************************************************************************************************************************************\\
        //*** Put the all attributes in a SectionTabItem so we can track for which tab they are. And then put SectionTabItems in a Collection  ***\\
        //****************************************************************************************************************************************\\
        $tabItems = new Collection();
        foreach($attributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));

        return $tabItems;
    }

    public function generateAttributesAndAddThemToTabs($renderForCreate = false)
    {
        $this->sectionTabItems = $this->generateAttributes($renderForCreate);
        $this->addAttributesToTabs();
    }

    private function enableOrDisableEntityButtons()
    {
        $enabledEntityButtonStatusses = [
            null,
            Order::ORDER_STATUS_NEW,
            Order::ORDER_STATUS_PROCESSING,
            Order::ORDER_STATUS_AWAITING_PAYMENT,
            Order::ORDER_STATUS_ASPERION_CANCELED,
            Order::ORDER_STATUS_CLOSED,
        ];

        $this->showDelete = $this->showSave = ($this->getModel() && in_array($this->getModel()->status, $enabledEntityButtonStatusses, true) ? 'all' : '');
    }

    public function render($renderForCreate = false)
    {
        $this->enableOrDisableEntityButtons();

        //        \Log::debug("KmsSection:300 render");
        if($this->showEntity) $this->generateAttributesAndAddThemToTabs($renderForCreate);

        if(!$this->getModel()) return $this->makeView(); //Return a view since we can't fill it with data because the model is not set.

        $this->getSectionService()->fillAttributesWithData($this->sectionTabItems, $this->getModel());

        return $this->makeView();
    }

    public function destroy(Model $model)
    {
        parent::destroy($model); // TODO: Change the autogenerated stub
    }


    /**
     * This method will stop the load entities of the kmsSiteSection
     *
     * @return array
     *
     */
    public function loadEntities(){
        return [];
    }
}