diff --git a/app/Services/System/AnalysisService.php b/app/Services/System/AnalysisService.php index 5bbb389..0b1e45e 100644 --- a/app/Services/System/AnalysisService.php +++ b/app/Services/System/AnalysisService.php @@ -2,6 +2,7 @@ namespace App\Services\System; +use App\Enums\EmployeeAdvanceStatus; use App\Enums\OrderStatus; use App\Enums\PriceType; use App\Enums\Role; @@ -19,6 +20,7 @@ use App\Models\RawMaterialPrice; use App\Models\User; use Carbon\Carbon; +use Illuminate\Support\Facades\DB; class AnalysisService { @@ -352,9 +354,10 @@ public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate = ->first(); $employeeAdvance = EmployeeAdvance::query() + ->whereIn('status', [EmployeeAdvanceStatus::APPROVED, EmployeeAdvanceStatus::PARTIALLY_PAID]) ->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate])) ->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id)) - ->selectRaw('COALESCE(SUM(amount), 0) as total') + ->selectRaw('COALESCE(SUM(amount - paid_amount), 0) as total') ->first(); $purchaseTotal = (int) ($purchase->total ?? 0); @@ -395,9 +398,10 @@ public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate = ->get(); $advances = EmployeeAdvance::query() + ->whereIn('status', [EmployeeAdvanceStatus::APPROVED, EmployeeAdvanceStatus::PARTIALLY_PAID]) ->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate])) ->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 - paid_amount), 0) as total") ->groupBy('month_key') ->orderBy('month_key') ->get(); @@ -524,9 +528,10 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n ->sum('amount'); $advanceTotal = EmployeeAdvance::query() + ->whereIn('status', [EmployeeAdvanceStatus::APPROVED, EmployeeAdvanceStatus::PARTIALLY_PAID]) ->when($startDate && $endDate, fn ($q) => $q->whereBetween('employee_advances.created_at', [$startDate, $endDate])) ->when(! $isSuper, fn ($q) => $q->where('employee_id', $user->employee?->id)) - ->sum('amount'); + ->sum(DB::raw('amount - paid_amount')); $totalExpenses = (int) $purchaseTotal + (int) $expenseTotal + (int) $advanceTotal; diff --git a/resources/js/lib/rupiah.ts b/resources/js/lib/rupiah.ts index 5c86850..32b2665 100644 --- a/resources/js/lib/rupiah.ts +++ b/resources/js/lib/rupiah.ts @@ -1,5 +1,8 @@ export function parseRupiah(value: string | number): string { - return String(value).replace(/\D/g, ''); + const str = String(value); + const isNegative = str.startsWith('-') || (typeof value === 'number' && value < 0); + + return isNegative ? '-' + str.replace(/[^0-9]/g, '') : str.replace(/[^0-9]/g, ''); } export function formatRupiah(value: string | number): string { diff --git a/resources/js/pages/admin/Analysis.vue b/resources/js/pages/admin/Analysis.vue index bc8950e..0c519f2 100644 --- a/resources/js/pages/admin/Analysis.vue +++ b/resources/js/pages/admin/Analysis.vue @@ -1,6 +1,6 @@