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,
|
|
|
|
'columns'=>['checkbox', 'Client', 'Contact', 'Balance', 'Last Login', 'Date Created', 'Email', 'Phone']
|
|
|
|
));
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable()
|
|
|
|
{
|
|
|
|
return Datatable::collection(Client::with('contacts')->where('account_id','=',Auth::user()->account_id)->get())
|
2013-12-01 08:33:17 +01:00
|
|
|
->addColumn('checkbox', function($model)
|
|
|
|
{
|
|
|
|
return '<input type="checkbox" name="ids[]" value="' . $model->id . '">';
|
|
|
|
})
|
2013-11-26 13:45:07 +01:00
|
|
|
->addColumn('name', function($model)
|
|
|
|
{
|
|
|
|
//return $model->name;
|
|
|
|
return link_to('clients/' . $model->id, $model->name);
|
|
|
|
})
|
|
|
|
->addColumn('contact', function($model)
|
|
|
|
{
|
2013-11-26 22:45:10 +01:00
|
|
|
return $model->contacts[0]->getFullName();
|
2013-11-26 13:45:07 +01:00
|
|
|
})
|
2013-12-01 08:33:17 +01:00
|
|
|
->addColumn('balance', function($model)
|
|
|
|
{
|
|
|
|
return '$' . $model->balance;
|
|
|
|
})
|
2013-11-26 13:45:07 +01:00
|
|
|
->addColumn('last_login', function($model)
|
|
|
|
{
|
2013-12-01 08:33:17 +01:00
|
|
|
return $model->contacts[0]->getLastLogin();
|
|
|
|
})
|
|
|
|
->addColumn('date_created', function($model)
|
|
|
|
{
|
|
|
|
return $model->getDateCreated();
|
2013-11-26 13:45:07 +01:00
|
|
|
})
|
|
|
|
->addColumn('email', function($model)
|
|
|
|
{
|
|
|
|
//return $model->contacts[0]->email;
|
|
|
|
return HTML::mailto($model->contacts[0]->email, $model->contacts[0]->email);
|
|
|
|
})
|
|
|
|
->addColumn('phone', function($model)
|
|
|
|
{
|
|
|
|
return $model->contacts[0]->phone;
|
|
|
|
})
|
|
|
|
->orderColumns('name')
|
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
'title' => 'New',
|
|
|
|
'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
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2013-11-29 13:09:21 +01:00
|
|
|
$client = Client::with('contacts')->find($id);
|
2013-12-01 13:22:08 +01:00
|
|
|
trackViewed($client->name);
|
2013-11-29 13:09:21 +01:00
|
|
|
|
|
|
|
|
2013-11-26 13:45:07 +01:00
|
|
|
return View::make('clients.show')->with('client', $client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2013-11-28 20:06:38 +01:00
|
|
|
$client = Client::with('contacts')->find($id);
|
2013-11-28 22:10:01 +01:00
|
|
|
$data = array(
|
|
|
|
'client' => $client,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'clients/' . $id,
|
|
|
|
'title' => 'Edit',
|
|
|
|
'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
|
|
|
|
*/
|
|
|
|
public function update($id)
|
2013-11-28 20:06:38 +01:00
|
|
|
{
|
|
|
|
return $this->save($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function save($id = null)
|
2013-11-26 13:45:07 +01:00
|
|
|
{
|
|
|
|
$rules = array(
|
|
|
|
'name' => 'required'
|
|
|
|
);
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to('clients/' . $id . '/edit')
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput(Input::except('password'));
|
|
|
|
} else {
|
2013-11-28 20:06:38 +01:00
|
|
|
if ($id) {
|
|
|
|
$client = Client::find($id);
|
|
|
|
} else {
|
|
|
|
$client = new Client;
|
2013-11-29 13:09:21 +01:00
|
|
|
$client->account_id = Auth::user()->account_id;
|
2013-11-28 20:06:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 17:40:13 +01:00
|
|
|
$client->name = Input::get('name');
|
|
|
|
$client->work_phone = Input::get('work_phone');
|
|
|
|
$client->address1 = Input::get('address1');
|
|
|
|
$client->address2 = Input::get('address2');
|
|
|
|
$client->city = Input::get('city');
|
|
|
|
$client->state = Input::get('state');
|
|
|
|
$client->notes = Input::get('notes');
|
|
|
|
$client->postal_code = 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 = [];
|
|
|
|
|
|
|
|
foreach ($data->contacts as $contact)
|
|
|
|
{
|
|
|
|
if (isset($contact->id) && $contact->id)
|
|
|
|
{
|
|
|
|
$record = Contact::find($contact->id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$record = new Contact;
|
|
|
|
}
|
|
|
|
|
|
|
|
$record->email = $contact->email;
|
|
|
|
$record->first_name = $contact->first_name;
|
|
|
|
$record->last_name = $contact->last_name;
|
|
|
|
$record->phone = $contact->phone;
|
|
|
|
|
|
|
|
$client->contacts()->save($record);
|
|
|
|
$contactIds[] = $record->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($client->contacts as $contact)
|
|
|
|
{
|
|
|
|
if (!in_array($contact->id, $contactIds))
|
|
|
|
{
|
|
|
|
$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-11-28 22:10:01 +01:00
|
|
|
return Redirect::to('clients/' . $client->id);
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-01 08:33:17 +01:00
|
|
|
* Bulk update the records
|
2013-11-26 13:45:07 +01:00
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
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');
|
|
|
|
$ids = Input::get('ids');
|
|
|
|
$clients = Client::find($ids);
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|