diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php
index 425b5d4b8c..97ab3adb7b 100644
--- a/app/Http/Controllers/ExpenseController.php
+++ b/app/Http/Controllers/ExpenseController.php
@@ -13,6 +13,7 @@ use App\Models\Expense;
use App\Models\ExpenseCategory;
use App\Models\Client;
use App\Models\TaxRate;
+use App\Ninja\Repositories\InvoiceRepository;
use App\Services\ExpenseService;
use App\Ninja\Repositories\ExpenseRepository;
use App\Http\Requests\ExpenseRequest;
@@ -26,12 +27,18 @@ class ExpenseController extends BaseController
protected $expenseService;
protected $entityType = ENTITY_EXPENSE;
- public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
+ /**
+ * @var InvoiceRepository
+ */
+ protected $invoiceRepo;
+
+ public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService, InvoiceRepository $invoiceRepo)
{
// parent::__construct();
$this->expenseRepo = $expenseRepo;
$this->expenseService = $expenseService;
+ $this->invoiceRepo = $invoiceRepo;
}
/**
@@ -106,6 +113,14 @@ class ExpenseController extends BaseController
$actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans('texts.view_invoice')];
} else {
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_expense')];
+
+ // check for any open invoices
+ $invoices = $expense->client_id ? $this->invoiceRepo->findOpenInvoices($expense->client_id, ENTITY_EXPENSE) : [];
+
+ foreach ($invoices as $invoice) {
+ $actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans('texts.add_to_invoice', ['invoice' => $invoice->invoice_number])];
+ }
+
}
$actions[] = \DropdownButton::DIVIDER;
@@ -151,7 +166,7 @@ class ExpenseController extends BaseController
Session::flash('message', trans('texts.updated_expense'));
$action = Input::get('action');
- if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
+ if (in_array($action, ['archive', 'delete', 'restore', 'invoice', 'add_to_invoice'])) {
return self::bulk();
}
@@ -178,6 +193,7 @@ class ExpenseController extends BaseController
switch($action)
{
case 'invoice':
+ case 'add_to_invoice':
$expenses = Expense::scope($ids)->with('client')->get();
$clientPublicId = null;
$currencyId = null;
@@ -207,9 +223,17 @@ class ExpenseController extends BaseController
}
}
- return Redirect::to("invoices/create/{$clientPublicId}")
- ->with('expenseCurrencyId', $currencyId)
- ->with('expenses', $ids);
+ if ($action == 'invoice') {
+ return Redirect::to("invoices/create/{$clientPublicId}")
+ ->with('expenseCurrencyId', $currencyId)
+ ->with('expenses', $ids);
+ } else {
+ $invoiceId = Input::get('invoice_id');
+ return Redirect::to("invoices/{$invoiceId}/edit")
+ ->with('expenseCurrencyId', $currencyId)
+ ->with('expenses', $ids);
+
+ }
break;
default:
diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php
index b18951594d..5f736cbde8 100644
--- a/app/Http/Controllers/InvoiceController.php
+++ b/app/Http/Controllers/InvoiceController.php
@@ -244,11 +244,6 @@ class InvoiceController extends BaseController
$invoice = $account->createInvoice($entityType, $clientId);
$invoice->public_id = 0;
- if (Session::get('expenses')) {
- $invoice->expenses = Expense::scope(Session::get('expenses'))->with('documents', 'expense_category')->get();
- }
-
-
$clients = Client::scope()->with('contacts', 'country')->orderBy('name');
if (!Auth::user()->hasPermission('view_all')) {
$clients = $clients->where('clients.user_id', '=', Auth::user()->id);
@@ -384,6 +379,7 @@ class InvoiceController extends BaseController
'invoiceLabels' => Auth::user()->account->getInvoiceLabels(),
'tasks' => Session::get('tasks') ? json_encode(Session::get('tasks')) : null,
'expenseCurrencyId' => Session::get('expenseCurrencyId') ?: null,
+ 'expenses' => Session::get('expenses') ? Expense::scope(Session::get('expenses'))->with('documents', 'expense_category')->get() : [],
];
}
diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php
index 839c06395b..42099026eb 100644
--- a/app/Http/Controllers/TaskController.php
+++ b/app/Http/Controllers/TaskController.php
@@ -160,7 +160,7 @@ class TaskController extends BaseController
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_task')];
// check for any open invoices
- $invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
+ $invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id, ENTITY_TASK) : [];
foreach ($invoices as $invoice) {
$actions[] = ['url' => 'javascript:submitAction("add_to_invoice", '.$invoice->public_id.')', 'label' => trans('texts.add_to_invoice', ['invoice' => $invoice->invoice_number])];
diff --git a/app/Ninja/Intents/ListProductsIntent.php b/app/Ninja/Intents/ListProductsIntent.php
index bba2fc7720..fc8c17490b 100644
--- a/app/Ninja/Intents/ListProductsIntent.php
+++ b/app/Ninja/Intents/ListProductsIntent.php
@@ -17,7 +17,7 @@ class ListProductsIntent extends ProductIntent
->transform(function($item, $key) use ($account) {
$card = $item->present()->skypeBot($account);
if ($this->stateEntity(ENTITY_INVOICE)) {
- $card->addButton('imBack', trans('texts.add_to_invoice'), trans('texts.add_product_to_invoice', ['product' => $item->product_key]));
+ $card->addButton('imBack', trans('texts.add_to_invoice', ['invoice' => '']), trans('texts.add_product_to_invoice', ['product' => $item->product_key]));
}
return $card;
});
diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php
index b310ac1318..9e1b5ab722 100644
--- a/app/Ninja/Repositories/InvoiceRepository.php
+++ b/app/Ninja/Repositories/InvoiceRepository.php
@@ -735,15 +735,21 @@ class InvoiceRepository extends BaseRepository
* @param $clientId
* @return mixed
*/
- public function findOpenInvoices($clientId)
+ public function findOpenInvoices($clientId, $entityType = false)
{
- return Invoice::scope()
+ $query = Invoice::scope()
->invoiceType(INVOICE_TYPE_STANDARD)
->whereClientId($clientId)
->whereIsRecurring(false)
- ->whereDeletedAt(null)
- ->whereHasTasks(true)
- ->where('invoice_status_id', '<', 5)
+ ->whereDeletedAt(null);
+
+ if ($entityType == ENTITY_TASK) {
+ $query->whereHasTasks(true);
+ } elseif ($entityType == ENTITY_EXPENSE) {
+ $query->whereHasExpenses(true);
+ }
+
+ return $query->where('invoice_status_id', '<', 5)
->select(['public_id', 'invoice_number'])
->get();
}
diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php
index a5aa49c040..89ce7448fe 100644
--- a/resources/lang/en/texts.php
+++ b/resources/lang/en/texts.php
@@ -2059,7 +2059,6 @@ $LANG = array(
'bot_emailed_invoice' => 'Your invoice has been sent.',
'bot_emailed_notify_viewed' => 'I\'ll email you when it\'s viewed.',
'bot_emailed_notify_paid' => 'I\'ll email you when it\'s paid.',
- 'add_to_invoice' => 'Add to invoice',
'add_product_to_invoice' => 'Add 1 :product',
'not_authorized' => 'Your are not authorized',
'bot_get_email' => 'Hi! (wave)
Thanks for trying the Invoice Ninja Bot.
Send me your account email to get started.',
diff --git a/resources/views/expenses/edit.blade.php b/resources/views/expenses/edit.blade.php
index 857e2fee22..c07d3dbb2f 100644
--- a/resources/views/expenses/edit.blade.php
+++ b/resources/views/expenses/edit.blade.php
@@ -26,7 +26,11 @@
@if ($expense)
{!! Former::populate($expense) !!}
{!! Former::populateField('should_be_invoiced', intval($expense->should_be_invoiced)) !!}
- {!! Former::hidden('public_id') !!}
+
+