2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreatePaymentRequest;
|
|
|
|
use App\Http\Requests\PaymentRequest;
|
|
|
|
use App\Http\Requests\UpdatePaymentRequest;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Ninja\Datatables\PaymentDatatable;
|
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
|
|
|
use App\Services\PaymentService;
|
|
|
|
use Cache;
|
|
|
|
use DropdownButton;
|
2015-03-26 07:24:02 +01:00
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
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
|
2017-01-30 20:40:43 +01:00
|
|
|
* @param ContactMailer $contactMailer
|
|
|
|
* @param PaymentService $paymentService
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
PaymentRepository $paymentRepo,
|
|
|
|
ContactMailer $contactMailer,
|
|
|
|
PaymentService $paymentService
|
2017-01-30 17:05:31 +01:00
|
|
|
) {
|
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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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()
|
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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-27 11:53:11 +02:00
|
|
|
* @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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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 = [];
|
2017-01-08 22:34:44 +01:00
|
|
|
if ($payment->invoiceJsonBackup()) {
|
|
|
|
$actions[] = ['url' => url("/invoices/invoice_history/{$payment->invoice->public_id}?payment_id={$payment->public_id}"), 'label' => trans('texts.view_invoice')];
|
|
|
|
}
|
2017-01-09 09:17:22 +01:00
|
|
|
$actions[] = ['url' => url("/invoices/{$payment->invoice->public_id}/edit"), 'label' => trans('texts.edit_invoice')];
|
|
|
|
$actions[] = DropdownButton::DIVIDER;
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $payment->trashed()) {
|
2017-01-02 21:23:29 +01:00
|
|
|
$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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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
|
|
|
{
|
2017-01-13 12:53:06 +01:00
|
|
|
// check payment has been marked sent
|
|
|
|
$request->invoice->markSentIfUnsent();
|
2016-06-08 16:56:13 +02:00
|
|
|
|
2017-01-13 12:53:06 +01:00
|
|
|
$input = $request->input();
|
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
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @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');
|
2017-01-30 20:40:43 +01:00
|
|
|
$count = $this->paymentService->bulk($ids, $action, ['amount' => $amount]);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($count > 0) {
|
2017-01-30 20:40:43 +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
|
|
|
}
|