Deposit model, controller, migration

This commit is contained in:
Anna-Sara Sélea 2025-05-05 18:31:38 +02:00
parent 1da5fccff8
commit 188303be93
3 changed files with 137 additions and 0 deletions

View file

@ -0,0 +1,90 @@
<?php
namespace App\Http\Controllers;
use App\Models\Deposit;
use App\Models\Customer;
use Illuminate\Http\Request;
class DepositController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$deposits = Deposit::get();
return $deposits->toJson();
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, $id)
{
$request->validate([
'deposit' => 'required',
]);
Deposit::create([
'customer_id' => $id,
'amount' => $request->deposit,
]);
$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);
}
/**
* Display the specified resource.
*/
public function show(Purchase $purchase)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Purchase $purchase)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Purchase $purchase)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$deposit = Deposit::findOrFail( $id );
$customer = Customer::where('id', $deposit->customer_id);
$customer->amount_left = $customer->amount_left + $deposit->amount;
$customer->amount_used = $customer->amount_used - $deposit->amount;
$customer->save();
$deposit->delete();
return response()->json([
'success' => true, 'message' => 'Deposit deleted successfully'
]);
}
}

18
app/Models/Deposit.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Deposit extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'customer_id',
'amount',
];
}

View file

@ -0,0 +1,29 @@
<?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('deposits', function (Blueprint $table) {
$table->id();
$table->integer('customer_id')->nullable();
$table->integer('amount')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('deposits');
}
};