From 20f6f32a068099ff29f2d39c10b31b2527c6612d Mon Sep 17 00:00:00 2001 From: Joshua Dwire Date: Mon, 9 May 2016 16:29:02 -0400 Subject: [PATCH] Update UI for configuring auto bill --- .../Controllers/PublicClientController.php | 4 +- app/Http/routes.php | 1 + app/Models/Client.php | 4 +- app/Models/Invoice.php | 2 +- app/Ninja/Repositories/InvoiceRepository.php | 23 +++++------ .../2016_04_23_182223_payments_changes.php | 28 ++++++++++---- resources/lang/en/texts.php | 5 ++- resources/views/invoices/edit.blade.php | 38 ++++++++++--------- resources/views/invoices/knockout.blade.php | 4 +- resources/views/public_list.blade.php | 2 +- 10 files changed, 63 insertions(+), 48 deletions(-) diff --git a/app/Http/Controllers/PublicClientController.php b/app/Http/Controllers/PublicClientController.php index 6f43314ad3d5..c5b22ee16204 100644 --- a/app/Http/Controllers/PublicClientController.php +++ b/app/Http/Controllers/PublicClientController.php @@ -963,8 +963,8 @@ class PublicClientController extends BaseController $enable = Input::get('enable'); $invoice = $client->invoices->where('public_id', intval($publicId))->first(); - if ($invoice && $invoice->is_recurring && $invoice->enable_auto_bill > AUTO_BILL_OFF) { - $invoice->auto_bill = $enable ? true : false; + if ($invoice && $invoice->is_recurring && ($invoice->auto_bill == AUTO_BILL_OPT_IN || $invoice->auto_bill == AUTO_BILL_OPT_OUT)) { + $invoice->client_enable_auto_bill = $enable ? true : false; $invoice->save(); } diff --git a/app/Http/routes.php b/app/Http/routes.php index 09d53a76b50c..a3bf59d22289 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -698,6 +698,7 @@ if (!defined('CONTACT_EMAIL')) { define('AUTO_BILL_OFF', 0); define('AUTO_BILL_OPT_IN', 1); define('AUTO_BILL_OPT_OUT', 2); + define('AUTO_BILL_ALWAYS', 3); // These must be lowercase define('PLAN_FREE', 'free'); diff --git a/app/Models/Client.php b/app/Models/Client.php index a60d44793c22..19a7968bf965 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -320,8 +320,8 @@ class Client extends EntityModel $this->save(); } - public function hasAutoBillInvoices(){ - return $this->invoices()->where('auto_bill', 1)->count() > 0; + public function hasAutoBillConfigurableInvoices(){ + return $this->invoices()->whereIn('auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT])->count() > 0; } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index c0da6b8faff6..33ce1bf8f648 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -35,7 +35,7 @@ class Invoice extends EntityModel implements BalanceAffecting protected $casts = [ 'is_recurring' => 'boolean', 'has_tasks' => 'boolean', - 'auto_bill' => 'boolean', + 'client_enable_auto_bill' => 'boolean', 'has_expenses' => 'boolean', ]; diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index ac7af064f71a..f0bc159191e5 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -160,7 +160,7 @@ class InvoiceRepository extends BaseRepository ->where('invoices.is_deleted', '=', false) ->where('clients.deleted_at', '=', null) ->where('invoices.is_recurring', '=', true) - ->where('invoices.enable_auto_bill', '>', AUTO_BILL_OFF) + ->whereIn('invoices.auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT]) //->where('invoices.start_date', '>=', date('Y-m-d H:i:s')) ->select( DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'), @@ -174,7 +174,7 @@ class InvoiceRepository extends BaseRepository 'invoices.amount', 'invoices.start_date', 'invoices.end_date', - 'invoices.auto_bill', + 'invoices.client_enable_auto_bill', 'frequencies.name as frequency' ); @@ -183,8 +183,8 @@ class InvoiceRepository extends BaseRepository ->addColumn('start_date', function ($model) { return Utils::fromSqlDate($model->start_date); }) ->addColumn('end_date', function ($model) { return Utils::fromSqlDate($model->end_date); }) ->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id); }) - ->addColumn('auto_bill', function ($model) { - if ($model->auto_bill) { + ->addColumn('client_enable_auto_bill', function ($model) { + if ($model->client_enable_auto_bill) { return trans('texts.enabled') . ' ('.trans('texts.disable').')'; } else { return trans('texts.disabled') . ' ('.trans('texts.enable').')'; @@ -317,17 +317,12 @@ class InvoiceRepository extends BaseRepository $invoice->frequency_id = $data['frequency_id'] ? $data['frequency_id'] : 0; $invoice->start_date = Utils::toSqlDate($data['start_date']); $invoice->end_date = Utils::toSqlDate($data['end_date']); - $invoice->auto_bill = isset($data['auto_bill']) && $data['auto_bill'] ? true : false; - $invoice->enable_auto_bill = isset($data['enable_auto_bill']) ? intval($data['enable_auto_bill']) : 0; + $invoice->client_enable_auto_bill = isset($data['client_enable_auto_bill']) && $data['client_enable_auto_bill'] ? true : false; + $invoice->auto_bill = isset($data['auto_bill']) ? intval($data['auto_bill']) : 0; - if ($invoice->enable_auto_bill != AUTO_BILL_OPT_IN && $invoice->enable_auto_bill != AUTO_BILL_OPT_OUT ) { - // Auto-bill is not enabled - $invoice->enable_auto_bill = AUTO_BILL_OFF; - $invoice->auto_bill = false; - } elseif ($isNew) { - $invoice->auto_bill = $invoice->enable_auto_bill == AUTO_BILL_OPT_OUT; + if ($invoice->auto_bill < AUTO_BILL_OFF || $invoice->auto_bill > AUTO_BILL_ALWAYS ) { + $invoice->auto_bill = AUTO_BILL_OFF; } - if (isset($data['recurring_due_date'])) { $invoice->due_date = $data['recurring_due_date']; @@ -811,7 +806,7 @@ class InvoiceRepository extends BaseRepository $recurInvoice->last_sent_date = date('Y-m-d'); $recurInvoice->save(); - if ($recurInvoice->auto_bill) { + if ($recurInvoice->auto_bill == AUTO_BILL_ALWAYS || ($recurInvoice->auto_bill != AUTO_BILL_OFF && $recurInvoice->client_enable_auto_bill)) { 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/database/migrations/2016_04_23_182223_payments_changes.php b/database/migrations/2016_04_23_182223_payments_changes.php index fea4e739775e..2e6cdcb6ec05 100644 --- a/database/migrations/2016_04_23_182223_payments_changes.php +++ b/database/migrations/2016_04_23_182223_payments_changes.php @@ -13,21 +13,21 @@ class PaymentsChanges extends Migration public function up() { Schema::dropIfExists('payment_statuses'); - + Schema::create('payment_statuses', function($table) { $table->increments('id'); $table->string('name'); }); - + (new \PaymentStatusSeeder())->run(); - + Schema::table('payments', function($table) { $table->decimal('refunded', 13, 2); $table->unsignedInteger('payment_status_id')->default(PAYMENT_STATUS_COMPLETED); $table->foreign('payment_status_id')->references('id')->on('payment_statuses'); - + $table->unsignedInteger('routing_number')->nullable(); $table->smallInteger('last4')->unsigned()->nullable(); $table->date('expiration')->nullable(); @@ -37,12 +37,12 @@ class PaymentsChanges extends Migration Schema::table('invoices', function($table) { - $table->tinyInteger('enable_auto_bill')->default(AUTO_BILL_OFF); + $table->boolean('client_enable_auto_bill')->default(false); }); \DB::table('invoices') ->where('auto_bill', '=', 1) - ->update(array('enable_auto_bill' => AUTO_BILL_OPT_OUT)); + ->update(array('client_enable_auto_bill' => 1, 'auto_bill' => AUTO_BILL_OPT_OUT)); } /** @@ -65,8 +65,22 @@ class PaymentsChanges extends Migration $table->dropColumn('email'); }); + \DB::table('invoices') + ->where(function($query){ + $query->where('auto_bill', '=', AUTO_BILL_ALWAYS); + $query->orwhere(function($query){ + $query->where('auto_bill', '!=', AUTO_BILL_OFF); + $query->where('client_enable_auto_bill', '=', 1); + }); + }) + ->update(array('auto_bill' => 1)); + + \DB::table('invoices') + ->where('auto_bill', '!=', 1) + ->update(array('auto_bill' => 0)); + Schema::table('invoices', function ($table) { - $table->dropColumn('enable_auto_bill'); + $table->dropColumn('client_enable_auto_bill'); }); Schema::dropIfExists('payment_statuses'); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 70f585bc0f5c..163538ed4f88 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1272,8 +1272,9 @@ $LANG = array( 'off' => 'Off', 'opt_in' => 'Opt-in', 'opt_out' => 'Opt-out', - 'enabled_by_client' => 'Enabled by client', - 'disabled_by_client' => 'Disabled by client', + 'always' => 'Always', + 'opted_out' => 'Opted out', + 'opted_in' => 'Opted in', 'manage_auto_bill' => 'Manage Auto-bill', 'enabled' => 'Enabled', 'paypal' => 'PayPal', diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index d69f4c201201..b99d11d9d955 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -159,23 +159,27 @@ @if($account->getTokenGatewayId()) - {!! Former::radios('enable_auto_bill')->radios([ - trans('texts.off') => array('name' => 'enable_auto_bill', 'value' => 0, 'data-bind' => "checked: enable_auto_bill, valueUpdate: 'afterkeydown', checkedValue:0"), - trans('texts.opt_in') => array('name' => 'enable_auto_bill', 'value' => 1, 'data-bind' => "checked: enable_auto_bill, valueUpdate: 'afterkeydown', checkedValue:1"), - trans('texts.opt_out') => array('name' => 'enable_auto_bill', 'value' => 2, 'data-bind' => "checked: enable_auto_bill, valueUpdate: 'afterkeydown', checkedValue:2"), - ])->inline() - ->label(trans('texts.auto_bill')) !!} - -
-
-
-
- {{trans('texts.enabled_by_client')}} ({{trans('texts.disable')}}) -
-
- {{trans('texts.disabled_by_client')}} ({{trans('texts.enable')}}) -
-
+
+ {!! Former::select('auto_bill') + ->data_bind("value: auto_bill, valueUpdate: 'afterkeydown', event:{change:function(){if(auto_bill()==1)client_enable_auto_bill(0);if(auto_bill()==2)client_enable_auto_bill(1)}}") + ->options([ + 0 => trans('texts.off'), + 1 => trans('texts.opt_in'), + 2 => trans('texts.opt_out'), + 3 => trans('texts.always'), + ]) !!} +
+ +
+
{{trans('texts.auto_bill')}}
+
+ {{trans('texts.opted_in')}} - ({{trans('texts.disable')}}) +
+
+
+
{{trans('texts.auto_bill')}}
+
+ {{trans('texts.opted_out')}} - ({{trans('texts.enable')}})
diff --git a/resources/views/invoices/knockout.blade.php b/resources/views/invoices/knockout.blade.php index 5b39703105ca..c7730eae2d22 100644 --- a/resources/views/invoices/knockout.blade.php +++ b/resources/views/invoices/knockout.blade.php @@ -188,8 +188,8 @@ function InvoiceModel(data) { self.tax_rate2 = ko.observable(); self.is_recurring = ko.observable(0); self.is_quote = ko.observable({{ $entityType == ENTITY_QUOTE ? '1' : '0' }}); - self.auto_bill = ko.observable(false); - self.enable_auto_bill = ko.observable(0); + self.auto_bill = ko.observable(0); + self.client_enable_auto_bill = ko.observable(false); self.invoice_status_id = ko.observable(0); self.invoice_items = ko.observableArray(); self.documents = ko.observableArray(); diff --git a/resources/views/public_list.blade.php b/resources/views/public_list.blade.php index d6ee070b50e3..1a5a17171b08 100644 --- a/resources/views/public_list.blade.php +++ b/resources/views/public_list.blade.php @@ -35,7 +35,7 @@
--> - @if($entityType == ENTITY_INVOICE && $account->getTokenGatewayId() && $client->hasAutoBillInvoices()) + @if($entityType == ENTITY_INVOICE && $account->getTokenGatewayId() && $client->hasAutoBillConfigurableInvoices())
{!! Button::info(trans("texts.manage_auto_bill"))->asLinkTo(URL::to('/client/invoices/recurring'))->appendIcon(Icon::create('repeat')) !!}