Files
SGF/app/Modules/Services/Http/ServiceDocumentsController.php
T
2026-11-11 09:00:17 +02:00

15 lines
2.1 KiB
PHP

<?php
namespace App\Modules\Services\Http;
use Illuminate\Http\Request;use App\Modules\Services\ServiceDocuments;
final class ServiceDocumentsController
{
private function response(\Closure $fn){try{return response()->json($fn())->header('Cache-Control','no-store, private');}catch(\Illuminate\Database\QueryException $e){if(!in_array($e->errorInfo[0]??null,['55P03','57014'],true))throw $e;return response()->json(['message'=>'Documento ocupado. Recupere a mesma confirmação.'],503)->header('Retry-After','5');}}
public function index(Request $r,ServiceDocuments $s){return $this->response(fn()=>$s->listing($r->user()->id,$r->query()));}
public function register(Request $r,\App\Modules\Services\BillingRegister $s){return $this->response(fn()=>$s->listing($r->user()->id,$r->query()));}
public function screen(Request $r,ServiceDocuments $s){$d=$r->validate(['kind'=>'sometimes|in:receipt,credit_note,invoice_receipt']);abort_if(array_diff(array_keys($r->query()),['kind']),422);$s->listing($r->user()->id,['per_page'=>25]);return response()->view('commercial.documents',['csrf'=>csrf_token(),'context'=>$r->session()->get('company_context.token'),'kind'=>$d['kind']??'receipt'])->header('Cache-Control','no-store');}
public function options(Request $r,ServiceDocuments $s){return $this->response(fn()=>$s->options($r->user()->id,$r->query()));}
public function preview(Request $r,ServiceDocuments $s){return $this->response(fn()=>$s->preview($r->user()->id,$r->all(),strtolower((string)$r->header('Idempotency-Key'))));}
public function confirm(Request $r,ServiceDocuments $s){$d=$r->validate(['preview'=>'required|string|max:131072']);abort_if(array_diff(array_keys($r->all()),['preview']),422);return $this->response(fn()=>$s->confirm($r->user()->id,$d['preview']));}
public function pdf(Request $r,ServiceDocuments $s,int $id){abort_if($r->query()!==[],422);$row=$s->pdf($r->user()->id,$id);return response(base64_decode($row->pdf_base64,true))->header('Content-Type','application/pdf')->header('Content-Disposition','inline; filename="TEST-SD-'.$row->id.'.pdf"')->header('Cache-Control','no-store, private');}
}