1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/PaymentController.php

144 lines
4.8 KiB
PHP
Raw Normal View History

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-02 15:06:16 +02:00
use Omnipay;
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;
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;
2015-03-26 07:24:02 +01:00
class PaymentController extends BaseController
2015-03-16 22:45:25 +01:00
{
2016-04-28 14:16:33 +02:00
protected $entityType = ENTITY_PAYMENT;
2016-06-08 16:56:13 +02:00
2016-06-20 16:14:43 +02:00
public function __construct(PaymentRepository $paymentRepo, ContactMailer $contactMailer, PaymentService $paymentService)
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
}
public function index()
{
return View::make('list', array(
'entityType' => ENTITY_PAYMENT,
'title' => trans('texts.payments'),
2016-06-22 11:22:38 +02:00
'sortCol' => '7',
2015-11-24 12:59:30 +01:00
'columns' => Utils::trans([
'checkbox',
'invoice',
'client',
'transaction_reference',
'method',
'source',
2015-11-24 12:59:30 +01:00
'payment_amount',
'payment_date',
2016-04-23 22:40:19 +02:00
'status',
2015-11-30 12:29:43 +01:00
''
2015-11-24 12:59:30 +01:00
]),
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-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-06-08 16:56:13 +02:00
->viewable()
2016-05-26 16:56:54 +02:00
->invoiceType(INVOICE_TYPE_STANDARD)
2015-04-22 21:21:04 +02:00
->where('is_recurring', '=', false)
->where('invoices.balance', '>', 0)
->with('client', 'invoice_status')
->orderBy('invoice_number')->get();
2015-03-16 22:45:25 +01:00
$data = array(
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',
'url' => "payments",
'title' => trans('texts.new_payment'),
2015-11-04 08:48:47 +01:00
'paymentTypeId' => Input::get('paymentTypeId'),
2016-06-08 16:56:13 +02:00
'clients' => Client::scope()->viewable()->with('contacts')->orderBy('name')->get(), );
2015-03-16 22:45:25 +01:00
return View::make('payments.edit', $data);
}
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();
2016-06-08 16:56:13 +02:00
2015-03-16 22:45:25 +01:00
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
$data = array(
'client' => null,
'invoice' => null,
2016-05-26 16:56:54 +02:00
'invoices' => Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->where('is_recurring', '=', false)
2015-03-16 22:45:25 +01:00
->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
'payment' => $payment,
'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'),
2015-04-08 15:19:17 +02:00
'paymentTypes' => Cache::get('paymentTypes'),
2015-03-16 22:45:25 +01:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);
}
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
$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'));
}
return redirect()->to($payment->client->getRoute());
2015-03-16 22:45:25 +01:00
}
2015-10-28 20:22:07 +01:00
public function update(UpdatePaymentRequest $request)
2015-03-16 22:45:25 +01: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
}
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-04-23 22:40:19 +02:00
$count = $this->paymentService->bulk($ids, $action, array('amount'=>$amount));
2015-03-16 22:45:25 +01:00
if ($count > 0) {
2016-04-23 22:40:19 +02: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-06-20 16:14:43 +02:00
return redirect()->to('payments');
2016-05-15 04:22:06 +02:00
}
2015-03-16 22:45:25 +01:00
}