1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/CreditController.php

138 lines
5.3 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-16 22:45:25 +01:00
2015-03-26 07:24:02 +01:00
use Datatable;
use Input;
use Redirect;
use Session;
2015-11-04 08:48:47 +01:00
use URL;
2015-03-26 07:24:02 +01:00
use Utils;
use View;
2015-04-01 17:44:55 +02:00
use Validator;
use App\Models\Client;
2015-10-28 20:22:07 +01:00
use App\Services\CreditService;
use App\Ninja\Repositories\CreditRepository;
2015-10-28 20:22:07 +01:00
use App\Http\Requests\CreateCreditRequest;
2015-03-16 22:45:25 +01:00
2015-03-26 07:24:02 +01:00
class CreditController extends BaseController
2015-03-16 22:45:25 +01:00
{
protected $creditRepo;
2015-10-28 20:22:07 +01:00
protected $CreditService;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
public function __construct(CreditRepository $creditRepo, CreditService $creditService)
2015-03-16 22:45:25 +01:00
{
parent::__construct();
$this->creditRepo = $creditRepo;
2015-10-28 20:22:07 +01:00
$this->creditService = $creditService;
2015-03-16 22:45:25 +01:00
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list', array(
'entityType' => ENTITY_CREDIT,
'title' => trans('texts.credits'),
2015-04-28 22:13:52 +02:00
'sortCol' => '4',
2015-03-16 22:45:25 +01:00
'columns' => Utils::trans(['checkbox', 'client', 'credit_amount', 'credit_balance', 'credit_date', 'private_notes', 'action']),
));
}
public function getDatatable($clientPublicId = null)
{
$credits = $this->creditRepo->find($clientPublicId, Input::get('sSearch'));
$table = Datatable::query($credits);
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)); });
}
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) {
return '<div style="height:38px"/>';
}
$str = '<div class="btn-group tr-action" style="visibility:hidden;">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
'.trans('texts.select').' <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">';
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
2015-11-04 08:48:47 +01:00
$str .= '<li><a href="'.URL::to('payments/create/'.$model->client_public_id).'?paymentTypeId=1">'.trans('texts.apply_credit').'</a></li>
<li class="divider"></li>
<li><a href="javascript:archiveEntity('.$model->public_id.')">'.trans('texts.archive_credit').'</a></li>';
2015-03-16 22:45:25 +01:00
} else {
$str .= '<li><a href="javascript:restoreEntity('.$model->public_id.')">'.trans('texts.restore_credit').'</a></li>';
}
return $str.'<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_credit').'</a></li></ul>
</div>';
})
->make();
}
public function create($clientPublicId = 0)
{
$data = array(
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
//'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
'credit' => null,
'method' => 'POST',
'url' => 'credits',
'title' => trans('texts.new_credit'),
//'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('credits.edit', $data);
}
public function edit($publicId)
{
$credit = Credit::scope($publicId)->firstOrFail();
$credit->credit_date = Utils::fromSqlDate($credit->credit_date);
$data = array(
'client' => null,
'credit' => $credit,
'method' => 'PUT',
'url' => 'credits/'.$publicId,
'title' => 'Edit Credit',
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('credit.edit', $data);
}
2015-10-28 20:22:07 +01:00
public function store(CreateCreditRequest $request)
2015-03-16 22:45:25 +01:00
{
2015-10-28 20:22:07 +01:00
$credit = $this->creditRepo->save($request->input());
Session::flash('message', trans('texts.created_credit'));
return redirect()->to($credit->client->getRoute());
2015-03-16 22:45:25 +01:00
}
public function bulk()
{
$action = Input::get('action');
2015-10-28 20:22:07 +01:00
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
$count = $this->creditService->bulk($ids, $action);
2015-03-16 22:45:25 +01:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_credit', $count);
Session::flash('message', $message);
}
return Redirect::to('credits');
}
}