1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/controllers/CreditController.php

160 lines
6.1 KiB
PHP
Raw Normal View History

2013-12-01 21:58:25 +01:00
<?php
class CreditController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list', array(
'entityType'=>ENTITY_CREDIT,
2013-12-03 23:00:01 +01:00
'title' => '- Credits',
2013-12-11 21:33:44 +01:00
'columns'=>['checkbox', 'Client', 'Credit Amount', 'Credit Date', 'Action']
2013-12-01 21:58:25 +01:00
));
}
2013-12-04 17:20:14 +01:00
public function getDatatable($clientPublicId = null)
2013-12-01 21:58:25 +01:00
{
2013-12-05 21:25:20 +01:00
$query = DB::table('credits')
->join('clients', 'clients.id', '=','credits.client_id')
2013-12-30 21:17:45 +01:00
->join('contacts', 'contacts.client_id', '=', 'clients.id')
2013-12-05 21:25:20 +01:00
->where('clients.account_id', '=', Auth::user()->account_id)
->where('clients.deleted_at', '=', null)
2013-12-15 13:55:50 +01:00
->where('credits.deleted_at', '=', null)
2013-12-30 21:17:45 +01:00
->where('contacts.is_primary', '=', true)
->select('credits.public_id', 'clients.name as client_name', 'clients.public_id as client_public_id', 'credits.amount', 'credits.credit_date', 'credits.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email');
2013-12-01 21:58:25 +01:00
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
2013-12-05 21:25:20 +01:00
$query->where('clients.public_id', '=', $clientPublicId);
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.'%');
});
}
2013-12-05 21:25:20 +01:00
$table = Datatable::query($query);
2013-12-01 21:58:25 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
2013-12-05 16:23:24 +01:00
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; })
2013-12-30 21:17:45 +01:00
->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
2013-12-01 21:58:25 +01:00
}
2013-12-29 18:40:11 +01:00
return $table->addColumn('amount', function($model){ return Utils::formatMoney($model->amount, $model->currency_id); })
->addColumn('credit_date', function($model) { return Utils::fromSqlDate($model->credit_date); })
2013-12-05 16:23:24 +01:00
->addColumn('dropdown', function($model)
{
2013-12-05 21:25:20 +01:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-05 16:23:24 +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">
<li><a href="' . URL::to('credits/'.$model->public_id.'/edit') . '">Edit Credit</a></li>
<li class="divider"></li>
2013-12-15 13:55:50 +01:00
<li><a href="javascript:archiveEntity(' . $model->public_id. ')">Archive Credit</a></li>
2013-12-05 16:23:24 +01:00
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Credit</a></li>
</ul>
</div>';
})
->orderColumns('number')
2013-12-01 21:58:25 +01:00
->make();
}
2013-12-05 16:23:24 +01:00
2013-12-15 13:55:50 +01:00
public function create($clientPublicId = null)
2013-12-05 16:23:24 +01:00
{
$data = array(
2013-12-30 21:17:45 +01:00
'clientPublicId' => $clientPublicId,
2013-12-05 16:23:24 +01:00
'credit' => null,
'method' => 'POST',
'url' => 'credits',
'title' => '- New Credit',
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-30 21:17:45 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
2013-12-05 16:23:24 +01:00
return View::make('credits.edit', $data);
}
public function edit($publicId)
2013-12-01 21:58:25 +01:00
{
2013-12-04 17:20:14 +01:00
$credit = Credit::scope($publicId)->firstOrFail();
2013-12-05 16:23:24 +01:00
$data = array(
'client' => null,
'credit' => $credit,
'method' => 'PUT',
'url' => 'credits/' . $publicId,
'title' => '- Edit Credit',
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-30 21:17:45 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
2013-12-05 16:23:24 +01:00
return View::make('credit.edit', $data);
}
2013-12-01 21:58:25 +01:00
2013-12-05 16:23:24 +01:00
public function store()
{
return $this->save();
2013-12-01 21:58:25 +01:00
}
2013-12-05 16:23:24 +01:00
public function update($publicId)
2013-12-01 21:58:25 +01:00
{
2013-12-05 16:23:24 +01:00
return $this->save($publicId);
}
private function save($publicId = null)
{
$rules = array(
'client' => 'required',
'amount' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
$url = $publicId ? 'credits/' . $publicId . '/edit' : 'credits/create';
return Redirect::to($url)
->withErrors($validator)
->withInput();
} else {
if ($publicId) {
$credit = Credit::scope($publicId)->firstOrFail();
} else {
$credit = Credit::createNew();
}
$credit->client_id = Input::get('client');
2013-12-07 21:33:07 +01:00
$credit->credit_date = Utils::toSqlDate(Input::get('credit_date'));
2013-12-07 19:45:00 +01:00
$credit->amount = floatval(Input::get('amount'));
2013-12-29 18:40:11 +01:00
$credit->currency_id = Input::get('currency_id') ? Input::get('currency_id') : null;
2013-12-05 16:23:24 +01:00
$credit->save();
$message = $publicId ? 'Successfully updated credit' : 'Successfully created credit';
Session::flash('message', $message);
return Redirect::to('clients/' . $credit->client_id);
}
}
public function bulk()
{
$action = Input::get('action');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2013-12-05 16:23:24 +01:00
$credits = Credit::scope($ids)->get();
foreach ($credits as $credit) {
2013-12-15 13:55:50 +01:00
if ($action == 'delete') {
$credit->is_deleted = true;
$credit->save();
2013-12-05 16:23:24 +01:00
}
2013-12-15 13:55:50 +01:00
$credit->delete();
2013-12-05 16:23:24 +01:00
}
2013-12-15 13:55:50 +01:00
$message = Utils::pluralize('Successfully '.$action.'d ? credit', count($credits));
2013-12-05 16:23:24 +01:00
Session::flash('message', $message);
2013-12-01 21:58:25 +01:00
2013-12-05 16:23:24 +01:00
return Redirect::to('credits');
2013-12-01 21:58:25 +01:00
}
}