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

246 lines
7.6 KiB
PHP
Raw Normal View History

2015-03-26 07:24:02 +01:00
<?php namespace App\Http\Controllers;
use Auth;
use Utils;
use View;
2015-03-31 20:50:58 +02:00
use URL;
2015-04-01 17:44:55 +02:00
use Input;
use Session;
use Redirect;
2015-04-08 15:19:17 +02:00
use Cache;
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;
2016-03-16 00:08:00 +01:00
use App\Models\Credit;
2015-05-27 18:52:10 +02:00
use App\Models\Task;
use App\Ninja\Repositories\ClientRepository;
2015-10-28 20:22:07 +01:00
use App\Services\ClientService;
2016-04-28 14:16:33 +02:00
use App\Http\Requests\ClientRequest;
2015-10-28 20:22:07 +01:00
use App\Http\Requests\CreateClientRequest;
use App\Http\Requests\UpdateClientRequest;
use App\Ninja\Datatables\ClientDatatable;
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;
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_CLIENT;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
public function __construct(ClientRepository $clientRepo, ClientService $clientService)
2015-03-16 22:45:25 +01:00
{
2016-03-02 14:36:42 +01:00
//parent::__construct();
2015-03-16 22:45:25 +01:00
$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_wrapper', [
2015-03-16 22:45:25 +01:00
'entityType' => ENTITY_CLIENT,
'datatable' => new ClientDatatable(),
2015-03-16 22:45:25 +01:00
'title' => trans('texts.clients'),
2016-11-18 14:31:43 +01:00
'statuses' => Client::getStatuses(),
]);
2015-03-16 22:45:25 +01:00
}
public function getDatatable()
{
2016-05-23 18:52:20 +02:00
$search = Input::get('sSearch');
$userId = Auth::user()->filterId();
return $this->clientService->getDatatable($search, $userId);
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
{
2016-04-28 14:16:33 +02:00
$client = $this->clientService->save($request->input());
2016-03-03 20:39:15 +01:00
2015-10-28 20:22:07 +01:00
Session::flash('message', trans('texts.created_client'));
2016-03-03 20:39:15 +01:00
2015-10-28 20:22:07 +01:00
return redirect()->to($client->getRoute());
2015-03-16 22:45:25 +01:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
2016-04-30 20:55:15 +02:00
public function show(ClientRequest $request)
2015-03-16 22:45:25 +01:00
{
2016-05-23 18:52:20 +02:00
$client = $request->entity();
2016-04-26 03:53:39 +02:00
$user = Auth::user();
2015-03-16 22:45:25 +01:00
2016-03-16 00:08:00 +01:00
$actionLinks = [];
2017-01-23 16:00:44 +01:00
if ($user->can('create', ENTITY_INVOICE)){
$actionLinks[] = ['label' => trans('texts.new_invoice'), 'url' => URL::to('/invoices/create/'.$client->public_id)];
}
if ($user->can('create', ENTITY_TASK)){
2016-04-13 11:57:03 +02:00
$actionLinks[] = ['label' => trans('texts.new_task'), 'url' => URL::to('/tasks/create/'.$client->public_id)];
2016-03-16 00:08:00 +01:00
}
2016-10-27 10:57:51 +02:00
if (Utils::hasFeature(FEATURE_QUOTES) && $user->can('create', ENTITY_QUOTE)) {
2016-04-13 11:57:03 +02:00
$actionLinks[] = ['label' => trans('texts.new_quote'), 'url' => URL::to('/quotes/create/'.$client->public_id)];
2016-03-16 00:08:00 +01:00
}
2016-05-23 18:52:20 +02:00
2016-03-16 00:08:00 +01:00
if(!empty($actionLinks)){
$actionLinks[] = \DropdownButton::DIVIDER;
}
2016-05-23 18:52:20 +02:00
2016-04-26 03:53:39 +02:00
if($user->can('create', ENTITY_PAYMENT)){
2016-04-13 11:57:03 +02:00
$actionLinks[] = ['label' => trans('texts.enter_payment'), 'url' => URL::to('/payments/create/'.$client->public_id)];
2016-03-16 00:08:00 +01:00
}
2016-05-23 18:52:20 +02:00
2016-04-26 03:53:39 +02:00
if($user->can('create', ENTITY_CREDIT)){
2016-04-13 11:57:03 +02:00
$actionLinks[] = ['label' => trans('texts.enter_credit'), 'url' => URL::to('/credits/create/'.$client->public_id)];
2016-03-16 00:08:00 +01:00
}
2016-05-23 18:52:20 +02:00
2016-04-26 03:53:39 +02:00
if($user->can('create', ENTITY_EXPENSE)){
2016-04-13 11:57:03 +02:00
$actionLinks[] = ['label' => trans('texts.enter_expense'), 'url' => URL::to('/expenses/create/0/'.$client->public_id)];
2015-03-16 22:45:25 +01:00
}
2016-03-03 20:39:15 +01:00
2016-06-20 16:14:43 +02:00
$token = $client->getGatewayToken();
$data = [
2015-03-16 22:45:25 +01:00
'actionLinks' => $actionLinks,
'showBreadcrumbs' => false,
'client' => $client,
'credit' => $client->getTotalCredit(),
'title' => trans('texts.view_client'),
2016-11-27 10:46:32 +01:00
'hasRecurringInvoices' => Invoice::scope()->recurring()->withArchived()->whereClientId($client->id)->count() > 0,
'hasQuotes' => Invoice::scope()->quotes()->withArchived()->whereClientId($client->id)->count() > 0,
'hasTasks' => Task::scope()->withArchived()->whereClientId($client->id)->count() > 0,
2016-06-20 16:14:43 +02:00
'gatewayLink' => $token ? $token->gatewayLink() : false,
'gatewayName' => $token ? $token->gatewayName() : false,
];
2016-12-08 16:09:45 +01:00
2015-03-16 22:45:25 +01:00
return View::make('clients.show', $data);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
2016-05-01 13:31:10 +02:00
public function create(ClientRequest $request)
2015-03-16 22:45:25 +01:00
{
2016-03-03 20:39:15 +01:00
if (Client::scope()->withTrashed()->count() > Auth::user()->getMaxNumClients()) {
return View::make('error', ['hideHeader' => true, 'error' => "Sorry, you've exceeded the limit of ".Auth::user()->getMaxNumClients().' clients']);
2015-03-16 22:45:25 +01:00
}
$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
*/
2016-04-28 14:16:33 +02:00
public function edit(ClientRequest $request)
2015-03-16 22:45:25 +01:00
{
2016-04-28 14:16:33 +02:00
$client = $request->entity();
2016-05-23 18:52:20 +02:00
2015-03-16 22:45:25 +01:00
$data = [
'client' => $client,
'method' => 'PUT',
2016-04-28 14:20:34 +02:00
'url' => 'clients/'.$client->public_id,
2015-03-16 22:45:25 +01:00
'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['planDetails'] = $account->getPlanDetails(false, false);
2015-11-09 20:24:22 +01:00
}
}
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'),
'account' => Auth::user()->account,
2015-04-08 15:19:17 +02:00
'sizes' => Cache::get('sizes'),
'paymentTerms' => Cache::get('paymentTerms'),
'currencies' => Cache::get('currencies'),
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
{
2016-04-28 14:16:33 +02:00
$client = $this->clientService->save($request->input(), $request->entity());
2016-03-03 20:39:15 +01:00
2015-10-28 20:22:07 +01:00
Session::flash('message', trans('texts.updated_client'));
2016-03-03 20:39:15 +01:00
2015-10-28 20:22:07 +01:00
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);
2016-09-19 15:30:46 +02:00
return $this->returnBulk(ENTITY_CLIENT, $action, $ids);
2015-03-16 22:45:25 +01:00
}
2017-01-23 16:00:44 +01:00
public function statement()
{
$account = Auth::user()->account;
$client = Client::scope(request()->client_id)->with('contacts')->firstOrFail();
$invoice = $account->createInvoice(ENTITY_INVOICE);
$invoice->client = $client;
$invoice->date_format = $account->date_format ? $account->date_format->format_moment : 'MMM D, YYYY';
$invoice->invoice_items = Invoice::scope()
->with(['client'])
->whereClientId($client->id)
->invoices()
->whereIsPublic(true)
->where('balance', '>', 0)
->get();
$data = [
'showBreadcrumbs' => false,
'client' => $client,
'invoice' => $invoice,
];
return view('clients.statement', $data);
}
2015-03-16 22:45:25 +01:00
}