1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/controllers/CreditController.php

144 lines
5.2 KiB
PHP
Raw Normal View History

2014-01-06 19:03:00 +01:00
<?php
use ninja\repositories\CreditRepository;
2013-12-01 21:58:25 +01:00
class CreditController extends \BaseController {
2014-01-06 19:03:00 +01:00
protected $creditRepo;
public function __construct(CreditRepository $creditRepo)
{
parent::__construct();
$this->creditRepo = $creditRepo;
}
2013-12-01 21:58:25 +01:00
/**
* 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',
2014-01-16 22:12:46 +01:00
'columns'=>['checkbox', 'Client', 'Credit Amount', 'Credit Balance', 'Credit Date', 'Private Notes', '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
{
2014-01-06 19:03:00 +01:00
$credits = $this->creditRepo->find($clientPublicId, Input::get('sSearch'));
2013-12-01 21:58:25 +01:00
2014-01-06 19:03:00 +01:00
$table = Datatable::query($credits);
2013-12-01 21:58:25 +01:00
2014-01-06 19:03:00 +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); })
2014-01-16 22:12:46 +01:00
->addColumn('balance', function($model){ return Utils::formatMoney($model->balance, $model->currency_id); })
2013-12-29 18:40:11 +01:00
->addColumn('credit_date', function($model) { return Utils::fromSqlDate($model->credit_date); })
2014-01-15 15:01:24 +01:00
->addColumn('private_notes', function($model) { return $model->private_notes; })
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">
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>';
})
2013-12-01 21:58:25 +01:00
->make();
}
2013-12-05 16:23:24 +01:00
2014-01-16 22:12:46 +01:00
public function create($clientPublicId = 0)
2013-12-05 16:23:24 +01:00
{
$data = array(
2014-01-14 12:52:56 +01:00
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
2014-01-16 22:12:46 +01:00
//'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
2013-12-05 16:23:24 +01:00
'credit' => null,
'method' => 'POST',
'url' => 'credits',
'title' => '- New Credit',
2014-01-15 15:01:24 +01:00
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-16 22:12:46 +01:00
//'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->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();
2014-01-01 00:50:13 +01:00
$credit->credit_date = Utils::fromSqlDate($credit->credit_date);
2013-12-05 16:23:24 +01:00
$data = array(
'client' => null,
'credit' => $credit,
'method' => 'PUT',
'url' => 'credits/' . $publicId,
'title' => '- Edit Credit',
2014-01-15 15:01:24 +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',
2014-01-14 12:52:56 +01:00
'amount' => 'required|positive',
2013-12-05 16:23:24 +01:00
);
2014-01-02 00:12:33 +01:00
2013-12-05 16:23:24 +01:00
$validator = Validator::make(Input::all(), $rules);
2014-01-06 19:03:00 +01:00
if ($validator->fails())
{
2013-12-05 16:23:24 +01:00
$url = $publicId ? 'credits/' . $publicId . '/edit' : 'credits/create';
return Redirect::to($url)
->withErrors($validator)
->withInput();
2014-01-06 19:03:00 +01:00
}
else
{
$this->creditRepo->save($publicId, Input::all());
2013-12-05 16:23:24 +01:00
$message = $publicId ? 'Successfully updated credit' : 'Successfully created credit';
Session::flash('message', $message);
2014-01-01 00:50:13 +01:00
return Redirect::to('clients/' . Input::get('client'));
2013-12-05 16:23:24 +01:00
}
}
public function bulk()
{
$action = Input::get('action');
2014-01-06 19:03:00 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
$count = $this->creditRepo->bulk($ids, $action);
2013-12-05 16:23:24 +01:00
2014-01-12 19:55:33 +01:00
if ($count > 0)
{
$message = Utils::pluralize('Successfully '.$action.'d ? credit', $count);
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
}
}