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'=>'Livro fiscal ocupado. Recupere a mesma confirmação; se a versão mudou, confira novamente.'],503)->header('Retry-After','5');}} public function screen(Request $r,FiscalAssessment $f){abort_if($r->query()!==[],422);$f->scope($r->user()->id);return response()->view('fiscal.workspace',['csrf'=>csrf_token(),'context'=>$r->session()->get('company_context.token')])->header('Cache-Control','no-store');} public function index(Request $r,FiscalAssessment $f){return $this->response(fn()=>$f->listing($r->user()->id,$r->query()));} public function preview(Request $r,FiscalAssessment $f){return $this->response(fn()=>$f->preview($r->user()->id,$r->all(),strtolower((string)$r->header('Idempotency-Key'))));} public function confirm(Request $r,FiscalAssessment $f){$d=$r->validate(['preview'=>'required|string|max:131072']);abort_if(array_diff(array_keys($r->all()),['preview']),422);return $this->response(fn()=>$f->confirm($r->user()->id,$d['preview']));} public function receipt(Request $r,FiscalAssessment $f,string $key){abort_if($r->query()!==[],422);return $this->response(fn()=>$f->receipt($r->user()->id,strtolower($key)));} public function export(Request $r,FiscalAssessment $f,int $id){abort_if($r->query()!==[],422);$r=$f->export($r->user()->id,$id);return response($r['bytes'])->header('Content-Type','text/csv; charset=UTF-8')->header('Content-Disposition','attachment; filename="mapa-interno-IVA-QA-v'.$r['version'].'.csv"')->header('X-Content-SHA256',$r['sha256'])->header('Cache-Control','no-store, private');} }