isEnabled(); } catch (\Exception $e) { return false; } } } // Make cache_remember safe for early loading function cache_remember(string $key, callable $callback, int $ttl = 5000): mixed { try { return cache()->remember($key, env('CACHE_LIFETIME', $ttl), $callback); } catch (\Exception $e) { // Cache not available yet, just execute callback return $callback(); } } // Make get_option safe function get_option($key) { try { return cache_remember($key, function () use ($key) { if (!class_exists('App\Models\Option')) { return []; } return Option::where('key', $key)->first()->value ?? []; }); } catch (\Exception $e) { return []; } } function invoice_setting() { try { return get_option('invoice_setting_' . auth()->user()->business_id); } catch (\Exception $e) { return []; } } function invoice_language() { try { return get_option('invoice_language_' . auth()->user()->business_id); } catch (\Exception $e) { return []; } } function product_setting() { try { $businessId = auth()->user()->business_id ?? null; if (!$businessId) return null; $cacheKey = 'product_setting_' . $businessId; return cache()->remember($cacheKey, 60, function () use ($businessId) { if (!class_exists('App\Models\ProductSetting')) return null; $productSetting = ProductSetting::where('business_id', $businessId)->first(); if ($productSetting) { $productSetting->modules = $productSetting->modules ?? []; } return $productSetting; }); } catch (\Exception $e) { return null; } } function is_module_enabled(?array $modules, string $key): bool { $defaultTrueKeys = [ 'show_product_category', 'show_product_stock', 'show_exclusive_price', 'show_inclusive_price', 'show_profit_percent', 'show_product_sale_price', 'show_product_wholesale_price', 'show_product_dealer_price', 'show_action', ]; if (in_array($key, $defaultTrueKeys)) { return !isset($modules[$key]) || (bool) $modules[$key]; } return isset($modules[$key]) && (bool) $modules[$key]; } function formatted_date(?string $date = null, string $format = 'd M, Y'): ?string { try { return !empty($date) ? Date::parse($date)->format($format) : null; } catch (\Exception $e) { return $date; } } function formatted_time(?string $time = null, string $format = 'h:i A'): ?string { try { return !empty($time) ? Date::parse($time)->format($format) : null; } catch (\Exception $e) { return $time; } } function sendNotification($id, $url, $message, $user = null) { try { $notify = [ 'id' => $id, 'url' => $url, 'user' => $user, 'message' => $message, ]; $notify_user = User::where('role', 'superadmin')->first(); if ($notify_user) { Notification::send($notify_user, new SendNotification($notify)); } } catch (\Exception $e) { Log::error('Failed to send notification: ' . $e->getMessage()); } } function sendNotifyToUser($id, $url, $message, $user) { try { $notify = [ 'id' => $id, 'url' => $url, 'user' => $user, 'message' => $message, ]; $notify_user = User::where('business_id', $user)->first(); if ($notify_user) { Notification::send($notify_user, new SendNotification($notify)); } } catch (\Exception $e) { Log::error('Failed to send notification: ' . $e->getMessage()); } } function currency_format($amount, $type = "icon", $decimals = 2, $currency = null, $abbreviate = false, $apply_rounding = false) { try { $currency = $currency ?? default_currency(); if ($apply_rounding) { $amount = sale_rounding((float) $amount); } if ($abbreviate) { $amount = format_number($amount, $decimals); } else { $has_fraction = $amount != floor($amount); $amount = $has_fraction ? number_format($amount, $decimals) : number_format($amount, 0); } if ($type == "icon" || $type == "symbol") { if ($currency->position == "right") { return $amount . $currency->symbol; } else { return $currency->symbol . $amount; } } else { if ($currency->position == "right") { return $amount . ' ' . $currency->code; } else { return $currency->code . ' ' . $amount; } } } catch (\Exception $e) { return $amount; } } function format_number(float|int $number, int $decimals = 2): string { if ($number >= 1e9) { return remove_trailing_zeros($number / 1e9, $decimals) . "B"; } elseif ($number >= 1e6) { return remove_trailing_zeros($number / 1e6, $decimals) . "M"; } elseif ($number >= 1e3) { return remove_trailing_zeros($number / 1e3, $decimals) . "K"; } else { return remove_trailing_zeros($number, $decimals); } } function remove_trailing_zeros(float|int $number, int $decimals = 2): string { return rtrim(rtrim(number_format($number, $decimals, '.', ''), '0'), '.'); } function amountInWords(float $amount, int $decimals = 2): string { if (!extension_loaded('intl')) { return ''; } try { $has_fraction = fmod($amount, 1) != 0; $amount = $has_fraction ? round($amount, $decimals) : round($amount); $formatter = new \NumberFormatter('en_US', \NumberFormatter::SPELLOUT); $words = $formatter->format($amount); return $words . ' ' . (business_currency()->name ?? ''); } catch (\Exception $e) { return (string) $amount; } } function convert_money($amount, $currency) { try { if ($currency->code == default_currency('code') || $amount == 0) { return round($amount, 2); } else { return $amount * $currency->rate / default_currency()->rate; } } catch (\Exception $e) { return $amount; } } function default_currency($key = null, ?Currency $currency = null): object|int|string { try { $currency = $currency ?? cache_remember('default_currency', function () { $currency = Currency::whereIsDefault(1)->first(); if (!$currency) { $currency = (object) ['name' => 'US Dollar', 'code' => 'USD', 'rate' => 1, 'symbol' => '$', 'position' => 'left', 'status' => true, 'is_default' => true]; } return $currency; }); return $key ? $currency->$key : $currency; } catch (\Exception $e) { if ($key) return ''; return (object) ['name' => 'US Dollar', 'code' => 'USD', 'rate' => 1, 'symbol' => '$', 'position' => 'left']; } } // Add similar try-catch blocks to ALL functions that use database or facades // ... (continue with the rest of your functions, wrapping them in try-catch) function restorePublicImages() { if (!env('DEMO_MODE')) { return true; } try { DB::table('sales')->where('business_id', 1)->delete(); DB::table('sale_returns')->where('business_id', 1)->delete(); DB::table('purchases')->where('business_id', 1)->delete(); DB::table('purchase_returns')->where('business_id', 1)->delete(); DB::table('due_collects')->where('business_id', 1)->delete(); DB::table('parties')->where('business_id', 1)->delete(); DB::table('expense_categories')->where('business_id', 1)->delete(); DB::table('income_categories')->where('business_id', 1)->delete(); Artisan::call('db:seed', ['--class' => 'DemoSeeder']); } catch (\Exception $e) { Log::error('Failed to restore images: ' . $e->getMessage()); } return true; } // Safely handle plan_data function plan_data($business_id = null) { try { $business_id = $business_id ?? (auth()->check() ? auth()->user()->business_id : null); if (!$business_id || !class_exists('App\Models\PlanSubscribe')) { return []; } return cache_remember('plan-data-' . $business_id, function () use ($business_id) { $planSubscribe = PlanSubscribe::with('plan:id,subscriptionName') ->where('business_id', $business_id) ->latest() ->first(); if ($planSubscribe && class_exists('App\Models\Business')) { $business = Business::findOrFail($planSubscribe->business_id); $planSubscribe->will_expire = $business->will_expire ?? null; } return $planSubscribe; }); } catch (\Exception $e) { return []; } } function multibranch_active() { try { $planData = plan_data(); return $planData['allow_multibranch'] ?? false; } catch (\Exception $e) { return false; } } // Update moduleCheck function at the end if (!function_exists('moduleCheck')) { function moduleCheck($module) { if (!class_exists('Nwidart\Modules\Facades\Module')) { return false; } try { $moduleInstance = \Nwidart\Modules\Facades\Module::find($module); return $moduleInstance && $moduleInstance->isEnabled(); } catch (\Exception $e) { return false; } } }