87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Invoice;
|
|
use App\Models\Option;
|
|
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
|
|
{
|
|
Option::updateOrCreate(['name' => 'payment_from'], ['value' => $from]);
|
|
Option::updateOrCreate(['name' => 'payment_end'], ['value' => $end]);
|
|
|
|
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)
|
|
{
|
|
//
|
|
}
|
|
}
|