1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/controllers/ClientController.php

234 lines
6.1 KiB
PHP
Raw Normal View History

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-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-01 21:58:25 +01:00
$clients = Client::with('contacts')->where('account_id','=',Auth::user()->account_id)->get();
return Datatable::collection($clients)
->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->id . '">'; })
->addColumn('name', function($model) { return link_to('clients/' . $model->id, $model->name); })
->addColumn('contact', function($model) { return $model->contacts[0]->getFullName(); })
->addColumn('balance', function($model) { return '$' . $model->balance; })
->addColumn('last_login', function($model) { return $model->contacts[0]->getLastLogin(); })
2013-12-02 13:22:29 +01:00
->addColumn('date_created', function($model) { return $model->created_at->toFormattedDateString(); })
2013-12-01 21:58:25 +01:00
->addColumn('email', function($model) { return HTML::mailto($model->contacts[0]->email, $model->contacts[0]->email); })
->addColumn('phone', function($model) { return $model->contacts[0]->phone; })
->addColumn('dropdown', function($model)
{
return '<div class="btn-group tr-action" style="display:none">
<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">
<li><a href="' . URL::to('clients/'.$model->id.'/edit') . '">Edit</a></li>
<li class="divider"></li>
<li><a href="' . URL::to('clients/'.$model->id.'/archive') . '">Archive</a></li>
<li><a href="javascript:deleteEntity(' . $model->id. ')">Delete</a></li>
</ul>
</div>';
})
2013-11-26 13:45:07 +01:00
->orderColumns('name')
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',
'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
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');
}
2013-12-01 21:58:25 +01:00
public function archive($id)
{
$client = Client::find($id);
$client->delete();
foreach ($client->invoices as $invoice)
{
$invoice->delete();
}
Session::flash('message', 'Successfully archived ' . $client->name);
return Redirect::to('clients');
}
public function delete($id)
{
$client = Client::find($id);
$client->forceDelete();
Session::flash('message', 'Successfully deleted ' . $client->name);
return Redirect::to('clients');
}
2013-11-26 13:45:07 +01:00
}