Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
3.8 kB
1
Indexable
Never
<?php
namespace App\Http\Controllers;

use App\Models\Transport;
use App\Models\Action;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;


class TransportProcessController extends Controller
{
    public function selectAction(Request $request)
    {
        $action = $request->query('action');
        return view('transport_process.scan_qr_code', compact('action'));
    }

    public function scanQRCode($action)
    {
        return view('transport_process.scan_qr_code', compact('action'));
    }

    public function showTransportForm(Request $request)
{
    $qrCode = $request->input('qr_code');
    $actionType = $request->input('action');
    $nuomotojai = Action::distinct()->pluck('nuomotojas')->map(function($nuomotojas) {
        return ['label' => $nuomotojas, 'value' => $nuomotojas];
    });

    $transport = Transport::where('qr_code_id', $qrCode)->first();

    if (!$transport) {
        return redirect()->route('transport-process.select-action')->with('error', 'Transport not found.');
    }

    return view('transport_process.show_transport_form', compact('nuomotojai', 'transport', 'actionType'));
}

    public function processForm(Request $request)
    {
        $qrCode = $request->input('qr_code');
        $actionType = $request->input('action');
        $nuomotojas = $request->input('nuomotojas');

        $transport = Transport::where('qr_code_id', $qrCode)->first();

        if (!$transport) {
            return redirect()->route('transport-process.select-action')->with('error', 'Transport not found.');
        }

        $action = new Action();
        $action->action = $actionType;
        $action->transport_id = $transport->id;
        $action->user_id = auth()->id();
        $action->nuomotojas = $nuomotojas;
        $action->odometer = $request->input('odometer');

        if ($request->hasFile('front_image')) {
            $action->front_image = $request->file('front_image')->store('images', 'public');
        }
        if ($request->hasFile('back_image')) {
            $action->back_image = $request->file('back_image')->store('images', 'public');
        }
        if ($request->hasFile('left_image')) {
            $action->left_image = $request->file('left_image')->store('images', 'public');
        }
        if ($request->hasFile('right_image')) {
            $action->right_image = $request->file('right_image')->store('images', 'public');
        }
        if ($request->filled('damage_report')) {
            $action->damage_report = $request->input('damage_report');
        }
        if ($request->hasFile('damage_images')) {
            $damageImages = [];
            foreach ($request->file('damage_images') as $damageImage) {
                $damageImages[] = $damageImage->store('images', 'public');
            }
            $action->damage_images = json_encode($damageImages); // Convert array to JSON string
        }

        $action->save();

        if ($actionType === 'issue') {
            Session::flash('deposite_alert', 'Iš nuomotojo paimkite 150 Eur užstatą.');
            return redirect()->route('transport-process.confirmation', ['action' => $action->id]);
        }

        if ($actionType === 'accept' && !empty($action->damage_report)) {
            return redirect()->route('transport-process.deposit-warning', ['action' => $action->id]);
        }

        return redirect()->route('transport-process.confirmation', ['action' => $action->id]);
    }

    public function depositWarning(Action $action)
    {
        return view('transport_process.deposit_warning', compact('action'));
    }

    public function confirmation(Action $action)
    {
        return view('transport_process.confirmation', compact('action'));
    }
}
Leave a Comment