From d19a3715f95ff8a2b1e67fa6bc77437a163b4810 Mon Sep 17 00:00:00 2001 From: Joshua Dwire Date: Wed, 25 May 2016 11:07:20 -0400 Subject: [PATCH] Warn user when editing an ACH auto bill invoice --- .../Commands/SendRecurringInvoices.php | 22 +++---------------- app/Http/Controllers/InvoiceController.php | 9 +++++++- app/Services/PaymentService.php | 6 +++++ resources/lang/en/texts.php | 1 + resources/views/invoices/edit.blade.php | 8 +++++++ 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index 2362184e8ccd..d07645bf9bc2 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -54,20 +54,12 @@ class SendRecurringInvoices extends Command $invoice->account->auto_bill_on_due_date; $autoBillLater = false; - if ($invoice->account->auto_bill_on_due_date) { + if ($invoice->account->auto_bill_on_due_date || $this->paymentService->getClientRequiresDelayedAutoBill($invoice->client)) { $autoBillLater = true; - } elseif ($paymentMethod = $this->paymentService->getClientDefaultPaymentMethod($invoice->client)) { - if ($paymentMethod && $paymentMethod->requiresDelayedAutoBill()) { - $autoBillLater = true; - } } if($autoBillLater) { - if (empty($paymentMethod)) { - $paymentMethod = $this->paymentService->getClientDefaultPaymentMethod($invoice->client); - } - - if($paymentMethod) { + if($paymentMethod = $this->paymentService->getClientDefaultPaymentMethod($invoice->client)) { $invoice->autoBillPaymentMethod = $paymentMethod; } } @@ -93,15 +85,7 @@ class SendRecurringInvoices extends Command $billNow = false; if ($autoBill && !$invoice->isPaid()) { - $billNow = $invoice->account->auto_bill_on_due_date; - - if (!$billNow) { - $paymentMethod = $this->paymentService->getClientDefaultPaymentMethod($invoice->client); - - if ($paymentMethod && $paymentMethod->requiresDelayedAutoBill()) { - $billNow = true; - } - } + $billNow = $invoice->account->auto_bill_on_due_date || $this->paymentService->getClientRequiresDelayedAutoBill($invoice->client); } $this->info('Processing Invoice '.$invoice->id.' - Should bill '.($billNow ? 'YES' : 'NO')); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 9a0dadf494c4..9fb049d3a47d 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -26,6 +26,7 @@ use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\DocumentRepository; use App\Services\InvoiceService; +use App\Services\PaymentService; use App\Services\RecurringInvoiceService; use App\Http\Requests\InvoiceRequest; @@ -39,10 +40,11 @@ class InvoiceController extends BaseController protected $clientRepo; protected $documentRepo; protected $invoiceService; + protected $paymentService; protected $recurringInvoiceService; protected $entityType = ENTITY_INVOICE; - public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService) + public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, InvoiceService $invoiceService, DocumentRepository $documentRepo, RecurringInvoiceService $recurringInvoiceService, PaymentService $paymentService) { // parent::__construct(); @@ -51,6 +53,7 @@ class InvoiceController extends BaseController $this->clientRepo = $clientRepo; $this->invoiceService = $invoiceService; $this->recurringInvoiceService = $recurringInvoiceService; + $this->paymentService = $paymentService; } public function index() @@ -196,6 +199,10 @@ class InvoiceController extends BaseController 'lastSent' => $lastSent); $data = array_merge($data, self::getViewModel($invoice)); + if ($invoice->isSent() && $invoice->getAutoBillEnabled() && !$invoice->isPaid()) { + $data['autoBillChangeWarning'] = $this->paymentService->getClientRequiresDelayedAutoBill($invoice->client); + } + if ($clone) { $data['formIsChanged'] = true; } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 40feac81f157..0f1ede8b3fa8 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -903,6 +903,12 @@ class PaymentService extends BaseService return $accountGatewayToken->default_payment_method; } + public function getClientRequiresDelayedAutoBill($client) { + $defaultPaymentMethod = $this->getClientDefaultPaymentMethod($client); + + return $defaultPaymentMethod?$defaultPaymentMethod->requiresDelayedAutoBill():null; + } + public function getDatatable($clientPublicId, $search) { $datatable = new PaymentDatatable( ! $clientPublicId, $clientPublicId); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 7d491f57d62a..307c37c1a152 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1325,6 +1325,7 @@ $LANG = array( 'auto_bill_on_due_date' => 'Auto bill on due date instead of send date', 'auto_bill_ach_date_help' => 'ACH auto bill will always happen on the due date', + 'warn_change_auto_bill' => 'Due to NACHA rules, changes to this invoice may prevent ACH auto bill.', ); return $LANG; diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 3ca6acafba59..630bd733aed7 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -1200,6 +1200,10 @@ } function onSaveClick() { + @if(!empty($autoBillChangeWarning)) + if(!confirm("{!! trans('texts.warn_change_auto_bill') !!}"))return; + @endif + if (model.invoice().is_recurring()) { // warn invoice will be emailed when saving new recurring invoice if ({{ $invoice->exists() ? 'false' : 'true' }}) { @@ -1324,6 +1328,10 @@ @if ($invoice->id) function onPaymentClick() { + @if(!empty($autoBillChangeWarning)) + if(!confirm("{!! trans('texts.warn_change_auto_bill') !!}"))return; + @endif + window.location = '{{ URL::to('payments/create/' . $invoice->client->public_id . '/' . $invoice->public_id ) }}'; }