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;
|
2016-05-01 13:31:10 +02:00
|
|
|
|
|
|
|
use App\Http\Requests\ExpenseRequest;
|
2016-01-06 20:52:09 +01:00
|
|
|
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',
|
2016-01-20 23:09:10 +01:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2016-01-20 23:09:10 +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('vendorcontacts')->firstOrFail();
|
2016-01-08 19:01:00 +01:00
|
|
|
} else {
|
|
|
|
$vendor = null;
|
|
|
|
}
|
2016-05-01 13:31:10 +02:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
$data = array(
|
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
|
|
|
'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-05-01 13:31:10 +02:00
|
|
|
'clientPublicId' => $request->client_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-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")];
|
|
|
|
}
|
|
|
|
|
|
|
|
$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',
|
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,
|
2016-01-08 19:01:00 +01:00
|
|
|
'vendors' => Vendor::scope()->with('vendorcontacts')->orderBy('name')->get(),
|
2016-01-20 23:09:10 +01:00
|
|
|
'vendorPublicId' => $expense->vendor ? $expense->vendor->public_id : null,
|
2016-01-09 09:08:24 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
|
2016-01-20 23:09:10 +01:00
|
|
|
'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)
|
|
|
|
{
|
2016-03-24 23:15:52 +01:00
|
|
|
$data = $request->input();
|
|
|
|
$data['documents'] = $request->file('documents');
|
2016-05-01 13:31:10 +02:00
|
|
|
|
2016-05-02 19:42:13 +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');
|
|
|
|
if (in_array($action, ['archive', 'delete', 'restore', 'invoice'])) {
|
|
|
|
return self::bulk();
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:09:10 +01:00
|
|
|
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-05-01 13:31:10 +02:00
|
|
|
|
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
|
|
|
|
2016-01-20 23:09:10 +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':
|
2016-01-31 14:10:33 +01:00
|
|
|
$expenses = Expense::scope($ids)->with('client')->get();
|
|
|
|
$clientPublicId = null;
|
|
|
|
$currencyId = null;
|
2016-03-24 23:15:52 +01: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
|
|
|
|
foreach ($expenses as $expense)
|
|
|
|
{
|
2016-01-31 14:10:33 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-31 14:10:33 +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) {
|
2016-01-31 14:10:33 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 14:10:33 +01:00
|
|
|
return Redirect::to("invoices/create/{$clientPublicId}")
|
|
|
|
->with('expenseCurrencyId', $currencyId)
|
2016-03-24 23:15:52 +01:00
|
|
|
->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)
|
|
|
|
{
|
2016-01-20 23:09:10 +01:00
|
|
|
Session::reflash();
|
2016-01-08 19:01:00 +01:00
|
|
|
|
2016-01-20 23:09:10 +01:00
|
|
|
return Redirect::to("expenses/{$publicId}/edit");
|
2016-01-10 11:25:05 +01:00
|
|
|
}
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|