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;
|
|
|
|
|
|
|
|
public function __construct(ExpenseRepository $expenseRepo, ExpenseService $expenseService)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$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-01-08 19:01:00 +01:00
|
|
|
'sortCol' => '1',
|
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',
|
|
|
|
'expense_amount',
|
|
|
|
'expense_date',
|
2016-01-07 12:04:01 +01:00
|
|
|
'public_notes',
|
2016-01-08 19:01:00 +01:00
|
|
|
'is_invoiced',
|
|
|
|
'should_be_invoiced',
|
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-08 19:01:00 +01:00
|
|
|
return $this->expenseService->getDatatable($expensePublicId, Input::get('sSearch'));
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create($vendorPublicId = 0)
|
|
|
|
{
|
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(),
|
|
|
|
'clientPublicId' => null,
|
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)->firstOrFail();
|
|
|
|
$expense->expense_date = Utils::fromSqlDate($expense->expense_date);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'vendor' => null,
|
|
|
|
'expense' => $expense,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'expenses/'.$publicId,
|
|
|
|
'title' => 'Edit Expense',
|
2016-01-08 19:01:00 +01:00
|
|
|
'vendors' => Vendor::scope()->with('vendorcontacts')->orderBy('name')->get(),
|
2016-01-09 06:24:43 +01:00
|
|
|
'vendorPublicId' => $expense->vendor_id,
|
2016-01-09 09:08:24 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
|
|
|
|
'clientPublicId' => $expense->invoice_client_id,
|
2016-01-09 06:24:43 +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
|
|
|
if (Auth::user()->account->isNinjaAccount()) {
|
|
|
|
if ($account = Account::whereId($client->public_id)->first()) {
|
|
|
|
$data['proPlanPaid'] = $account['pro_plan_paid'];
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 11:25:05 +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)
|
|
|
|
{
|
|
|
|
$client = $this->expenseRepo->save($request->input());
|
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-08 19:01:00 +01:00
|
|
|
return redirect()->to('expenses');
|
|
|
|
}
|
2016-01-10 11:25:05 +01:00
|
|
|
|
2016-01-06 20:52:09 +01:00
|
|
|
public function store(CreateExpenseRequest $request)
|
|
|
|
{
|
|
|
|
$expense = $this->expenseRepo->save($request->input());
|
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-06 20:52:09 +01:00
|
|
|
return redirect()->to('expenses');
|
|
|
|
}
|
|
|
|
|
|
|
|
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)->get();
|
|
|
|
$clientPublicId = null;
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
// 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_id) {
|
|
|
|
if (!$clientPublicId) {
|
|
|
|
$clientPublicId = $expense->client_id;
|
|
|
|
} else if ($clientPublicId != $expense->client_id) {
|
|
|
|
Session::flash('error', trans('texts.expense_error_multiple_clients'));
|
|
|
|
return Redirect::to('expenses');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expense->invoice_id) {
|
|
|
|
Session::flash('error', trans('texts.expense_error_invoiced'));
|
|
|
|
return Redirect::to('expenses');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($expense->should_be_invoiced == 0) {
|
|
|
|
Session::flash('error', trans('texts.expense_error_should_not_be_invoiced'));
|
|
|
|
return Redirect::to('expenses');
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
$data[] = [
|
|
|
|
'publicId' => $expense->public_id,
|
|
|
|
'description' => $expense->public_notes,
|
|
|
|
'qty' => 1,
|
|
|
|
'cost' => $expense->amount,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to("invoices/create/{$clientPublicId}")->with('expenses', $data);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$expense = Expense::withTrashed()->scope($publicId)->firstOrFail();
|
2016-01-10 11:25:05 +01:00
|
|
|
|
2016-01-08 19:01:00 +01:00
|
|
|
if($expense) {
|
|
|
|
Utils::trackViewed($expense->getDisplayName(), 'expense');
|
|
|
|
}
|
|
|
|
|
|
|
|
$actionLinks = [
|
|
|
|
['label' => trans('texts.new_expense'), 'url' => '/expenses/create/']
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'actionLinks' => $actionLinks,
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'expense' => $expense,
|
|
|
|
'credit' =>0,
|
|
|
|
'vendor' => $expense->vendor,
|
|
|
|
'title' => trans('texts.view_expense',['expense' => $expense->public_id]),
|
|
|
|
);
|
|
|
|
|
|
|
|
return View::make('expenses.show', $data);
|
2016-01-10 11:25:05 +01:00
|
|
|
}
|
2016-01-06 20:52:09 +01:00
|
|
|
}
|