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

269 lines
11 KiB
PHP

<?php
namespace App\Http\Controllers\Admins\Dashboard;
use App\Helpers\ResponseFormatter;
use App\Http\Controllers\Controller;
use App\Models\Pesanan;
use Carbon\Carbon;
use Illuminate\Http\Request;
class ChartPenjualanController extends Controller
{
public function getChartWeek()
{
// get data grand_total this week from first day until last day of week from 00:00:00 until 23:59:59
$grandTotalThisWeek = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->startOfWeek()->format('Y-m-d') . ' 00:00:00', Carbon::today()->endOfWeek()->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
// get data grand_total last week from first day until last day of week from 00:00:00 until 23:59:59
$grandTotalLastWeek = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->subWeek()->startOfWeek()->format('Y-m-d') . ' 00:00:00', Carbon::today()->subWeek()->endOfWeek()->format('Y-m-d') . ' 23:59:59'])->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 ($grandTotalLastWeek != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($grandTotalThisWeek - $grandTotalLastWeek) / $grandTotalLastWeek * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
$thisWeek = $this->getDataEachDayThisWeek();
// $lastWeek = $this->getDataEachDayLastWeek();
return ResponseFormatter::success(['thisWeek' => $thisWeek, 'grandTotalThisWeek' => $grandTotalThisWeek, 'grandTotalLastWeek' => $grandTotalLastWeek, 'percentageOfSalesGrowth' => $percentageOfSalesGrowth, 'statusCalculation' => $statusCalculation]);
}
private function getDataEachDayThisWeek()
{
// get data grand_total per day this week from 00:00:00 until 23:59:59
$datapenjualan = [];
$today = Carbon::today();
$date = $today->copy()->startOfWeek()->startOfDay();
$eow = $today->copy()->endOfWeek()->startOfDay();
for ($i = 1; $date->lte($eow); $i++) {
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$date->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan[tanggal_indonesia($date->format('Y-m-d'))] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
private function getDataEachDayLastWeek()
{
// get data grand_total per day last week from 00:00:00 until 23:59:59
$datapenjualan = [];
$today = Carbon::today()->subWeek();
$date = $today->copy()->startOfWeek()->startOfDay();
$eow = $today->copy()->endOfWeek()->startOfDay();
for ($i = 1; $date->lte($eow); $i++) {
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$date->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan[tanggal_indonesia($date->format('Y-m-d'))] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
public function getChartMonth()
{
// get data grand_total this month from first day until last day of month from 00:00:00 until 23:59:59
$grandTotalThisMonth = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->startOfMonth()->format('Y-m-d') . ' 00:00:00', Carbon::today()->endOfMonth()->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
// get data grand_total last month from first day until last day of month from 00:00:00 until 23:59:59
$grandTotalLastMonth = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->subMonth()->startOfMonth()->format('Y-m-d') . ' 00:00:00', Carbon::today()->subMonth()->endOfMonth()->format('Y-m-d') . ' 23:59:59'])->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 ($grandTotalLastMonth != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($grandTotalThisMonth - $grandTotalLastMonth) / $grandTotalLastMonth * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
$weekThisMonth = $this->getDataEachWeekThisMonth();
// $weekLastMonth = $this->getDataEachWeekLastMonth();
return ResponseFormatter::success(['weekThisMonth' => $weekThisMonth, 'grandTotalThisMonth' => $grandTotalThisMonth, 'grandTotalLastMonth' => $grandTotalLastMonth, 'percentageOfSalesGrowth' => $percentageOfSalesGrowth, 'statusCalculation' => $statusCalculation]);
}
private function getDataEachWeekThisMonth()
{
// get data grand_total per week this month from 00:00:00 until 23:59:59 each day of week (Sunday)
$datapenjualan = [];
$today = Carbon::today();
$date = $today->copy()->startOfMonth()->startOfDay();
$eom = $today->copy()->endOfMonth()->startOfDay();
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();
}
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$startDate->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan['Minggu ke ' . $i] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
private function getDataEachWeekLastMonth()
{
// get data grand_total per week this month from 00:00:00 until 23:59:59 each day of week (Sunday) last month
$datapenjualan = [];
$today = Carbon::today()->subMonth();
$date = $today->copy()->startOfMonth()->startOfDay();
$eom = $today->copy()->endOfMonth()->startOfDay();
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();
}
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$startDate->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan['Minggu ke ' . $i] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
public function getChartYear()
{
// get data grand_total this year from first day until last day of year from 00:00:00 until 23:59:59
$grandTotalThisYear = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->startOfYear()->format('Y-m-d') . ' 00:00:00', Carbon::today()->endOfYear()->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
// get data grand_total last year from first day until last day of year from 00:00:00 until 23:59:59
$grandTotalLastYear = Pesanan::whereBetween('tanggal_pesanan', [Carbon::today()->subYear()->startOfYear()->format('Y-m-d') . ' 00:00:00', Carbon::today()->subYear()->endOfYear()->format('Y-m-d') . ' 23:59:59'])->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 ($grandTotalLastYear != 0) {
$statusCalculation = 2;
$percentageOfSalesGrowth = ($grandTotalThisYear - $grandTotalLastYear) / $grandTotalLastYear * 100;
// minus or not
if ($percentageOfSalesGrowth < 0) {
$statusCalculation = 3;
$percentageOfSalesGrowth = $percentageOfSalesGrowth * -1;
}
} else {
$statusCalculation = 1;
$percentageOfSalesGrowth = 0;
}
$monthThisYear = $this->getDataEachMonthThisYear();
// $monthLastYear = $this->getDataEachMonthLastYear();
return ResponseFormatter::success(['monthThisYear' => $monthThisYear, 'grandTotalThisYear' => $grandTotalThisYear, 'grandTotalLastYear' => $grandTotalLastYear, 'percentageOfSalesGrowth' => $percentageOfSalesGrowth, 'statusCalculation' => $statusCalculation]);
}
private function getDataEachMonthThisYear()
{
// get data grand_total per month this year from 00:00:00 until 23:59:59 each day of month (last day of month)
$datapenjualan = [];
$today = Carbon::today();
$date = $today->copy()->startOfYear()->startOfDay();
$eoy = $today->copy()->endOfYear()->startOfDay();
for ($i = 1; $date->lte($eoy); $i++) {
//record start date
$startDate = $date->copy();
//loop to end of the week while not crossing the last date of month
while ($date->day != $date->daysInMonth && $date->lte($eoy)) {
$date->addDay();
}
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$startDate->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan[$date->format('F')] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
private function getDataEachMonthLastYear()
{
// get data grand_total per month this year from 00:00:00 until 23:59:59 each day of month (last day of month) last year
$datapenjualan = [];
$today = Carbon::today()->subYear();
$date = $today->copy()->startOfYear()->startOfDay();
$eoy = $today->copy()->endOfYear()->startOfDay();
for ($i = 1; $date->lte($eoy); $i++) {
//record start date
$startDate = $date->copy();
//loop to end of the week while not crossing the last date of month
while ($date->day != $date->daysInMonth && $date->lte($eoy)) {
$date->addDay();
}
$dataPenjualan = Pesanan::whereBetween('tanggal_pesanan', [$startDate->format('Y-m-d') . ' 00:00:00', $date->format('Y-m-d') . ' 23:59:59'])->sum('grand_total');
$datapenjualan['Bulan ke ' . $i] = $dataPenjualan;
$date->addDay();
}
return $datapenjualan;
}
}