resto-dhepot/app/Http/Controllers/Admins/Dashboard/CalculationController.php

257 lines
10 KiB
PHP

<?php
namespace App\Http\Controllers\Admins\Dashboard;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use App\Models\Pengeluaran;
use App\Models\Pesanan;
use Carbon\Carbon;
use Illuminate\Http\Request;
class CalculationController extends Controller
{
public function getCalculationPendapatan()
{
$pendapatanBulan = $this->calculatePendapatanBulan();
$pendapatanMinggu = $this->calculatePendapatanMinggu();
$pendapatanHari = $this->calculatePendapatanHari();
// dd($pendapatanMinggu);
return ResponseFormatter::success(['pendapatanBulan' => $pendapatanBulan, 'pendapatanMinggu' => $pendapatanMinggu, 'pendapatanHari' => $pendapatanHari]);
}
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 calculatePendapatanMinggu()
{
// tanggal minggu ini
$first_day_of_the_current_week = Carbon::today()->startOfWeek();
$last_day_of_the_current_week = $first_day_of_the_current_week->copy()->endOfWeek();
// dd($last_day_of_the_current_week);
// tanggal minggu lalu
$first_day_of_the_previous_week = $first_day_of_the_current_week->copy()->subWeek()->startOfWeek();
$last_day_of_the_previous_week = $first_day_of_the_current_week->copy()->subWeek()->endOfWeek();
// get data jumlah penjualan keseluruhan dalam satu minggu
$penjualanSatuMinggu = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_current_week, $last_day_of_the_current_week])->sum('grand_total');
$penjualanMingguLalu = Pesanan::whereBetween('tanggal_pesanan', [$first_day_of_the_previous_week, $last_day_of_the_previous_week])->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 week and this week
if ($penjualanMingguLalu != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($penjualanSatuMinggu - $penjualanMingguLalu) / $penjualanMingguLalu * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'penjualanSatuMinggu' => $penjualanSatuMinggu,
'penjualanMingguLalu' => $penjualanMingguLalu,
'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 pengeluaran keseluruhan dalam satu bulan
$pengeluaranSatuBulan = pengeluaran::whereBetween('tanggal', [$first_day_of_the_current_month, $last_day_of_the_current_month])->sum('nominal');
$pengeluaranBulanLalu = 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 ($pengeluaranBulanLalu != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($pengeluaranSatuBulan - $pengeluaranBulanLalu) / $pengeluaranBulanLalu * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'pengeluaranSatuBulan' => $pengeluaranSatuBulan,
'pengeluaranBulanLalu' => $pengeluaranBulanLalu,
'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 pengeluaran keseluruhan dalam satu bulan
$pengeluaranHariIni = Pengeluaran::whereBetween('tanggal', [$first_day_of_the_current_day, $last_day_of_the_current_day])->sum('nominal');
$pengeluaranKemarin = 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 ($pengeluaranKemarin != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($pengeluaranHariIni - $pengeluaranKemarin) / $pengeluaranKemarin * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
return [
'pengeluaranHariIni' => $pengeluaranHariIni,
'pengeluaranKemarin' => $pengeluaranKemarin,
'statusCalculation' => $statusCalculation,
'percentageOfSalesGrowth' => $percentageOfSalesGrowth
];
}
}