You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.9 KiB
PHTML

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
1 year ago
use App\Models\DepositHistory;
use App\Models\Sale;
use App\Models\SaleItem;
1 year ago
use Illuminate\Http\Request;
1 year ago
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
1 year ago
use Illuminate\Support\Str;
class GeneralController extends Controller
{
1 year ago
public function index(Request $request)
{
1 year ago
$deposites = DepositHistory::whereDate('created_at', now()->format('Y-m-d'))
->where('is_valid', DepositHistory::STATUS_VALID)
->where('debit', '!=', '0')
->groupBy('is_valid', 'customer_id')
->orderBy('total', 'desc')
1 year ago
->with('customer')
->limit(20)
->selectRaw('sum(debit) as total, is_valid, customer_id')
->get();
$sales = SaleItem::whereDate('sale_items.created_at', now()->format('Y-m-d'))
->join('sales', 'sales.id', '=', 'sale_items.sale_id')
->join('customers', 'customers.id', '=', 'sales.customer_id')
->groupBy('sales.customer_id', 'customers.name', 'entity_id')
->orderBy('total', 'desc')
1 year ago
->limit(20)
->selectRaw('sum(sale_items.price) as total, sum(quantity) as count, sales.customer_id, customers.name, entity_id')
->get();
// charts
$startDate = now()->startOfMonth();
$endDate = now()->endOfMonth();
$year = $startDate;
1 year ago
if ($request->start_date != '') {
$startDate = Carbon::parse($request->start_date);
1 year ago
}
if ($request->end_date != '') {
$endDate = Carbon::parse($request->end_date);
}
if ($request->year != '') {
$year = Carbon::parse($request->year);
1 year ago
}
$saleDepositCharts = Sale::selectRaw('SUM(amount) as sale_total, DATE(date_time)')
->whereBetween('date_time', [$startDate, $endDate])
->where('payed_with', Sale::PAYED_WITH_DEPOSIT)
->groupBy(DB::raw('DATE(date_time)'));
$salePaylaterCharts = Sale::selectRaw('SUM(amount) as sale_total, DATE(date_time)')
->whereBetween('date_time', [$startDate, $endDate])
->where('payed_with', Sale::PAYED_WITH_PAYLATER)
->groupBy(DB::raw('DATE(date_time)'));
1 year ago
// filter lokasi
1 year ago
if ($request->location_id != '') {
$saleDepositCharts->whereHas('items', function ($q) use ($request) {
$q->join('vouchers', 'vouchers.id', '=', 'sale_items.entity_id')
->join('location_profiles', 'vouchers.location_profile_id', '=', 'location_profiles.id')
->where('location_profiles.location_id', $request->location_id);
});
$salePaylaterCharts->whereHas('items', function ($q) use ($request) {
1 year ago
$q->join('vouchers', 'vouchers.id', '=', 'sale_items.entity_id')
->join('location_profiles', 'vouchers.location_profile_id', '=', 'location_profiles.id')
->where('location_profiles.location_id', $request->location_id);
1 year ago
});
}
// filter customer
if ($request->customer_id != '') {
$saleDepositCharts->where('customer_id', $request->customer_id);
$salePaylaterCharts->where('customer_id', $request->customer_id);
1 year ago
}
$depositSaleCharts = [];
$paylaterSaleCharts = [];
$dates = [];
$date = Carbon::parse($startDate);
$end = Carbon::parse($endDate);
$saleDepositCharts = $saleDepositCharts->get();
$salePaylaterCharts = $salePaylaterCharts->get();
while ($date <= $end) {
if ($request->payment == 'deposit' || $request->payment == '') {
$sdc = $saleDepositCharts->where('date', $date->format('Y-m-d'))->value('sale_total') ?? 0;
$depositSaleCharts[] = ['sale_total' => $sdc, 'date' => $date->format('d/m/Y')];
}
if ($request->payment == 'paylater' || $request->payment == '') {
$spc = $salePaylaterCharts->where('date', $date->format('Y-m-d'))->value('sale_total') ?? 0;
$paylaterSaleCharts[] = ['sale_total' => $spc, 'date' => $date->format('d/m/Y')];
}
$dates[] = ['date' => $date->format('d/m/Y')];
$date = $date->addDay();
}
1 year ago
$saleYearDepositCharts = Sale::selectRaw('SUM(amount) as sale_total, MONTH(date_time) as month')
->where('payed_with', Sale::PAYED_WITH_DEPOSIT)
1 year ago
->whereRaw('YEAR(sales.date_time) = '.Carbon::parse($year)->year)
->groupBy(DB::raw('MONTH(date_time)'));
1 year ago
$saleYearPaylaterCharts = Sale::selectRaw('SUM(amount) as sale_total, MONTH(date_time) as month')
->where('payed_with', Sale::PAYED_WITH_PAYLATER)
1 year ago
->whereRaw('YEAR(sales.date_time) = '.Carbon::parse($year)->year)
->groupBy(DB::raw('MONTH(date_time)'));
// filter lokasi
if ($request->year_location_id != '') {
$saleYearDepositCharts->whereHas('items', function ($q) use ($request) {
$q->join('vouchers', 'vouchers.id', '=', 'sale_items.entity_id')
->join('location_profiles', 'vouchers.location_profile_id', '=', 'location_profiles.id')
->where('location_profiles.location_id', $request->year_location_id);
});
$saleYearPaylaterCharts->whereHas('items', function ($q) use ($request) {
$q->join('vouchers', 'vouchers.id', '=', 'sale_items.entity_id')
->join('location_profiles', 'vouchers.location_profile_id', '=', 'location_profiles.id')
->where('location_profiles.location_id', $request->year_location_id);
});
}
// filter customer
if ($request->year_customer_id != '') {
$saleYearDepositCharts->where('customer_id', $request->year_customer_id);
$saleYearPaylaterCharts->where('customer_id', $request->year_customer_id);
}
$saleYearDepositCharts = $saleYearDepositCharts->get();
$saleYearPaylaterCharts = $saleYearPaylaterCharts->get();
$depositYearSaleCharts = [];
$paylaterYearSaleCharts = [];
$months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
$ms = [];
foreach ($months as $month) {
if ($request->year_payment == 'deposit' || $request->year_payment == '') {
$depositYearSaleCharts[] = ['sale_total' => $saleYearDepositCharts->where('month', $month)->value('sale_total') ?? 0, 'month' => $month];
}
if ($request->year_payment == 'paylater' || $request->year_payment == '') {
$paylaterYearSaleCharts[] = ['sale_total' => $saleYearPaylaterCharts->where('month', $month)->value('sale_total') ?? 0, 'month' => $month];
}
$ms[] = ['month' => $month];
}
1 year ago
return inertia('Dashboard', [
'deposites' => $deposites,
'sales' => $sales,
'sales_deposit_charts' => $depositSaleCharts,
'sales_paylater_charts' => $paylaterSaleCharts,
'_dates' => $dates,
1 year ago
'_startDate' => $startDate,
'_endDate' => $endDate,
'_payment' => $request->payment,
'deposit_year_sale_charts' => $depositYearSaleCharts,
'paylater_year_sale_charts' => $paylaterYearSaleCharts,
'_year_payment' => $request->year_payment,
'_months' => $ms,
'_year' => $year,
1 year ago
]);
}
public function maintance()
{
return inertia('Maintance');
}
1 year ago
public function upload(Request $request)
{
$request->validate(['image' => 'required|file']);
1 year ago
$file = $request->file('image');
$file->store('uploads', 'public');
return response()->json([
'id' => Str::ulid(),
'name' => $file->getClientOriginalName(),
'url' => asset($file->hashName('uploads')),
]);
}
}