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 Input;
|
|
|
|
use Session;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-04-08 15:19:17 +02:00
|
|
|
use Cache;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Client;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Services\PaymentService;
|
2016-05-01 13:31:10 +02:00
|
|
|
use App\Http\Requests\PaymentRequest;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Http\Requests\CreatePaymentRequest;
|
|
|
|
use App\Http\Requests\UpdatePaymentRequest;
|
2016-11-24 10:22:37 +01:00
|
|
|
use App\Ninja\Datatables\PaymentDatatable;
|
2015-10-28 20:22:07 +01:00
|
|
|
|
2015-03-26 07:24:02 +01:00
|
|
|
class PaymentController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-04-28 14:16:33 +02:00
|
|
|
protected $entityType = ENTITY_PAYMENT;
|
2016-06-08 16:56:13 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var PaymentRepository
|
|
|
|
*/
|
|
|
|
protected $paymentRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ContactMailer
|
|
|
|
*/
|
|
|
|
protected $contactMailer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PaymentService
|
|
|
|
*/
|
|
|
|
protected $paymentService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PaymentController constructor.
|
|
|
|
*
|
|
|
|
* @param PaymentRepository $paymentRepo
|
|
|
|
* @param ContactMailer $contactMailer
|
|
|
|
* @param PaymentService $paymentService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
PaymentRepository $paymentRepo,
|
|
|
|
ContactMailer $contactMailer,
|
|
|
|
PaymentService $paymentService
|
|
|
|
)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
|
|
|
$this->contactMailer = $contactMailer;
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->paymentService = $paymentService;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2016-11-24 10:22:37 +01:00
|
|
|
return View::make('list_wrapper', [
|
2015-03-16 22:45:25 +01:00
|
|
|
'entityType' => ENTITY_PAYMENT,
|
2016-11-24 10:22:37 +01:00
|
|
|
'datatable' => new PaymentDatatable(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'title' => trans('texts.payments'),
|
2016-07-03 18:11:58 +02:00
|
|
|
]);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param null $clientPublicId
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDatatable($clientPublicId = null)
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
return $this->paymentService->getDatatable($clientPublicId, Input::get('sSearch'));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param PaymentRequest $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-05-01 13:31:10 +02:00
|
|
|
public function create(PaymentRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-04-22 21:21:04 +02:00
|
|
|
$invoices = Invoice::scope()
|
2016-12-05 09:11:33 +01:00
|
|
|
->invoices()
|
|
|
|
->whereIsPublic(true)
|
2015-04-22 21:21:04 +02:00
|
|
|
->where('invoices.balance', '>', 0)
|
|
|
|
->with('client', 'invoice_status')
|
|
|
|
->orderBy('invoice_number')->get();
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$data = [
|
2016-05-01 13:31:10 +02:00
|
|
|
'clientPublicId' => Input::old('client') ? Input::old('client') : ($request->client_id ?: 0),
|
|
|
|
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : ($request->invoice_id ?: 0),
|
2015-03-16 22:45:25 +01:00
|
|
|
'invoice' => null,
|
2015-04-22 21:21:04 +02:00
|
|
|
'invoices' => $invoices,
|
2015-03-16 22:45:25 +01:00
|
|
|
'payment' => null,
|
|
|
|
'method' => 'POST',
|
2016-07-03 18:11:58 +02:00
|
|
|
'url' => 'payments',
|
2015-03-16 22:45:25 +01:00
|
|
|
'title' => trans('texts.new_payment'),
|
2015-11-04 08:48:47 +01:00
|
|
|
'paymentTypeId' => Input::get('paymentTypeId'),
|
2016-07-04 18:49:01 +02:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), ];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-27 11:53:11 +02:00
|
|
|
/**
|
|
|
|
* @param $publicId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function show($publicId)
|
|
|
|
{
|
|
|
|
Session::reflash();
|
|
|
|
|
|
|
|
return redirect()->to("payments/{$publicId}/edit");
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param PaymentRequest $request
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-05-01 13:31:10 +02:00
|
|
|
public function edit(PaymentRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-05-01 13:31:10 +02:00
|
|
|
$payment = $request->entity();
|
2015-03-16 22:45:25 +01:00
|
|
|
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
|
|
|
|
|
2017-01-02 21:23:29 +01:00
|
|
|
$actions = [];
|
|
|
|
if ( ! $payment->trashed()) {
|
|
|
|
$actions[] = ['url' => 'javascript:submitAction("archive")', 'label' => trans('texts.archive_payment')];
|
|
|
|
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans('texts.delete_payment')];
|
|
|
|
} else {
|
|
|
|
$actions[] = ['url' => 'javascript:submitAction("restore")', 'label' => trans('texts.restore_expense')];
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$data = [
|
2015-03-16 22:45:25 +01:00
|
|
|
'client' => null,
|
|
|
|
'invoice' => null,
|
2016-12-05 09:11:33 +01:00
|
|
|
'invoices' => Invoice::scope()
|
|
|
|
->invoices()
|
|
|
|
->whereIsPublic(true)
|
|
|
|
->with('client', 'invoice_status')
|
|
|
|
->orderBy('invoice_number')->get(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'payment' => $payment,
|
2016-10-18 16:55:07 +02:00
|
|
|
'entity' => $payment,
|
2015-03-16 22:45:25 +01:00
|
|
|
'method' => 'PUT',
|
2016-05-01 13:31:10 +02:00
|
|
|
'url' => 'payments/'.$payment->public_id,
|
2015-03-16 22:45:25 +01:00
|
|
|
'title' => trans('texts.edit_payment'),
|
2017-01-02 21:23:29 +01:00
|
|
|
'actions' => $actions,
|
2015-04-08 15:19:17 +02:00
|
|
|
'paymentTypes' => Cache::get('paymentTypes'),
|
2016-10-18 16:55:07 +02:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
|
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param CreatePaymentRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function store(CreatePaymentRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
$input = $request->input();
|
2016-06-08 16:56:13 +02:00
|
|
|
|
2015-12-07 22:41:48 +01:00
|
|
|
$input['invoice_id'] = Invoice::getPrivateId($input['invoice']);
|
|
|
|
$input['client_id'] = Client::getPrivateId($input['client']);
|
2015-10-28 20:22:07 +01:00
|
|
|
$payment = $this->paymentRepo->save($input);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
if (Input::get('email_receipt')) {
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
Session::flash('message', trans('texts.created_payment_emailed_client'));
|
|
|
|
} else {
|
|
|
|
Session::flash('message', trans('texts.created_payment'));
|
|
|
|
}
|
|
|
|
|
2016-12-05 09:11:33 +01:00
|
|
|
return redirect()->to($payment->client->getRoute() . '#payments');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param UpdatePaymentRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function update(UpdatePaymentRequest $request)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2017-01-02 21:23:29 +01:00
|
|
|
if (in_array($request->action, ['archive', 'delete', 'restore'])) {
|
|
|
|
return self::bulk();
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
$payment = $this->paymentRepo->save($request->input(), $request->entity());
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
Session::flash('message', trans('texts.updated_payment'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return redirect()->to($payment->getRoute());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
2016-04-23 22:40:19 +02:00
|
|
|
$amount = Input::get('amount');
|
2015-10-28 20:22:07 +01:00
|
|
|
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
|
2016-07-03 18:11:58 +02:00
|
|
|
$count = $this->paymentService->bulk($ids, $action, ['amount'=>$amount]);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($count > 0) {
|
2017-01-02 21:23:29 +01:00
|
|
|
$message = Utils::pluralize($action=='refund' ? 'refunded_payment':$action.'d_payment', $count);
|
2015-03-16 22:45:25 +01:00
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:46:32 +01:00
|
|
|
return $this->returnBulk(ENTITY_PAYMENT, $action, $ids);
|
2016-05-15 04:22:06 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|