Files
compras/app/Http/Controllers/SgfInvoiceDispatchController.php
T
nala 976271f948
tests / ci (push) Canceled after 0s
commit melhorias1
2026-11-10 12:58:19 +02:00

48 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Services\SgfInvoiceDispatches;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
final class SgfInvoiceDispatchController extends Controller
{
private function actor(Request $r,SgfInvoiceDispatches $service):User
{ $user=$r->user('web');abort_unless($user instanceof User,403);return $service->actor($user); }
public function index(Request $r,SgfInvoiceDispatches $service)
{
$user=$this->actor($r,$service);$d=$r->validate(['page'=>'sometimes|integer|min:1|max:100000','phase'=>'nullable|in:draft,previewing,previewed,sending,uncertain,received_pending,blocked,review_required,not_sent','connection'=>'nullable|string|max:64','mapping'=>'nullable|string|max:64']);
abort_if(array_diff(array_keys($r->query()),array_keys($d)),422);
$rows=DB::table('sgf_invoice_dispatches')->where('tenant_id',$user->tenant_id)->when($d['phase']??null,fn($q,$v)=>$q->where('phase',$v))
->orderByDesc('id')->paginate(25,['id','reference','number','phase','version','updated_at'])->withQueryString();
$mapping=null;foreach($service->mappings($user)as$m)if($m['connection']===($d['connection']??null)&&$m['id']===($d['mapping']??null))$mapping=$m;
return response()->view('sgf-invoices.index',['rows'=>$rows,'options'=>$service->mappings($user),'selection'=>$mapping])->header('Cache-Control','private, no-store');
}
public function show(int $id,Request $r,SgfInvoiceDispatches $service)
{
$user=$this->actor($r,$service);$d=$r->validate(['events_page'=>'sometimes|integer|min:1|max:100000']);abort_if(array_diff(array_keys($r->query()),array_keys($d)),422);
$row=$service->row($user,$id);$events=DB::table('sgf_invoice_dispatch_events')->where('tenant_id',$user->tenant_id)->where('dispatch_id',$id)->orderByDesc('id')
->paginate(25,['actor_id','action','reason','before_state','after_state','created_at'],'events_page')->withQueryString();
return response()->view('sgf-invoices.show',['row'=>$service->presentation($user,$row),'events'=>$events,'options'=>$service->mappings($user),'transportEnabled'=>config('sgf_invoice.enabled')===true])->header('Cache-Control','private, no-store');
}
public function create(Request $r,SgfInvoiceDispatches $service)
{
$user=$this->actor($r,$service);$input=$r->except('_token');$id=$service->create($user,$input);
return redirect('/facturas/envio-sgf/'.$id);
}
public function revise(int $id,Request $r,SgfInvoiceDispatches $service)
{
$user=$this->actor($r,$service);$d=$r->validate(['version'=>'required|integer|min:1']);
$service->revise($user,$id,(int)$d['version'],$r->except('_token','version'));
return redirect('/facturas/envio-sgf/'.$id);
}
public function dispatch(int $id,Request $r,SgfInvoiceDispatches $service)
{
$user=$this->actor($r,$service);$d=$r->validate(['version'=>'required|integer|min:1','action'=>'required|in:preview,confirm,recover','reason'=>'required|string|min:10|max:2000','approval_hash'=>'nullable|string|size:64']);
abort_if(array_diff(array_keys($r->except('_token')),array_keys($d)),422);
$service->dispatch($user,$id,(int)$d['version'],$d['action'],$d['reason'],$d['approval_hash']??null);
return redirect('/facturas/envio-sgf/'.$id);
}
}