Build the stuff for payments.
This commit is contained in:
82
app/Http/Controllers/Api/PaymentController.php
Normal file
82
app/Http/Controllers/Api/PaymentController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PaymentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function indexFilter($from = null, $end = null): JsonResponse
|
||||
{
|
||||
return response()->json(Payment::whereBetween('payment_date', [$from, $end])->with(['invoice'])->orderBy('payment_date', 'desc')->get());
|
||||
}
|
||||
|
||||
public function index(Invoice $invoice): JsonResponse
|
||||
{
|
||||
return response()->json($invoice->payments);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request, Invoice $invoice): JsonResponse
|
||||
{
|
||||
$paymentData = $request->validate([
|
||||
'invoice_id' => 'required|integer|exists:invoices,id',
|
||||
'paid_amount' => 'required|numeric',
|
||||
'payment_date' => 'required|date',
|
||||
'payment_method' => 'nullable|string',
|
||||
'status' => 'required|string|in:full,partial',
|
||||
]);
|
||||
|
||||
if ($paymentData['status'] == 'full') {
|
||||
$invoice->update(['status' => 'paid']);
|
||||
}
|
||||
|
||||
return response()->json($invoice->payments()->create($request->all()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Payment $payment)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Payment $payment): JsonResponse
|
||||
{
|
||||
$paymentData = $request->validate([
|
||||
'invoice_id' => 'required|integer|exists:invoices,id',
|
||||
'paid_amount' => 'required|numeric',
|
||||
'payment_date' => 'required|date',
|
||||
'payment_method' => 'nullable|string',
|
||||
'status' => 'required|string|in:full,partial',
|
||||
]);
|
||||
|
||||
$payment->update($paymentData);
|
||||
if ($payment->invoice->payments->where('status', 'full')->count() === 0) {
|
||||
$payment->invoice->update(['status' => 'sent']);
|
||||
}
|
||||
|
||||
return response()->json($payment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Payment $payment)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user