2015-03-17 07:45:25 +10:00
< ? php namespace App\Console\Commands ;
2015-04-12 17:18:05 +03:00
use DateTime ;
use Carbon ;
use Utils ;
2015-03-17 07:45:25 +10:00
use Illuminate\Console\Command ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Input\InputArgument ;
2015-03-24 18:21:12 +10:00
use App\Ninja\Mailers\ContactMailer as Mailer ;
2015-08-10 18:48:41 +03:00
use App\Ninja\Repositories\InvoiceRepository ;
2016-05-24 22:49:06 -04:00
use App\Services\PaymentService ;
2015-04-12 17:18:05 +03:00
use App\Models\Invoice ;
use App\Models\InvoiceItem ;
use App\Models\Invitation ;
2015-03-17 07:45:25 +10:00
class SendRecurringInvoices extends Command
{
protected $name = 'ninja:send-invoices' ;
protected $description = 'Send recurring invoices' ;
protected $mailer ;
2015-08-10 18:48:41 +03:00
protected $invoiceRepo ;
2016-05-24 22:49:06 -04:00
protected $paymentService ;
2015-03-17 07:45:25 +10:00
2016-05-24 22:49:06 -04:00
public function __construct ( Mailer $mailer , InvoiceRepository $invoiceRepo , PaymentService $paymentService )
2015-03-17 07:45:25 +10:00
{
parent :: __construct ();
$this -> mailer = $mailer ;
2015-08-10 18:48:41 +03:00
$this -> invoiceRepo = $invoiceRepo ;
2016-05-24 22:49:06 -04:00
$this -> paymentService = $paymentService ;
2015-03-17 07:45:25 +10:00
}
public function fire ()
{
$this -> info ( date ( 'Y-m-d' ) . ' Running SendRecurringInvoices...' );
$today = new DateTime ();
$invoices = Invoice :: with ( 'account.timezone' , 'invoice_items' , 'client' , 'user' )
2015-10-20 11:23:38 +03:00
-> 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 >= ?)' , array ( $today , $today ))
-> orderBy ( 'id' , 'asc' )
-> get ();
2015-03-17 07:45:25 +10:00
$this -> info ( count ( $invoices ) . ' recurring invoice(s) found' );
foreach ( $invoices as $recurInvoice ) {
2015-10-20 11:23:38 +03:00
if ( ! $recurInvoice -> user -> confirmed ) {
continue ;
}
2015-10-20 20:12:34 +03:00
2015-10-14 17:15:39 +03:00
$recurInvoice -> account -> loadLocalizationSettings ( $recurInvoice -> client );
2015-09-17 22:01:06 +03:00
$this -> info ( 'Processing Invoice ' . $recurInvoice -> id . ' - Should send ' . ( $recurInvoice -> shouldSendToday () ? 'YES' : 'NO' ));
2015-08-11 17:38:36 +03:00
$invoice = $this -> invoiceRepo -> createRecurringInvoice ( $recurInvoice );
2015-09-10 20:50:09 +03:00
if ( $invoice && ! $invoice -> isPaid ()) {
2016-05-24 23:14:19 -04:00
$invoice -> account -> auto_bill_on_due_date ;
$autoBillLater = false ;
2016-05-25 11:07:20 -04:00
if ( $invoice -> account -> auto_bill_on_due_date || $this -> paymentService -> getClientRequiresDelayedAutoBill ( $invoice -> client )) {
2016-05-24 23:14:19 -04:00
$autoBillLater = true ;
}
if ( $autoBillLater ) {
2016-05-25 11:07:20 -04:00
if ( $paymentMethod = $this -> paymentService -> getClientDefaultPaymentMethod ( $invoice -> client )) {
2016-05-24 23:14:19 -04:00
$invoice -> autoBillPaymentMethod = $paymentMethod ;
}
}
2015-10-20 11:23:38 +03:00
$this -> info ( 'Sending Invoice' );
2015-08-11 17:38:36 +03:00
$this -> mailer -> sendInvoice ( $invoice );
}
2015-03-17 07:45:25 +10:00
}
2016-05-24 22:49:06 -04:00
$delayedAutoBillInvoices = Invoice :: with ( 'account.timezone' , 'recurring_invoice' , 'invoice_items' , 'client' , 'user' )
2016-05-26 15:22:09 -04:00
-> 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 ' ,
array ( $today -> format ( 'Y-m-d' )))
2016-05-24 22:49:06 -04:00
-> orderBy ( 'invoices.id' , 'asc' )
-> get ();
2016-05-26 15:22:09 -04:00
$this -> info ( count ( $delayedAutoBillInvoices ) . ' due recurring invoice instance(s) found' );
2016-05-24 22:49:06 -04:00
foreach ( $delayedAutoBillInvoices as $invoice ) {
$autoBill = $invoice -> getAutoBillEnabled ();
$billNow = false ;
if ( $autoBill && ! $invoice -> isPaid ()) {
2016-05-25 11:07:20 -04:00
$billNow = $invoice -> account -> auto_bill_on_due_date || $this -> paymentService -> getClientRequiresDelayedAutoBill ( $invoice -> client );
2016-05-24 22:49:06 -04:00
}
$this -> info ( 'Processing Invoice ' . $invoice -> id . ' - Should bill ' . ( $billNow ? 'YES' : 'NO' ));
if ( $billNow ) {
2016-05-26 15:22:09 -04:00
// autoBillInvoice will check for changes to ACH invoices, so we're not checking here
2016-05-24 22:49:06 -04:00
$this -> paymentService -> autoBillInvoice ( $invoice );
}
}
2015-03-17 07:45:25 +10:00
$this -> info ( 'Done' );
}
protected function getArguments ()
{
return array (
//array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
protected function getOptions ()
{
return array (
//array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}