Build the stuff for payments.
This commit is contained in:
@@ -19,6 +19,11 @@ class InvoiceController extends Controller
|
||||
return response()->json(Invoice::whereBetween('created_at', [$from, $end])->with('address')->orderBy('created_at', 'desc')->get());
|
||||
}
|
||||
|
||||
public function open(): JsonResponse
|
||||
{
|
||||
return response()->json(Invoice::where('status', '=', 'sent')->orderBy('created_at', 'desc')->with(['customer', 'address', 'payments'])->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
|
||||
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)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
35
app/Http/Controllers/PaymentController.php
Normal file
35
app/Http/Controllers/PaymentController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class PaymentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
return view('payment.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('payment.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Payment $payment): View
|
||||
{
|
||||
return view('payment.edit', ['payment' => $payment->load('invoice')]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user