mirror of
https://github.com/anna-sara/lan_kiosk
synced 2025-10-26 13:07:14 +01:00
Backend: Customer
This commit is contained in:
parent
47e16e7ca2
commit
182bdedde1
3 changed files with 160 additions and 0 deletions
97
app/Http/Controllers/CustomerController.php
Normal file
97
app/Http/Controllers/CustomerController.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Purchase;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$customers = Customer::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([
|
||||
'name' => 'required',
|
||||
'guardian_name' => 'required',
|
||||
'give_leftover' => 'nullable',
|
||||
]);
|
||||
|
||||
Customer::create([
|
||||
'name' => $request->name,
|
||||
'guardian_name' => $request->guardian_name,
|
||||
'give_leftover' => $request->give_leftover,
|
||||
]);
|
||||
|
||||
return redirect(route('thankyou', absolute: false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$customer = Customer::with('purchases')->findOrFail($id);
|
||||
|
||||
return Inertia::render('Customer', ['customer' => $customer]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Customers $customers)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
31
app/Models/Customer.php
Normal file
31
app/Models/Customer.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\Puchase;
|
||||
|
||||
class Customer extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'guardian_name',
|
||||
'deposit',
|
||||
'amount left',
|
||||
'amount used',
|
||||
'give_leftover',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the purchases for the customer.
|
||||
*/
|
||||
public function purchases()
|
||||
{
|
||||
return $this->hasMany(Purchase::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('customers', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('guardian_name');
|
||||
$table->integer('deposit')->nullable();
|
||||
$table->integer('amount_left')->nullable();
|
||||
$table->boolean('give_leftover')->default(false);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('customers');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue