<?php
namespace App\Http\Controllers\Api\V1\Admin;use App\Http\Controllers\Controller;use App\Http\Resources\SettlementBatchResource;use App\Http\Resources\MoneyResource;use App\Http\Requests\Admin\SettlementRequest;use App\Models\Merchant;use App\Models\SettlementBatch;use App\Services\AuditService;use App\Services\SettlementService;use Carbon\Carbon;use Illuminate\Http\Request;
class SettlementController extends Controller {public function positions(){return ['data'=>Merchant::query()->get()->map(fn($m)=>['merchant_id'=>$m->id,'code'=>$m->code,'name'=>$m->name,'position'=>new MoneyResource($m->float_balance_minor,$m->currency_code)])];}public function store(Request $r,SettlementService $s){$d=$r->validate(['merchant_id'=>['required','exists:merchants,id'],'period_from'=>['required','date'],'period_to'=>['required','date','after:period_from']]);$b=$s->generate(Merchant::findOrFail($d['merchant_id']),Carbon::parse($d['period_from']),Carbon::parse($d['period_to']));return (new SettlementBatchResource($b))->response()->setStatusCode(201);}public function show(SettlementBatch $batch){return new SettlementBatchResource($batch->load('merchant','transfers'));}public function confirm(SettlementBatch $batch,SettlementService $s,AuditService $a){$old=$batch->toArray();$batch=$s->confirm($batch);$a->record('settlement.confirmed',$batch,$old,$batch->toArray());return new SettlementBatchResource($batch);}public function settle(SettlementRequest $r,SettlementBatch $batch,SettlementService $s,AuditService $a){$old=$batch->toArray();$batch=$s->settle($batch,$r->user(),$r->validated());$a->record('settlement.settled',$batch,$old,$batch->toArray());return new SettlementBatchResource($batch);}}
