1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/controllers/ClientController.php

255 lines
8.2 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-03 23:00:01 +01:00
'title' => '- Clients',
2013-12-11 21:33:44 +01:00
'columns'=>['checkbox', 'Client', 'Contact', 'Date Created', 'Email', 'Phone', '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
{
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)
2013-12-07 21:33:07 +01:00
->select('clients.public_id','clients.name','contacts.first_name','contacts.last_name','clients.balance','clients.last_login','clients.created_at','clients.work_phone','contacts.email');
2013-12-01 21:58:25 +01:00
2013-12-11 21:33:44 +01:00
$filter = Input::get('sSearch');
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
->orWhere('contacts.email', 'like', '%'.$filter.'%');
});
}
//$query->get();
//dd(DB::getQueryLog());
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-24 07:22:16 +01:00
->addColumn('created_at', function($model) { return Utils::timestampToDateString(strtotime($model->created_at)); })
2013-12-05 21:25:20 +01:00
->addColumn('email', function($model) { return $model->email ? HTML::mailto($model->email, $model->email) : ''; })
2013-12-07 21:33:07 +01:00
->addColumn('work_phone', function($model) { return Utils::formatPhoneNumber($model->work_phone); })
2013-12-11 21:33:44 +01:00
->addColumn('last_login', function($model) { return Utils::timestampToDateString($model->last_login); })
->addColumn('balance', function($model) { return '$' . $model->balance; })
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>';
})
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-12-11 21:33:44 +01:00
'clientSizes' => ClientSize::orderBy('id')->get(),
'clientIndustries' => ClientIndustry::orderBy('name')->get(),
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-11 21:33:44 +01:00
$client = Client::scope($publicId)->with('contacts', 'client_size', 'client_industry')->firstOrFail();
2013-12-07 21:33:07 +01:00
Utils::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,
2013-12-10 23:10:43 +01:00
'title' => '- ' . $client->name,
2013-12-11 21:33:44 +01:00
'hasRecurringInvoices' => Invoice::scope()->where('frequency_id', '>', '0')->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,
2013-12-11 21:33:44 +01:00
'clientSizes' => ClientSize::orderBy('id')->get(),
'clientIndustries' => ClientIndustry::orderBy('name')->get(),
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'));
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-15 13:55:50 +01:00
$client->notes = trim(Input::get('notes'));
2013-12-11 21:33:44 +01:00
$client->client_size_id = Input::get('client_size_id') ? Input::get('client_size_id') : null;
$client->client_industry_id = Input::get('client_industry_id') ? Input::get('client_industry_id') : null;
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
}
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-12-11 21:33:44 +01:00
if ($publicId) {
Session::flash('message', 'Successfully updated client');
} else {
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');
2013-12-04 17:20:14 +01:00
$clients = Client::scope($ids)->get();
2013-12-01 08:33:17 +01:00
2013-12-15 13:55:50 +01:00
foreach ($clients as $client) {
if ($action == 'delete') {
$client->is_deleted = true;
$client->save();
2013-12-01 08:33:17 +01:00
}
2013-12-15 13:55:50 +01:00
$client->delete();
2013-12-01 08:33:17 +01:00
}
2013-12-15 13:55:50 +01:00
$message = Utils::pluralize('Successfully '.$action.'d ? client', count($clients));
2013-12-01 08:33:17 +01:00
Session::flash('message', $message);
2013-11-26 13:45:07 +01:00
return Redirect::to('clients');
}
}