resto-dhepot/app/Http/Controllers/Admins/DashboardController.php

324 lines
12 KiB
PHP

<?php
namespace App\Http\Controllers\Admins;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Kasir\Menu;
use App\Models\MenuTerlarisView;
use App\Models\Pengeluaran;
use App\Models\Pesanan;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\Facades\DataTables;
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('pages.admin.dashboard');
}
/**
* @Author: xelz
* @Date: 2024-01-26 18:59:12
* @Desc: Display Menu Terlaris
*/
public function menuTerlaris()
{
$data = MenuTerlarisView::where([['kategori_produk_id', '=', 2], ['tgl_start_promo', '=', null]])->get();
return DataTables::of($data)
->addIndexColumn()
->make(true);
}
public function getCalculationPendapatan()
{
$pendapatanBulan = $this->calculatePendapatanBulan();
$pendapatanHari = $this->calculatePendapatanHari();
$pengeluaranBulan = $this->calculatePengeluaranBulan();
$pengeluaranHari = $this->calculatePengeluaranHari();
return ResponseFormatter::success(['pendapatanBulan' => $pendapatanBulan, 'pendapatanHari' => $pendapatanHari, 'pengeluaranBulan' => $pengeluaranBulan, 'pengeluaranHari' => $pengeluaranHari]);
}
private function calculatePendapatanBulan()
{
// tanggal bulan ini
$first_day_of_the_current_month = Carbon::today()->startOfMonth();
$last_day_of_the_current_month = $first_day_of_the_current_month->copy()->endOfMonth();
// tanggal bulan lalu
$first_day_of_the_previous_month = $first_day_of_the_current_month->copy()->subMonth()->startOfMonth();
$last_day_of_the_previous_month = $first_day_of_the_current_month->copy()->subMonth()->endOfMonth();
// get data jumlah penjualan keseluruhan dalam satu bulan
$penjualanSatuBulan = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_current_month, $last_day_of_the_current_month])->sum('grand_total');
$penjualanBulanLalu = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_previous_month, $last_day_of_the_previous_month])->sum('grand_total');
/**
* @Author: xelz
* @Date: 2024-01-30 15:52:41
* @Desc: constant status
* 1 netral
* 2 plus
* 3 minus
*/
$statusCalculation = 1;
// calculate percentage of sales growth in the last month and this month
if ($penjualanBulanLalu != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($penjualanSatuBulan - $penjualanBulanLalu) / $penjualanBulanLalu * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'penjualanSatuBulan' => $penjualanSatuBulan,
'penjualanBulanLalu' => $penjualanBulanLalu,
'statusCalculation' => $statusCalculation,
'percentageOfSalesGrowth' => $percentageOfSalesGrowth
];
}
private function calculatePendapatanHari()
{
// tanggal hari ini
$first_day_of_the_current_day = Carbon::today()->startOfDay();
$last_day_of_the_current_day = $first_day_of_the_current_day->copy()->endOfDay();
// tanggal kemarin
$first_day_of_the_previous_day = $first_day_of_the_current_day->copy()->subDay()->startOfDay();
$last_day_of_the_previous_day = $first_day_of_the_current_day->copy()->subDay()->endOfDay();
// get data jumlah penjualan keseluruhan dalam satu bulan
$penjualanHariIni = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_current_day, $last_day_of_the_current_day])->sum('grand_total');
$penjualanKemarin = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_previous_day, $last_day_of_the_previous_day])->sum('grand_total');
/**
* @Author: xelz
* @Date: 2024-01-30 15:52:41
* @Desc: constant status
* 1 netral
* 2 plus
* 3 minus
*/
$statusCalculation = 1;
// calculate percentage of sales growth in the last month and this month
if ($penjualanKemarin != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($penjualanHariIni - $penjualanKemarin) / $penjualanKemarin * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'penjualanHariIni' => $penjualanHariIni,
'penjualanKemarin' => $penjualanKemarin,
'statusCalculation' => $statusCalculation,
'percentageOfSalesGrowth' => $percentageOfSalesGrowth
];
}
private function calculatePengeluaranBulan()
{
// tanggal bulan ini
$first_day_of_the_current_month = Carbon::today()->startOfMonth();
$last_day_of_the_current_month = $first_day_of_the_current_month->copy()->endOfMonth();
// tanggal bulan lalu
$first_day_of_the_previous_month = $first_day_of_the_current_month->copy()->subMonth()->startOfMonth();
$last_day_of_the_previous_month = $first_day_of_the_current_month->copy()->subMonth()->endOfMonth();
// get data jumlah penjualan keseluruhan dalam satu bulan
$penjualanSatuBulan = pengeluaran::whereBetween('tanggal', [$first_day_of_the_current_month, $last_day_of_the_current_month])->sum('nominal');
$penjualanBulanLalu = pengeluaran::whereBetween('tanggal', [$first_day_of_the_previous_month, $last_day_of_the_previous_month])->sum('nominal');
/**
* @Author: xelz
* @Date: 2024-01-30 15:52:41
* @Desc: constant status
* 1 netral
* 2 plus
* 3 minus
*/
$statusCalculation = 1;
// calculate percentage of sales growth in the last month and this month
if ($penjualanBulanLalu != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($penjualanSatuBulan - $penjualanBulanLalu) / $penjualanBulanLalu * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'penjualanSatuBulan' => $penjualanSatuBulan,
'penjualanBulanLalu' => $penjualanBulanLalu,
'statusCalculation' => $statusCalculation,
'percentageOfSalesGrowth' => $percentageOfSalesGrowth
];
}
private function calculatePengeluaranHari()
{
// tanggal hari ini
$first_day_of_the_current_day = Carbon::today()->startOfDay();
$last_day_of_the_current_day = $first_day_of_the_current_day->copy()->endOfDay();
// tanggal kemarin
$first_day_of_the_previous_day = $first_day_of_the_current_day->copy()->subDay()->startOfDay();
$last_day_of_the_previous_day = $first_day_of_the_current_day->copy()->subDay()->endOfDay();
// get data jumlah penjualan keseluruhan dalam satu bulan
$penjualanHariIni = Pengeluaran::whereBetween('tanggal', [$first_day_of_the_current_day, $last_day_of_the_current_day])->sum('nominal');
$penjualanKemarin = pengeluaran::whereBetween('tanggal', [$first_day_of_the_previous_day, $last_day_of_the_previous_day])->sum('nominal');
/**
* @Author: xelz
* @Date: 2024-01-30 15:52:41
* @Desc: constant status
* 1 netral
* 2 plus
* 3 minus
*/
$statusCalculation = 1;
// calculate percentage of sales growth in the last month and this month
if ($penjualanKemarin != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($penjualanHariIni - $penjualanKemarin) / $penjualanKemarin * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'penjualanHariIni' => $penjualanHariIni,
'penjualanKemarin' => $penjualanKemarin,
'statusCalculation' => $statusCalculation,
'percentageOfSalesGrowth' => $percentageOfSalesGrowth
];
}
public function getChart()
{
$allPenjualan = Pesanan::sum('grand_total');
$weekThisMonth = $this->getDataEachWeekThisMonth();
$weekLastMonth = $this->getDataEachWeekLastMonth();
return ResponseFormatter::success(['allPenjualan' => $allPenjualan, 'weekThisMonth' => $weekThisMonth, 'weekLastMonth' => $weekLastMonth]);
}
private function getDataEachWeekThisMonth()
{
//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();
}
$datapenjualan = [];
foreach ($dates as $key => $value) {
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', explode('|', $value))->sum('grand_total');
$datapenjualan[$key] = $dataPenjualan;
}
return $datapenjualan;
}
private function getDataEachWeekLastMonth()
{
//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();
}
$datapenjualan = [];
foreach ($dates as $key => $value) {
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', explode('|', $value))->sum('grand_total');
$datapenjualan[$key] = $dataPenjualan;
}
return $datapenjualan;
}
}