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

142 lines
4.8 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-05 16:23:24 +01:00
'columns'=>['checkbox', 'Client', '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-03 18:32:33 +01:00
$collection = Credit::scope()->with('client');
2013-12-01 21:58:25 +01:00
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
$clientId = Client::getPrivateId($clientPublicId);
2013-12-01 21:58:25 +01:00
$collection->where('client_id','=',$clientId);
}
$table = Datatable::collection($collection->get());
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 . '">'; })
->addColumn('client', function($model) { return link_to('clients/' . $model->client->public_id, $model->client->name); });
2013-12-01 21:58:25 +01:00
}
return $table->addColumn('amount', function($model){ return '$' . money_format('%i', $model->amount); })
->addColumn('credit_date', function($model) { return (new Carbon($model->credit_date))->toFormattedDateString(); })
2013-12-05 16:23:24 +01:00
->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('credits/'.$model->public_id.'/edit') . '">Edit Credit</a></li>
<li class="divider"></li>
<li><a href="' . URL::to('credits/'.$model->public_id.'/archive') . '">Archive Credit</a></li>
<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
public function create()
{
$data = array(
'client' => null,
'credit' => null,
'method' => 'POST',
'url' => 'credits',
'title' => '- New Credit',
'clients' => Client::scope()->orderBy('name')->get());
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',
'clients' => Client::scope()->orderBy('name')->get());
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');
$credit->credit_date = toSqlDate(Input::get('credit_date'));
$credit->amount = Input::get('amount');
$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');
$ids = Input::get('ids');
$credits = Credit::scope($ids)->get();
foreach ($credits as $credit) {
if ($action == 'archive') {
$credit->delete();
} else if ($action == 'delete') {
$credit->forceDelete();
}
}
$message = pluralize('Successfully '.$action.'d ? credit', count($ids));
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
}
}