1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/controllers/ClientController.php

255 lines
8.1 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2014-01-06 19:03:00 +01:00
use ninja\repositories\ClientRepository;
2013-11-26 13:45:07 +01:00
class ClientController extends \BaseController {
2014-01-06 19:03:00 +01:00
protected $clientRepo;
public function __construct(ClientRepository $clientRepo)
{
parent::__construct();
$this->clientRepo = $clientRepo;
}
2013-11-26 13:45:07 +01:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2013-12-01 08:33:17 +01:00
return View::make('list', array(
'entityType'=>ENTITY_CLIENT,
2013-12-03 23:00:01 +01:00
'title' => '- Clients',
2014-01-02 18:16:00 +01:00
'columns'=>['checkbox', 'Client', 'Contact', 'Email', 'Date Created', 'Last Login', 'Balance', 'Action']
2013-12-01 08:33:17 +01:00
));
2013-11-26 13:45:07 +01:00
}
public function getDatatable()
2013-12-11 21:33:44 +01:00
{
2014-01-06 19:03:00 +01:00
$clients = $this->clientRepo->find(Input::get('sSearch'));
2013-12-11 21:33:44 +01:00
2014-01-06 19:03:00 +01:00
return Datatable::query($clients)
2013-12-04 17:20:14 +01:00
->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; })
->addColumn('name', function($model) { return link_to('clients/' . $model->public_id, $model->name); })
2013-12-30 21:17:45 +01:00
->addColumn('first_name', function($model) { return link_to('clients/' . $model->public_id, $model->first_name . ' ' . $model->last_name); })
->addColumn('email', function($model) { return link_to('clients/' . $model->public_id, $model->email); })
2013-12-24 07:22:16 +01:00
->addColumn('created_at', function($model) { return Utils::timestampToDateString(strtotime($model->created_at)); })
2014-01-09 20:00:08 +01:00
->addColumn('last_login', function($model) { return Utils::timestampToDateString(strtotime($model->last_login)); })
2013-12-29 18:40:11 +01:00
->addColumn('balance', function($model) { return Utils::formatMoney($model->balance, $model->currency_id); })
2013-12-01 21:58:25 +01:00
->addColumn('dropdown', function($model)
{
2013-12-05 16:23:24 +01:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-01 21:58:25 +01:00
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
Select <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
2013-12-09 10:38:49 +01:00
<li><a href="' . URL::to('clients/'.$model->public_id.'/edit') . '">Edit Client</a></li>
<li class="divider"></li>
<li><a href="' . URL::to('invoices/create/'.$model->public_id) . '">New Invoice</a></li>
<li><a href="' . URL::to('payments/create/'.$model->public_id) . '">New Payment</a></li>
<li><a href="' . URL::to('credits/create/'.$model->public_id) . '">New Credit</a></li>
2013-12-01 21:58:25 +01:00
<li class="divider"></li>
2013-12-05 16:23:24 +01:00
<li><a href="javascript:archiveEntity(' . $model->public_id. ')">Archive Client</a></li>
2013-12-04 17:20:14 +01:00
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Client</a></li>
2013-12-01 21:58:25 +01:00
</ul>
</div>';
})
->make();
2013-11-26 13:45:07 +01:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
2014-02-01 21:01:32 +01:00
if (Client::scope()->count() > MAX_NUM_CLIENTS)
{
return View::make('error', ['error' => "Sorry, you've exceeded the limit of " . MAX_NUM_CLIENTS . " clients"]);
}
2013-11-28 22:10:01 +01:00
$data = array(
'client' => null,
'method' => 'POST',
'url' => 'clients',
2013-12-03 23:00:01 +01:00
'title' => '- New Client',
2014-01-06 19:03:00 +01:00
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-31 10:43:39 +01:00
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get());
2013-11-28 22:10:01 +01:00
2013-11-26 13:45:07 +01:00
return View::make('clients.edit', $data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
2013-11-28 20:06:38 +01:00
return $this->save();
2013-11-26 13:45:07 +01:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function show($publicId)
2013-11-26 13:45:07 +01:00
{
2014-01-06 19:03:00 +01:00
$client = Client::scope($publicId)->with('contacts', 'size', 'industry')->firstOrFail();
2014-01-01 00:50:13 +01:00
Utils::trackViewed($client->getDisplayName(), ENTITY_CLIENT);
2013-11-29 13:09:21 +01:00
2013-12-03 23:00:01 +01:00
$data = array(
'client' => $client,
2014-01-16 22:12:46 +01:00
'credit' => $client->getTotalCredit(),
2014-01-01 00:50:13 +01:00
'title' => '- ' . $client->getDisplayName(),
2013-12-30 21:17:45 +01:00
'hasRecurringInvoices' => Invoice::scope()->where('is_recurring', '=', true)->whereClientId($client->id)->count() > 0
2013-12-10 23:10:43 +01:00
);
2013-12-03 23:00:01 +01:00
return View::make('clients.show', $data);
2013-11-26 13:45:07 +01:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function edit($publicId)
2013-11-26 13:45:07 +01:00
{
2013-12-04 17:20:14 +01:00
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
2013-11-28 22:10:01 +01:00
$data = array(
'client' => $client,
'method' => 'PUT',
2013-12-04 17:20:14 +01:00
'url' => 'clients/' . $publicId,
2013-12-03 23:00:01 +01:00
'title' => '- ' . $client->name,
2014-01-06 19:03:00 +01:00
'sizes' => Size::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
2013-12-31 10:43:39 +01:00
'paymentTerms' => PaymentTerm::remember(DEFAULT_QUERY_CACHE)->orderBy('num_days')->get(['name', 'num_days']),
2014-01-06 19:03:00 +01:00
'industries' => Industry::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get());
2013-11-26 13:45:07 +01:00
return View::make('clients.edit', $data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
2013-12-04 17:20:14 +01:00
public function update($publicId)
2013-11-28 20:06:38 +01:00
{
2013-12-04 17:20:14 +01:00
return $this->save($publicId);
2013-11-28 20:06:38 +01:00
}
2013-12-04 17:20:14 +01:00
private function save($publicId = null)
2013-11-26 13:45:07 +01:00
{
$rules = array(
2013-12-30 21:17:45 +01:00
'email' => 'required'
2013-11-26 13:45:07 +01:00
);
$validator = Validator::make(Input::all(), $rules);
2014-02-01 21:01:32 +01:00
if ($validator->fails())
{
2013-12-05 16:23:24 +01:00
$url = $publicId ? 'clients/' . $publicId . '/edit' : 'clients/create';
return Redirect::to($url)
2013-11-26 13:45:07 +01:00
->withErrors($validator)
->withInput(Input::except('password'));
2014-02-01 21:01:32 +01:00
}
else
{
if ($publicId)
{
2013-12-04 17:20:14 +01:00
$client = Client::scope($publicId)->firstOrFail();
2014-02-01 21:01:32 +01:00
}
else
{
2013-12-04 17:20:14 +01:00
$client = Client::createNew();
2013-11-28 20:06:38 +01:00
}
2013-12-07 19:45:00 +01:00
$client->name = trim(Input::get('name'));
$client->work_phone = trim(Input::get('work_phone'));
$client->address1 = trim(Input::get('address1'));
$client->address2 = trim(Input::get('address2'));
$client->city = trim(Input::get('city'));
$client->state = trim(Input::get('state'));
2013-12-11 21:33:44 +01:00
$client->postal_code = trim(Input::get('postal_code'));
$client->country_id = Input::get('country_id') ? Input::get('country_id') : null;
2013-12-30 21:17:45 +01:00
$client->private_notes = trim(Input::get('private_notes'));
2014-01-06 19:03:00 +01:00
$client->size_id = Input::get('size_id') ? Input::get('size_id') : null;
$client->industry_id = Input::get('industry_id') ? Input::get('industry_id') : null;
2014-02-02 19:14:56 +01:00
$client->currency_id = Input::get('currency_id') ? Input::get('currency_id') : 1;
2013-12-31 10:43:39 +01:00
$client->payment_terms = Input::get('payment_terms');
2013-12-15 13:55:50 +01:00
$client->website = trim(Input::get('website'));
2013-12-11 21:33:44 +01:00
2013-11-26 13:45:07 +01:00
$client->save();
2013-11-28 20:06:38 +01:00
$data = json_decode(Input::get('data'));
$contactIds = [];
2013-12-05 16:23:24 +01:00
$isPrimary = true;
2013-12-04 17:20:14 +01:00
2013-11-28 20:06:38 +01:00
foreach ($data->contacts as $contact)
{
2013-12-05 16:23:24 +01:00
if (isset($contact->public_id) && $contact->public_id)
2013-11-28 20:06:38 +01:00
{
2013-12-05 16:23:24 +01:00
$record = Contact::scope($contact->public_id)->firstOrFail();
2013-11-28 20:06:38 +01:00
}
else
{
2013-12-04 17:20:14 +01:00
$record = Contact::createNew();
2013-11-28 20:06:38 +01:00
}
2014-02-09 16:38:50 +01:00
$record->email = trim(strtolower($contact->email));
2013-12-07 19:45:00 +01:00
$record->first_name = trim($contact->first_name);
$record->last_name = trim($contact->last_name);
$record->phone = trim($contact->phone);
2013-12-05 16:23:24 +01:00
$record->is_primary = $isPrimary;
$isPrimary = false;
2013-11-28 20:06:38 +01:00
$client->contacts()->save($record);
2013-12-05 16:23:24 +01:00
$contactIds[] = $record->public_id;
2013-11-28 20:06:38 +01:00
}
foreach ($client->contacts as $contact)
{
2013-12-05 16:23:24 +01:00
if (!in_array($contact->public_id, $contactIds))
2013-11-28 20:06:38 +01:00
{
2014-01-02 14:21:15 +01:00
$contact->delete();
2013-11-28 20:06:38 +01:00
}
}
2014-01-09 20:00:08 +01:00
if ($publicId)
{
2013-12-11 21:33:44 +01:00
Session::flash('message', 'Successfully updated client');
2014-01-09 20:00:08 +01:00
}
else
{
Activity::createClient($client);
2013-12-11 21:33:44 +01:00
Session::flash('message', 'Successfully created client');
}
2013-12-04 17:20:14 +01:00
return Redirect::to('clients/' . $client->public_id);
2013-11-26 13:45:07 +01:00
}
}
2013-12-01 08:33:17 +01:00
public function bulk()
2013-11-26 13:45:07 +01:00
{
2013-12-01 08:33:17 +01:00
$action = Input::get('action');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2014-01-08 00:59:06 +01:00
$count = $this->clientRepo->bulk($ids, $action);
2013-12-01 08:33:17 +01:00
2014-01-08 00:59:06 +01:00
$message = Utils::pluralize('Successfully '.$action.'d ? client', $count);
2013-12-01 08:33:17 +01:00
Session::flash('message', $message);
2013-11-26 13:45:07 +01:00
return Redirect::to('clients');
}
}