Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit d8d510e

Browse files
author
Mario Basic
committed
Swap now uses database cache. Preferred currency is now used everywhere.
1 parent 7915ae6 commit d8d510e

File tree

7 files changed

+165
-59
lines changed

7 files changed

+165
-59
lines changed

app/Service.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Occurrence;
88
use Illuminate\Database\Eloquent\Model;
99
use Collective\Html\Eloquent\FormAccessible;
10+
use Illuminate\Database\Eloquent\Collection;
1011

1112
class Service extends Model
1213
{
@@ -66,4 +67,34 @@ public function getFormattedCostAttribute()
6667
{
6768
return number_format($this->cost / 100, 2, ',', '.') . ' ' . strtoupper($this->currency);
6869
}
70+
71+
public function getSum(Collection $services = null)
72+
{
73+
if(!($services instanceof Collection)) {
74+
$services = $this->all();
75+
}
76+
$preferredCurrency = strtoupper(auth()->user()->preferred_currency);
77+
78+
$sum = 0;
79+
foreach($services as $service) {
80+
$currentCurrency = strtoupper($service->currency);
81+
$exchange_rate = \Swap::quote("{$currentCurrency}/{$preferredCurrency}")
82+
->getValue();
83+
$sum+= ($service->cost / 100) * $exchange_rate;
84+
}
85+
//$sum = ceil($sum);
86+
87+
return number_format($sum, 2, ',', '.') . ' ' . $preferredCurrency;
88+
}
89+
90+
public function getSumForMonth(int $month, $onlyActive = false)
91+
{
92+
$services = $this->where('month', $month);
93+
if($onlyActive) {
94+
$services->where('active', 1);
95+
}
96+
$services = $services->get();
97+
98+
return $this->getSum($services);
99+
}
69100
}

config/cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@
7878
|
7979
*/
8080

81-
'prefix' => 'laravel',
81+
'prefix' => 'kyle',
8282

8383
];

config/swap.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Swap.
5+
*
6+
* (c) Florian Voutzinos <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return [
13+
14+
/*
15+
|--------------------------------------------------------------------------
16+
| Http Adapter
17+
|--------------------------------------------------------------------------
18+
|
19+
| This option specifies a service id to use as http adapter
20+
| (defaults to FileGetContentsHttpAdapter).
21+
|
22+
*/
23+
'http_adapter' => null,
24+
25+
/*
26+
|--------------------------------------------------------------------------
27+
| Providers
28+
|--------------------------------------------------------------------------
29+
|
30+
| This option specifies the providers to use with their name as key and
31+
| their config as value. The providers will be wrapped in a ChainProvider
32+
| in the order they appear in this array.
33+
|
34+
| Here is the config spec for each provider:
35+
|
36+
| * "yahoo_finance", "google_finance", "european_central_bank", "webservicex"
37+
| "national_bank_of_romania" can be enabled with "true" as value.
38+
|
39+
| * 'open_exchange_rates' => [
40+
| 'app_id' => 'secret', // Your app id
41+
| 'enterprise' => true, // True if your AppId is an enterprise one
42+
| ]
43+
|
44+
| * 'xignite' => [
45+
| 'token' => 'secret', // The API token
46+
| ]
47+
|
48+
*/
49+
50+
'providers' => [
51+
'yahoo_finance' => true,
52+
],
53+
54+
/*
55+
|--------------------------------------------------------------------------
56+
| Cache
57+
|--------------------------------------------------------------------------
58+
|
59+
| This option specifies which cache to use to store rate values and its ttl.
60+
| Currently only Illuminate cache is supported:
61+
|
62+
| 'cache' => [
63+
| 'type' => 'illuminate',
64+
| 'store' => 'apc', // Name of the cache store
65+
| 'ttl' => 60 // Ttl in minutes (defaults to 0)
66+
| ],
67+
*/
68+
'cache' => [
69+
'type' => 'illuminate',
70+
'store' => 'database', // Name of the cache store
71+
'ttl' => 60 // Ttl in minutes (defaults to 0)
72+
],
73+
74+
];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateCacheTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('cache', function (Blueprint $table) {
16+
$table->string('key')->unique();
17+
$table->text('value');
18+
$table->integer('expiration');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::drop('cache');
30+
}
31+
}

resources/views/home.blade.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@inject('Service', 'App\Service')
2+
13
@extends('layouts.app')
24

35
@section('meta_title', 'Overview')
@@ -16,19 +18,12 @@
1618
@if(count($occurrences) > 0)
1719
<div class="row">
1820
<div class="col-md-12">
19-
<?php
20-
$usd_sum = 0;
21-
foreach($occurrences as $occurrence) {
22-
$usd_sum+= ($occurrence->service->cost / 100) * $occurrence->service->exchange_rate;
23-
}
24-
$usd_sum = ceil($usd_sum);
25-
?>
26-
<h2>
21+
<h3>
2722
This month
2823
<small class="pull-right" style="margin-top: 15px;">
29-
TOTAL {{ number_format($usd_sum, 2, ',', '.') }} USD
24+
TOTAL {{ $Service->getSumForMonth(date('n'), true) }}
3025
</small>
31-
</h2>
26+
</h3>
3227
<div class="table-responsive">
3328
<table class="table">
3429
<tr>
@@ -73,19 +68,12 @@
7368
@if(count($upcomingOccurrences) > 0)
7469
<div class="row">
7570
<div class="col-md-12">
76-
<?php
77-
$usd_sum = 0;
78-
foreach($upcomingOccurrences as $occurrence) {
79-
$usd_sum+= ($occurrence->service->cost / 100) * $occurrence->service->exchange_rate;
80-
}
81-
$usd_sum = ceil($usd_sum);
82-
?>
83-
<h2>
71+
<h3>
8472
Upcoming month
8573
<small class="pull-right" style="margin-top: 15px;">
86-
TOTAL {{ number_format($usd_sum, 2, ',', '.') }} USD
74+
TOTAL {{ $Service->getSumForMonth(date('n') + 1, true) }}
8775
</small>
88-
</h2>
76+
</h3>
8977
<div class="table-responsive">
9078
<table class="table">
9179
<tr>

resources/views/report.blade.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@inject('Service', 'App\Service')
2+
13
@extends('layouts.app')
24

35
@section('meta_title', 'Report')
@@ -11,6 +13,12 @@
1113
'header' => 'Report',
1214
'subtext' => 'Yearly report of services, clients and categories.'
1315
])
16+
<dl>
17+
<dt>Date</dt>
18+
<dd>{{ date('d.m.Y') }}</dd>
19+
<dt>User</dt>
20+
<dd>{{ auth()->user()->name }}</dd>
21+
</dl>
1422
</div>
1523
</div>
1624

@@ -29,7 +37,7 @@
2937
$total += ($service->cost / 100) * $service->exchange_rate;
3038
}
3139
?>
32-
<span class="badge">{{ number_format($total, 2, ',', '.') }} USD</span>
40+
<span class="badge">{{ $Service->getSum($services) }}</span>
3341
TOTAL INCOME
3442
</li>
3543
<li class="list-group-item">
@@ -51,7 +59,7 @@
5159
</div>
5260

5361
<div class="row">
54-
<div class="col-md-6">
62+
<div class="col-md-6 col-xs-6">
5563
<div class="panel panel-default">
5664
<div class="panel-heading">
5765
<h3 class="panel-title">Categories</h3>
@@ -68,21 +76,15 @@
6876
<tr>
6977
<td>{{ $category->name }}</td>
7078
<td>{{ $category->services->count() }}</td>
71-
<?php
72-
$total = 0;
73-
foreach($category->services as $service) {
74-
$total += ($service->cost / 100) * $service->exchange_rate;
75-
}
76-
?>
77-
<td class="text-right">{{ number_format($total, 2, ',', '.') }} USD</td>
79+
<td class="text-right">{{ $Service->getSum($category->services) }}</td>
7880
</tr>
7981
@endforeach
8082
</table>
8183
</div>
8284
</div>
8385
</div>
8486
</div>
85-
<div class="col-md-6">
87+
<div class="col-md-6 col-xs-6">
8688
<div class="panel panel-default">
8789
<div class="panel-heading">
8890
<h3 class="panel-title">Clients</h3>
@@ -99,13 +101,7 @@
99101
<tr>
100102
<td>{{ $client->name }}</td>
101103
<td>{{ $client->services->count() }}</td>
102-
<?php
103-
$total = 0;
104-
foreach($client->services as $service) {
105-
$total += ($service->cost / 100) * $service->exchange_rate;
106-
}
107-
?>
108-
<td class="text-right">{{ number_format($total, 2, ',', '.') }} USD</td>
104+
<td class="text-right">{{ $Service->getSum($client->services) }}</td>
109105
</tr>
110106
@endforeach
111107
</table>
@@ -120,25 +116,17 @@
120116

121117
<div class="panel panel-default">
122118
<div class="panel-heading">
123-
<h3 class="panel-title">Services</h3>
119+
<h3 class="panel-title">Services by month</h3>
124120
</div>
125121
<div class="panel-body">
126122
@for($i = 1; $i <= 12; $i++)
127123
@if($services->where('month', $i)->count() > 0)
128124

129-
<?php
130-
$usd_sum = 0;
131-
foreach($services->where('month', $i) as $service) {
132-
$usd_sum+= ($service->cost / 100) * $service->exchange_rate;
133-
}
134-
$usd_sum = ceil($usd_sum);
135-
?>
136-
137125
<h3>
138126
{{ date('F', mktime(0, 0, 0, $i)) }}
139127
<small>{{ $i }}</small>
140128
<small class="pull-right" style="margin-top: 15px;">
141-
TOTAL {{ number_format($usd_sum, 2, ',', '.') }} USD
129+
TOTAL {{ $Service->getSumForMonth($i, true) }}
142130
</small>
143131
</h3>
144132

resources/views/services/index.blade.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@inject('Service', 'App\Service')
2+
13
@extends('layouts.app')
24

35
@section('meta_title', 'Services - Index')
@@ -22,21 +24,13 @@
2224
@for($i = 1; $i <= 12; $i++)
2325
@if($services->where('month', $i)->count() > 0)
2426

25-
<?php
26-
$usd_sum = 0;
27-
foreach($services->where('month', $i) as $service) {
28-
$usd_sum+= ($service->cost / 100) * $service->exchange_rate;
29-
}
30-
$usd_sum = ceil($usd_sum);
31-
?>
32-
33-
<h2>
27+
<h3>
3428
{{ date('F', mktime(0, 0, 0, $i)) }}
3529
<small>{{ $i }}</small>
3630
<small class="pull-right" style="margin-top: 15px;">
37-
TOTAL {{ number_format($usd_sum, 2, ',', '.') }} USD
31+
TOTAL {{ $Service->getSumForMonth($i) }}
3832
</small>
39-
</h2>
33+
</h3>
4034

4135
<div class="table-responsive">
4236
<table class="table">

0 commit comments

Comments
 (0)