58 lines
3.3 KiB
PHP
58 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\PurchasingRole;
|
|
use App\Models\ExternalPayment;
|
|
use App\Models\Invoice;
|
|
use App\Models\Supplier;
|
|
use App\Models\SupplierBankDetail;
|
|
use App\Services\ExternalPaymentWorkflow;
|
|
use App\Services\RequisitionWorkflow;
|
|
use App\Services\SupplierBankWorkflow;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ExternalPaymentController extends Controller
|
|
{
|
|
public function index(Request $request, ExternalPaymentWorkflow $workflow): Response
|
|
{
|
|
abort_unless(in_array($request->user()->purchasing_role, [PurchasingRole::Admin, PurchasingRole::Finance]), 403);
|
|
$canManage = $request->user()->purchasing_role === PurchasingRole::Finance;
|
|
$payments = ExternalPayment::query()->latest('id')->paginate(15)->through(fn ($payment) => [...$payment->toArray(), 'external_reference' => $canManage ? $payment->external_reference : null, 'evidence_reference' => $canManage ? $payment->evidence_reference : null]);
|
|
if ($canManage && $payments->count() > 0) {
|
|
app(RequisitionWorkflow::class)->audit($request->user(), 'payment.viewed', null, null, ['payment_ids' => $payments->pluck('id')->all()]);
|
|
}
|
|
$invoices = Invoice::query()->where('status', 'validated')->latest('id')->get()->map(function (Invoice $invoice) use ($workflow) {
|
|
$supplier = Supplier::query()->findOrFail($invoice->supplier_id);
|
|
$bank = SupplierBankDetail::query()->where('supplier_id', $supplier->id)->latest('revision')->first();
|
|
|
|
return ['id' => $invoice->id, 'number' => $invoice->number, 'supplier_id' => $supplier->id, 'supplier_name' => $supplier->name, 'total_minor' => $invoice->total_minor, 'outstanding_minor' => $workflow->outstanding($invoice), 'bank_id' => $bank->id ?? 0, 'bank_revision' => $bank->revision ?? 0, 'bank_state' => app(SupplierBankWorkflow::class)->state($supplier)];
|
|
});
|
|
Inertia::encryptHistory();
|
|
$response = Inertia::render('payments/index', ['invoices' => $invoices, 'payments' => $payments, 'canManage' => $canManage])->toResponse($request);
|
|
$response->headers->set('Cache-Control', 'private, no-store, max-age=0');
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function store(Request $request, Invoice $invoice, ExternalPaymentWorkflow $workflow): RedirectResponse
|
|
{
|
|
abort_unless($request->user()->purchasing_role === PurchasingRole::Finance, 403);
|
|
$data = $request->validate(['supplier_bank_detail_id' => 'required|integer|min:1', 'amount' => ['required', 'regex:/^\d{1,12}(\.\d{1,2})?$/'], 'paid_on' => 'required|date_format:Y-m-d', 'external_reference' => 'required|string|max:150', 'evidence_reference' => 'required|string|max:1000']);
|
|
$workflow->register($request->user(), $invoice, $data);
|
|
|
|
return to_route('payments.index');
|
|
}
|
|
|
|
public function review(Request $request, ExternalPayment $payment, ExternalPaymentWorkflow $workflow): RedirectResponse
|
|
{
|
|
$data = $request->validate(['decision' => 'required|in:confirmed,rejected', 'reason' => 'required|string|max:2000', 'independently_verified' => 'accepted']);
|
|
$workflow->review($request->user(), $payment, $data['decision'], $data['reason']);
|
|
|
|
return back();
|
|
}
|
|
}
|