invoiceninja/app/Console/Commands/SendRecurringInvoices.php

157 lines
4.4 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
2015-03-17 07:45:25 +10:00
2017-01-30 21:40:43 +02:00
namespace App\Console\Commands;
2017-03-23 11:39:44 +02:00
use App\Models\Account;
2017-01-30 21:40:43 +02:00
use App\Models\Invoice;
2017-06-26 15:57:40 +03:00
use App\Models\RecurringExpense;
use App\Ninja\Repositories\InvoiceRepository;
2017-06-26 15:57:40 +03:00
use App\Ninja\Repositories\RecurringExpenseRepository;
2018-03-27 15:59:48 +03:00
use App\Jobs\SendInvoiceEmail;
2017-01-30 21:40:43 +02:00
use DateTime;
use Illuminate\Console\Command;
2017-05-01 15:46:57 +03:00
use Symfony\Component\Console\Input\InputOption;
2017-05-29 13:43:28 +03:00
use Auth;
2017-07-12 12:37:19 +03:00
use Exception;
use Utils;
2015-03-17 07:45:25 +10:00
/**
2017-01-30 21:40:43 +02:00
* Class SendRecurringInvoices.
*/
2015-03-17 07:45:25 +10:00
class SendRecurringInvoices extends Command
{
/**
* @var string
*/
2015-03-17 07:45:25 +10:00
protected $name = 'ninja:send-invoices';
/**
* @var string
*/
2015-03-17 07:45:25 +10:00
protected $description = 'Send recurring invoices';
/**
* @var InvoiceRepository
*/
protected $invoiceRepo;
/**
* SendRecurringInvoices constructor.
2017-01-30 21:40:43 +02:00
*
* @param InvoiceRepository $invoiceRepo
*/
2018-09-23 09:08:48 +03:00
public function __construct(InvoiceRepository $invoiceRepo, RecurringExpenseRepository $recurringExpenseRepo)
2015-03-17 07:45:25 +10:00
{
parent::__construct();
$this->invoiceRepo = $invoiceRepo;
2017-06-26 15:57:40 +03:00
$this->recurringExpenseRepo = $recurringExpenseRepo;
2015-03-17 07:45:25 +10:00
}
2019-09-12 19:03:15 -04:00
public function handle()
2015-03-17 07:45:25 +10:00
{
2017-10-24 10:59:26 +03:00
$this->info(date('r') . ' Running SendRecurringInvoices...');
2015-03-17 07:45:25 +10:00
2017-05-01 15:17:52 +03:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
2017-06-26 12:45:42 +03:00
$this->resetCounters();
$this->createInvoices();
$this->createExpenses();
2017-10-24 10:59:26 +03:00
$this->info(date('r') . ' Done');
2017-06-26 12:45:42 +03:00
}
private function resetCounters()
{
2017-03-23 11:39:44 +02:00
$accounts = Account::where('reset_counter_frequency_id', '>', 0)
->orderBy('id', 'asc')
->get();
foreach ($accounts as $account) {
$account->checkCounterReset();
}
2017-06-26 12:45:42 +03:00
}
private function createInvoices()
{
$today = new DateTime();
2017-03-23 11:39:44 +02:00
2015-03-17 07:45:25 +10:00
$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 is_public IS TRUE AND frequency_id > 0 AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today])
2015-10-20 11:23:38 +03:00
->orderBy('id', 'asc')
->get();
2018-04-18 22:56:55 +03:00
$this->info(date('r ') . $invoices->count() . ' recurring invoice(s) found');
2015-03-17 07:45:25 +10:00
foreach ($invoices as $recurInvoice) {
$shouldSendToday = $recurInvoice->shouldSendToday();
2017-01-30 18:05:31 +02:00
if (! $shouldSendToday) {
2015-10-20 11:23:38 +03:00
continue;
}
2016-06-20 17:14:43 +03:00
2018-04-18 22:56:55 +03:00
$this->info(date('r') . ' Processing Invoice: '. $recurInvoice->id);
2017-03-23 11:39:44 +02:00
$account = $recurInvoice->account;
$account->loadLocalizationSettings($recurInvoice->client);
2018-01-16 14:57:37 +02:00
Auth::loginUsingId($recurInvoice->activeUser()->id);
2015-08-11 17:38:36 +03:00
2017-07-12 12:37:19 +03:00
try {
$invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice);
2018-03-13 22:05:57 +02:00
if ($invoice && ! $invoice->isPaid() && $account->auto_email_invoice) {
2018-04-18 22:56:55 +03:00
$this->info(date('r') . ' Not billed - Sending Invoice');
2018-03-27 15:59:48 +03:00
dispatch(new SendInvoiceEmail($invoice, $invoice->user_id));
2017-12-03 16:40:02 +02:00
} elseif ($invoice) {
2018-04-18 22:56:55 +03:00
$this->info(date('r') . ' Successfully billed invoice');
2017-07-12 12:37:19 +03:00
}
} catch (Exception $exception) {
2018-04-18 22:56:55 +03:00
$this->info(date('r') . ' Error: ' . $exception->getMessage());
2017-07-12 12:37:19 +03:00
Utils::logError($exception);
2015-08-11 17:38:36 +03:00
}
2017-07-12 12:37:19 +03:00
2017-05-29 13:43:28 +03:00
Auth::logout();
2015-03-17 07:45:25 +10:00
}
2017-06-26 12:45:42 +03:00
}
private function createExpenses()
{
$today = new DateTime();
2017-06-26 15:57:40 +03:00
$expenses = RecurringExpense::with('client')
2017-06-26 12:45:42 +03:00
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND start_date <= ? AND (end_date IS NULL OR end_date >= ?)', [$today, $today])
->orderBy('id', 'asc')
->get();
2018-04-18 22:56:55 +03:00
$this->info(date('r ') . $expenses->count() . ' recurring expenses(s) found');
2017-06-26 12:45:42 +03:00
foreach ($expenses as $expense) {
$shouldSendToday = $expense->shouldSendToday();
if (! $shouldSendToday) {
continue;
}
2018-04-18 22:56:55 +03:00
$this->info(date('r') . ' Processing Expense: '. $expense->id);
2017-06-26 12:45:42 +03:00
$this->recurringExpenseRepo->createRecurringExpense($expense);
}
2015-03-17 07:45:25 +10:00
}
/**
* @return array
*/
2015-03-17 07:45:25 +10:00
protected function getArguments()
{
return [];
2015-03-17 07:45:25 +10:00
}
/**
* @return array
*/
2015-03-17 07:45:25 +10:00
protected function getOptions()
{
2017-05-01 15:17:52 +03:00
return [
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
];
2015-03-17 07:45:25 +10:00
}
}