invoiceninja/app/controllers/CreditController.php

150 lines
5.5 KiB
PHP
Raw Normal View History

2015-01-11 14:30:08 +02:00
<?php
2014-01-06 20:03:00 +02:00
use ninja\repositories\CreditRepository;
2013-12-01 22:58:25 +02:00
2015-01-11 14:30:08 +02:00
class CreditController extends \BaseController
{
2014-01-06 20:03:00 +02:00
protected $creditRepo;
public function __construct(CreditRepository $creditRepo)
{
parent::__construct();
$this->creditRepo = $creditRepo;
2015-01-11 14:30:08 +02:00
}
2014-01-06 20:03:00 +02:00
2013-12-01 22:58:25 +02:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list', array(
2015-01-11 14:30:08 +02:00
'entityType' => ENTITY_CREDIT,
2014-06-15 23:54:58 +03:00
'title' => trans('texts.credits'),
2015-01-11 14:30:08 +02:00
'columns' => Utils::trans(['checkbox', 'client', 'credit_amount', 'credit_balance', 'credit_date', 'private_notes', 'action']),
2013-12-01 22:58:25 +02:00
));
}
2013-12-04 18:20:14 +02:00
public function getDatatable($clientPublicId = null)
2013-12-01 22:58:25 +02:00
{
2014-01-06 20:03:00 +02:00
$credits = $this->creditRepo->find($clientPublicId, Input::get('sSearch'));
2013-12-01 22:58:25 +02:00
2015-01-11 14:30:08 +02:00
$table = Datatable::query($credits);
2013-12-01 22:58:25 +02:00
2015-01-11 14:30:08 +02:00
if (!$clientPublicId) {
$table->addColumn('checkbox', function ($model) { return '<input type="checkbox" name="ids[]" value="'.$model->public_id.'" '.Utils::getEntityRowClass($model).'>'; })
->addColumn('client_name', function ($model) { return link_to('clients/'.$model->client_public_id, Utils::getClientDisplayName($model)); });
2013-12-01 22:58:25 +02:00
}
2015-01-11 14:30:08 +02:00
return $table->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id).'<span '.Utils::getEntityRowClass($model).'/>'; })
->addColumn('balance', function ($model) { return Utils::formatMoney($model->balance, $model->currency_id); })
->addColumn('credit_date', function ($model) { return Utils::fromSqlDate($model->credit_date); })
->addColumn('private_notes', function ($model) { return $model->private_notes; })
->addColumn('dropdown', function ($model) {
if ($model->is_deleted) {
2014-11-23 23:47:10 +02:00
return '<div style="height:38px"/>';
}
$str = '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-05 17:23:24 +02:00
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
2014-03-27 14:25:31 +02:00
'.trans('texts.select').' <span class="caret"></span>
2013-12-05 17:23:24 +02:00
</button>
2014-11-23 23:47:10 +02:00
<ul class="dropdown-menu" role="menu">';
2015-01-11 14:30:08 +02:00
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
$str .= '<li><a href="javascript:archiveEntity('.$model->public_id.')">'.trans('texts.archive_credit').'</a></li>';
} else {
$str .= '<li><a href="javascript:restoreEntity('.$model->public_id.')">'.trans('texts.restore_credit').'</a></li>';
2014-11-23 23:47:10 +02:00
}
2015-01-11 14:30:08 +02:00
return $str.'<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_credit').'</a></li></ul>
2013-12-05 17:23:24 +02:00
</div>';
2015-01-11 14:30:08 +02:00
})
->make();
2013-12-01 22:58:25 +02:00
}
2014-01-16 21:12:46 +00:00
public function create($clientPublicId = 0)
2015-01-11 14:30:08 +02:00
{
2013-12-05 17:23:24 +02:00
$data = array(
2014-01-14 11:52:56 +00:00
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
2014-01-16 21:12:46 +00:00
//'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
2015-01-11 14:30:08 +02:00
'credit' => null,
'method' => 'POST',
'url' => 'credits',
2014-06-15 23:54:58 +03:00
'title' => trans('texts.new_credit'),
2014-01-15 14:01:24 +00:00
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-16 21:12:46 +00:00
//'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
2015-01-11 14:30:08 +02:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
2013-12-05 17:23:24 +02:00
return View::make('credits.edit', $data);
}
public function edit($publicId)
2013-12-01 22:58:25 +02:00
{
2013-12-04 18:20:14 +02:00
$credit = Credit::scope($publicId)->firstOrFail();
2014-01-01 01:50:13 +02:00
$credit->credit_date = Utils::fromSqlDate($credit->credit_date);
2013-12-05 17:23:24 +02:00
$data = array(
'client' => null,
2015-01-11 14:30:08 +02:00
'credit' => $credit,
'method' => 'PUT',
'url' => 'credits/'.$publicId,
2014-06-15 23:54:58 +03:00
'title' => 'Edit Credit',
2014-01-15 14:01:24 +00:00
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2015-01-11 14:30:08 +02:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
2013-12-05 17:23:24 +02:00
return View::make('credit.edit', $data);
}
2013-12-01 22:58:25 +02:00
2013-12-05 17:23:24 +02:00
public function store()
{
return $this->save();
2013-12-01 22:58:25 +02:00
}
2013-12-05 17:23:24 +02:00
public function update($publicId)
2013-12-01 22:58:25 +02:00
{
2013-12-05 17:23:24 +02:00
return $this->save($publicId);
}
private function save($publicId = null)
{
$rules = array(
'client' => 'required',
2014-01-14 11:52:56 +00:00
'amount' => 'required|positive',
2013-12-05 17:23:24 +02:00
);
2014-01-02 01:12:33 +02:00
2013-12-05 17:23:24 +02:00
$validator = Validator::make(Input::all(), $rules);
2015-01-11 14:30:08 +02:00
if ($validator->fails()) {
$url = $publicId ? 'credits/'.$publicId.'/edit' : 'credits/create';
2013-12-05 17:23:24 +02:00
return Redirect::to($url)
->withErrors($validator)
->withInput();
2015-01-11 14:30:08 +02:00
} else {
2014-01-06 20:03:00 +02:00
$this->creditRepo->save($publicId, Input::all());
2013-12-05 17:23:24 +02:00
2014-04-01 13:30:43 +03:00
$message = trans('texts.created_credit');
2013-12-05 17:23:24 +02:00
Session::flash('message', $message);
2015-01-11 14:30:08 +02:00
return Redirect::to('clients/'.Input::get('client'));
2013-12-05 17:23:24 +02:00
}
}
public function bulk()
{
$action = Input::get('action');
2015-01-11 14:30:08 +02:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2014-01-06 20:03:00 +02:00
$count = $this->creditRepo->bulk($ids, $action);
2013-12-05 17:23:24 +02:00
2015-01-11 14:30:08 +02:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_credit', $count);
2014-01-12 18:55:33 +00:00
Session::flash('message', $message);
}
2013-12-01 22:58:25 +02:00
2013-12-05 17:23:24 +02:00
return Redirect::to('credits');
2013-12-01 22:58:25 +02:00
}
2015-01-11 14:30:08 +02:00
}