1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Controllers/ExpenseController.php

306 lines
11 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 Request;
2017-01-30 20:40:43 +01:00
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'),
2016-01-08 19:01:00 +01:00
'vendor' => $vendor,
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);
}
2017-08-08 06:21:41 +02:00
public function cloneExpense(ExpenseRequest $request, $publicId)
2017-07-20 17:03:05 +02:00
{
return self::edit($request, $publicId, true);
}
public function edit(ExpenseRequest $request, $publicId = false, $clone = false)
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 = [];
2017-07-20 17:03:05 +02:00
if (! $clone) {
$actions[] = ['url' => 'javascript:submitAction("clone")', 'label' => trans("texts.clone_expense")];
}
2016-01-21 23:29:10 +01:00
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) : [];
2016-08-21 17:25:35 +02:00
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
}
2017-06-26 20:36:58 +02:00
if ($expense->recurring_expense_id) {
$actions[] = ['url' => URL::to("recurring_expenses/{$expense->recurring_expense->public_id}/edit"), 'label' => trans('texts.view_recurring_expense')];
}
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')];
}
2017-07-20 17:03:05 +02:00
if ($clone) {
$expense->id = null;
$expense->public_id = null;
$expense->expense_date = date_create()->format('Y-m-d');
$expense->deleted_at = null;
$expense->invoice_id = null;
$expense->payment_date = null;
$expense->payment_type_id = null;
$expense->transaction_reference = null;
while ($expense->documents->count()) {
$expense->documents->pop();
}
2017-07-20 17:03:05 +02:00
$method = 'POST';
$url = 'expenses';
} else {
$method = 'PUT';
$url = 'expenses/' . $expense->public_id;
}
$data = [
2016-01-06 20:52:09 +01:00
'vendor' => null,
'expense' => $expense,
2016-10-18 16:55:07 +02:00
'entity' => $expense,
2017-07-20 17:03:05 +02:00
'method' => $method,
'url' => $url,
2016-01-06 20:52:09 +01:00
'title' => 'Edit Expense',
2016-01-21 23:29:10 +01:00
'actions' => $actions,
'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null,
'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
2018-03-14 11:21:50 +01:00
$data = array_merge($data, self::getViewModel($expense));
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();
}
2017-07-20 17:03:05 +02:00
if ($action == 'clone') {
return redirect()->to(sprintf('expenses/%s/clone', $expense->public_id));
} else {
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');
$referer = Request::server('HTTP_REFERER');
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) {
if ($expense->client->trashed()) {
return redirect($referer)->withError(trans('texts.client_must_be_active'));
}
2017-01-30 20:40:43 +01:00
if (! $clientPublicId) {
$clientPublicId = $expense->client->public_id;
} elseif ($clientPublicId != $expense->client->public_id) {
return redirect($referer)->withError(trans('texts.expense_error_multiple_clients'));
2016-01-21 23:29:10 +01:00
}
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) {
return redirect($referer)->withError(trans('texts.expense_error_multiple_currencies'));
}
2016-01-10 11:25:05 +01:00
if ($expense->invoice_id) {
return redirect($referer)->withError(trans('texts.expense_error_invoiced'));
2016-01-10 11:25:05 +01:00
}
}
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
2018-03-14 11:21:50 +01:00
private static function getViewModel($expense = false)
2016-01-06 20:52:09 +01:00
{
return [
'data' => Input::old('data'),
'account' => Auth::user()->account,
2018-03-14 11:21:50 +01:00
'vendors' => Vendor::scope()->withActiveOrSelected($expense ? $expense->vendor_id : false)->with('vendor_contacts')->orderBy('name')->get(),
'clients' => Client::scope()->withActiveOrSelected($expense ? $expense->client_id : false)->with('contacts')->orderBy('name')->get(),
'categories' => ExpenseCategory::whereAccountId(Auth::user()->account_id)->withActiveOrSelected($expense ? $expense->expense_category_id : false)->orderBy('name')->get(),
2017-01-02 12:38:58 +01:00
'taxRates' => TaxRate::scope()->whereIsInclusive(false)->orderBy('name')->get(),
2017-06-26 06:16:29 +02:00
'isRecurring' => false,
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
}