lan_kiosk/app/Http/Controllers/CustomerController.php
Anna-Sara Sélea df9f1cf17e Swish feature
2026-08-22 16:13:42 +02:00

234 lines
7.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use App\Models\CustomerGroup;
use Illuminate\Http\Request;
use App\Models\Purchase;
use Inertia\Inertia;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Storage;
use App\Models\Tableversion;
class CustomerController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$customers = Customer::where('is_in_group', 0)->orderBy('name', 'asc')->get();
return Inertia::render('Dashboard', ['customers' => $customers]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'lan_id' => ['required'],
'name' => 'required',
'guardian_name' => 'required',
'give_leftover' => 'nullable',
]);
$customer = Customer::create([
'lan_id' => $request->lan_id,
'name' => $request->name,
'guardian_name' => $request->guardian_name,
'give_leftover' => $request->give_leftover,
]);
return redirect('customer/' . $customer->id);
}
/**
* Load customers
*/
public function load()
{
$latestVersionParticipant = Tableversion::where('table', 'participants')->latest()->first();
$latestVersionVolunteer = Tableversion::where('table', 'volunteers')->latest()->first();
$client = new Client();
$responseVersions= $client->request(
'GET',
config('app.apilan_url') . "version",
[
'headers'=> [
'X-Api-Key' => config('app.apilan_key'),
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'cert' => config('app.apilan_clientcert_path')
//'cert' => Storage::disk('public')->path('lan.vbytes.se.pem')
],
);
$versions = json_decode((string) $responseVersions->getBody(), true);
if ($latestVersionParticipant === null ) {
Tableversion::create([
'table' => 'participants',
'version' => $versions['participants'] - 1,
]);
$latestVersionParticipant = Tableversion::where('table', 'participants')->latest()->first();
}
if ( $latestVersionVolunteer === null ) {
Tableversion::create([
'table' => 'volunteers',
'version' => $versions['volunteers'] - 1,
]);
$latestVersionVolunteer = Tableversion::where('table', 'volunteers')->latest()->first();
}
if($latestVersionParticipant->version < $versions['participants'] || $latestVersionVolunteer->version < $versions['volunteers'] ) {
$response = $client->request(
'GET',
config('app.apilan_url') . "data",
[
'headers'=> [
'X-Api-Key' => config('app.apilan_key'),
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'cert' => config('app.apilan_clientcert_path')
//'cert' => Storage::disk('public')->path('lan.vbytes.se.pem')
],
);
$response_data = json_decode((string) $response->getBody(), true);
foreach ($response_data['participants'] as $participant) {
Customer::updateOrCreate(
['lan_id' => $participant['lan_id']],
[
'lan_id' => $participant['lan_id'],
'name' => $participant['first_name'] . " " . $participant['surname'],
'guardian_name' => $participant['guardian_name'],
]
);
}
foreach ($response_data['volunteers'] as $volunteer) {
Customer::updateOrCreate(
['lan_id' => $volunteer['lan_id']],
[
'lan_id' => $volunteer['lan_id'],
'name' => $volunteer['first_name'] . " " . $volunteer['surname'],
'guardian_name' => $volunteer['first_name'] . " " . $volunteer['surname']
]
);
}
if($latestVersionParticipant->version < $versions['participants']) {
Tableversion::create([
'table' => 'participants',
'version' =>$latestVersionParticipant->version + 1,
]);
}
if($latestVersionVolunteer->version < $versions['volunteers']) {
Tableversion::create([
'table' => 'volunteers',
'version' =>$latestVersionVolunteer->version + 1,
]);
}
}
}
/**
* Display the specified resource.
*/
public function show($id)
{
$customer = Customer::with('purchases')->with('deposits')->findOrFail($id);
$groupmembers = Customer::where('is_in_group', 1)->where('customer_group_id', $customer->customer_group_id)->get();
return Inertia::render('Customer', [
'customer' => $customer,
'groupmembers' => $groupmembers,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$customer = Customer::findOrFail($id);
$customer->customer_group_id = null;
$customer->is_in_group = 0;
$customer->save();
}
/**
* Update the specified resource in storage.
*/
public function updateDeposit(Request $request, $id)
{
$request->validate([
'deposit' => 'required',
]);
$customer = Customer::findOrFail($id);
$customer->deposit = $customer->deposit + $request->deposit;
$customer->amount_left = $customer->amount_left + $request->deposit;
$customer->save();
return redirect('customer/' . $customer->id);
}
/**
* Update the specified resource in storage.
*/
public function updateComment(Request $request)
{
$request->validate([
'customer_id' => 'required',
'comment' => 'required',
]);
$customer = Customer::findOrFail($request->customer_id);
$customer->comment = $request->comment;
$customer->save();
return redirect('customer/' . $customer->id);
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$customer = Customer::findOrFail( $id );
$customer->delete();
return response()->json([
'success' => true, 'message' => 'Customer deleted successfully'
]);
}
}