diff --git a/app/Console/Commands/BillReminder.php b/app/Console/Commands/BillReminder.php index 02ddda567..5559ebe3a 100644 --- a/app/Console/Commands/BillReminder.php +++ b/app/Console/Commands/BillReminder.php @@ -5,8 +5,8 @@ namespace App\Console\Commands; use App\Models\Company\Company; use App\Models\Expense\Bill; use App\Notifications\Expense\Bill as Notification; - -use Jenssegers\Date\Date; +use App\Utilities\Overrider; +use Date; use Illuminate\Console\Command; class BillReminder extends Command @@ -27,8 +27,6 @@ class BillReminder extends Command /** * Create a new command instance. - * - * @return void */ public function __construct() { @@ -46,6 +44,13 @@ class BillReminder extends Command $companies = Company::all(); foreach ($companies as $company) { + // Set company id + session(['company_id' => $company->id]); + + // Override settings and currencies + Overrider::load('settings'); + Overrider::load('currencies'); + $company->setSettings(); //$days = explode(',', setting('general.schedule_bill_days', '1,3')); @@ -57,6 +62,9 @@ class BillReminder extends Command $this->remind($day, $company); } } + + // Unset company_id + session()->forget('company_id'); } protected function remind($day, $company) @@ -65,7 +73,7 @@ class BillReminder extends Command $date = Date::today()->addDays($day)->toDateString(); // Get upcoming bills - $bills = Bill::companyId($company->id)->due($date)->with('vendor')->get(); + $bills = Bill::with('vendor')->due($date)->get(); foreach ($bills as $bill) { // Notify all users assigned to this company diff --git a/app/Console/Commands/InvoiceReminder.php b/app/Console/Commands/InvoiceReminder.php index f24fe0dc9..f29bfb21d 100644 --- a/app/Console/Commands/InvoiceReminder.php +++ b/app/Console/Commands/InvoiceReminder.php @@ -5,8 +5,8 @@ namespace App\Console\Commands; use App\Models\Company\Company; use App\Models\Income\Invoice; use App\Notifications\Income\Invoice as Notification; - -use Jenssegers\Date\Date; +use App\Utilities\Overrider; +use Date; use Illuminate\Console\Command; class InvoiceReminder extends Command @@ -27,8 +27,6 @@ class InvoiceReminder extends Command /** * Create a new command instance. - * - * @return void */ public function __construct() { @@ -46,6 +44,13 @@ class InvoiceReminder extends Command $companies = Company::all(); foreach ($companies as $company) { + // Set company id + session(['company_id' => $company->id]); + + // Override settings and currencies + Overrider::load('settings'); + Overrider::load('currencies'); + $company->setSettings(); //$days = explode(',', config('general.schedule_invoice_days', '1,3')); @@ -57,6 +62,9 @@ class InvoiceReminder extends Command $this->remind($day, $company); } } + + // Unset company_id + session()->forget('company_id'); } protected function remind($day, $company) @@ -65,7 +73,7 @@ class InvoiceReminder extends Command $date = Date::today()->subDays($day)->toDateString(); // Get upcoming bills - $invoices = Invoice::companyId($company->id)->due($date)->with('customer')->get(); + $invoices = Invoice::with('customer')->due($date)->get(); foreach ($invoices as $invoice) { // Notify the customer diff --git a/app/Http/Middleware/LoadCurrencies.php b/app/Http/Middleware/LoadCurrencies.php index 334788a66..e79caef62 100644 --- a/app/Http/Middleware/LoadCurrencies.php +++ b/app/Http/Middleware/LoadCurrencies.php @@ -2,8 +2,7 @@ namespace App\Http\Middleware; -use Akaunting\Money\Currency; -use App\Models\Setting\Currency as Model; +use App\Utilities\Overrider; use Closure; class LoadCurrencies @@ -23,22 +22,7 @@ class LoadCurrencies return $next($request); } - $currencies = Model::all(); - - foreach ($currencies as $currency) { - if (!isset($currency->precision)) { - continue; - } - - config(['money.' . $currency->code . '.precision' => $currency->precision]); - config(['money.' . $currency->code . '.symbol' => $currency->symbol]); - config(['money.' . $currency->code . '.symbol_first' => $currency->symbol_first]); - config(['money.' . $currency->code . '.decimal_mark' => $currency->decimal_mark]); - config(['money.' . $currency->code . '.thousands_separator' => $currency->thousands_separator]); - } - - // Set currencies with new settings - Currency::setCurrencies(config('money')); + Overrider::load('currencies'); return $next($request); } diff --git a/app/Http/Middleware/LoadSettings.php b/app/Http/Middleware/LoadSettings.php index 2ce73b1ec..07cde329c 100644 --- a/app/Http/Middleware/LoadSettings.php +++ b/app/Http/Middleware/LoadSettings.php @@ -2,6 +2,7 @@ namespace App\Http\Middleware; +use App\Utilities\Overrider; use Closure; class LoadSettings @@ -21,39 +22,7 @@ class LoadSettings return $next($request); } - // Set the active company settings - setting()->setExtraColumns(['company_id' => $company_id]); - setting()->load(true); - - // Timezone - config(['app.timezone' => setting('general.timezone', 'UTC')]); - - // Email - $email_protocol = setting('general.email_protocol', 'mail'); - config(['mail.driver' => $email_protocol]); - config(['mail.from.name' => setting('general.company_name')]); - config(['mail.from.address' => setting('general.company_email')]); - - if ($email_protocol == 'sendmail') { - config(['mail.sendmail' => setting('general.email_sendmail_path')]); - } elseif ($email_protocol == 'smtp') { - config(['mail.host' => setting('general.email_smtp_host')]); - config(['mail.port' => setting('general.email_smtp_port')]); - config(['mail.username' => setting('general.email_smtp_username')]); - config(['mail.password' => setting('general.email_smtp_password')]); - config(['mail.encryption' => setting('general.email_smtp_encryption')]); - } - - // Session - config(['session.driver' => setting('general.session_handler', 'file')]); - config(['session.lifetime' => setting('general.session_lifetime', '30')]); - - // Locale - if (session('locale') == '') { - //App::setLocale(setting('general.default_language')); - //Session::put('locale', setting('general.default_language')); - config(['app.locale' => setting('general.default_locale')]); - } + Overrider::load('settings'); return $next($request); } diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index 1eee9352e..5f04c1f1d 100644 --- a/app/Scopes/Company.php +++ b/app/Scopes/Company.php @@ -18,8 +18,8 @@ class Company implements Scope */ public function apply(Builder $builder, Model $model) { - // Session not available in console - if (App::runningInConsole()) { + $company_id = session('company_id'); + if (empty($company_id)) { return; } @@ -37,8 +37,6 @@ class Company implements Scope } // Apply company scope - $company_id = session('company_id'); - $builder->where($table . '.company_id', '=', $company_id); } diff --git a/app/Utilities/Overrider.php b/app/Utilities/Overrider.php new file mode 100644 index 000000000..6ba2b9bf7 --- /dev/null +++ b/app/Utilities/Overrider.php @@ -0,0 +1,83 @@ +setExtraColumns(['company_id' => static::$company_id]); + setting()->load(true); + + // Timezone + config(['app.timezone' => setting('general.timezone', 'UTC')]); + + // Email + $email_protocol = setting('general.email_protocol', 'mail'); + config(['mail.driver' => $email_protocol]); + config(['mail.from.name' => setting('general.company_name')]); + config(['mail.from.address' => setting('general.company_email')]); + + if ($email_protocol == 'sendmail') { + config(['mail.sendmail' => setting('general.email_sendmail_path')]); + } elseif ($email_protocol == 'smtp') { + config(['mail.host' => setting('general.email_smtp_host')]); + config(['mail.port' => setting('general.email_smtp_port')]); + config(['mail.username' => setting('general.email_smtp_username')]); + config(['mail.password' => setting('general.email_smtp_password')]); + config(['mail.encryption' => setting('general.email_smtp_encryption')]); + } + + // Session + config(['session.driver' => setting('general.session_handler', 'file')]); + config(['session.lifetime' => setting('general.session_lifetime', '30')]); + + // Locale + if (session('locale') == '') { + //App::setLocale(setting('general.default_language')); + //Session::put('locale', setting('general.default_language')); + config(['app.locale' => setting('general.default_locale')]); + } + } + + protected static function loadCurrencies() + { + $currencies = Currency::all(); + + foreach ($currencies as $currency) { + if (!isset($currency->precision)) { + continue; + } + + config(['money.' . $currency->code . '.precision' => $currency->precision]); + config(['money.' . $currency->code . '.symbol' => $currency->symbol]); + config(['money.' . $currency->code . '.symbol_first' => $currency->symbol_first]); + config(['money.' . $currency->code . '.decimal_mark' => $currency->decimal_mark]); + config(['money.' . $currency->code . '.thousands_separator' => $currency->thousands_separator]); + } + + // Set currencies with new settings + \Akaunting\Money\Currency::setCurrencies(config('money')); + } + +} \ No newline at end of file