Build the stuff for payments.

This commit is contained in:
2025-01-16 15:34:10 +01:00
parent f120559e3b
commit dcedbc71c4
17 changed files with 670 additions and 31 deletions

View File

@@ -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.
*/

View 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)
{
//
}
}

View 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')]);
}
}

View File

@@ -38,8 +38,6 @@ class Invoice extends Model
/**
* Get the invoice state as translated string.
*
* @return string
*/
public function getLocalizedStateAttribute(): string
{
@@ -56,35 +54,57 @@ class Invoice extends Model
/**
* Get the created_at attribute in local time format.
*
* @return string
*/
public function getCreatedAttribute(): string
{
return $this->created_at->format('d.m.Y');
}
/**
* Get the user, that has created the invoice.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the invoice's customer.
*/
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
/**
* Get the invoice's address.
*/
public function address(): BelongsTo
{
return $this->belongsTo(Address::class)->withTrashed();
}
/**
* Get the invoice's delivery address.
*/
public function delivery(): BelongsTo
{
return $this->belongsTo(Address::class)->withTrashed();
}
/**
* Get the items (invoice positions) of this invoice.
*/
public function items(): HasMany
{
return $this->hasMany(Invoiceitem::class);
}
/**
* Get the payments for the invoice
*/
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
}

View File

@@ -3,9 +3,58 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Date;
class Payment extends Model
{
use SoftDeletes;
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'invoice_id',
'paid_amount',
'status',
'payment_method',
'payment_date',
];
/**
* The attributes that are appended with attribute getters.
*
* @var string[]
*/
protected $appends = [
'date',
'localized_state'
];
/**
* Get the payment state as translated string.
*/
public function getLocalizedStateAttribute(): string
{
return __('invoice.Payment state ' . $this->status);
}
/**
* Get the payment date as localized string.
*/
public function getDateAttribute(): string
{
return Date::createFromFormat('Y-m-d', $this->payment_date)->format('d.m.Y');
}
/**
* Get the invoice the payment belongs to.
*/
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}
}