diff --git a/app/Http/Controllers/ClientPortal/StatementController.php b/app/Http/Controllers/ClientPortal/StatementController.php index d5d95aa885..3579376cc6 100644 --- a/app/Http/Controllers/ClientPortal/StatementController.php +++ b/app/Http/Controllers/ClientPortal/StatementController.php @@ -13,11 +13,47 @@ namespace App\Http\Controllers\ClientPortal; use App\Http\Controllers\Controller; +use App\Http\Requests\ClientPortal\Statements\ShowStatementRequest; +use Illuminate\Http\JsonResponse; +use Illuminate\View\View; +use Symfony\Component\HttpFoundation\StreamedResponse; class StatementController extends Controller { - public function index() + /** + * Show the statement in the client portal. + * + * @return View + */ + public function index(): View { return render('statement.index'); } + + /** + * Show the raw stream of statement PDF. + * + * @param ShowStatementRequest $request + * @return StreamedResponse|JsonResponse + */ + public function raw(ShowStatementRequest $request) + { + $pdf = $request->client()->service()->statement( + $request->only(['start_date', 'end_date', 'show_payments_table', 'show_aging_table']) + ); + + if ($pdf && $request->query('download')) { + return response()->streamDownload(function () use ($pdf) { + echo $pdf; + }, 'statement.pdf', ['Content-Type' => 'application/pdf']); + } + + if ($pdf) { + return response($pdf, 200)->withHeaders([ + 'Content-Type' => 'application/pdf', + ]); + } + + return response()->json(['message' => 'Something went wrong. Please check logs.']); + } } diff --git a/app/Http/Livewire/Statement.php b/app/Http/Livewire/Statement.php index 8459e40558..42f5a7527d 100644 --- a/app/Http/Livewire/Statement.php +++ b/app/Http/Livewire/Statement.php @@ -12,12 +12,38 @@ namespace App\Http\Livewire; +use Illuminate\View\View; use Livewire\Component; class Statement extends Component { - public function render() + public string $url; + + public array $options = [ + 'show_payments_table' => 0, + 'show_aging_table' => 0, + ]; + + public function mount(): void { + $this->options['start_date'] = now()->startOfYear()->format('Y-m-d'); + $this->options['end_date'] = now()->format('Y-m-d'); + } + + protected function getCurrentUrl(): string + { + return route('client.statement.raw', $this->options); + } + + public function download() + { + return redirect()->route('client.statement.raw', \array_merge($this->options, ['download' => 1])); + } + + public function render(): View + { + $this->url = route('client.statement.raw', $this->options); + return render('components.statement'); } } diff --git a/app/Http/Requests/ClientPortal/Statements/ShowStatementRequest.php b/app/Http/Requests/ClientPortal/Statements/ShowStatementRequest.php new file mode 100644 index 0000000000..c205aac909 --- /dev/null +++ b/app/Http/Requests/ClientPortal/Statements/ShowStatementRequest.php @@ -0,0 +1,49 @@ +merge([ + 'show_payments_table' => $this->has('show_payments_table') ? \boolval($this->show_payments_table) : false, + 'show_aging_table' => $this->has('show_aging_table') ? \boolval($this->show_aging_table) : false, + ]); + } + + public function client(): Client + { + return auth('contact')->user()->client; + } +}