From 78a2a667292fe08dbc4ded9eb103322e52de1773 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 5 Dec 2016 10:11:33 +0200 Subject: [PATCH] Hide draft invoices from the client portal --- .../Commands/SendRecurringInvoices.php | 4 +-- app/Http/Controllers/InvoiceController.php | 2 +- app/Http/Controllers/PaymentController.php | 13 ++++---- app/Http/Requests/CreatePaymentAPIRequest.php | 1 + app/Http/Requests/CreatePaymentRequest.php | 1 + .../ClientPortalHeaderComposer.php | 2 +- app/Models/Client.php | 8 +++++ app/Ninja/Repositories/AccountRepository.php | 1 + app/Ninja/Repositories/InvoiceRepository.php | 6 ++++ resources/views/invoices/edit.blade.php | 30 +++++++++++-------- 10 files changed, 46 insertions(+), 22 deletions(-) diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index 89e08bf582ba..3a546f187edf 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -58,7 +58,7 @@ class SendRecurringInvoices extends Command $today = new DateTime(); $invoices = Invoice::with('account.timezone', 'invoice_items', 'client', 'user') - ->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 >= ?)', [$today, $today]) + ->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]) ->orderBy('id', 'asc') ->get(); $this->info(count($invoices).' recurring invoice(s) found'); @@ -81,7 +81,7 @@ class SendRecurringInvoices extends Command } $delayedAutoBillInvoices = Invoice::with('account.timezone', 'recurring_invoice', 'invoice_items', 'client', 'user') - ->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE + ->whereRaw('is_deleted IS FALSE AND deleted_at IS NULL AND is_recurring IS FALSE AND is_public IS TRUE AND balance > 0 AND due_date = ? AND recurring_invoice_id IS NOT NULL', [$today->format('Y-m-d')]) ->orderBy('invoices.id', 'asc') diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 7e900bb53b81..512f7774e270 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -141,7 +141,7 @@ class InvoiceController extends BaseController $actions[] = ['url' => URL::to("quotes/{$invoice->quote_id}/edit"), 'label' => trans('texts.view_quote')]; } - if (!$invoice->is_recurring && $invoice->balance > 0) { + if (!$invoice->is_recurring && $invoice->balance > 0 && $invoice->is_public) { $actions[] = ['url' => 'javascript:onPaymentClick()', 'label' => trans('texts.enter_payment')]; } diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 5b38a2f37aff..2b38ba25d76b 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -83,8 +83,8 @@ class PaymentController extends BaseController public function create(PaymentRequest $request) { $invoices = Invoice::scope() - ->invoiceType(INVOICE_TYPE_STANDARD) - ->where('is_recurring', '=', false) + ->invoices() + ->whereIsPublic(true) ->where('invoices.balance', '>', 0) ->with('client', 'invoice_status') ->orderBy('invoice_number')->get(); @@ -128,8 +128,11 @@ class PaymentController extends BaseController $data = [ 'client' => null, 'invoice' => null, - 'invoices' => Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->where('is_recurring', '=', false) - ->with('client', 'invoice_status')->orderBy('invoice_number')->get(), + 'invoices' => Invoice::scope() + ->invoices() + ->whereIsPublic(true) + ->with('client', 'invoice_status') + ->orderBy('invoice_number')->get(), 'payment' => $payment, 'entity' => $payment, 'method' => 'PUT', @@ -161,7 +164,7 @@ class PaymentController extends BaseController Session::flash('message', trans('texts.created_payment')); } - return redirect()->to($payment->client->getRoute()); + return redirect()->to($payment->client->getRoute() . '#payments'); } /** diff --git a/app/Http/Requests/CreatePaymentAPIRequest.php b/app/Http/Requests/CreatePaymentAPIRequest.php index 2151db745a28..115dc11f822c 100644 --- a/app/Http/Requests/CreatePaymentAPIRequest.php +++ b/app/Http/Requests/CreatePaymentAPIRequest.php @@ -30,6 +30,7 @@ class CreatePaymentAPIRequest extends PaymentRequest $invoice = Invoice::scope($this->invoice_id) ->invoices() + ->whereIsPublic(true) ->firstOrFail(); $this->merge([ diff --git a/app/Http/Requests/CreatePaymentRequest.php b/app/Http/Requests/CreatePaymentRequest.php index 745f0afa3ba4..ff30f0da9d8e 100644 --- a/app/Http/Requests/CreatePaymentRequest.php +++ b/app/Http/Requests/CreatePaymentRequest.php @@ -24,6 +24,7 @@ class CreatePaymentRequest extends PaymentRequest $input = $this->input(); $invoice = Invoice::scope($input['invoice']) ->invoices() + ->whereIsPublic(true) ->firstOrFail(); $rules = [ diff --git a/app/Http/ViewComposers/ClientPortalHeaderComposer.php b/app/Http/ViewComposers/ClientPortalHeaderComposer.php index ac0d6ca68618..7525b80bf699 100644 --- a/app/Http/ViewComposers/ClientPortalHeaderComposer.php +++ b/app/Http/ViewComposers/ClientPortalHeaderComposer.php @@ -45,7 +45,7 @@ class ClientPortalHeaderComposer ->join('documents', 'documents.invoice_id', '=', 'invoices.id') ->count(); - $view->with('hasQuotes', $client->quotes->count()); + $view->with('hasQuotes', $client->publicQuotes->count()); $view->with('hasCredits', $client->creditsWithBalance->count()); $view->with('hasDocuments', $hasDocuments); } diff --git a/app/Models/Client.php b/app/Models/Client.php index a1db398ef58b..02da4dc912a5 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -173,6 +173,14 @@ class Client extends EntityModel return $this->hasMany('App\Models\Invoice')->where('invoice_type_id', '=', INVOICE_TYPE_QUOTE); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function publicQuotes() + { + return $this->hasMany('App\Models\Invoice')->where('invoice_type_id', '=', INVOICE_TYPE_QUOTE)->whereIsPublic(true); + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ diff --git a/app/Ninja/Repositories/AccountRepository.php b/app/Ninja/Repositories/AccountRepository.php index 04342ddca229..25818db30d8d 100644 --- a/app/Ninja/Repositories/AccountRepository.php +++ b/app/Ninja/Repositories/AccountRepository.php @@ -280,6 +280,7 @@ class AccountRepository $lastInvoice = Invoice::withTrashed()->whereAccountId($account->id)->orderBy('public_id', 'DESC')->first(); $publicId = $lastInvoice ? ($lastInvoice->public_id + 1) : 1; $invoice = new Invoice(); + $invoice->is_public = true; $invoice->account_id = $account->id; $invoice->user_id = $account->users()->first()->id; $invoice->public_id = $publicId; diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 68a1ac9bbbad..d1d0f5c3b748 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -179,6 +179,7 @@ class InvoiceRepository extends BaseRepository ->where('invoices.is_deleted', '=', false) ->where('clients.deleted_at', '=', null) ->where('invoices.is_recurring', '=', true) + ->where('invoices.is_public', '=', true) ->whereIn('invoices.auto_bill', [AUTO_BILL_OPT_IN, AUTO_BILL_OPT_OUT]) //->where('invoices.start_date', '>=', date('Y-m-d H:i:s')) ->select( @@ -693,6 +694,10 @@ class InvoiceRepository extends BaseRepository if ($account->invoice_terms) { $clone->terms = $account->invoice_terms; } + if ($account->auto_convert_quote) { + $clone->is_public = true; + $clone->invoice_status_id = INVOICE_STATUS_SENT; + } } $clone->save(); @@ -822,6 +827,7 @@ class InvoiceRepository extends BaseRepository } $invoice = Invoice::createNew($recurInvoice); + $invoice->is_public = true; $invoice->invoice_type_id = INVOICE_TYPE_STANDARD; $invoice->client_id = $recurInvoice->client_id; $invoice->recurring_invoice_id = $recurInvoice->id; diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index 7b7e6b47eb7f..b7c307e913ab 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -557,7 +557,7 @@ @if ($invoice->isSent()) {!! Button::success(trans("texts.save_{$entityType}"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onSaveClick()'))->appendIcon(Icon::create('floppy-disk')) !!} @else - {!! Button::normal(trans("texts.save_draft"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onSaveClick()'))->appendIcon(Icon::create('floppy-disk')) !!} + {!! Button::normal(trans("texts.save_draft"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onSaveDraftClick()'))->appendIcon(Icon::create('floppy-disk')) !!} {!! Button::success(trans("texts.mark_sent"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onMarkSentClick()'))->appendIcon(Icon::create('globe')) !!} @endif {!! Button::info(trans("texts.email_{$entityType}"))->withAttributes(array('id' => 'emailButton', 'onclick' => 'onEmailClick()'))->appendIcon(Icon::create('send')) !!} @@ -1291,23 +1291,27 @@ }, getSendToEmails()); } - function onMarkSentClick() { - model.invoice().is_public(true); + function onSaveDraftClick() { + model.invoice().is_public(false); onSaveClick(); } + function onMarkSentClick() { + if (model.invoice().is_recurring()) { + // warn invoice will be emailed when saving new recurring invoice + var text = getSendToEmails() + '\n' + "{!! trans("texts.confirm_recurring_timing") !!}"; + var title = "{!! trans("texts.confirm_recurring_email_$entityType") !!}"; + sweetConfirm(function() { + model.invoice().is_public(true); + submitAction(''); + }, text, title); + return; + } + } + function onSaveClick() { if (model.invoice().is_recurring()) { - // warn invoice will be emailed when saving new recurring invoice - if ({{ $invoice->exists ? 'false' : 'true' }}) { - var text = getSendToEmails() + '\n' + "{!! trans("texts.confirm_recurring_timing") !!}"; - var title = "{!! trans("texts.confirm_recurring_email_$entityType") !!}"; - sweetConfirm(function() { - submitAction(''); - }, text, title); - return; - // warn invoice will be emailed again if start date is changed - } else if (model.invoice().start_date() != model.invoice().start_date_orig()) { + if (model.invoice().start_date() != model.invoice().start_date_orig()) { var text = "{!! trans("texts.original_start_date") !!}: " + model.invoice().start_date_orig() + '\n' + "{!! trans("texts.new_start_date") !!}: " + model.invoice().start_date(); var title = "{!! trans("texts.warn_start_date_changed") !!}";