2015-03-26 07:24:02 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Auth;
|
2015-04-02 06:09:32 +02:00
|
|
|
use Datatable;
|
2015-03-26 07:24:02 +01:00
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-03-31 20:50:58 +02:00
|
|
|
use URL;
|
2015-04-01 17:44:55 +02:00
|
|
|
use Validator;
|
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use Redirect;
|
2015-04-08 15:19:17 +02:00
|
|
|
use Cache;
|
2015-03-31 20:50:58 +02:00
|
|
|
|
2015-04-02 06:09:32 +02:00
|
|
|
use App\Models\Activity;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Models\Client;
|
2015-11-09 20:24:22 +01:00
|
|
|
use App\Models\Account;
|
2015-04-01 17:44:55 +02:00
|
|
|
use App\Models\Contact;
|
2015-03-31 20:50:58 +02:00
|
|
|
use App\Models\Invoice;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Models\Size;
|
|
|
|
use App\Models\PaymentTerm;
|
|
|
|
use App\Models\Industry;
|
|
|
|
use App\Models\Currency;
|
|
|
|
use App\Models\Country;
|
2015-05-27 18:52:10 +02:00
|
|
|
use App\Models\Task;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Repositories\ClientRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Services\ClientService;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreateClientRequest;
|
|
|
|
use App\Http\Requests\UpdateClientRequest;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-26 07:24:02 +01:00
|
|
|
class ClientController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $clientService;
|
2015-03-16 22:45:25 +01:00
|
|
|
protected $clientRepo;
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
public function __construct(ClientRepository $clientRepo, ClientService $clientService)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->clientRepo = $clientRepo;
|
2015-10-28 20:22:07 +01:00
|
|
|
$this->clientService = $clientService;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return View::make('list', array(
|
|
|
|
'entityType' => ENTITY_CLIENT,
|
|
|
|
'title' => trans('texts.clients'),
|
2015-04-28 22:13:52 +02:00
|
|
|
'sortCol' => '4',
|
2015-11-24 12:59:30 +01:00
|
|
|
'columns' => Utils::trans([
|
|
|
|
'checkbox',
|
|
|
|
'client',
|
|
|
|
'contact',
|
|
|
|
'email',
|
|
|
|
'date_created',
|
|
|
|
'last_login',
|
|
|
|
'balance',
|
2015-11-30 12:29:43 +01:00
|
|
|
''
|
2015-11-24 12:59:30 +01:00
|
|
|
]),
|
2015-03-16 22:45:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable()
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
return $this->clientService->getDatatable(Input::get('sSearch'));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function store(CreateClientRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$client = $this->clientService->save($request->input());
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.created_client'));
|
|
|
|
|
|
|
|
return redirect()->to($client->getRoute());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show($publicId)
|
|
|
|
{
|
|
|
|
$client = Client::withTrashed()->scope($publicId)->with('contacts', 'size', 'industry')->firstOrFail();
|
|
|
|
Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT);
|
|
|
|
|
|
|
|
$actionLinks = [
|
2015-05-31 14:37:29 +02:00
|
|
|
['label' => trans('texts.new_task'), 'url' => '/tasks/create/'.$client->public_id]
|
2015-04-01 17:44:55 +02:00
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if (Utils::isPro()) {
|
2015-05-31 14:37:29 +02:00
|
|
|
array_push($actionLinks, ['label' => trans('texts.new_quote'), 'url' => '/quotes/create/'.$client->public_id]);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:37:29 +02:00
|
|
|
array_push($actionLinks,
|
2016-01-21 20:15:30 +01:00
|
|
|
\DropdownButton::DIVIDER,
|
2015-05-31 14:37:29 +02:00
|
|
|
['label' => trans('texts.enter_payment'), 'url' => '/payments/create/'.$client->public_id],
|
2016-01-21 20:15:30 +01:00
|
|
|
['label' => trans('texts.enter_credit'), 'url' => '/credits/create/'.$client->public_id],
|
|
|
|
['label' => trans('texts.enter_expense'), 'url' => '/expenses/create/0/'.$client->public_id]
|
2015-05-31 14:37:29 +02:00
|
|
|
);
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array(
|
|
|
|
'actionLinks' => $actionLinks,
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'client' => $client,
|
|
|
|
'credit' => $client->getTotalCredit(),
|
|
|
|
'title' => trans('texts.view_client'),
|
|
|
|
'hasRecurringInvoices' => Invoice::scope()->where('is_recurring', '=', true)->whereClientId($client->id)->count() > 0,
|
2015-05-27 18:52:10 +02:00
|
|
|
'hasQuotes' => Invoice::scope()->where('is_quote', '=', true)->whereClientId($client->id)->count() > 0,
|
|
|
|
'hasTasks' => Task::scope()->whereClientId($client->id)->count() > 0,
|
2015-03-16 22:45:25 +01:00
|
|
|
'gatewayLink' => $client->getGatewayLink(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return View::make('clients.show', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
if (Client::scope()->count() > Auth::user()->getMaxNumClients()) {
|
|
|
|
return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of ".Auth::user()->getMaxNumClients()." clients"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'client' => null,
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => 'clients',
|
|
|
|
'title' => trans('texts.new_client'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = array_merge($data, self::getViewModel());
|
|
|
|
|
|
|
|
return View::make('clients.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
|
|
|
|
$data = [
|
|
|
|
'client' => $client,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'clients/'.$publicId,
|
|
|
|
'title' => trans('texts.edit_client'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = array_merge($data, self::getViewModel());
|
|
|
|
|
2015-11-09 20:24:22 +01:00
|
|
|
if (Auth::user()->account->isNinjaAccount()) {
|
|
|
|
if ($account = Account::whereId($client->public_id)->first()) {
|
|
|
|
$data['proPlanPaid'] = $account['pro_plan_paid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return View::make('clients.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getViewModel()
|
|
|
|
{
|
|
|
|
return [
|
2015-10-28 20:22:07 +01:00
|
|
|
'data' => Input::old('data'),
|
2015-09-10 19:50:09 +02:00
|
|
|
'account' => Auth::user()->account,
|
2015-04-08 15:19:17 +02:00
|
|
|
'sizes' => Cache::get('sizes'),
|
|
|
|
'paymentTerms' => Cache::get('paymentTerms'),
|
|
|
|
'industries' => Cache::get('industries'),
|
|
|
|
'currencies' => Cache::get('currencies'),
|
2015-09-10 19:50:09 +02:00
|
|
|
'languages' => Cache::get('languages'),
|
2015-04-08 15:19:17 +02:00
|
|
|
'countries' => Cache::get('countries'),
|
2015-03-26 07:24:02 +01:00
|
|
|
'customLabel1' => Auth::user()->account->custom_client_label1,
|
2015-03-16 22:45:25 +01:00
|
|
|
'customLabel2' => Auth::user()->account->custom_client_label2,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function update(UpdateClientRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$client = $this->clientService->save($request->input());
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.updated_client'));
|
|
|
|
|
|
|
|
return redirect()->to($client->getRoute());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
2015-10-28 20:22:07 +01:00
|
|
|
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
|
|
|
|
$count = $this->clientService->bulk($ids, $action);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$message = Utils::pluralize($action.'d_client', $count);
|
|
|
|
Session::flash('message', $message);
|
|
|
|
|
|
|
|
if ($action == 'restore' && $count == 1) {
|
2015-08-20 10:02:03 +02:00
|
|
|
return Redirect::to('clients/'.Utils::getFirst($ids));
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
return Redirect::to('clients');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|