2
2
3
3
namespace CodeFin \Repositories \Traits ;
4
4
5
+ use Carbon \Carbon ;
5
6
use CodeFin \Events \BillStoredEvent ;
7
+ use CodeFin \Serializer \BillSerializer ;
6
8
7
9
trait BillRepositoryTrait
8
10
{
@@ -49,4 +51,63 @@ public function update(array $attributes, $id)
49
51
return $ this ->parserResult ($ model );
50
52
}
51
53
54
+ public function paginate ($ limit = null , $ columns = ['* ' ], $ method = "paginate " )
55
+ {
56
+ $ skipPresenter = $ this ->skipPresenter ;
57
+ $ this ->skipPresenter ();
58
+ $ collection = parent ::paginate ($ limit ,$ columns ,$ method );
59
+ $ this ->skipPresenter ($ skipPresenter );
60
+ return $ this ->parserResult (new BillSerializer ($ collection ,$ this ->formatBillsData ()));
61
+ }
62
+
63
+ public function getTotalFromPeriod (Carbon $ dateStart , Carbon $ dateEnd )
64
+ {
65
+ $ result = $ this ->getQueryTotal ()
66
+ ->whereBetween ('date_due ' ,[$ dateStart ->format ('Y-m-d ' ),$ dateEnd ->format ('Y-m-d ' )])
67
+ ->get ();
68
+ return [
69
+ 'total ' => (float )$ result ->first ()->total
70
+ ];
71
+ }
72
+
73
+ protected function getTotalByDone ($ done )
74
+ {
75
+ $ result = $ this ->getQueryTotalByDone ($ done )->get ();
76
+ return (float )$ result ->first ()->total ;
77
+ }
78
+
79
+ protected function getQueryTotal ()
80
+ {
81
+ $ this ->resetModel ();
82
+ $ this ->applyCriteria ();
83
+ return $ this ->model ->selectRaw ('SUM(value) as total ' );
84
+ }
85
+
86
+ protected function getQueryTotalByDone ($ done )
87
+ {
88
+ return $ this ->getQueryTotal ()
89
+ ->where ('done ' ,'= ' ,$ done );
90
+ }
91
+
92
+ protected function getTotalExpired ()
93
+ {
94
+ $ result = $ this ->getQueryTotalByDone (0 )
95
+ ->where ('date_due ' ,'< ' ,(new Carbon ())->format ('Y-m-d ' ))
96
+ ->get ();
97
+ return (float )$ result ->first ()->total ;
98
+ }
99
+
100
+ protected function formatBillsData ()
101
+ {
102
+ $ totalPaid = $ this ->getTotalByDone (1 );
103
+ $ totalToPay = $ this ->getTotalByDone (0 );
104
+ $ totalExpired = $ this ->getTotalExpired ();
105
+
106
+ return [
107
+ 'total_paid ' => $ totalPaid ,
108
+ 'total_to_pay ' => $ totalToPay ,
109
+ 'total_expired ' => $ totalExpired ,
110
+ ];
111
+ }
112
+
52
113
}
0 commit comments