public function fire() { $this->info(date('Y-m-d') . ' ChargeRenewalInvoices...'); $ninjaAccount = $this->accountRepo->getNinjaAccount(); $invoices = Invoice::whereAccountId($ninjaAccount->id)->whereDueDate(date('Y-m-d'))->where('balance', '>', 0)->with('client')->orderBy('id')->get(); $this->info(count($invoices) . ' invoices found'); foreach ($invoices as $invoice) { // check if account has switched to free since the invoice was created $account = Account::find($invoice->client->public_id); if (!$account) { continue; } $company = $account->company; if (!$company->plan || $company->plan == PLAN_FREE) { continue; } try { $this->info("Charging invoice {$invoice->invoice_number}"); $this->paymentService->autoBillInvoice($invoice); } catch (Exception $exception) { $this->info('Error: ' . $exception->getMessage()); } } $this->info('Done'); }
public function fire() { $this->info(date('Y-m-d') . ' Running SendRecurringInvoices...'); $today = new DateTime(); $invoices = Invoice::with('account.timezone', 'invoice_items', 'client', 'user')->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS TRUE AND frequency_id > 0 AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today])->orderBy('id', 'asc')->get(); $this->info(count($invoices) . ' recurring invoice(s) found'); foreach ($invoices as $recurInvoice) { $shouldSendToday = $recurInvoice->shouldSendToday(); $this->info('Processing Invoice ' . $recurInvoice->id . ' - Should send ' . ($shouldSendToday ? 'YES' : 'NO')); if (!$shouldSendToday) { continue; } $recurInvoice->account->loadLocalizationSettings($recurInvoice->client); $invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice); if ($invoice && !$invoice->isPaid()) { $this->info('Sending Invoice'); $this->mailer->sendInvoice($invoice); } } $delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user')->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL', [$today->format('Y-m-d')])->orderBy('invoices.id', 'asc')->get(); $this->info(count($delayedAutoBillInvoices) . ' due recurring invoice instance(s) found'); /** @var Invoice $invoice */ foreach ($delayedAutoBillInvoices as $invoice) { if ($invoice->isPaid()) { continue; } if ($invoice->getAutoBillEnabled() && $invoice->client->autoBillLater()) { $this->info('Processing Autobill-delayed Invoice ' . $invoice->id); $this->paymentService->autoBillInvoice($invoice); } } $this->info('Done'); }