97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admins\Dashboard;
|
|
|
|
use App\Helpers\ResponseFormatter;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Pengeluaran;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ChartPengeluaranController extends Controller
|
|
{
|
|
|
|
public function getChartPengeluaran()
|
|
{
|
|
$allPengeluaran = Pengeluaran::sum('nominal');
|
|
|
|
$weekThisMonth = $this->getDataEachWeekThisMonthPengeluaran();
|
|
$weekLastMonth = $this->getDataEachWeekLastMonthPengeluaran();
|
|
|
|
return ResponseFormatter::success(['allPengeluaran' => $allPengeluaran, 'weekThisMonth' => $weekThisMonth, 'weekLastMonth' => $weekLastMonth]);
|
|
}
|
|
|
|
private function getDataEachWeekThisMonthPengeluaran()
|
|
{
|
|
//format string
|
|
$f = 'Y-m-d';
|
|
|
|
//if you want to record time as well, then replace today() with now()
|
|
//and remove startOfDay()
|
|
$today = Carbon::today();
|
|
$date = $today->copy()->firstOfMonth()->startOfDay();
|
|
$eom = $today->copy()->endOfMonth()->startOfDay();
|
|
|
|
$dates = [];
|
|
|
|
for ($i = 1; $date->lte($eom); $i++) {
|
|
|
|
//record start date
|
|
$startDate = $date->copy();
|
|
|
|
//loop to end of the week while not crossing the last date of month
|
|
while ($date->dayOfWeek != Carbon::SUNDAY && $date->lte($eom)) {
|
|
$date->addDay();
|
|
}
|
|
|
|
$dates['Minggu ke ' . $i] = $startDate->format($f) . ' 00:00:00 |' . $date->format($f) . ' 23:59:59';
|
|
$date->addDay();
|
|
}
|
|
|
|
$datapengeluaran = [];
|
|
|
|
foreach ($dates as $key => $value) {
|
|
$dataPengeluaran = Pengeluaran::whereBetween('tanggal', explode('|', $value))->sum('nominal');
|
|
$datapengeluaran[$key] = $dataPengeluaran;
|
|
}
|
|
|
|
return $datapengeluaran;
|
|
}
|
|
|
|
private function getDataEachWeekLastMonthPengeluaran()
|
|
{
|
|
//format string
|
|
$f = 'Y-m-d';
|
|
|
|
//if you want to record time as well, then replace today() with now()
|
|
//and remove startOfDay()
|
|
$today = Carbon::today()->subMonth();
|
|
$date = $today->copy()->firstOfMonth()->startOfDay();
|
|
$eom = $today->copy()->endOfMonth()->startOfDay();
|
|
|
|
$dates = [];
|
|
|
|
for ($i = 1; $date->lte($eom); $i++) {
|
|
|
|
//record start date
|
|
$startDate = $date->copy();
|
|
|
|
//loop to end of the week while not crossing the last date of month
|
|
while ($date->dayOfWeek != Carbon::SUNDAY && $date->lte($eom)) {
|
|
$date->addDay();
|
|
}
|
|
|
|
$dates['Minggu ke ' . $i] = $startDate->format($f) . ' 00:00:00 |' . $date->format($f) . ' 23:59:59';
|
|
$date->addDay();
|
|
}
|
|
|
|
$datapengeluaran = [];
|
|
foreach ($dates as $key => $value) {
|
|
$dataPengeluaran = Pengeluaran::whereBetween('tanggal', explode('|', $value))->sum('nominal');
|
|
$datapengeluaran[$key] = $dataPengeluaran;
|
|
}
|
|
|
|
return $datapengeluaran;
|
|
}
|
|
}
|