refactor: update user type hints in services for improved clarity and consistency
This commit is contained in:
parent
175ec30a5c
commit
25c501c011
@ -8,7 +8,6 @@
|
|||||||
use App\Models\CuttingResultPrice;
|
use App\Models\CuttingResultPrice;
|
||||||
use App\Models\ProductPrice;
|
use App\Models\ProductPrice;
|
||||||
use App\Models\ProductVariant;
|
use App\Models\ProductVariant;
|
||||||
use App\Models\RawMaterialPrice;
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Services\System\PushNotificationService;
|
use App\Services\System\PushNotificationService;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|||||||
@ -23,7 +23,7 @@ class AnalysisService
|
|||||||
{
|
{
|
||||||
public function getAttendance(): array
|
public function getAttendance(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ public function getAttendance(): array
|
|||||||
->where('start_date', '<=', Carbon::today())
|
->where('start_date', '<=', Carbon::today())
|
||||||
->where('end_date', '>=', Carbon::today());
|
->where('end_date', '>=', Carbon::today());
|
||||||
|
|
||||||
if (!$isSuper) {
|
if (! $isSuper) {
|
||||||
$employeeId = $user?->employee?->id;
|
$employeeId = $user?->employee?->id;
|
||||||
$employeeQuery->where('id', $employeeId);
|
$employeeQuery->where('id', $employeeId);
|
||||||
$attendanceQuery->where('employee_id', $employeeId);
|
$attendanceQuery->where('employee_id', $employeeId);
|
||||||
@ -120,14 +120,14 @@ public function getMyAttendance(User $user, ?Carbon $startDate = null, ?Carbon $
|
|||||||
|
|
||||||
public function getCashOverview(): array
|
public function getCashOverview(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
$totalBalanceQuery = CashAccount::query();
|
$totalBalanceQuery = CashAccount::query();
|
||||||
$transactionQuery = CashTransaction::query()->whereDate('created_at', Carbon::today());
|
$transactionQuery = CashTransaction::query()->whereDate('created_at', Carbon::today());
|
||||||
|
|
||||||
if (!$isSuper) {
|
if (! $isSuper) {
|
||||||
$transactionQuery->where('created_by_id', $user->id);
|
$transactionQuery->where('created_by_id', $user->id);
|
||||||
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
||||||
}
|
}
|
||||||
@ -151,13 +151,13 @@ public function getCashOverview(): array
|
|||||||
|
|
||||||
public function getTopSuppliers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getTopSuppliers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
return Purchase::query()
|
return Purchase::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
||||||
->join('suppliers', 'purchases.supplier_id', '=', 'suppliers.id')
|
->join('suppliers', 'purchases.supplier_id', '=', 'suppliers.id')
|
||||||
->selectRaw('suppliers.name, SUM(purchases.total) as total_amount, COUNT(purchases.id) as purchase_count')
|
->selectRaw('suppliers.name, SUM(purchases.total) as total_amount, COUNT(purchases.id) as purchase_count')
|
||||||
->groupBy('suppliers.id', 'suppliers.name')
|
->groupBy('suppliers.id', 'suppliers.name')
|
||||||
@ -174,15 +174,15 @@ public function getTopSuppliers(?Carbon $startDate = null, ?Carbon $endDate = nu
|
|||||||
|
|
||||||
public function getTopCustomers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getTopCustomers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
return Order::query()
|
return Order::query()
|
||||||
->completed()
|
->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->join('customers', 'orders.customer_id', '=', 'customers.id')
|
->join('customers', 'orders.customer_id', '=', 'customers.id')
|
||||||
->selectRaw('customers.name, SUM(orders.total_amount) as total_amount, COUNT(orders.id) as order_count')
|
->selectRaw('customers.name, SUM(orders.total_amount) as total_amount, COUNT(orders.id) as order_count')
|
||||||
@ -200,23 +200,23 @@ public function getTopCustomers(?Carbon $startDate = null, ?Carbon $endDate = nu
|
|||||||
|
|
||||||
public function getRevenueSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getRevenueSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$revenueSummary = Order::query()
|
$revenueSummary = Order::query()
|
||||||
->completed()
|
->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->selectRaw('SUM(total_amount) as total_revenue, SUM(discount) as total_discount, COUNT(*) as total_orders, AVG(total_amount) as avg_order')
|
->selectRaw('SUM(total_amount) as total_revenue, SUM(discount) as total_discount, COUNT(*) as total_orders, AVG(total_amount) as avg_order')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$totalMarketplaceFees = Order::query()
|
$totalMarketplaceFees = Order::query()
|
||||||
->completed()
|
->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereNotNull('marketplace_settings_snapshot')
|
->whereNotNull('marketplace_settings_snapshot')
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->get()
|
->get()
|
||||||
@ -234,17 +234,17 @@ public function getRevenueSummary(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
public function getMonthlyRevenue(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getMonthlyRevenue(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$query = Order::query()->completed()
|
$query = Order::query()->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id));
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id));
|
||||||
$feeQuery = Order::query()->completed()
|
$feeQuery = Order::query()->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereNotNull('marketplace_settings_snapshot');
|
->whereNotNull('marketplace_settings_snapshot');
|
||||||
|
|
||||||
if ($startDate && $endDate) {
|
if ($startDate && $endDate) {
|
||||||
@ -301,25 +301,25 @@ public function getMonthlyRevenue(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
$purchase = Purchase::query()
|
$purchase = Purchase::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
||||||
->selectRaw('COALESCE(SUM(total), 0) as total')
|
->selectRaw('COALESCE(SUM(total), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$expenses = Expense::query()
|
$expenses = Expense::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
||||||
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$employeeAdvance = EmployeeAdvance::query()
|
$employeeAdvance = EmployeeAdvance::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
||||||
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
@ -340,13 +340,13 @@ public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
$purchases = Purchase::query()
|
$purchases = Purchase::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
||||||
->selectRaw("DATE_FORMAT(purchases.created_at, '%Y-%m') as month_key, COALESCE(SUM(total), 0) as total")
|
->selectRaw("DATE_FORMAT(purchases.created_at, '%Y-%m') as month_key, COALESCE(SUM(total), 0) as total")
|
||||||
->groupBy('month_key')
|
->groupBy('month_key')
|
||||||
->orderBy('month_key')
|
->orderBy('month_key')
|
||||||
@ -354,7 +354,7 @@ public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
$expenses = Expense::query()
|
$expenses = Expense::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
||||||
->selectRaw("DATE_FORMAT(expenses.created_at, '%Y-%m') as month_key, COALESCE(SUM(amount), 0) as total")
|
->selectRaw("DATE_FORMAT(expenses.created_at, '%Y-%m') as month_key, COALESCE(SUM(amount), 0) as total")
|
||||||
->groupBy('month_key')
|
->groupBy('month_key')
|
||||||
->orderBy('month_key')
|
->orderBy('month_key')
|
||||||
@ -362,7 +362,7 @@ public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
$advances = EmployeeAdvance::query()
|
$advances = EmployeeAdvance::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
||||||
->selectRaw("DATE_FORMAT(employee_advances.created_at, '%Y-%m') as month_key, COALESCE(SUM(amount), 0) as total")
|
->selectRaw("DATE_FORMAT(employee_advances.created_at, '%Y-%m') as month_key, COALESCE(SUM(amount), 0) as total")
|
||||||
->groupBy('month_key')
|
->groupBy('month_key')
|
||||||
->orderBy('month_key')
|
->orderBy('month_key')
|
||||||
@ -412,14 +412,14 @@ public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate =
|
|||||||
|
|
||||||
public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$orderQuery = Order::query()->completed()
|
$orderQuery = Order::query()->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id));
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id));
|
||||||
if ($startDate && $endDate) {
|
if ($startDate && $endDate) {
|
||||||
$orderQuery->whereBetween('orders.created_at', [$startDate, $endDate]);
|
$orderQuery->whereBetween('orders.created_at', [$startDate, $endDate]);
|
||||||
}
|
}
|
||||||
@ -446,8 +446,8 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n
|
|||||||
$itemsData = OrderItem::query()
|
$itemsData = OrderItem::query()
|
||||||
->join('orders', 'order_items.order_id', '=', 'orders.id')
|
->join('orders', 'order_items.order_id', '=', 'orders.id')
|
||||||
->where('orders.status', OrderStatus::COMPLETED)
|
->where('orders.status', OrderStatus::COMPLETED)
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->selectRaw('
|
->selectRaw('
|
||||||
SUM(order_items.quantity) as total_qty,
|
SUM(order_items.quantity) as total_qty,
|
||||||
@ -463,8 +463,8 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n
|
|||||||
->join('orders', 'order_items.order_id', '=', 'orders.id')
|
->join('orders', 'order_items.order_id', '=', 'orders.id')
|
||||||
->leftJoin('cutting_result_prices', 'order_items.product_variant_id', '=', 'cutting_result_prices.product_variant_id')
|
->leftJoin('cutting_result_prices', 'order_items.product_variant_id', '=', 'cutting_result_prices.product_variant_id')
|
||||||
->where('orders.status', OrderStatus::COMPLETED)
|
->where('orders.status', OrderStatus::COMPLETED)
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->selectRaw('COALESCE(SUM(order_items.quantity * cutting_result_prices.cost_per_unit), 0) as total_hpp')
|
->selectRaw('COALESCE(SUM(order_items.quantity * cutting_result_prices.cost_per_unit), 0) as total_hpp')
|
||||||
->value('total_hpp');
|
->value('total_hpp');
|
||||||
@ -474,7 +474,7 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n
|
|||||||
// Expenses
|
// Expenses
|
||||||
$purchaseTotal = Purchase::query()
|
$purchaseTotal = Purchase::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('purchases.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('purchases.created_by_id', $user->id))
|
||||||
->sum('total');
|
->sum('total');
|
||||||
|
|
||||||
if ($user?->hasRole(Role::ADMIN_TOKO->value)) {
|
if ($user?->hasRole(Role::ADMIN_TOKO->value)) {
|
||||||
@ -483,12 +483,12 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n
|
|||||||
|
|
||||||
$expenseTotal = Expense::query()
|
$expenseTotal = Expense::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('expenses.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('expenses.created_by_id', $user->id))
|
||||||
->sum('amount');
|
->sum('amount');
|
||||||
|
|
||||||
$advanceTotal = EmployeeAdvance::query()
|
$advanceTotal = EmployeeAdvance::query()
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate]))
|
||||||
->when(!$isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
||||||
->sum('amount');
|
->sum('amount');
|
||||||
|
|
||||||
$totalExpenses = (int) $purchaseTotal + (int) $expenseTotal + (int) $advanceTotal;
|
$totalExpenses = (int) $purchaseTotal + (int) $expenseTotal + (int) $advanceTotal;
|
||||||
@ -573,14 +573,14 @@ public function getProductStock(): array
|
|||||||
|
|
||||||
public function getBusyHours(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getBusyHours(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$hourlyData = Order::query()
|
$hourlyData = Order::query()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->selectRaw('HOUR(orders.created_at) as hour, COUNT(*) as order_count')
|
->selectRaw('HOUR(orders.created_at) as hour, COUNT(*) as order_count')
|
||||||
->groupBy('hour')
|
->groupBy('hour')
|
||||||
@ -601,7 +601,7 @@ public function getBusyHours(?Carbon $startDate = null, ?Carbon $endDate = null)
|
|||||||
|
|
||||||
public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
@ -611,8 +611,8 @@ public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = nul
|
|||||||
->join('product_variants', 'order_items.product_variant_id', '=', 'product_variants.id')
|
->join('product_variants', 'order_items.product_variant_id', '=', 'product_variants.id')
|
||||||
->join('products', 'product_variants.product_id', '=', 'products.id')
|
->join('products', 'product_variants.product_id', '=', 'products.id')
|
||||||
->where('orders.status', OrderStatus::COMPLETED)
|
->where('orders.status', OrderStatus::COMPLETED)
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('orders.marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('orders.created_by_id', $user->id))
|
||||||
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
->when($startDate && $endDate, fn ($q) => $q->whereBetween('orders.created_at', [$startDate, $endDate]))
|
||||||
->selectRaw("CONCAT(products.name, ' - ', product_variants.name) as full_name, SUM(order_items.quantity) as total_qty, SUM(order_items.subtotal) as total_revenue")
|
->selectRaw("CONCAT(products.name, ' - ', product_variants.name) as full_name, SUM(order_items.quantity) as total_qty, SUM(order_items.subtotal) as total_revenue")
|
||||||
->groupBy('order_items.product_variant_id', 'products.name', 'product_variants.name')
|
->groupBy('order_items.product_variant_id', 'products.name', 'product_variants.name')
|
||||||
|
|||||||
@ -60,8 +60,8 @@ public function canCheckInForUser(User $user): bool
|
|||||||
|
|
||||||
public function getAttendance(): array
|
public function getAttendance(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ public function getAttendance(): array
|
|||||||
->where('start_date', '<=', Carbon::today())
|
->where('start_date', '<=', Carbon::today())
|
||||||
->where('end_date', '>=', Carbon::today());
|
->where('end_date', '>=', Carbon::today());
|
||||||
|
|
||||||
if (!$isSuper) {
|
if (! $isSuper) {
|
||||||
$employeeId = $user?->employee?->id;
|
$employeeId = $user?->employee?->id;
|
||||||
$employeeQuery->where('id', $employeeId);
|
$employeeQuery->where('id', $employeeId);
|
||||||
$attendanceQuery->where('employee_id', $employeeId);
|
$attendanceQuery->where('employee_id', $employeeId);
|
||||||
@ -95,14 +95,14 @@ public function getAttendance(): array
|
|||||||
|
|
||||||
public function getCashOverview(): array
|
public function getCashOverview(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
$totalBalanceQuery = CashAccount::query();
|
$totalBalanceQuery = CashAccount::query();
|
||||||
$transactionQuery = CashTransaction::query()->whereDate('created_at', Carbon::today());
|
$transactionQuery = CashTransaction::query()->whereDate('created_at', Carbon::today());
|
||||||
|
|
||||||
if (!$isSuper) {
|
if (! $isSuper) {
|
||||||
$transactionQuery->where('created_by_id', $user->id);
|
$transactionQuery->where('created_by_id', $user->id);
|
||||||
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
$totalBalanceQuery->whereHas('transactions', fn ($q) => $q->where('created_by_id', $user->id));
|
||||||
}
|
}
|
||||||
@ -126,23 +126,23 @@ public function getCashOverview(): array
|
|||||||
|
|
||||||
public function getRevenueSummary(): array
|
public function getRevenueSummary(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$revenueSummary = Order::query()
|
$revenueSummary = Order::query()
|
||||||
->completed()
|
->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereDate('created_at', Carbon::today())
|
->whereDate('created_at', Carbon::today())
|
||||||
->selectRaw('SUM(total_amount) as total_revenue, SUM(discount) as total_discount, COUNT(*) as total_orders, AVG(total_amount) as avg_order')
|
->selectRaw('SUM(total_amount) as total_revenue, SUM(discount) as total_discount, COUNT(*) as total_orders, AVG(total_amount) as avg_order')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$totalMarketplaceFees = Order::query()
|
$totalMarketplaceFees = Order::query()
|
||||||
->completed()
|
->completed()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereNotNull('marketplace_settings_snapshot')
|
->whereNotNull('marketplace_settings_snapshot')
|
||||||
->whereDate('created_at', Carbon::today())
|
->whereDate('created_at', Carbon::today())
|
||||||
->get()
|
->get()
|
||||||
@ -161,25 +161,25 @@ public function getRevenueSummary(): array
|
|||||||
public function getExpenseSummary(): array
|
public function getExpenseSummary(): array
|
||||||
{
|
{
|
||||||
$today = Carbon::today();
|
$today = Carbon::today();
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
|
|
||||||
$purchase = Purchase::query()
|
$purchase = Purchase::query()
|
||||||
->whereDate('created_at', $today)
|
->whereDate('created_at', $today)
|
||||||
->when(!$isSuper, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->selectRaw('COALESCE(SUM(total), 0) as total')
|
->selectRaw('COALESCE(SUM(total), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$expenses = Expense::query()
|
$expenses = Expense::query()
|
||||||
->whereDate('created_at', $today)
|
->whereDate('created_at', $today)
|
||||||
->when(!$isSuper, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$employeeAdvance = EmployeeAdvance::query()
|
$employeeAdvance = EmployeeAdvance::query()
|
||||||
->whereDate('created_at', $today)
|
->whereDate('created_at', $today)
|
||||||
->when(!$isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id))
|
||||||
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
->selectRaw('COALESCE(SUM(amount), 0) as total')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
@ -200,14 +200,14 @@ public function getExpenseSummary(): array
|
|||||||
|
|
||||||
public function getOrderStats(): array
|
public function getOrderStats(): array
|
||||||
{
|
{
|
||||||
/** @var \App\Models\User|null $user */
|
/** @var User|null $user */
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||||
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
$isMarketing = $user?->hasAnyRole(['marketing-offline', 'marketing-online']) ?? false;
|
||||||
|
|
||||||
$byChannel = Order::query()
|
$byChannel = Order::query()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereDate('created_at', Carbon::today())
|
->whereDate('created_at', Carbon::today())
|
||||||
->selectRaw('channel, COUNT(*) as count, SUM(total_amount) as total')
|
->selectRaw('channel, COUNT(*) as count, SUM(total_amount) as total')
|
||||||
->groupBy('channel')
|
->groupBy('channel')
|
||||||
@ -220,8 +220,8 @@ public function getOrderStats(): array
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$byPaymentType = Order::query()
|
$byPaymentType = Order::query()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereDate('created_at', Carbon::today())
|
->whereDate('created_at', Carbon::today())
|
||||||
->selectRaw('payment_type, COUNT(*) as count, SUM(total_amount) as total')
|
->selectRaw('payment_type, COUNT(*) as count, SUM(total_amount) as total')
|
||||||
->groupBy('payment_type')
|
->groupBy('payment_type')
|
||||||
@ -234,8 +234,8 @@ public function getOrderStats(): array
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$byMarketing = Order::query()
|
$byMarketing = Order::query()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereNotNull('marketing_id')
|
->whereNotNull('marketing_id')
|
||||||
->whereDate('orders.created_at', Carbon::today())
|
->whereDate('orders.created_at', Carbon::today())
|
||||||
->join('users', 'orders.marketing_id', '=', 'users.id')
|
->join('users', 'orders.marketing_id', '=', 'users.id')
|
||||||
@ -252,8 +252,8 @@ public function getOrderStats(): array
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$byStatus = Order::query()
|
$byStatus = Order::query()
|
||||||
->when(!$isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
->when(! $isSuper && $isMarketing, fn ($q) => $q->where('marketing_id', $user->id))
|
||||||
->when(!$isSuper && !$isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
->when(! $isSuper && ! $isMarketing, fn ($q) => $q->where('created_by_id', $user->id))
|
||||||
->whereDate('created_at', Carbon::today())
|
->whereDate('created_at', Carbon::today())
|
||||||
->selectRaw('status, COUNT(*) as count')
|
->selectRaw('status, COUNT(*) as count')
|
||||||
->groupBy('status')
|
->groupBy('status')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user