invoiceninja/app/Http/Controllers/PaymentController.php

144 lines
4.8 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 Session;
use Utils;
use View;
2015-04-02 16:06:16 +03:00
use Omnipay;
2015-04-08 16:19:17 +03:00
use Cache;
2015-03-31 20:42:37 +03:00
use App\Models\Invoice;
use App\Models\Client;
2015-03-26 16:24:02 +10:00
use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Mailers\ContactMailer;
use App\Services\PaymentService;
2016-05-01 14:31:10 +03:00
use App\Http\Requests\PaymentRequest;
2015-10-28 21:22:07 +02:00
use App\Http\Requests\CreatePaymentRequest;
use App\Http\Requests\UpdatePaymentRequest;
2015-03-26 16:24:02 +10:00
class PaymentController extends BaseController
2015-03-17 07:45:25 +10:00
{
2016-04-28 15:16:33 +03:00
protected $entityType = ENTITY_PAYMENT;
2016-06-08 17:56:13 +03:00
2016-06-20 17:14:43 +03:00
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer, PaymentService $paymentService)
2015-03-17 07:45:25 +10:00
{
$this->paymentRepo = $paymentRepo;
$this->contactMailer = $contactMailer;
$this->paymentService = $paymentService;
2015-03-17 07:45:25 +10:00
}
public function index()
{
return View::make('list', array(
'entityType' => ENTITY_PAYMENT,
'title' => trans('texts.payments'),
2016-06-22 12:22:38 +03:00
'sortCol' => '7',
2015-11-24 06:59:30 -05:00
'columns' => Utils::trans([
'checkbox',
'invoice',
'client',
'transaction_reference',
'method',
'source',
2015-11-24 06:59:30 -05:00
'payment_amount',
'payment_date',
2016-04-23 16:40:19 -04:00
'status',
2015-11-30 06:29:43 -05:00
''
2015-11-24 06:59:30 -05:00
]),
2015-03-17 07:45:25 +10:00
));
}
public function getDatatable($clientPublicId = null)
{
2015-11-06 00:37:04 +02:00
return $this->paymentService->getDatatable($clientPublicId, Input::get('sSearch'));
2015-03-17 07:45:25 +10:00
}
2016-05-01 14:31:10 +03:00
public function create(PaymentRequest $request)
2015-03-17 07:45:25 +10:00
{
2015-04-22 22:21:04 +03:00
$invoices = Invoice::scope()
2016-06-08 17:56:13 +03:00
->viewable()
2016-05-26 17:56:54 +03:00
->invoiceType(INVOICE_TYPE_STANDARD)
2015-04-22 22:21:04 +03:00
->where('is_recurring', '=', false)
->where('invoices.balance', '>', 0)
->with('client', 'invoice_status')
->orderBy('invoice_number')->get();
2015-03-17 07:45:25 +10:00
$data = array(
2016-05-01 14:31:10 +03: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-17 07:45:25 +10:00
'invoice' => null,
2015-04-22 22:21:04 +03:00
'invoices' => $invoices,
2015-03-17 07:45:25 +10:00
'payment' => null,
'method' => 'POST',
'url' => "payments",
'title' => trans('texts.new_payment'),
2015-11-04 09:48:47 +02:00
'paymentTypeId' => Input::get('paymentTypeId'),
2016-06-08 17:56:13 +03:00
'clients' => Client::scope()->viewable()->with('contacts')->orderBy('name')->get(), );
2015-03-17 07:45:25 +10:00
return View::make('payments.edit', $data);
}
2016-05-01 14:31:10 +03:00
public function edit(PaymentRequest $request)
2015-03-17 07:45:25 +10:00
{
2016-05-01 14:31:10 +03:00
$payment = $request->entity();
2016-06-08 17:56:13 +03:00
2015-03-17 07:45:25 +10:00
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
$data = array(
'client' => null,
'invoice' => null,
2016-05-26 17:56:54 +03:00
'invoices' => Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->where('is_recurring', '=', false)
2015-03-17 07:45:25 +10:00
->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
'payment' => $payment,
'method' => 'PUT',
2016-05-01 14:31:10 +03:00
'url' => 'payments/'.$payment->public_id,
2015-03-17 07:45:25 +10:00
'title' => trans('texts.edit_payment'),
2015-04-08 16:19:17 +03:00
'paymentTypes' => Cache::get('paymentTypes'),
2015-03-17 07:45:25 +10:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);
}
2015-10-28 21:22:07 +02:00
public function store(CreatePaymentRequest $request)
2015-03-17 07:45:25 +10:00
{
2015-10-28 21:22:07 +02:00
$input = $request->input();
2016-06-08 17:56:13 +03:00
$input['invoice_id'] = Invoice::getPrivateId($input['invoice']);
$input['client_id'] = Client::getPrivateId($input['client']);
2015-10-28 21:22:07 +02:00
$payment = $this->paymentRepo->save($input);
2015-03-17 07:45:25 +10:00
2015-10-28 21:22:07 +02: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'));
}
return redirect()->to($payment->client->getRoute());
2015-03-17 07:45:25 +10:00
}
2015-10-28 21:22:07 +02:00
public function update(UpdatePaymentRequest $request)
2015-03-17 07:45:25 +10:00
{
$payment = $this->paymentRepo->save($request->input(), $request->entity());
2015-03-17 07:45:25 +10:00
2015-10-28 21:22:07 +02:00
Session::flash('message', trans('texts.updated_payment'));
2015-03-17 07:45:25 +10:00
2015-10-28 21:22:07 +02:00
return redirect()->to($payment->getRoute());
2015-03-17 07:45:25 +10:00
}
public function bulk()
{
$action = Input::get('action');
2016-04-23 16:40:19 -04:00
$amount = Input::get('amount');
2015-10-28 21:22:07 +02:00
$ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
2016-04-23 16:40:19 -04:00
$count = $this->paymentService->bulk($ids, $action, array('amount'=>$amount));
2015-03-17 07:45:25 +10:00
if ($count > 0) {
2016-04-23 16:40:19 -04:00
$message = Utils::pluralize($action=='refund'?'refunded_payment':$action.'d_payment', $count);
2015-03-17 07:45:25 +10:00
Session::flash('message', $message);
}
2016-06-20 17:14:43 +03:00
return redirect()->to('payments');
2016-05-14 22:22:06 -04:00
}
2015-03-17 07:45:25 +10:00
}