3
3
namespace App \Services ;
4
4
5
5
use Illuminate \Support \Facades \DB ;
6
+ use Illuminate \Support \Facades \Cache ;
6
7
7
8
class DeliveryKPIService
8
9
{
@@ -21,24 +22,30 @@ public function getDeliveriesDataRate()
21
22
22
23
public function getDeliveryMonthlyRecap ($ year )
23
24
{
24
- return DB ::table ('delivery_lines ' )
25
- ->join ('order_lines ' , 'delivery_lines.order_line_id ' , '= ' , 'order_lines.id ' )
26
- ->selectRaw ('
27
- MONTH(delivery_lines.created_at) AS month,
28
- SUM((order_lines.selling_price * delivery_lines.qty)-(order_lines.selling_price * delivery_lines.qty)*(order_lines.discount/100)) AS orderSum
29
- ' )
30
- ->whereYear ('delivery_lines.created_at ' , $ year )
31
- ->groupByRaw ('MONTH(delivery_lines.created_at) ' )
32
- ->get ();
25
+ $ cacheKey = 'delivery_monthly_recap_ ' . now ()->year ;
26
+ return Cache::remember ($ cacheKey , now ()->addHours (1 ), function () use ($ year ) {
27
+ return DB ::table ('delivery_lines ' )
28
+ ->join ('order_lines ' , 'delivery_lines.order_line_id ' , '= ' , 'order_lines.id ' )
29
+ ->selectRaw ('
30
+ MONTH(delivery_lines.created_at) AS month,
31
+ SUM((order_lines.selling_price * delivery_lines.qty)-(order_lines.selling_price * delivery_lines.qty)*(order_lines.discount/100)) AS orderSum
32
+ ' )
33
+ ->whereYear ('delivery_lines.created_at ' , $ year )
34
+ ->groupByRaw ('MONTH(delivery_lines.created_at) ' )
35
+ ->get ();
36
+ });
33
37
}
34
38
35
39
public function getDeliveryMonthlyProgress ($ month ,$ year )
36
40
{
37
- return DB ::table ('delivery_lines ' )
38
- ->join ('order_lines ' , 'delivery_lines.order_line_id ' , '= ' , 'order_lines.id ' )
39
- ->selectRaw ('FLOOR(SUM((order_lines.selling_price * delivery_lines.qty)-(order_lines.selling_price * delivery_lines.qty)*(order_lines.discount/100))) AS orderSum ' )
40
- ->whereYear ('delivery_lines.created_at ' , '= ' , $ year )
41
- ->whereMonth ('delivery_lines.created_at ' , $ month )
42
- ->first () ?? (object ) ['orderSum ' => 0 ];
41
+ $ cacheKey = 'delivery_monthly_progress_ ' . now ()->year ;
42
+ return Cache::remember ($ cacheKey , now ()->addHours (1 ), function () use ($ month , $ year ) {
43
+ return DB ::table ('delivery_lines ' )
44
+ ->join ('order_lines ' , 'delivery_lines.order_line_id ' , '= ' , 'order_lines.id ' )
45
+ ->selectRaw ('FLOOR(SUM((order_lines.selling_price * delivery_lines.qty)-(order_lines.selling_price * delivery_lines.qty)*(order_lines.discount/100))) AS orderSum ' )
46
+ ->whereYear ('delivery_lines.created_at ' , '= ' , $ year )
47
+ ->whereMonth ('delivery_lines.created_at ' , $ month )
48
+ ->first () ?? (object ) ['orderSum ' => 0 ];
49
+ });
43
50
}
44
51
}
0 commit comments