1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Console/Commands/SendRecurringInvoices.php

122 lines
3.5 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Console\Commands;
2015-03-16 22:45:25 +01:00
2015-04-12 16:18:05 +02:00
use DateTime;
2015-03-16 22:45:25 +01:00
use Illuminate\Console\Command;
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\InvoiceRepository;
2016-05-25 04:49:06 +02:00
use App\Services\PaymentService;
2015-04-12 16:18:05 +02:00
use App\Models\Invoice;
2015-03-16 22:45:25 +01:00
/**
* Class SendRecurringInvoices
*/
2015-03-16 22:45:25 +01:00
class SendRecurringInvoices extends Command
{
/**
* @var string
*/
2015-03-16 22:45:25 +01:00
protected $name = 'ninja:send-invoices';
/**
* @var string
*/
2015-03-16 22:45:25 +01:00
protected $description = 'Send recurring invoices';
/**
* @var Mailer
*/
2015-03-16 22:45:25 +01:00
protected $mailer;
/**
* @var InvoiceRepository
*/
protected $invoiceRepo;
/**
* @var PaymentService
*/
2016-05-25 04:49:06 +02:00
protected $paymentService;
2015-03-16 22:45:25 +01:00
/**
* SendRecurringInvoices constructor.
* @param Mailer $mailer
* @param InvoiceRepository $invoiceRepo
* @param PaymentService $paymentService
*/
2016-05-25 04:49:06 +02:00
public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, PaymentService $paymentService)
2015-03-16 22:45:25 +01:00
{
parent::__construct();
$this->mailer = $mailer;
$this->invoiceRepo = $invoiceRepo;
2016-05-25 04:49:06 +02:00
$this->paymentService = $paymentService;
2015-03-16 22:45:25 +01:00
}
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])
2015-10-20 10:23:38 +02:00
->orderBy('id', 'asc')
->get();
2015-03-16 22:45:25 +01:00
$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) {
2015-10-20 10:23:38 +02:00
continue;
}
2016-06-20 16:14:43 +02:00
2015-10-14 16:15:39 +02:00
$recurInvoice->account->loadLocalizationSettings($recurInvoice->client);
2015-08-11 16:38:36 +02:00
$invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice);
if ($invoice && !$invoice->isPaid()) {
2015-10-20 10:23:38 +02:00
$this->info('Sending Invoice');
2015-08-11 16:38:36 +02:00
$this->mailer->sendInvoice($invoice);
}
2015-03-16 22:45:25 +01:00
}
2016-05-25 04:49:06 +02:00
$delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user')
2016-06-20 16:14:43 +02:00
->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE
2016-05-26 21:22:09 +02:00
AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL',
[$today->format('Y-m-d')])
2016-05-25 04:49:06 +02:00
->orderBy('invoices.id', 'asc')
->get();
2016-05-26 21:22:09 +02:00
$this->info(count($delayedAutoBillInvoices).' due recurring invoice instance(s) found');
2016-05-25 04:49:06 +02:00
/** @var Invoice $invoice */
2016-05-25 04:49:06 +02:00
foreach ($delayedAutoBillInvoices as $invoice) {
2016-06-20 16:14:43 +02:00
if ($invoice->isPaid()) {
continue;
2016-05-25 04:49:06 +02:00
}
2016-06-20 16:14:43 +02:00
if ($invoice->getAutoBillEnabled() && $invoice->client->autoBillLater()) {
2016-07-31 20:27:11 +02:00
$this->info('Processing Autobill-delayed Invoice ' . $invoice->id);
2016-05-25 04:49:06 +02:00
$this->paymentService->autoBillInvoice($invoice);
}
}
2015-03-16 22:45:25 +01:00
$this->info('Done');
}
/**
* @return array
*/
2015-03-16 22:45:25 +01:00
protected function getArguments()
{
return [];
2015-03-16 22:45:25 +01:00
}
/**
* @return array
*/
2015-03-16 22:45:25 +01:00
protected function getOptions()
{
return [];
2015-03-16 22:45:25 +01:00
}
}