|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Storgman - Student Organizations Management |
|
5
|
|
|
* Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This file is part of Storgman. |
|
8
|
|
|
* |
|
9
|
|
|
* Storgman is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by |
|
11
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Storgman is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with Storgman. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Storgman |
|
23
|
|
|
* @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]> |
|
24
|
|
|
* @license https://github.com/angelov/storgman/blob/master/LICENSE |
|
25
|
|
|
* @author Dejan Angelov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Angelov\Storgman\Membership\Repositories; |
|
29
|
|
|
|
|
30
|
|
|
use Angelov\Storgman\Core\Repositories\AbstractEloquentRepository; |
|
31
|
|
|
use Angelov\Storgman\Core\DateTime; |
|
32
|
|
|
use Angelov\Storgman\Membership\Fee; |
|
33
|
|
|
use Angelov\Storgman\Membership\Reports\ExpectedFeesPerMonthReport; |
|
34
|
|
|
use Angelov\Storgman\Membership\Reports\PaidFeesPerMonthReport; |
|
35
|
|
|
use DB; |
|
36
|
|
|
|
|
37
|
|
|
class EloquentFeesRepository extends AbstractEloquentRepository implements FeesRepositoryInterface |
|
38
|
|
|
{ |
|
39
|
|
|
public function __construct(Fee $entity) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct($entity); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function store(Fee $fee) |
|
45
|
|
|
{ |
|
46
|
|
|
$fee->save(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getSoonToExpire($count = 10) |
|
50
|
|
|
{ |
|
51
|
|
|
$now = DateTime::nowAsDateString(); |
|
52
|
|
|
$fees = $this->entity |
|
53
|
|
|
->where('to_date', '>', $now) |
|
54
|
|
|
->orderBy('to_date') |
|
55
|
|
|
->take($count) |
|
56
|
|
|
->get() |
|
57
|
|
|
->all(); |
|
58
|
|
|
|
|
59
|
|
|
return $fees; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
public function calculateExpectedFeesPerMonth(DateTime $from, DateTime $to) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$to = $to->modify('last day of this month'); |
|
65
|
|
|
|
|
66
|
|
|
$report = new ExpectedFeesPerMonthReport($from, $to); |
|
67
|
|
|
|
|
68
|
|
|
$res = DB::select( |
|
69
|
|
|
' |
|
70
|
|
|
SELECT concat(YEAR, \'-\', lpad(cast(MONTH AS CHAR(2)), 2, \'0\')) AS month, |
|
71
|
|
|
count(*) AS count |
|
72
|
|
|
FROM |
|
73
|
|
|
(SELECT extract(MONTH |
|
74
|
|
|
FROM to_date) AS MONTH, |
|
75
|
|
|
extract(YEAR |
|
76
|
|
|
FROM to_date) AS YEAR |
|
77
|
|
|
FROM |
|
78
|
|
|
(SELECT to_date |
|
79
|
|
|
FROM fees |
|
80
|
|
|
WHERE to_date BETWEEN ? AND ?) tbl1) tbl2 |
|
81
|
|
|
GROUP BY concat(MONTH, YEAR), MONTH, YEAR |
|
82
|
|
|
ORDER BY YEAR, MONTH |
|
83
|
|
|
', [$from->toDateString(), $to->toDateString()] |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
/** @todo Similar code is duplicated across the project */ |
|
87
|
|
|
foreach ($res as &$crnt) { |
|
88
|
|
|
$report->addMonth($crnt->month, (int) $crnt->count); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $report; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* /\ |
|
96
|
|
|
* @todo These two queries can be combined |
|
97
|
|
|
* \/ |
|
98
|
|
|
*/ |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
public function calculatePaidFeesPerMonth(DateTime $from, DateTime $to) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$report = new PaidFeesPerMonthReport($from, $to); |
|
103
|
|
|
|
|
104
|
|
|
$res = DB::select( |
|
105
|
|
|
' |
|
106
|
|
|
SELECT concat(YEAR, \'-\', lpad(cast(MONTH AS CHAR(2)), 2, \'0\')) AS month, |
|
107
|
|
|
count(*) AS count |
|
108
|
|
|
FROM |
|
109
|
|
|
(SELECT extract(MONTH |
|
110
|
|
|
FROM from_date) AS MONTH, |
|
111
|
|
|
extract(YEAR |
|
112
|
|
|
FROM from_date) AS YEAR |
|
113
|
|
|
FROM |
|
114
|
|
|
(SELECT from_date |
|
115
|
|
|
FROM fees |
|
116
|
|
|
WHERE from_date BETWEEN ? AND ?) tbl1) tbl2 |
|
117
|
|
|
GROUP BY concat(MONTH, YEAR), MONTH, YEAR |
|
118
|
|
|
ORDER BY YEAR, MONTH |
|
119
|
|
|
|
|
120
|
|
|
', [$from->toDateString(), $to->toDateString()] |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($res as &$crnt) { |
|
124
|
|
|
$report->addMonth($crnt->month, (int) $crnt->count); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $report; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.