1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Enabled adding expense to open invoice

This commit is contained in:
Hillel Coren 2016-08-21 18:25:35 +03:00
parent 9bb64d4984
commit 6c92a6a94a
8 changed files with 61 additions and 31 deletions

View File

@ -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:

View File

@ -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() : [],
];
}

View File

@ -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])];

View File

@ -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;
});

View File

@ -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();
}

View File

@ -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)<br/>Thanks for trying the Invoice Ninja Bot.<br/>Send me your account email to get started.',

View File

@ -26,7 +26,11 @@
@if ($expense)
{!! Former::populate($expense) !!}
{!! Former::populateField('should_be_invoiced', intval($expense->should_be_invoiced)) !!}
{!! Former::hidden('public_id') !!}
<div style="display:none">
{!! Former::text('public_id') !!}
{!! Former::text('invoice_id') !!}
</div>
@endif
<div class="panel panel-default">
@ -221,8 +225,9 @@
}
}
function submitAction(action) {
function submitAction(action, invoice_id) {
$('#action').val(action);
$('#invoice_id').val(invoice_id);
$('.main-form').submit();
}

View File

@ -857,29 +857,29 @@
model.invoice().has_tasks(true);
@endif
if(model.invoice().expenses().length && !model.invoice().public_id()){
@if (isset($expenses) && $expenses)
model.expense_currency_id({{ isset($expenseCurrencyId) ? $expenseCurrencyId : 0 }});
// move the blank invoice line item to the end
var blank = model.invoice().invoice_items.pop();
var expenses = model.invoice().expenses();
var expenses = {!! $expenses !!}
for (var i=0; i<expenses.length; i++) {
var expense = expenses[i];
var item = model.invoice().addItem();
item.product_key(expense.expense_category ? expense.expense_category.name() : '');
item.notes(expense.public_notes());
item.product_key(expense.expense_category ? expense.expense_category.name : '');
item.notes(expense.public_notes);
item.qty(1);
item.expense_public_id(expense.public_id());
item.cost(expense.converted_amount());
item.tax_rate1(expense.tax_rate1());
item.tax_name1(expense.tax_name1());
item.tax_rate2(expense.tax_rate2());
item.tax_name2(expense.tax_name2());
item.expense_public_id(expense.public_id);
item.cost(expense.converted_amount);
item.tax_rate1(expense.tax_rate1);
item.tax_name1(expense.tax_name1);
item.tax_rate2(expense.tax_rate2);
item.tax_name2(expense.tax_name2);
}
model.invoice().invoice_items.push(blank);
model.invoice().has_expenses(true);
}
@endif
@endif