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

264 lines
8.7 KiB
PHP
Raw Normal View History

2016-01-06 20:52:09 +01:00
<?php namespace App\Http\Controllers;
2016-01-08 19:01:00 +01:00
use Debugbar;
use DB;
2016-01-06 20:52:09 +01:00
use Auth;
use Datatable;
use Utils;
use View;
use URL;
use Validator;
use Input;
use Session;
use Redirect;
use Cache;
use App\Models\Vendor;
2016-01-08 19:01:00 +01:00
use App\Models\Expense;
2016-01-09 06:24:43 +01:00
use App\Models\Client;
2016-01-06 20:52:09 +01:00
use App\Services\ExpenseService;
use App\Ninja\Repositories\ExpenseRepository;
use App\Http\Requests\CreateExpenseRequest;
2016-01-08 19:01:00 +01:00
use App\Http\Requests\UpdateExpenseRequest;
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
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
{
2016-03-16 00:08:00 +01:00
// parent::__construct();
2016-01-06 20:52:09 +01:00
$this->expenseRepo = $expenseRepo;
$this->expenseService = $expenseService;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list', array(
'entityType' => ENTITY_EXPENSE,
'title' => trans('texts.expenses'),
2016-03-17 23:04:03 +01:00
'sortCol' => '3',
2016-01-06 20:52:09 +01:00
'columns' => Utils::trans([
2016-01-09 06:24:43 +01:00
'checkbox',
2016-01-06 20:52:09 +01:00
'vendor',
2016-01-23 22:36:31 +01:00
'client',
2016-01-06 20:52:09 +01:00
'expense_date',
2016-01-21 23:29:10 +01:00
'amount',
2016-01-07 12:04:01 +01:00
'public_notes',
'status',
2016-01-09 06:24:43 +01:00
''
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-01-21 20:15:30 +01:00
public function create($vendorPublicId = null, $clientPublicId = null)
2016-01-06 20:52:09 +01:00
{
2016-04-26 03:53:39 +02:00
$this->authorizeCreate();
2016-03-16 00:08:00 +01:00
2016-01-08 19:01:00 +01:00
if($vendorPublicId != 0) {
$vendor = Vendor::scope($vendorPublicId)->with('vendorcontacts')->firstOrFail();
} else {
$vendor = null;
}
2016-01-06 20:52:09 +01:00
$data = array(
'vendorPublicId' => Input::old('vendor') ? Input::old('vendor') : $vendorPublicId,
'expense' => null,
'method' => 'POST',
'url' => 'expenses',
'title' => trans('texts.new_expense'),
2016-01-08 19:01:00 +01:00
'vendors' => Vendor::scope()->with('vendorcontacts')->orderBy('name')->get(),
'vendor' => $vendor,
2016-01-09 06:24:43 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
2016-01-21 20:15:30 +01:00
'clientPublicId' => $clientPublicId,
2016-01-06 20:52:09 +01:00
);
$data = array_merge($data, self::getViewModel());
return View::make('expenses.edit', $data);
}
public function edit($publicId)
{
$expense = Expense::scope($publicId)->with('documents')->firstOrFail();
2016-03-16 00:08:00 +01:00
2016-04-26 03:53:39 +02:00
$this->authorize('edit', $expense);
2016-03-16 00:08:00 +01:00
2016-01-06 20:52:09 +01:00
$expense->expense_date = Utils::fromSqlDate($expense->expense_date);
2016-02-18 11:02:30 +01: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")];
} else {
$actions[] = ['url' => 'javascript:submitAction("invoice")', 'label' => trans("texts.invoice_expense")];
/*
// check for any open invoices
$invoices = $task->client_id ? $this->invoiceRepo->findOpenInvoices($task->client_id) : [];
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;
if (!$expense->trashed()) {
$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')];
}
2016-01-06 20:52:09 +01:00
$data = array(
'vendor' => null,
'expense' => $expense,
'method' => 'PUT',
'url' => 'expenses/'.$publicId,
'title' => 'Edit Expense',
2016-01-21 23:29:10 +01:00
'actions' => $actions,
2016-01-08 19:01:00 +01:00
'vendors' => Vendor::scope()->with('vendorcontacts')->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-01-21 23:29:10 +01:00
);
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.
*
* @param int $id
* @return Response
*/
public function update(UpdateExpenseRequest $request)
{
$data = $request->input();
$data['documents'] = $request->file('documents');
2016-04-26 03:53:39 +02:00
$this->authorizeUpdate($data);
2016-04-27 22:56:14 +02:00
$expense = $this->expenseService->save($data);
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');
if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
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-04-26 03:53:39 +02:00
$this->authorizeUpdate($data);
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');
2016-01-10 11:25:05 +01:00
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
switch($action)
{
case 'invoice':
$expenses = Expense::scope($ids)->with('client')->get();
$clientPublicId = null;
$currencyId = null;
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
foreach ($expenses as $expense)
{
if ($expense->client) {
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'));
return Redirect::to('expenses');
}
2016-01-10 11:25:05 +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'));
return Redirect::to('expenses');
}
2016-01-10 11:25:05 +01:00
if ($expense->invoice_id) {
Session::flash('error', trans('texts.expense_error_invoiced'));
return Redirect::to('expenses');
}
}
return Redirect::to("invoices/create/{$clientPublicId}")
->with('expenseCurrencyId', $currencyId)
->with('expenses', $ids);
2016-01-10 11:25:05 +01:00
break;
default:
$count = $this->expenseService->bulk($ids, $action);
}
2016-01-06 20:52:09 +01:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_expense', $count);
Session::flash('message', $message);
}
return Redirect::to('expenses');
}
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-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
}