1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Http/Controllers/PaymentController.php

268 lines
8.7 KiB
PHP
Raw Normal View History

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;
2017-05-16 12:13:53 +02:00
use App\Models\Payment;
use App\Models\Credit;
2017-01-30 20:40:43 +01:00
use App\Models\Invoice;
use App\Ninja\Datatables\PaymentDatatable;
use App\Ninja\Mailers\ContactMailer;
use App\Ninja\Repositories\PaymentRepository;
use App\Services\PaymentService;
2017-02-23 19:20:15 +01:00
use Auth;
2017-01-30 20:40:43 +01:00
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
{
/**
* @var string
*/
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_PAYMENT;
2016-06-08 16:56:13 +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
*/
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;
$this->paymentService = $paymentService;
2015-03-16 22:45:25 +01:00
}
/**
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function index()
{
return View::make('list_wrapper', [
2015-03-16 22:45:25 +01:00
'entityType' => ENTITY_PAYMENT,
'datatable' => new PaymentDatatable(),
2015-03-16 22:45:25 +01:00
'title' => trans('texts.payments'),
]);
2015-03-16 22:45:25 +01:00
}
/**
* @param null $clientPublicId
2017-01-30 20:40:43 +01: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
}
/**
* @param PaymentRequest $request
2017-01-30 20:40:43 +01: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
{
2018-02-15 10:53:51 +01:00
$user = auth()->user();
$account = $user->account;
2015-04-22 21:21:04 +02:00
$invoices = Invoice::scope()
->invoices()
2017-08-09 09:57:24 +02:00
->where('invoices.invoice_status_id', '!=', INVOICE_STATUS_PAID)
2015-04-22 21:21:04 +02:00
->with('client', 'invoice_status')
->orderBy('invoice_number')->get();
2018-02-15 10:53:51 +01:00
$clientPublicId = Input::old('client') ? Input::old('client') : ($request->client_id ?: 0);
$invoicePublicId = Input::old('invoice') ? Input::old('invoice') : ($request->invoice_id ?: 0);
$totalCredit = false;
if ($clientPublicId && $client = Client::scope($clientPublicId)->first()) {
$totalCredit = $account->formatMoney($client->getTotalCredit(), $client);
} elseif ($invoicePublicId && $invoice = Invoice::scope($invoicePublicId)->first()) {
$totalCredit = $account->formatMoney($invoice->client->getTotalCredit(), $client);
}
$data = [
2017-02-23 19:20:15 +01:00
'account' => Auth::user()->account,
2018-02-15 10:53:51 +01:00
'clientPublicId' => $clientPublicId,
'invoicePublicId' => $invoicePublicId,
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',
'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'),
2018-02-15 10:53:51 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(),
'totalCredit' => $totalCredit,
];
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");
}
/**
* @param PaymentRequest $request
2017-01-30 20:40:43 +01: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);
$actions = [];
if ($payment->invoiceJsonBackup()) {
$actions[] = ['url' => url("/invoices/invoice_history/{$payment->invoice->public_id}?payment_id={$payment->public_id}"), 'label' => trans('texts.view_invoice')];
}
2017-05-16 12:13:53 +02:00
2017-01-09 09:17:22 +01:00
$actions[] = ['url' => url("/invoices/{$payment->invoice->public_id}/edit"), 'label' => trans('texts.edit_invoice')];
2017-05-16 12:13:53 +02:00
$actions[] = DropdownButton::DIVIDER;
$actions[] = ['url' => 'javascript:submitAction("email")', 'label' => trans('texts.email_payment')];
if ($payment->canBeRefunded()) {
$actions[] = ['url' => "javascript:showRefundModal({$payment->public_id}, \"{$payment->getCompletedAmount()}\", \"{$payment->present()->completedAmount}\", \"{$payment->present()->currencySymbol}\")", 'label' => trans('texts.refund_payment')];
}
2017-01-09 09:17:22 +01:00
$actions[] = DropdownButton::DIVIDER;
2017-01-30 17:05:31 +01:00
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')];
}
$data = [
2017-02-23 19:20:15 +01:00
'account' => Auth::user()->account,
2015-03-16 22:45:25 +01:00
'client' => null,
'invoice' => null,
'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'),
'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);
}
/**
* @param CreatePaymentRequest $request
2017-01-30 20:40:43 +01: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
{
// check payment has been marked sent
$request->invoice->markSentIfUnsent();
$input = $request->input();
$amount = Utils::parseFloat($input['amount']);
$credit = false;
// if the payment amount is more than the balance create a credit
if ($amount > $request->invoice->balance) {
2017-10-24 12:16:46 +02:00
$credit = true;
}
2017-10-24 12:16:46 +02:00
$payment = $this->paymentService->save($input, null, $request->invoice);
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($credit ? 'texts.created_payment_and_credit_emailed_client' : 'texts.created_payment_emailed_client'));
2015-10-28 20:22:07 +01:00
} else {
Session::flash('message', trans($credit ? 'texts.created_payment_and_credit' : 'texts.created_payment'));
2015-10-28 20:22:07 +01:00
}
2019-01-30 11:45:46 +01:00
return url($payment->client->getRoute());
2015-03-16 22:45:25 +01:00
}
/**
* @param UpdatePaymentRequest $request
2017-01-30 20:40:43 +01: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-05-16 12:13:53 +02:00
if (in_array($request->action, ['archive', 'delete', 'restore', 'refund', 'email'])) {
return self::bulk();
}
$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
}
/**
* @return mixed
*/
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');
2015-03-16 22:45:25 +01:00
2017-05-16 12:13:53 +02:00
if ($action === 'email') {
2017-08-14 16:03:18 +02:00
$payment = Payment::scope($ids)->withArchived()->first();
2017-05-16 12:13:53 +02:00
$this->contactMailer->sendPaymentConfirmation($payment);
Session::flash('message', trans('texts.emailed_payment'));
} else {
$count = $this->paymentService->bulk($ids, $action, [
'refund_amount' => Input::get('refund_amount'),
'refund_email' => Input::get('refund_email'),
]);
2017-05-16 12:13:53 +02:00
if ($count > 0) {
$message = Utils::pluralize($action == 'refund' ? 'refunded_payment' : $action.'d_payment', $count);
Session::flash('message', $message);
}
2015-03-16 22:45:25 +01:00
}
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
}