invoiceninja/app/Http/Controllers/CreditController.php

122 lines
3.3 KiB
PHP
Raw Normal View History

2015-03-17 11:30:56 +10:00
<?php namespace App\Http\Controllers;
2015-03-17 07:45:25 +10:00
2015-03-26 16:24:02 +10:00
use Input;
use Redirect;
use Session;
2015-11-04 09:48:47 +02:00
use URL;
2015-03-26 16:24:02 +10:00
use Utils;
use View;
2015-04-01 18:44:55 +03:00
use App\Models\Client;
2016-11-30 20:21:50 +02:00
use App\Models\Credit;
2015-10-28 21:22:07 +02:00
use App\Services\CreditService;
use App\Ninja\Repositories\CreditRepository;
2016-11-30 20:21:50 +02:00
use App\Http\Requests\UpdateCreditRequest;
2015-10-28 21:22:07 +02:00
use App\Http\Requests\CreateCreditRequest;
2016-05-01 14:31:10 +03:00
use App\Http\Requests\CreditRequest;
use App\Ninja\Datatables\CreditDatatable;
2015-03-17 07:45:25 +10:00
2015-03-26 16:24:02 +10:00
class CreditController extends BaseController
2015-03-17 07:45:25 +10:00
{
protected $creditRepo;
2015-11-06 00:37:04 +02:00
protected $creditService;
2016-04-28 15:16:33 +03:00
protected $entityType = ENTITY_CREDIT;
2015-03-17 07:45:25 +10:00
2015-10-28 21:22:07 +02:00
public function __construct(CreditRepository $creditRepo, CreditService $creditService)
2015-03-17 07:45:25 +10:00
{
2016-03-15 19:08:00 -04:00
// parent::__construct();
2015-03-17 07:45:25 +10:00
$this->creditRepo = $creditRepo;
2015-10-28 21:22:07 +02:00
$this->creditService = $creditService;
2015-03-17 07:45:25 +10:00
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return View::make('list_wrapper', [
2015-03-17 07:45:25 +10:00
'entityType' => ENTITY_CREDIT,
'datatable' => new CreditDatatable(),
2015-03-17 07:45:25 +10:00
'title' => trans('texts.credits'),
]);
2015-03-17 07:45:25 +10:00
}
public function getDatatable($clientPublicId = null)
{
2015-11-06 00:37:04 +02:00
return $this->creditService->getDatatable($clientPublicId, Input::get('sSearch'));
2015-03-17 07:45:25 +10:00
}
2016-05-01 14:31:10 +03:00
public function create(CreditRequest $request)
2015-03-17 07:45:25 +10:00
{
$data = [
2016-05-01 14:31:10 +03:00
'clientPublicId' => Input::old('client') ? Input::old('client') : ($request->client_id ?: 0),
2015-03-17 07:45:25 +10:00
'credit' => null,
'method' => 'POST',
'url' => 'credits',
'title' => trans('texts.new_credit'),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
];
2015-03-17 07:45:25 +10:00
return View::make('credits.edit', $data);
}
public function edit($publicId)
{
2016-11-30 20:21:50 +02:00
$credit = Credit::withTrashed()->scope($publicId)->firstOrFail();
2016-06-08 17:56:48 +03:00
2016-04-25 21:53:39 -04:00
$this->authorize('edit', $credit);
2016-06-08 17:56:48 +03:00
2015-03-17 07:45:25 +10:00
$credit->credit_date = Utils::fromSqlDate($credit->credit_date);
$data = array(
'client' => null,
2016-11-30 20:21:50 +02:00
'clientPublicId' => $credit->client->public_id,
2015-03-17 07:45:25 +10:00
'credit' => $credit,
'method' => 'PUT',
'url' => 'credits/'.$publicId,
'title' => 'Edit Credit',
2016-11-30 20:21:50 +02:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
);
2015-03-17 07:45:25 +10:00
2016-11-30 20:21:50 +02:00
return View::make('credits.edit', $data);
}
public function update(UpdateCreditRequest $request)
{
$credit = $request->entity();
return $this->save($credit);
2015-03-17 07:45:25 +10:00
}
2016-06-08 17:56:48 +03:00
2015-10-28 21:22:07 +02:00
public function store(CreateCreditRequest $request)
2015-03-17 07:45:25 +10:00
{
2016-11-30 20:21:50 +02:00
return $this->save();
}
private function save($credit = null)
{
$credit = $this->creditService->save(Input::all(), $credit);
2016-03-02 15:36:42 +02:00
2016-11-30 20:21:50 +02:00
$message = $credit->wasRecentlyCreated ? trans('texts.created_created') : trans('texts.updated_credit');
Session::flash('message', $message);
2016-03-02 15:36:42 +02:00
2016-11-30 20:21:50 +02:00
return redirect()->to("credits/{$credit->public_id}/edit");
2015-03-17 07:45:25 +10:00
}
public function bulk()
{
$action = Input::get('action');
2015-10-28 21:22:07 +02:00
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
$count = $this->creditService->bulk($ids, $action);
2015-03-17 07:45:25 +10:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_credit', $count);
Session::flash('message', $message);
}
2016-11-27 11:46:32 +02:00
return $this->returnBulk(ENTITY_CREDIT, $action, $ids);
2015-03-17 07:45:25 +10:00
}
}