invoiceRepo = $invoiceRepo; $this->accountRepo = $accountRepo; $this->userMailer = $userMailer; } public function fire() { $this->info(date('r') . ' Running SendReminders...'); if ($database = $this->option('database')) { config(['database.default' => $database]); } $this->chargeLateFees(); $this->sendReminderEmails(); $this->sendScheduledReports(); $this->loadExchangeRates(); $this->info(date('r') . ' Done'); if ($errorEmail = env('ERROR_EMAIL')) { \Mail::raw('EOM', function ($message) use ($errorEmail, $database) { $message->to($errorEmail) ->from(CONTACT_EMAIL) ->subject("SendReminders [{$database}]: Finished successfully"); }); } } private function chargeLateFees() { $accounts = $this->accountRepo->findWithFees(); $this->info(date('r ') . $accounts->count() . ' accounts found with fees'); foreach ($accounts as $account) { if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) { continue; } $invoices = $this->invoiceRepo->findNeedingReminding($account, false); $this->info(date('r ') . $account->name . ': ' . $invoices->count() . ' invoices found'); foreach ($invoices as $invoice) { if ($reminder = $account->getInvoiceReminder($invoice, false)) { $this->info(date('r') . ' Charge fee: ' . $invoice->id); $account->loadLocalizationSettings($invoice->client); // support trans to add fee line item $number = preg_replace('/[^0-9]/', '', $reminder); $amount = $account->account_email_settings->{"late_fee{$number}_amount"}; $percent = $account->account_email_settings->{"late_fee{$number}_percent"}; $this->invoiceRepo->setLateFee($invoice, $amount, $percent); } } } } private function sendReminderEmails() { $accounts = $this->accountRepo->findWithReminders(); $this->info(date('r ') . count($accounts) . ' accounts found with reminders'); foreach ($accounts as $account) { if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) { continue; } // standard reminders $invoices = $this->invoiceRepo->findNeedingReminding($account); $this->info(date('r ') . $account->name . ': ' . $invoices->count() . ' invoices found'); foreach ($invoices as $invoice) { if ($reminder = $account->getInvoiceReminder($invoice)) { if ($invoice->last_sent_date == date('Y-m-d')) { continue; } $this->info(date('r') . ' Send email: ' . $invoice->id); dispatch(new SendInvoiceEmail($invoice, $invoice->user_id, $reminder)); } } // endless reminders $invoices = $this->invoiceRepo->findNeedingEndlessReminding($account); $this->info(date('r ') . $account->name . ': ' . $invoices->count() . ' endless invoices found'); foreach ($invoices as $invoice) { if ($invoice->last_sent_date == date('Y-m-d')) { continue; } $this->info(date('r') . ' Send email: ' . $invoice->id); dispatch(new SendInvoiceEmail($invoice, $invoice->user_id, 'reminder4')); } } } private function sendScheduledReports() { $scheduledReports = ScheduledReport::where('send_date', '<=', date('Y-m-d')) ->with('user', 'account.company') ->get(); $this->info(date('r ') . $scheduledReports->count() . ' scheduled reports'); foreach ($scheduledReports as $scheduledReport) { $this->info(date('r') . ' Processing report: ' . $scheduledReport->id); $user = $scheduledReport->user; $account = $scheduledReport->account; $account->loadLocalizationSettings(); if (! $account->hasFeature(FEATURE_REPORTS)) { continue; } $config = (array) json_decode($scheduledReport->config); $reportType = $config['report_type']; // send email as user auth()->onceUsingId($user->id); $report = dispatch(new RunReport($scheduledReport->user, $reportType, $config, true)); $file = dispatch(new ExportReportResults($scheduledReport->user, $config['export_format'], $reportType, $report->exportParams)); if ($file) { try { $this->userMailer->sendScheduledReport($scheduledReport, $file); $this->info(date('r') . ' Sent report'); } catch (Exception $exception) { $this->info(date('r') . ' ERROR: ' . $exception->getMessage()); } } else { $this->info(date('r') . ' ERROR: Failed to run report'); } $scheduledReport->updateSendDate(); auth()->logout(); } } private function loadExchangeRates() { if (Utils::isNinjaDev()) { return; } $this->info(date('r') . ' Loading latest exchange rates...'); $data = CurlUtils::get(config('ninja.exchange_rates_url')); $data = json_decode($data); Currency::whereCode(config('ninja.exchange_rates_base'))->update(['exchange_rate' => 1]); foreach ($data->rates as $code => $rate) { Currency::whereCode($code)->update(['exchange_rate' => $rate]); } CurlUtils::get(SITE_URL . '?clear_cache=true'); } /** * @return array */ protected function getArguments() { return []; } /** * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], ]; } }