Skip to content

Commit

Permalink
Invoice list view
Browse files Browse the repository at this point in the history
  • Loading branch information
lovett committed Dec 6, 2016
1 parent e8294d4 commit ad894fa
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 28 deletions.
34 changes: 34 additions & 0 deletions app/Helpers/CurrencyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Helpers;

/**
* Helper functions for displaying currency values.
*/
class CurrencyHelper
{

/**
* Format a monetary value with cents and currency symbol.
*
* @param float $value The value to format.
*
* @return string
*/
public static function withSymbol($value, $symbol='$')
{
return $symbol . money_format('%.2n', $value);
}

/**
* Format a monetary value as a whole number.
*
* @param float $value The value to format.
*
* @return string
*/
public static function wholeNumberWithSymbol($value, $symbol='$')
{
return $symbol . money_format('%.0n', $value);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function index(Request $request)
'searchFields' => array_keys(Invoice::$searchables),
];

return view('invocies.list', $viewVars);
return view('invoices.list', $viewVars);
}

/**
Expand Down
40 changes: 20 additions & 20 deletions app/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,23 @@ class Invoice extends Model
public static function listing(Builder $builder)
{
$builder = $builder->selectRaw(
'invoices.*,
count(distinct projects.id) as projectCount,
coalesce(sum(times.minutes), 0) as totalTime,
max(times.start) as latestTime'
'invoices.*'
);

$builder = $builder->leftJoin(
'projects',
function ($join) {
$join->on('invoices.project_id', '=', 'projects.id')
->where('projects.active', '=', 1)
->whereNull('projects.deleted_at');
}
);

$builder = $builder->orderBy('invoices.updated_at', 'DESC');
$builder = $builder->orderBy('invoices.sent', 'DESC');

$builder->with('project.client');
return $builder;
}

/**
* Time entries entries associated with the invoice.
*
* @return HasManyThrough
* @return HasMany
*/
public function times()
{
return $this->hasManyThrough('App\Time', 'App\Project');
return $this->hasMany('App\Time');
}

/**
Expand All @@ -121,13 +110,24 @@ public function users()
}

/**
* Projects associated with the invoice.
* Project associated with the invoice.
*
* @return HasMany
* @return HasOne
*/
public function projects()
public function project()
{
return $this->hasOne('App\Project');
return $this->belongsTo('App\Project');
}

/**
* Client associated with the invoice.
*
* @return HasOne
*/
public function client()
{
return $this->project()->with('client');
}


}
14 changes: 7 additions & 7 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,

'Debugbar' => Barryvdh\Debugbar\Facade::class,
'TimeHelper' => App\Helpers\TimeHelper::class,
'AddressHelper' => App\Helpers\AddressHelper::class,
'LinkHelper' => App\Helpers\LinkHelper::class,
'Form' => Collective\Html\FormFacade::class,
'HTML' => Collective\Html\HtmlFacade::class,

'Debugbar' => Barryvdh\Debugbar\Facade::class,
'TimeHelper' => App\Helpers\TimeHelper::class,
'AddressHelper' => App\Helpers\AddressHelper::class,
'LinkHelper' => App\Helpers\LinkHelper::class,
'CurrencyHelper' => App\Helpers\CurrencyHelper::class,
'Form' => Collective\Html\FormFacade::class,
'HTML' => Collective\Html\HtmlFacade::class,
],

];
56 changes: 56 additions & 0 deletions resources/views/invoices/list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@extends('layouts.master')


@section('page_main')
@include('partials.search', ['route' => 'invoice.index', 'search' => $search, 'fields' => $searchFields])
<table class="table">
<thead>
<tr>
<th>Invoice</th>
<th>Client</th>
<th>Project</th>
<th class="text-right">Amount</th>
</tr>
</thead>
<tbody>
@foreach ($invoices as $invoice)
<tr>
<td>
<a href="{{ route('invoice.show', ['record' => $invoice]) }}">
{{ $invoice->name }}
</a>
<div class="small">
{{ TimeHelper::dateFromRaw($invoice->start) }} to
{{ TimeHelper::dateFromRaw($invoice->end) }}
</div>
</td>
<td>
<a href="{{ route('client.show', ['record' => $invoice->project->client]) }}">
{{ $invoice->project->client->name }}
</a>
</td>
<td>
<a href="{{ route('project.show', ['record' => $invoice->project]) }}">
{{ $invoice->project->name }}
</a>
</td>
<td class="text-right">
{{ CurrencyHelper::withSymbol($invoice->amount) }}
</td>
</tr>
@endforeach
</tbody>
</table>
<nav>
{!! $invoices->render() !!}
</nav>
@endsection

@section('nav_primary')
{!! link_to_route('invoice.create', 'Add an invoice') !!}
@endsection

@section('page_scripts')
<script src="{{ asset('js/vue.min.js') }}"></script>
<script src="{{ asset('js/searchby.js') }}"></script>
@endsection

0 comments on commit ad894fa

Please sign in to comment.