|
1
|
|
|
<?php namespace App\Modules\V1\Reporting\Repositories; |
|
2
|
|
|
|
|
3
|
|
|
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository; |
|
4
|
|
|
|
|
5
|
|
|
class ReportRepository extends AbstractRepository |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Return the model full namespace. |
|
9
|
|
|
* |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected function getModel() |
|
13
|
|
|
{ |
|
14
|
|
|
return 'App\Modules\V1\Reporting\Report'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Render the given report db view based on the given |
|
19
|
|
|
* condition. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $reportName |
|
22
|
|
|
* @param array $conditions array of conditions |
|
23
|
|
|
* @param integer $perPage |
|
24
|
|
|
* @param array $relations |
|
25
|
|
|
* @return object |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getReport($reportName, $conditions = false, $perPage = 0, $relations = []) |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Fetch the report from db. |
|
31
|
|
|
*/ |
|
32
|
|
|
$reportConditions = $this->constructConditions(['report_name' => $reportName]); |
|
33
|
|
|
$report = call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($reportConditions['conditionString'], $reportConditions['conditionValues'])->first(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Check report existance and permission. |
|
37
|
|
|
*/ |
|
38
|
|
|
if ( ! $report) |
|
39
|
|
|
{ |
|
40
|
|
|
\ErrorHandler::notFound('report'); |
|
41
|
|
|
} |
|
42
|
|
|
else if ( ! \Core::users()->can($report->view_name, 'reports')) |
|
43
|
|
|
{ |
|
44
|
|
|
\ErrorHandler::noPermissions(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Fetch data from the report based on the given conditions. |
|
49
|
|
|
*/ |
|
50
|
|
|
$report = \DB::table($report->view_name); |
|
51
|
|
|
unset($conditions['page']); |
|
52
|
|
|
if (count($conditions)) |
|
53
|
|
|
{ |
|
54
|
|
|
$conditions = $this->constructConditions($conditions); |
|
|
|
|
|
|
55
|
|
|
$report->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
56
|
|
|
} |
|
57
|
|
|
/** |
|
58
|
|
|
* Paginate or all data. |
|
59
|
|
|
*/ |
|
60
|
|
|
if ($perPage) |
|
61
|
|
|
{ |
|
62
|
|
|
return $report->paginate($perPage); |
|
63
|
|
|
} |
|
64
|
|
|
else |
|
65
|
|
|
{ |
|
66
|
|
|
return $report->get(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|