<!-- $o = Cost::class -->
@extends('adminlte::layouts.app')

@section('htmlheader_title')
	Cost #{{ $o->id }}
@endsection

@section('contentheader_title')
	Cost #{{ $o->id }}
@endsection
@section('contentheader_description')

@endsection

<!-- $o = App\Models\Costs -->
@php($cost = 0)
@php($charge = 0)
@section('main-content')
	<div class="row">
		<div class="col-12">
			<div class="card card-dark">
				<div class="card-body">
					<table class="table table-striped" id="table">
						<thead>
						<tr>
							<th>Category</th>
							<th>Service</th>
							<th>Active</th>
							<th>From</th>
							<th>To</th>
							<th class="text-right">Base</th>
							<th class="text-right">Excess</th>
							<th class="text-right">Cost</th>
							<th class="text-right">Charge</th>
							<th class="text-right">Profit</th>
						</tr>
						</thead>

						<tbody>

						<!-- Broadband -->
						<tr>
							<td>Broadband</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>{{ $o->broadbands->min('start_at')->format('Y-m-d') }}</td>
							<td>{{ $o->broadbands->max('end_at')->format('Y-m-d') }}</td>
							<td class="text-right">${{ number_format($a=$o->broadbands->sum('base'),2) }}</td>
							<td class="text-right">${{ number_format($o->broadbands->sum('excess'),2) }}</td>
							<td class="text-right">${{ number_format($x=$o->broadbands->sum('cost'),2) }}</td>
							<td class="text-right">${{ number_format($b=$o->broadbands->sum('service.billing_monthly_price'),2) }}</td>
							<td class="text-right {{ $b-$a < 0 ? 'text-danger' : '' }}">${{ number_format($b-$a,2) }}</td>
						</tr>
						@php($cost += $x)
						@php($charge += $b)

						@foreach ($o->broadbands->groupBy('service_broadband_id') as $oo)
							<tr>
								<td>&nbsp;</td>
								<td>{{ $oo->first()->service->name }}</td>
								<td>{{ $oo->first()->service->active ? 'YES' : 'NO' }}</td>
								<td>{{ $oo->min('start_at')->format('Y-m-d') }}</td>
								<td>{{ $oo->max('end_at')->format('Y-m-d') }}</td>
								<td class="text-right">${{ number_format($a=$oo->sum('base'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('excess'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('cost'),2) }}</td>
								<td class="text-right">${{ number_format($b=$oo->first()->service->billing_monthly_price,2) }}</td>
								<td class="text-right {{ $b-$a < 0 ? 'text-danger' : '' }}">${{ number_format($b-$a,2) }}</td>
							</tr>
						@endforeach

						<!-- Phone -->
						<tr>
							<td>Phone</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>{{ $o->phones->min('start_at')->format('Y-m-d') }}</td>
							<td>{{ $o->phones->max('end_at')->format('Y-m-d') }}</td>
							<td class="text-right">${{ number_format($a=$o->phones->sum('base'),2) }}</td>
							<td class="text-right">${{ number_format($o->phones->sum('excess'),2) }}</td>
							<td class="text-right">${{ number_format($x=$o->phones->sum('cost'),2) }}</td>
							<td class="text-right">${{ number_format($b=$o->phones->sum('service.billing_monthly_price'),2) }}</td>
							<td class="text-right {{ $b-$a < 0 ? 'text-danger' : '' }}">${{ number_format($b-$a,2) }}</td>
						</tr>
						@php($cost += $x)
						@php($charge += $b)

						@foreach ($o->phones->groupBy('service_phone_id') as $oo)
							<tr>
								<td>&nbsp;</td>
								<td>{{ $oo->first()->service->name }}</td>
								<td>{{ $oo->first()->service->active ? 'YES' : 'NO' }}</td>
								<td>{{ $oo->min('start_at')->format('Y-m-d') }}</td>
								<td>{{ $oo->max('end_at')->format('Y-m-d') }}</td>
								<td class="text-right">${{ number_format($a=$oo->sum('base'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('excess'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('cost'),2) }}</td>
								<td class="text-right">${{ number_format($b=$oo->first()->service->billing_monthly_price,2) }}</td>
								<td class="text-right {{ $b-$a < 0 ? 'text-danger' : '' }}">${{ number_format($b-$a,2) }}</td>
							</tr>
						@endforeach

						<!-- Generic -->
						<tr>
							<td>Generic</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>{{ ($x=$o->generics)->count() ? $x->min('start_at')->format('Y-m-d') : '-' }}</td>
							<td>{{ $x->count() ? $x->max('end_at')->format('Y-m-d') : '-' }}</td>
							<td class="text-right">${{ number_format($a=$o->generics->sum('base'),2) }}</td>
							<td class="text-right">${{ number_format($o->generics->sum('excess'),2) }}</td>
							<td class="text-right">${{ number_format($x=$o->generics->sum('cost'),2) }}</td>
							<td class="text-right">$0.00</td>
							<td class="text-right {{ 0-$a < 0 ? 'text-danger' : '' }}">${{ number_format(0-$a,2) }}</td>
						</tr>

						@php($cost += $x)

						@foreach ($o->generics->groupBy('service_generic_id') as $oo)
							<tr>
								<td>&nbsp;</td>
								<td>{{ ($x=$oo->first()->service) ? $x->name : '-' }}</td>
								<td>{{ $x ? ($x->active ? 'YES' : 'NO') : '-' }}</td>
								<td>{{ $oo->min('start_at')->format('Y-m-d') }}</td>
								<td>{{ $oo->max('end_at')->format('Y-m-d') }}</td>
								<td class="text-right">${{ number_format($a=$oo->sum('base'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('excess'),2) }}</td>
								<td class="text-right">${{ number_format($oo->sum('cost'),2) }}</td>
								<td class="text-right">${{ number_format($b=$x ? $x->billing_monthly_price : 0,2) }}</td>
								<td class="text-right {{ $b-$a < 0 ? 'text-danger' : '' }}">${{ number_format($b-$a,2) }}</td>
							</tr>
						@endforeach

						</tbody>
						<tfoot>
						<tr>
							<th colspan="7">TOTAL</th>
							<th class="text-right">${{ number_format($cost,2) }}</th>
							<th class="text-right">${{ number_format($charge,2) }}</th>
							<td class="text-right {{ $charge-$cost < 0 ? 'text-danger' : '' }}">${{ number_format($charge-$cost,2) }}</td>
						</tr>
						</tfoot>
					</table>
				</div>
			</div>
		</div>
	</div>
@endsection

@section('page-scripts')
	@css(datatables,bootstrap4|rowgroup)
	@js(datatables,bootstrap4|rowgroup)

	<script>
		$(document).ready(function() {
			$('#table').DataTable({
				order: [[0,'asc'],[1,'desc']],
			});

			$('tbody').on('click','tr', function () {
				$(this).toggleClass('selected');
			});
		});
	</script>
@append