2013-11-26 13:45:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ClientController extends \BaseController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
//$clients = Client::orderBy('name')->get();
|
|
|
|
//return View::make('clients.index')->with('clients', $clients);
|
|
|
|
|
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',
|
2013-12-01 21:58:25 +01:00
|
|
|
'columns'=>['checkbox', 'Client', 'Contact', 'Balance', 'Last Login', 'Date Created', 'Email', 'Phone', 'Action']
|
2013-12-01 08:33:17 +01:00
|
|
|
));
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable()
|
|
|
|
{
|
2013-12-05 21:25:20 +01:00
|
|
|
$query = DB::table('clients')
|
|
|
|
->join('contacts', 'contacts.client_id', '=', 'clients.id')
|
|
|
|
->where('clients.account_id', '=', Auth::user()->account_id)
|
|
|
|
->where('clients.deleted_at', '=', null)
|
|
|
|
->where('contacts.is_primary', '=', true)
|
|
|
|
->select('clients.public_id','clients.name','contacts.first_name','contacts.last_name','clients.balance','clients.last_login','clients.created_at','contacts.phone','contacts.email');
|
2013-12-01 21:58:25 +01:00
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
return Datatable::query($query)
|
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-05 16:23:24 +01:00
|
|
|
->addColumn('first_name', function($model) { return $model->first_name . ' ' . $model->last_name; })
|
2013-12-01 21:58:25 +01:00
|
|
|
->addColumn('balance', function($model) { return '$' . $model->balance; })
|
2013-12-05 21:25:20 +01:00
|
|
|
->addColumn('last_login', function($model) { return timestampToDateString($model->last_login); })
|
|
|
|
->addColumn('created_at', function($model) { return timestampToDateString($model->created_at); })
|
|
|
|
->addColumn('email', function($model) { return $model->email ? HTML::mailto($model->email, $model->email) : ''; })
|
2013-12-05 16:23:24 +01:00
|
|
|
->addColumn('phone', function($model) { return $model->phone; })
|
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-04 17:20:14 +01:00
|
|
|
<li><a href="' . URL::to('invoices/create/'.$model->public_id) . '">New Invoice</a></li>
|
|
|
|
<li><a href="' . URL::to('clients/'.$model->public_id.'/edit') . '">Edit Client</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>';
|
|
|
|
})
|
2013-12-05 21:25:20 +01:00
|
|
|
->orderColumns('name','first_name','balance','last_login','created_at','email','phone')
|
2013-12-01 21:58:25 +01:00
|
|
|
->make();
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
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',
|
2013-11-28 22:10:01 +01:00
|
|
|
'countries' => Country::orderBy('name')->get());
|
|
|
|
|
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
|
|
|
{
|
2013-12-04 17:20:14 +01:00
|
|
|
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
|
2013-12-07 19:45:00 +01:00
|
|
|
trackViewed($client->name, ENTITY_CLIENT);
|
2013-11-29 13:09:21 +01:00
|
|
|
|
2013-12-03 23:00:01 +01:00
|
|
|
$data = array(
|
|
|
|
'client' => $client,
|
|
|
|
'title' => '- ' . $client->name);
|
|
|
|
|
|
|
|
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,
|
2013-11-28 22:10:01 +01:00
|
|
|
'countries' => Country::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(
|
|
|
|
'name' => 'required'
|
|
|
|
);
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
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'));
|
|
|
|
} else {
|
2013-12-04 17:20:14 +01:00
|
|
|
if ($publicId) {
|
|
|
|
$client = Client::scope($publicId)->firstOrFail();
|
2013-11-28 20:06:38 +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'));
|
|
|
|
$client->notes = trim(Input::get('notes'));
|
|
|
|
$client->postal_code = trim(Input::get('postal_code'));
|
2013-12-01 13:22:08 +01:00
|
|
|
if (Input::get('country_id')) {
|
|
|
|
$client->country_id = Input::get('country_id');
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-12-07 19:45:00 +01:00
|
|
|
$record->email = trim($contact->email);
|
|
|
|
$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
|
|
|
{
|
|
|
|
$contact->forceDelete();
|
|
|
|
}
|
|
|
|
}
|
2013-11-28 17:40:13 +01:00
|
|
|
|
2013-11-26 13:45:07 +01:00
|
|
|
Session::flash('message', 'Successfully updated 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');
|
2013-12-04 17:20:14 +01:00
|
|
|
$clients = Client::scope($ids)->get();
|
2013-12-01 08:33:17 +01:00
|
|
|
|
|
|
|
foreach ($clients as $client) {
|
|
|
|
if ($action == 'archive') {
|
|
|
|
$client->delete();
|
|
|
|
} else if ($action == 'delete') {
|
|
|
|
$client->forceDelete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = pluralize('Successfully '.$action.'d ? client', count($ids));
|
|
|
|
Session::flash('message', $message);
|
2013-11-26 13:45:07 +01:00
|
|
|
|
|
|
|
return Redirect::to('clients');
|
|
|
|
}
|
|
|
|
}
|