@extends('layouts.app')
@section('title','Depreciation Runs')
@section('crumbs','<b>Depreciation Runs</b>')
@section('content')
<div class="card"><div class="card-h"><h2>Monthly depreciation</h2>
@if(perm('finance.create'))
<form method="POST" action="{{ route('depreciation-runs.store') }}" class="sp no-print" data-confirm="Post depreciation for this period? This creates a GL journal.">@csrf
<input type="month" name="period" value="{{ $defaultPeriod }}" required>
<button class="btn btn-sm">▶ Run depreciation</button>
</form>
@endif
</div>
<div class="card-b">
@if($errors->any())<div class="flash err">✕ {{ $errors->first() }}</div>@endif
<h3 style="margin:0 0 8px">Charge preview — active assets</h3>
<div class="twrap"><table class="tbl">
<thead><tr><th>Asset</th><th>Name</th><th>Cost</th><th>Salvage</th><th>Acc. depn</th><th>NBV</th><th>Monthly charge</th></tr></thead>
<tbody>
@forelse($preview as $r)
<tr><td><a href="{{ crud_url('fixed-assets','show',$r['asset']->id) }}">{{ $r['asset']->asset_no }}</a></td>
<td>{{ $r['asset']->name }}</td><td>{{ money($r['asset']->purchase_cost) }}</td>
<td>{{ money($r['asset']->salvage_value) }}</td><td>{{ money($r['asset']->accumulated_depreciation) }}</td>
<td>{{ money($r['asset']->net_book_value) }}</td><td><b>{{ money($r['charge']) }}</b></td></tr>
@empty
<tr><td colspan="7" class="muted">No depreciable assets (all fully depreciated or disposed).</td></tr>
@endforelse
</tbody></table></div>
<p class="muted" style="margin-top:6px">Straight-line · Dr 5260 Depreciation / Cr 1510 Accumulated Depreciation · capped so NBV never falls below salvage.</p>
</div></div>
<div class="card"><div class="card-h"><h2>Run history</h2></div>
<div class="card-b"><div class="twrap"><table class="tbl">
<thead><tr><th>Period</th><th>Run date</th><th>Assets</th><th>Total</th><th>Journal</th><th>By</th></tr></thead>
<tbody>
@forelse($runs as $run)
<tr><td><a href="{{ route('depreciation-runs.show',$run->id) }}">{{ $run->period }}</a></td>
<td>{{ fdate($run->run_date) }}</td><td>{{ $run->assets_count }}</td><td>{{ money($run->total_amount) }}</td>
<td>@if($run->journal)<a href="{{ route('journals.show',$run->journal->id) }}">{{ $run->journal->journal_no }}</a>@else — @endif</td>
<td>{{ $run->runner->name ?? '—' }}</td></tr>
@empty
<tr><td colspan="6" class="muted">No runs posted yet.</td></tr>
@endforelse
</tbody></table></div>
@if($runs->hasPages())<div style="margin-top:10px">{{ $runs->links() }}</div>@endif
</div></div>
@endsection
