From 1c119fe35a955fbaa451944ded02c1ee427d7e38 Mon Sep 17 00:00:00 2001 From: Joshua Dwire Date: Thu, 26 May 2016 15:22:09 -0400 Subject: [PATCH] ACH autobill bug fixes --- app/Console/Commands/SendRecurringInvoices.php | 11 +++++------ app/Models/PaymentMethod.php | 2 +- app/Ninja/Repositories/InvoiceRepository.php | 1 + app/Services/PaymentService.php | 17 ++++++++++------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index d07645bf9bc2..574d324d3842 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -71,14 +71,12 @@ class SendRecurringInvoices extends Command } $delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user') - ->leftJoin('invoices as recurring_invoice', 'invoices.recurring_invoice_id', '=', 'recurring_invoice.id') - ->whereRaw('invoices.is_deleted IS FALSE AND invoices.deleted_at IS NULL AND invoices.is_recurring IS FALSE - AND invoices.balance > 0 AND invoices.due_date = ? - AND (recurring_invoice.auto_bill = ? OR (recurring_invoice.auto_bill != ? AND recurring_invoice.client_enable_auto_bill IS TRUE))', - array($today->format('Y-m-d'), AUTO_BILL_ALWAYS, AUTO_BILL_OFF)) + ->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'))) ->orderBy('invoices.id', 'asc') ->get(); - $this->info(count($delayedAutoBillInvoices).' due recurring auto bill invoice instance(s) found'); + $this->info(count($delayedAutoBillInvoices).' due recurring invoice instance(s) found'); foreach ($delayedAutoBillInvoices as $invoice) { $autoBill = $invoice->getAutoBillEnabled(); @@ -91,6 +89,7 @@ class SendRecurringInvoices extends Command $this->info('Processing Invoice '.$invoice->id.' - Should bill '.($billNow ? 'YES' : 'NO')); if ($billNow) { + // autoBillInvoice will check for changes to ACH invoices, so we're not checking here $this->paymentService->autoBillInvoice($invoice); } } diff --git a/app/Models/PaymentMethod.php b/app/Models/PaymentMethod.php index c18d9d2b8bae..8c3ea8b2dfd8 100644 --- a/app/Models/PaymentMethod.php +++ b/app/Models/PaymentMethod.php @@ -160,7 +160,7 @@ class PaymentMethod extends EntityModel } public function requiresDelayedAutoBill(){ - return $this->payment_type_id == PAYMENT_TYPE_DIRECT_DEBIT; + return $this->payment_type_id == PAYMENT_TYPE_ACH; } } diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 6bbd4f9fa639..d007971e777d 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -816,6 +816,7 @@ class InvoiceRepository extends BaseRepository $recurInvoice->save(); if ($recurInvoice->getAutoBillEnabled() && !$recurInvoice->account->auto_bill_on_due_date) { + // autoBillInvoice will check for ACH, so we're not checking here if ($this->paymentService->autoBillInvoice($invoice)) { // update the invoice reference to match its actual state // this is to ensure a 'payment received' email is sent diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 77f489049f68..efa27d945b7a 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -16,6 +16,7 @@ use App\Models\Account; use App\Models\Country; use App\Models\Client; use App\Models\Invoice; +use App\Models\Activity; use App\Models\AccountGateway; use App\Http\Controllers\PaymentController; use App\Models\AccountGatewayToken; @@ -872,16 +873,21 @@ class PaymentService extends BaseService } if ($defaultPaymentMethod->requiresDelayedAutoBill()) { - $invoiceDate = DateTime::createFromFormat('Y-m-d', $invoice_date); + $invoiceDate = \DateTime::createFromFormat('Y-m-d', $invoice->invoice_date); $minDueDate = clone $invoiceDate; $minDueDate->modify('+10 days'); - if (DateTime::create() < $minDueDate) { + if (date_create() < $minDueDate) { // Can't auto bill now return false; } - $firstUpdate = \App\Models\Activities::where('invoice_id', '=', $invoice->id) + if ($invoice->partial > 0) { + // The amount would be different than the amount in the email + return false; + } + + $firstUpdate = Activity::where('invoice_id', '=', $invoice->id) ->where('activity_type_id', '=', ACTIVITY_TYPE_UPDATE_INVOICE) ->first(); @@ -894,10 +900,7 @@ class PaymentService extends BaseService } } - $invoicePayments = \App\Models\Activities::where('invoice_id', '=', $invoice->id) - ->where('activity_type_id', '=', ACTIVITY_TYPE_CREATE_PAYMENT); - - if ($invoicePayments->count()) { + if ($invoice->payments->count()) { // ACH requirements are strict; don't auto bill this return false; }