<?php
 namespace App\Providers; use App\Models\InvoiceBilling; use App\Services\InvoiceBillingService; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class InvoiceBillingServiceProvider extends ServiceProvider { public function register(): void { $this->app->singleton(InvoiceBillingService::class, function ($app) { return new InvoiceBillingService; }); $this->app->bind('invoice.billing.modules', function ($app) { return InvoiceBilling::active()->get(); }); $this->app->bind('invoice.billing.default', function ($app) { $defaultModule = setting('billing_default_module', 'gib'); return InvoiceBilling::where('name', $defaultModule)->first(); }); } public function boot(): void { $this->registerViewComposers(); $this->registerBladeDirectives(); } protected function registerViewComposers(): void { View::composer('*', function ($view) { try { $view->with('billingModules', InvoiceBilling::active()->get()); $view->with('defaultBillingModule', setting('billing_default_module', 'gib')); } catch (\Throwable $e) { $view->with('billingModules', collect()); $view->with('defaultBillingModule', 'gib'); } }); View::composer('admin.layouts.app', function ($view) { try { $view->with('billingModules', InvoiceBilling::active()->get()); $view->with('billingSettings', [ 'enable_automatic' => setting('billing_enable_automatic', false), 'default_module' => setting('billing_default_module', 'gib'), 'auto_process_delay' => setting('billing_auto_process_delay', 5), 'retry_failed_jobs' => setting('billing_retry_failed_jobs', true), 'max_retry_attempts' => setting('billing_max_retry_attempts', 3), 'notify_on_failure' => setting('billing_notify_on_failure', true), 'failure_notification_email' => setting('billing_failure_notification_email', ''), ]); } catch (\Throwable $e) { $view->with('billingModules', collect()); $view->with('billingSettings', []); } }); } protected function registerBladeDirectives(): void { Blade::if('billingModuleActive', function ($moduleName) { return InvoiceBilling::where('name', $moduleName)->where('is_active', true)->exists(); }); Blade::if('automaticBillingEnabled', function () { return setting('billing_enable_automatic', false); }); Blade::if('billingModuleTestable', function ($moduleName) { $module = InvoiceBilling::where('name', $moduleName)->first(); return $module && $module->is_active && ! empty($module->settings); }); } } 