<?php

namespace App\Listeners\PHPXPress;

#use Session;

use Illuminate\Support\Facades\Validator;

#use Session;

class HwplusInvoices {

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

    
    public function creating($event) {
    
        $model = $event->model;
        
        if($model->header);
        elseif($model->_ticket->getKey()) {
            $model->header = file_get_contents(storage_path('app/invoice-header.txt'));
        }
        else {
            $model->header = file_get_contents(storage_path('app/bill-header.txt'));
        }
        
        if("Stornorechnung"== $model->descriptor) {
            $model->header = file_get_contents(storage_path('app/reverse-invoice-header.txt'));
        }
        elseif("Korrekturrechnung"== $model->descriptor) {
            $model->header .= "\n\n" . file_get_contents(storage_path('app/adjusted-invoice-header.txt'));
        }

    
    }
    
    public function created($event) {
           
        $form = \Runtime::make('forms')->findOrFail('5b895c53f265570cdcfa59b26e633be4e179a38e');
        
        if('standing_order' == $event->model->form->identifier);
        else foreach(\App\Models\PHPXPress\HwplusInvoiceNotesPatterns::where('flag_default_selected', 1)->orderBy('order')->get() AS $pattern) {
        
            $note =new \App\Models\PHPXPress\HwplusInvoiceNotes();
            $note->form = $form;
            $note->_notes_at_hwplus_invoices = $event->model->getKey();
            $note->_notes_at_hwplus_invoices_order = $pattern->order;
            $note->descriptor = $pattern->descriptor;
            $note->text = $pattern->text;
            $note->sort = $pattern->order;
            $note->save();
        
        }
    
    }

    public function delete($event) {
    
        if($event->model->invoice_number) throw new \Exception('Diese Rechnung wurde geschlossen und kann nicht mehr gelöscht werden.');
    
    }
}
 
