1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Http/Controllers/ClientController.php

277 lines
8.6 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-26 07:24:02 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Http\Requests\ClientRequest;
use App\Http\Requests\CreateClientRequest;
use App\Http\Requests\UpdateClientRequest;
2015-11-09 20:24:22 +01:00
use App\Models\Account;
2017-01-30 20:40:43 +01:00
use App\Models\Client;
2016-03-16 00:08:00 +01:00
use App\Models\Credit;
2017-01-30 20:40:43 +01:00
use App\Models\Invoice;
2015-05-27 18:52:10 +02:00
use App\Models\Task;
2017-01-30 20:40:43 +01:00
use App\Ninja\Datatables\ClientDatatable;
use App\Ninja\Repositories\ClientRepository;
2015-10-28 20:22:07 +01:00
use App\Services\ClientService;
2017-01-30 20:40:43 +01:00
use Auth;
use Cache;
use Input;
use Redirect;
use Session;
use URL;
use Utils;
use View;
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.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2015-03-16 22:45:25 +01:00
* @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-30 17:05:31 +01:00
if ($user->can('create', ENTITY_INVOICE)) {
2017-01-23 16:00:44 +01:00
$actionLinks[] = ['label' => trans('texts.new_invoice'), 'url' => URL::to('/invoices/create/'.$client->public_id)];
}
2017-01-30 17:05:31 +01:00
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
}
if ($user->can('create', ENTITY_RECURRING_INVOICE)) {
$actionLinks[] = ['label' => trans('texts.new_recurring_invoice'), 'url' => URL::to('/recurring_invoices/create/'.$client->public_id)];
}
2016-05-23 18:52:20 +02:00
2017-01-30 20:40:43 +01:00
if (! empty($actionLinks)) {
2016-03-16 00:08:00 +01:00
$actionLinks[] = \DropdownButton::DIVIDER;
}
2016-05-23 18:52:20 +02:00
2017-01-30 17:05:31 +01: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
2017-01-30 17:05:31 +01: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
2017-01-30 17:05:31 +01: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.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2015-03-16 22:45:25 +01:00
* @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'),
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.
*
2017-01-30 20:40:43 +01:00
* @param int $id
*
2015-03-16 22:45:25 +01:00
* @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($clientPublicId, $statusId = false, $startDate = false, $endDate = false)
2017-01-23 16:00:44 +01:00
{
$account = Auth::user()->account;
$statusId = intval($statusId);
2017-01-23 16:00:44 +01:00
$client = Client::scope(request()->client_id)->with('contacts')->firstOrFail();
if (! $startDate) {
$startDate = Utils::today(false)->modify('-6 month')->format('Y-m-d');
$endDate = Utils::today(false)->format('Y-m-d');
}
2017-01-23 16:00:44 +01:00
$invoice = $account->createInvoice(ENTITY_INVOICE);
$invoice->client = $client;
$invoice->date_format = $account->date_format ? $account->date_format->format_moment : 'MMM D, YYYY';
$invoices = Invoice::scope()
2017-01-23 16:00:44 +01:00
->with(['client'])
->invoices()
->whereClientId($client->id)
2017-01-23 16:00:44 +01:00
->whereIsPublic(true)
->orderBy('invoice_date', 'asc');
if ($statusId == INVOICE_STATUS_PAID) {
$invoices->where('invoice_status_id', '=', INVOICE_STATUS_PAID);
} elseif ($statusId == INVOICE_STATUS_UNPAID) {
$invoices->where('invoice_status_id', '!=', INVOICE_STATUS_PAID);
}
if ($statusId == INVOICE_STATUS_PAID || ! $statusId) {
$invoices->where('invoice_date', '>=', $startDate)
->where('invoice_date', '<=', $endDate);
}
$invoice->invoice_items = $invoices->get();
if (request()->json) {
return json_encode($invoice);
}
2017-01-23 16:00:44 +01:00
$data = [
'showBreadcrumbs' => false,
'client' => $client,
'account' => $account,
'startDate' => $startDate,
'endDate' => $endDate,
2017-01-23 16:00:44 +01:00
];
return view('clients.statement', $data);
}
2015-03-16 22:45:25 +01:00
}