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-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 ;
2018-03-27 14:59:48 +02:00
use App\Jobs\SendInvoiceEmail ;
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 InvoiceRepository
*/
2015-08-10 17:48:41 +02:00
protected $invoiceRepo ;
2016-07-03 18:11:58 +02:00
/**
* SendRecurringInvoices constructor .
2017-01-30 20:40:43 +01:00
*
2016-07-03 18:11:58 +02:00
* @ param InvoiceRepository $invoiceRepo
*/
2018-09-23 08:08:48 +02:00
public function __construct ( InvoiceRepository $invoiceRepo , RecurringExpenseRepository $recurringExpenseRepo )
2015-03-16 22:45:25 +01:00
{
parent :: __construct ();
2015-08-10 17:48:41 +02:00
$this -> invoiceRepo = $invoiceRepo ;
2017-06-26 14:57:40 +02:00
$this -> recurringExpenseRepo = $recurringExpenseRepo ;
2015-03-16 22:45:25 +01:00
}
2019-09-13 01:30:10 +02:00
public function handle ()
2015-03-16 22:45:25 +01:00
{
2017-10-24 09:59:26 +02:00
$this -> info ( date ( 'r' ) . ' 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 -> createExpenses ();
2017-10-24 09:59:26 +02:00
$this -> info ( date ( 'r' ) . ' Done' );
2017-06-26 11:45:42 +02:00
}
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' )
2020-11-01 16:34:29 +01:00
-> cursor ();
$this -> info ( date ( 'r ' ) . ' Recurring invoice(s) found' );
2015-03-16 22:45:25 +01:00
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
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r' ) . ' Processing Invoice: ' . $recurInvoice -> id );
2017-06-06 09:58:49 +02:00
2017-03-23 10:39:44 +01:00
$account = $recurInvoice -> account ;
$account -> loadLocalizationSettings ( $recurInvoice -> client );
2018-01-16 13:57:37 +01:00
Auth :: loginUsingId ( $recurInvoice -> activeUser () -> id );
2015-08-11 16:38:36 +02:00
2017-07-12 11:37:19 +02:00
try {
$invoice = $this -> invoiceRepo -> createRecurringInvoice ( $recurInvoice );
2018-03-13 21:05:57 +01:00
if ( $invoice && ! $invoice -> isPaid () && $account -> auto_email_invoice ) {
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r' ) . ' Not billed - Sending Invoice' );
2018-03-27 14:59:48 +02:00
dispatch ( new SendInvoiceEmail ( $invoice , $invoice -> user_id ));
2017-12-03 15:40:02 +01:00
} elseif ( $invoice ) {
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r' ) . ' Successfully billed invoice' );
2017-07-12 11:37:19 +02:00
}
} catch ( Exception $exception ) {
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r' ) . ' Error: ' . $exception -> getMessage ());
2017-07-12 11:37:19 +02:00
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 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 ();
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r ' ) . $expenses -> count () . ' recurring expenses(s) found' );
2017-06-26 11:45:42 +02:00
foreach ( $expenses as $expense ) {
$shouldSendToday = $expense -> shouldSendToday ();
if ( ! $shouldSendToday ) {
continue ;
}
2018-04-18 21:56:55 +02:00
$this -> info ( date ( 'r' ) . ' Processing Expense: ' . $expense -> id );
2017-06-26 11:45:42 +02:00
$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
}
}