2017-01-30 20:40:43 +01:00
< ? php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Console\Commands ;
2017-03-23 10:39:44 +01:00
use App\Models\Account ;
2017-01-30 20:40:43 +01:00
use App\Models\Invoice ;
2017-06-26 14:57:40 +02:00
use App\Models\RecurringExpense ;
2015-03-24 09:21:12 +01:00
use App\Ninja\Mailers\ContactMailer as Mailer ;
2015-08-10 17:48:41 +02:00
use App\Ninja\Repositories\InvoiceRepository ;
2017-06-26 14:57:40 +02:00
use App\Ninja\Repositories\RecurringExpenseRepository ;
2016-05-25 04:49:06 +02:00
use App\Services\PaymentService ;
2017-01-30 20:40:43 +01:00
use DateTime ;
use Illuminate\Console\Command ;
2017-05-01 14:46:57 +02:00
use Symfony\Component\Console\Input\InputOption ;
2017-05-29 12:43:28 +02:00
use Auth ;
2017-07-12 11:37:19 +02:00
use Exception ;
use Utils ;
2015-03-16 22:45:25 +01:00
2016-07-03 18:11:58 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class SendRecurringInvoices .
2016-07-03 18:11:58 +02:00
*/
2015-03-16 22:45:25 +01:00
class SendRecurringInvoices extends Command
{
2016-07-03 18:11:58 +02:00
/**
* @ var string
*/
2015-03-16 22:45:25 +01:00
protected $name = 'ninja:send-invoices' ;
2016-07-03 18:11:58 +02:00
/**
* @ var string
*/
2015-03-16 22:45:25 +01:00
protected $description = 'Send recurring invoices' ;
2016-07-03 18:11:58 +02:00
/**
* @ var Mailer
*/
2015-03-16 22:45:25 +01:00
protected $mailer ;
2016-07-03 18:11:58 +02:00
/**
* @ var InvoiceRepository
*/
2015-08-10 17:48:41 +02:00
protected $invoiceRepo ;
2016-07-03 18:11:58 +02:00
/**
* @ var PaymentService
*/
2016-05-25 04:49:06 +02:00
protected $paymentService ;
2015-03-16 22:45:25 +01:00
2016-07-03 18:11:58 +02:00
/**
* SendRecurringInvoices constructor .
2017-01-30 20:40:43 +01:00
*
* @ param Mailer $mailer
2016-07-03 18:11:58 +02:00
* @ param InvoiceRepository $invoiceRepo
2017-01-30 20:40:43 +01:00
* @ param PaymentService $paymentService
2016-07-03 18:11:58 +02:00
*/
2017-06-26 14:57:40 +02:00
public function __construct ( Mailer $mailer , InvoiceRepository $invoiceRepo , PaymentService $paymentService , RecurringExpenseRepository $recurringExpenseRepo )
2015-03-16 22:45:25 +01:00
{
parent :: __construct ();
$this -> mailer = $mailer ;
2015-08-10 17:48:41 +02:00
$this -> invoiceRepo = $invoiceRepo ;
2016-05-25 04:49:06 +02:00
$this -> paymentService = $paymentService ;
2017-06-26 14:57:40 +02:00
$this -> recurringExpenseRepo = $recurringExpenseRepo ;
2015-03-16 22:45:25 +01:00
}
public function fire ()
{
2017-03-31 13:20:46 +02:00
$this -> info ( date ( 'Y-m-d H:i:s' ) . ' Running SendRecurringInvoices...' );
2015-03-16 22:45:25 +01:00
2017-05-01 14:17:52 +02:00
if ( $database = $this -> option ( 'database' )) {
config ([ 'database.default' => $database ]);
}
2017-06-26 11:45:42 +02:00
$this -> resetCounters ();
$this -> createInvoices ();
$this -> billInvoices ();
$this -> createExpenses ();
$this -> info ( date ( 'Y-m-d H:i:s' ) . ' Done' );
}
private function resetCounters ()
{
2017-03-23 10:39:44 +01:00
$accounts = Account :: where ( 'reset_counter_frequency_id' , '>' , 0 )
-> orderBy ( 'id' , 'asc' )
-> get ();
foreach ( $accounts as $account ) {
$account -> checkCounterReset ();
}
2017-06-26 11:45:42 +02:00
}
private function createInvoices ()
{
$today = new DateTime ();
2017-03-23 10:39:44 +01:00
2015-03-16 22:45:25 +01:00
$invoices = Invoice :: with ( 'account.timezone' , 'invoice_items' , 'client' , 'user' )
2016-12-05 09:11:33 +01:00
-> 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 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 ) {
2016-07-01 09:29:24 +02:00
$shouldSendToday = $recurInvoice -> shouldSendToday ();
2017-01-30 17:05:31 +01:00
if ( ! $shouldSendToday ) {
2015-10-20 10:23:38 +02:00
continue ;
}
2016-06-20 16:14:43 +02:00
2017-06-06 09:58:49 +02:00
$this -> info ( 'Processing Invoice: ' . $recurInvoice -> id );
2017-03-23 10:39:44 +01:00
$account = $recurInvoice -> account ;
$account -> loadLocalizationSettings ( $recurInvoice -> client );
2017-05-29 12:43:28 +02:00
Auth :: loginUsingId ( $recurInvoice -> user_id );
2015-08-11 16:38:36 +02:00
2017-07-12 11:37:19 +02:00
try {
$invoice = $this -> invoiceRepo -> createRecurringInvoice ( $recurInvoice );
if ( $invoice && ! $invoice -> isPaid ()) {
$this -> info ( 'Sending Invoice' );
$this -> mailer -> sendInvoice ( $invoice );
}
} catch ( Exception $exception ) {
$this -> info ( 'Error: ' . $exception -> getMessage ());
Utils :: logError ( $exception );
2015-08-11 16:38:36 +02:00
}
2017-07-12 11:37:19 +02:00
2017-05-29 12:43:28 +02:00
Auth :: logout ();
2015-03-16 22:45:25 +01:00
}
2017-06-26 11:45:42 +02:00
}
private function billInvoices ()
{
$today = new DateTime ();
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-12-05 09:11:33 +01:00
-> whereRaw ( ' is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE AND is_public IS TRUE
2016-05-26 21:22:09 +02:00
AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL ' ,
2016-07-03 18:11:58 +02:00
[ $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
2016-07-03 18:11:58 +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 ()) {
2017-06-06 09:58:49 +02:00
$this -> info ( 'Processing Autobill-delayed Invoice: ' . $invoice -> id );
2017-05-29 12:43:28 +02:00
Auth :: loginUsingId ( $invoice -> user_id );
2016-05-25 04:49:06 +02:00
$this -> paymentService -> autoBillInvoice ( $invoice );
2017-05-29 12:43:28 +02:00
Auth :: logout ();
2016-05-25 04:49:06 +02:00
}
}
2017-06-26 11:45:42 +02:00
}
2016-05-25 04:49:06 +02:00
2017-06-26 11:45:42 +02:00
private function createExpenses ()
{
$today = new DateTime ();
2017-06-26 14:57:40 +02:00
$expenses = RecurringExpense :: with ( 'client' )
2017-06-26 11:45:42 +02: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 ();
$this -> info ( count ( $expenses ) . ' recurring expenses(s) found' );
foreach ( $expenses as $expense ) {
$shouldSendToday = $expense -> shouldSendToday ();
if ( ! $shouldSendToday ) {
continue ;
}
$this -> info ( 'Processing Expense: ' . $expense -> id );
$this -> recurringExpenseRepo -> createRecurringExpense ( $expense );
}
2015-03-16 22:45:25 +01:00
}
2016-07-03 18:11:58 +02:00
/**
* @ return array
*/
2015-03-16 22:45:25 +01:00
protected function getArguments ()
{
2016-07-03 18:11:58 +02:00
return [];
2015-03-16 22:45:25 +01:00
}
2016-07-03 18:11:58 +02:00
/**
* @ return array
*/
2015-03-16 22:45:25 +01:00
protected function getOptions ()
{
2017-05-01 14:17:52 +02:00
return [
[ 'database' , null , InputOption :: VALUE_OPTIONAL , 'Database' , null ],
];
2015-03-16 22:45:25 +01:00
}
}