<?php

namespace App\Listeners\PHPXPress;

use Illuminate\Support\Facades\Validator;
use Jackiedo\DotenvEditor\Facades\DotenvEditor;

class HwplusTickets extends HwplusTicketItems {

    public function handle($event) {
        throw new \Exception('should never be called');
    }


    public function creating($event) {

        $model = $event->model;
        $editor = DotenvEditor::load(storage_path('app/settings.env'));

        if(\App\Models\PHPXPress\HwplusTickets::with('_meta', '_client')
            ->whereHas('_meta', function($query) use ($model) {
                    $query->where('_doctype', $model->form->getKey());
                })
            ->whereHas('_client', function($query) use ($model) {
                    $query->where('ID', $model->_client->getKey());
                })
            ->where('date', $model->date)
            ->where('commission_number', $model->commission_number)
            ->exists()) throw new \Exception('Ein Ticket mit dieser Kommissionsnummer exisiert schon');

        switch($model->form->identifier) {
            case "shipping_order":
                $model->descriptor = "Lieferschein";
                $model->header = file_get_contents(storage_path('app/shipping-order-header.txt'));
                #$model->header = \Blade::render($model->header, $model->prepareBladeData());
                $model->footer = file_get_contents(storage_path('app/shipping-order-footer.txt'));
                #$model->footer = \Blade::render($model->footer, $model->prepareBladeData());
                if(!$model->fare_rate) $model->fare_rate = $model->_client->fare_rate;
                if(!$model->fare_rate_vat) $model->fare_rate_vat = \Runtime::make('hwplus_vat_rates')->where('flag_default', 1)->first()->rate;
                break;
            case "order":
                $model->descriptor = "Auftrag";
                $model->header = file_get_contents(storage_path('app/order-header.txt'));
                #$model->header = \Blade::render($model->header, $model->prepareBladeData());
                $model->footer = file_get_contents(storage_path('app/order-footer.txt'));
                #$model->footer = \Blade::render($model->footer, $model->prepareBladeData());
                if(!$model->fare_rate) $model->fare_rate = $model->_client->fare_rate;
                if(!$model->fare_rate_vat) $model->fare_rate_vat = \Runtime::make('hwplus_vat_rates')->where('flag_default', 1)->first()->rate;
                break;
            case "lot":
                $model->descriptor = "Materialbedarf";
                $model->header = file_get_contents(storage_path('app/lot-header.txt'));
                #$model->header = \Blade::render($model->header, $model->prepareBladeData());
                $model->footer = file_get_contents(storage_path('app/lot-footer.txt'));
                #$model->footer = \Blade::render($model->footer, $model->prepareBladeData());
                break;
            case "offer":
                $model->descriptor = "Angebot";
                $model->header = file_get_contents(storage_path('app/offer-header.txt'));
                #$model->header = \Blade::render($model->header, $model->prepareBladeData());
                $model->footer = file_get_contents(storage_path('app/offer-footer.txt'));
                #$model->footer = \Blade::render($model->footer, $model->prepareBladeData());
                if(!$model->fare_rate) $model->fare_rate = $model->_client->fare_rate;
                if(!$model->fare_rate_vat) $model->fare_rate_vat = \Runtime::make('hwplus_vat_rates')->where('flag_default', 1)->first()->rate;
                break;
            case "material_offer":
                $model->descriptor = "Materialangebot";
                $model->header = file_get_contents(storage_path('app/offer-header.txt'));
                #$model->header = \Blade::render($model->header, $model->prepareBladeData());
                $model->footer = file_get_contents(storage_path('app/offer-footer.txt'));
                #$model->footer = \Blade::render($model->footer, $model->prepareBladeData());
                if(!$model->fare_rate) $model->fare_rate = $model->_client->fare_rate;
                if(!$model->fare_rate_vat) $model->fare_rate_vat = \Runtime::make('hwplus_vat_rates')->where('flag_default', 1)->first()->rate;
                break;
            default: throw new \Exception('have a look');

            $this->calculateSum($model);
        }
    }

    public function updating($event) {

        $model = $event->model;
        $this->calculateSum($model);

    }

    public function delete($event) {

         if($event->model->job_number) throw new \Exception('Dieses Ticket wurde unterschrieben und kann nicht mehr gelöscht werden.');
    }
       
}
