1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/ExpenseController.php

279 lines
9.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2016-01-06 20:52:09 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Http\Requests\CreateExpenseRequest;
use App\Http\Requests\ExpenseRequest;
use App\Http\Requests\UpdateExpenseRequest;
use App\Models\Client;
2016-01-08 19:01:00 +01:00
use App\Models\Expense;
2016-07-05 20:49:47 +02:00
use App\Models\ExpenseCategory;
2016-07-07 18:54:06 +02:00
use App\Models\TaxRate;
2017-01-30 20:40:43 +01:00
use App\Models\Vendor;
use App\Ninja\Datatables\ExpenseDatatable;
use App\Ninja\Repositories\ExpenseRepository;
2016-08-21 17:25:35 +02:00
use App\Ninja\Repositories\InvoiceRepository;
2016-01-06 20:52:09 +01:00
use App\Services\ExpenseService;
2017-01-30 20:40:43 +01:00
use Auth;
use Cache;
use Input;
use Redirect;
use Session;
use URL;
use Utils;
use View;
2016-01-06 20:52:09 +01:00
class ExpenseController extends BaseController
{
2016-01-07 16:14:11 +01:00
// Expenses
2016-01-06 20:52:09 +01:00
protected $expenseRepo;
protected $expenseService;
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_EXPENSE;
2016-01-06 20:52:09 +01:00
2016-08-21 17:25:35 +02:00
/**
* @var InvoiceRepository
*/
protected $invoiceRepo;
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService, InvoiceRepository $invoiceRepo)
2016-01-06 20:52:09 +01:00
{
2016-03-16 00:08:00 +01:00
// parent::__construct();
2016-01-06 20:52:09 +01:00
$this->expenseRepo = $expenseRepo;
$this->expenseService = $expenseService;
2016-08-21 17:25:35 +02:00
$this->invoiceRepo = $invoiceRepo;
2016-01-06 20:52:09 +01:00
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list_wrapper', [
2016-01-06 20:52:09 +01:00
'entityType' => ENTITY_EXPENSE,
'datatable' => new ExpenseDatatable(),
2016-01-06 20:52:09 +01:00
'title' => trans('texts.expenses'),
]);
2016-01-06 20:52:09 +01:00
}
2016-01-08 19:01:00 +01:00
public function getDatatable($expensePublicId = null)
2016-01-06 20:52:09 +01:00
{
2016-01-28 15:07:03 +01:00
return $this->expenseService->getDatatable(Input::get('sSearch'));
2016-01-06 20:52:09 +01:00
}
public function getDatatableVendor($vendorPublicId = null)
{
return $this->expenseService->getDatatableVendor($vendorPublicId);
}
2016-05-01 13:31:10 +02:00
public function create(ExpenseRequest $request)
2016-01-06 20:52:09 +01:00
{
2016-05-01 13:31:10 +02:00
if ($request->vendor_id != 0) {
$vendor = Vendor::scope($request->vendor_id)->with('vendor_contacts')->firstOrFail();
2016-01-08 19:01:00 +01:00
} else {
$vendor = null;
}
2016-07-05 20:49:47 +02:00
$data = [
2016-05-01 13:31:10 +02:00
'vendorPublicId' => Input::old('vendor') ? Input::old('vendor') : $request->vendor_id,
2016-01-06 20:52:09 +01:00
'expense' => null,
'method' => 'POST',
'url' => 'expenses',
'title' => trans('texts.new_expense'),
'vendors' => Vendor::scope()->with('vendor_contacts')->orderBy('name')->get(),
2016-01-08 19:01:00 +01:00
'vendor' => $vendor,
2016-01-09 06:24:43 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
2016-05-01 13:31:10 +02:00
'clientPublicId' => $request->client_id,
2016-07-07 10:03:43 +02:00
'categoryPublicId' => $request->category_id,
];
2016-01-06 20:52:09 +01:00
$data = array_merge($data, self::getViewModel());
return View::make('expenses.edit', $data);
}
2016-05-01 13:31:10 +02:00
public function edit(ExpenseRequest $request)
2016-01-06 20:52:09 +01:00
{
2016-05-01 13:31:10 +02:00
$expense = $request->entity();
2016-07-05 20:49:47 +02:00
2016-01-21 23:29:10 +01:00
$actions = [];
if ($expense->invoice) {
$actions[] = ['url' => URL::to("invoices/{$expense->invoice->public_id}/edit"), 'label' => trans('texts.view_invoice')];
2016-01-21 23:29:10 +01:00
} else {
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans('texts.invoice_expense')];
2016-08-21 17:25:35 +02:00
// 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])];
}
2016-01-21 23:29:10 +01:00
}
$actions[] = \DropdownButton::DIVIDER;
2017-01-30 20:40:43 +01:00
if (! $expense->trashed()) {
2016-01-21 23:29:10 +01:00
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_expense')];
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_expense')];
} else {
$actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
}
$data = [
2016-01-06 20:52:09 +01:00
'vendor' => null,
'expense' => $expense,
2016-10-18 16:55:07 +02:00
'entity' => $expense,
2016-01-06 20:52:09 +01:00
'method' => 'PUT',
2016-05-01 13:31:10 +02:00
'url' => 'expenses/'.$expense->public_id,
2016-01-06 20:52:09 +01:00
'title' => 'Edit Expense',
2016-01-21 23:29:10 +01:00
'actions' => $actions,
'vendors' => Vendor::scope()->with('vendor_contacts')->orderBy('name')->get(),
'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null,
2016-01-09 09:08:24 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
'clientPublicId' => $expense->client ? $expense->client->public_id : null,
2016-07-07 10:03:43 +02:00
'categoryPublicId' => $expense->expense_category ? $expense->expense_category->public_id : null,
];
2016-01-08 19:01:00 +01:00
$data = array_merge($data, self::getViewModel());
2016-01-06 20:52:09 +01:00
2016-01-08 19:01:00 +01:00
return View::make('expenses.edit', $data);
2016-01-06 20:52:09 +01:00
}
2016-01-08 19:01:00 +01:00
/**
* Update the specified resource in storage.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2016-01-08 19:01:00 +01:00
* @return Response
*/
public function update(UpdateExpenseRequest $request)
{
$data = $request->input();
$data['documents'] = $request->file('documents');
2016-07-05 20:49:47 +02:00
$expense = $this->expenseService->save($data, $request->entity());
2016-01-10 11:25:05 +01:00
2016-01-08 19:01:00 +01:00
Session::flash('message', trans('texts.updated_expense'));
2016-01-10 11:25:05 +01:00
2016-01-21 23:29:10 +01:00
$action = Input::get('action');
2016-08-21 17:25:35 +02:00
if (in_array($action, ['archive', 'delete', 'restore', 'invoice', 'add_to_invoice'])) {
2016-01-21 23:29:10 +01:00
return self::bulk();
}
return redirect()->to("expenses/{$expense->public_id}/edit");
2016-01-08 19:01:00 +01:00
}
2016-01-10 11:25:05 +01:00
2016-01-06 20:52:09 +01:00
public function store(CreateExpenseRequest $request)
{
2016-03-25 00:55:56 +01:00
$data = $request->input();
$data['documents'] = $request->file('documents');
2016-07-05 20:49:47 +02:00
2017-05-22 10:17:28 +02:00
// check for possible duplicate expense
$duplcate = Expense::scope()
->whereAmount($request->amount)
->whereExpenseDate(Utils::toSqlDate($request->expense_date))
->orderBy('created_at')
->first();
if ($duplcate) {
Session::flash('warning', trans('texts.duplicate_expense_warning',
['link' => link_to($duplcate->present()->url, trans('texts.expense_link'), ['target' => '_blank'])]));
}
2016-03-25 00:55:56 +01:00
$expense = $this->expenseService->save($data);
2016-01-07 12:04:01 +01:00
2016-01-06 20:52:09 +01:00
Session::flash('message', trans('texts.created_expense'));
2016-01-10 11:25:05 +01:00
return redirect()->to("expenses/{$expense->public_id}/edit");
2016-01-06 20:52:09 +01:00
}
public function bulk()
{
$action = Input::get('action');
2017-01-30 20:40:43 +01:00
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
2016-01-10 11:25:05 +01:00
2017-01-30 17:05:31 +01:00
switch ($action) {
2016-01-10 11:25:05 +01:00
case 'invoice':
2016-08-21 17:25:35 +02:00
case 'add_to_invoice':
$expenses = Expense::scope($ids)->with('client')->get();
$clientPublicId = null;
$currencyId = null;
2016-07-05 20:49:47 +02:00
2016-01-10 11:25:05 +01:00
// Validate that either all expenses do not have a client or if there is a client, it is the same client
2017-01-30 17:05:31 +01:00
foreach ($expenses as $expense) {
if ($expense->client) {
2017-01-30 20:40:43 +01:00
if (! $clientPublicId) {
$clientPublicId = $expense->client->public_id;
} elseif ($clientPublicId != $expense->client->public_id) {
2016-01-21 23:29:10 +01:00
Session::flash('error', trans('texts.expense_error_multiple_clients'));
2017-01-30 20:40:43 +01:00
2016-01-21 23:29:10 +01:00
return Redirect::to('expenses');
}
2016-01-10 11:25:05 +01:00
}
2017-01-30 20:40:43 +01:00
if (! $currencyId) {
2016-02-01 23:07:09 +01:00
$currencyId = $expense->invoice_currency_id;
} elseif ($currencyId != $expense->invoice_currency_id && $expense->invoice_currency_id) {
Session::flash('error', trans('texts.expense_error_multiple_currencies'));
2017-01-30 20:40:43 +01:00
return Redirect::to('expenses');
}
2016-01-10 11:25:05 +01:00
if ($expense->invoice_id) {
Session::flash('error', trans('texts.expense_error_invoiced'));
2017-01-30 20:40:43 +01:00
2016-01-10 11:25:05 +01:00
return Redirect::to('expenses');
}
}
2016-08-21 17:25:35 +02:00
if ($action == 'invoice') {
return Redirect::to("invoices/create/{$clientPublicId}")
->with('expenseCurrencyId', $currencyId)
->with('expenses', $ids);
} else {
$invoiceId = Input::get('invoice_id');
2017-01-30 20:40:43 +01:00
2016-08-21 17:25:35 +02:00
return Redirect::to("invoices/{$invoiceId}/edit")
->with('expenseCurrencyId', $currencyId)
->with('expenses', $ids);
}
2016-01-10 11:25:05 +01:00
break;
default:
2017-01-30 20:40:43 +01:00
$count = $this->expenseService->bulk($ids, $action);
2016-01-10 11:25:05 +01:00
}
2016-01-06 20:52:09 +01:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_expense', $count);
Session::flash('message', $message);
}
2016-09-19 15:30:46 +02:00
return $this->returnBulk($this->entityType, $action, $ids);
2016-01-06 20:52:09 +01:00
}
2016-01-10 11:25:05 +01:00
2016-01-06 20:52:09 +01:00
private static function getViewModel()
{
return [
'data' => Input::old('data'),
'account' => Auth::user()->account,
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'industries' => Cache::get('industries'),
'currencies' => Cache::get('currencies'),
'languages' => Cache::get('languages'),
'countries' => Cache::get('countries'),
'customLabel1' => Auth::user()->account->custom_vendor_label1,
'customLabel2' => Auth::user()->account->custom_vendor_label2,
2016-11-29 18:47:26 +01:00
'categories' => ExpenseCategory::whereAccountId(Auth::user()->account_id)->withArchived()->orderBy('name')->get(),
2017-01-02 12:38:58 +01:00
'taxRates' => TaxRate::scope()->whereIsInclusive(false)->orderBy('name')->get(),
2016-01-06 20:52:09 +01:00
];
}
2016-01-08 19:01:00 +01:00
public function show($publicId)
{
Session::reflash();
2016-01-08 19:01:00 +01:00
return Redirect::to("expenses/{$publicId}/edit");
2016-01-10 11:25:05 +01:00
}
2016-01-06 20:52:09 +01:00
}