mailer = $mailer; $this->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->setReminderEmails(); $this->sendScheduledReports(); $this->info('Done'); if (\Utils::isNinjaDev()) { $this->info('Stopping early on ninja dev'); exit; } 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(count($accounts) . ' accounts found with fees'); foreach ($accounts as $account) { if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) { continue; } $invoices = $this->invoiceRepo->findNeedingReminding($account, false); $this->info($account->name . ': ' . count($invoices) . ' invoices found'); foreach ($invoices as $invoice) { if ($reminder = $account->getInvoiceReminder($invoice, false)) { $this->info('Charge fee: ' . $invoice->id); $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 setReminderEmails() { $accounts = $this->accountRepo->findWithReminders(); $this->info(count($accounts) . ' accounts found with reminders'); foreach ($accounts as $account) { if (! $account->hasFeature(FEATURE_EMAIL_TEMPLATES_REMINDERS)) { continue; } $invoices = $this->invoiceRepo->findNeedingReminding($account); $this->info($account->name . ': ' . count($invoices) . ' invoices found'); foreach ($invoices as $invoice) { if ($reminder = $account->getInvoiceReminder($invoice)) { $this->info('Send email: ' . $invoice->id); $this->mailer->sendInvoice($invoice, $reminder); } } } } private function sendScheduledReports() { $scheduledReports = ScheduledReport::where('send_date', '=', date('Y-m-d'))->get(); $this->info(count($scheduledReports) . ' scheduled reports'); foreach ($scheduledReports as $scheduledReport) { $config = json_decode($scheduledReport->config); $reportType = $config->report_type; $reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report'; if ($config->range) { switch ($config->range) { case 'this_month': $startDate = Carbon::now()->firstOfMonth()->toDateString(); $endDate = Carbon::now()->lastOfMonth()->toDateString(); break; case 'last_month': $startDate = Carbon::now()->subMonth()->firstOfMonth()->toDateString(); $endDate = Carbon::now()->subMonth()->lastOfMonth()->toDateString(); break; case 'this_year': $startDate = Carbon::now()->firstOfYear()->toDateString(); $endDate = Carbon::now()->lastOfYear()->toDateString(); break; case 'last_year': $startDate = Carbon::now()->subYear()->firstOfYear()->toDateString(); $endDate = Carbon::now()->subYear()->lastOfYear()->toDateString(); break; } } else { $startDate = Carbon::now()->subDays($config->start_date)->toDateString(); $endDate = Carbon::now()->subDays($config->end_date)->toDateString(); } $report = new $reportClass($startDate, $endDate, true, (array) $config); $params = [ 'startDate' => $startDate, 'endDate' => $endDate, 'report' => $report, ]; $report->run(); $params = array_merge($params, $report->results()); $file = dispatch(new ExportReportResults($scheduledReport->user, $config->export_format, $reportType, $params)); if ($file) { $this->userMailer->sendScheduledReport($scheduledReport, $file); } } } /** * @return array */ protected function getArguments() { return []; } /** * @return array */ protected function getOptions() { return [ ['database', null, InputOption::VALUE_OPTIONAL, 'Database', null], ]; } }