Skip to content

Commit 809360b

Browse files
committed
1 parent 610952d commit 809360b

File tree

5 files changed

+248
-180
lines changed

5 files changed

+248
-180
lines changed

app/Http/Controllers/HomeController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class HomeController extends Controller
2727
protected $invoiceKPIService;
2828

2929
public function __construct(
30-
OrderKPIService $orderKPIService,
31-
DeliveryKPIService $deliveryKPIService,
32-
QuoteKPIService $quoteKPIService,
33-
InvoiceKPIService $invoiceKPIService,
34-
){
35-
$this->orderKPIService = $orderKPIService;
36-
$this->deliveryKPIService = $deliveryKPIService;
37-
$this->quoteKPIService = $quoteKPIService;
38-
$this->invoiceKPIService = $invoiceKPIService;
39-
}
30+
OrderKPIService $orderKPIService,
31+
DeliveryKPIService $deliveryKPIService,
32+
QuoteKPIService $quoteKPIService,
33+
InvoiceKPIService $invoiceKPIService,
34+
){
35+
$this->orderKPIService = $orderKPIService;
36+
$this->deliveryKPIService = $deliveryKPIService;
37+
$this->quoteKPIService = $quoteKPIService;
38+
$this->invoiceKPIService = $invoiceKPIService;
39+
}
4040

4141
/**
4242
* @return \Illuminate\Contracts\View\View

app/Services/DeliveryKPIService.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Services;
44

55
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Cache;
67

78
class DeliveryKPIService
89
{
@@ -21,24 +22,30 @@ public function getDeliveriesDataRate()
2122

2223
public function getDeliveryMonthlyRecap($year)
2324
{
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+
});
3337
}
3438

3539
public function getDeliveryMonthlyProgress($month ,$year)
3640
{
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+
});
4350
}
4451
}

0 commit comments

Comments
 (0)