invoiceninja/app/controllers/PaymentController.php

142 lines
5.7 KiB
PHP
Raw Normal View History

2013-11-26 14:45:07 +02:00
<?php
2014-01-06 20:03:00 +02:00
use ninja\repositories\PaymentRepository;
2013-11-26 14:45:07 +02:00
class PaymentController extends \BaseController
{
2014-01-06 20:03:00 +02:00
protected $creditRepo;
public function __construct(PaymentRepository $paymentRepo)
{
parent::__construct();
$this->paymentRepo = $paymentRepo;
}
2013-11-26 14:45:07 +02:00
public function index()
{
2013-12-04 00:00:01 +02:00
return View::make('list', array(
'entityType'=>ENTITY_PAYMENT,
'title' => '- Payments',
2014-01-16 21:12:46 +00:00
'columns'=>['checkbox', 'Invoice', 'Client', 'Transaction Reference', 'Method', 'Payment Amount', 'Payment Date', 'Action']
2013-12-04 00:00:01 +02:00
));
2013-11-26 14:45:07 +02:00
}
2013-12-04 18:20:14 +02:00
public function getDatatable($clientPublicId = null)
2013-11-26 14:45:07 +02:00
{
2014-01-06 20:03:00 +02:00
$payments = $this->paymentRepo->find($clientPublicId, Input::get('sSearch'));
$table = Datatable::query($payments);
2013-11-29 14:09:21 +02:00
2013-12-04 18:20:14 +02:00
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
2013-11-29 14:09:21 +02:00
}
2014-01-16 21:12:46 +00:00
$table->addColumn('invoice_number', function($model) { return $model->invoice_public_id ? link_to('invoices/' . $model->invoice_public_id . '/edit', $model->invoice_number) : ''; });
2013-12-04 00:00:01 +02:00
2013-12-04 18:20:14 +02:00
if (!$clientPublicId) {
2013-12-30 22:17:45 +02:00
$table->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
2014-01-16 21:12:46 +00:00
}
$table->addColumn('transaction_reference', function($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; })
->addColumn('method', function($model) { return $model->payment_type ? $model->payment_type : ($model->transaction_reference ? '<i>Online payment</i>' : ''); });
2013-12-04 00:00:01 +02:00
2014-01-16 21:12:46 +00:00
return $table->addColumn('amount', function($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
2013-12-16 21:27:32 +00:00
->addColumn('payment_date', function($model) { return Utils::dateToString($model->payment_date); })
2013-12-05 17:23:24 +02:00
->addColumn('dropdown', function($model)
{
2013-12-05 22:25:20 +02:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-05 17:23:24 +02:00
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
Select <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
2013-12-15 14:55:50 +02:00
<li><a href="javascript:archiveEntity(' . $model->public_id. ')">Archive Payment</a></li>
2013-12-05 17:23:24 +02:00
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Payment</a></li>
</ul>
</div>';
})
2013-11-26 14:45:07 +02:00
->make();
}
2013-12-04 18:20:14 +02:00
2014-01-02 15:21:15 +02:00
public function create($clientPublicId = 0, $invoicePublicId = 0)
2013-12-04 18:20:14 +02:00
{
$data = array(
2014-01-14 11:52:56 +00:00
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
2013-12-05 17:23:24 +02:00
'invoice' => null,
2014-01-15 14:01:24 +00:00
'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
2013-12-04 18:20:14 +02:00
'payment' => null,
'method' => 'POST',
2014-01-14 11:52:56 +00:00
'url' => "payments",
2013-12-05 17:23:24 +02:00
'title' => '- New Payment',
2014-01-15 14:01:24 +00:00
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-13 19:22:43 +00:00
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
2013-12-30 22:17:45 +02:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
2013-12-04 18:20:14 +02:00
return View::make('payments.edit', $data);
}
public function edit($publicId)
{
$payment = Payment::scope($publicId)->firstOrFail();
2014-01-01 01:50:13 +02:00
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
2013-12-04 18:20:14 +02:00
$data = array(
2013-12-05 17:23:24 +02:00
'client' => null,
'invoice' => null,
2014-01-08 08:35:35 +00:00
'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(array('public_id','invoice_number')),
2013-12-04 18:20:14 +02:00
'payment' => $payment,
'method' => 'PUT',
'url' => 'payments/' . $publicId,
2013-12-05 17:23:24 +02:00
'title' => '- Edit Payment',
2014-01-15 14:01:24 +00:00
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-01-13 19:22:43 +00:00
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
2013-12-30 22:17:45 +02:00
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
2013-12-04 18:20:14 +02:00
return View::make('payments.edit', $data);
}
2013-12-05 17:23:24 +02:00
public function store()
{
return $this->save();
}
2013-12-04 18:20:14 +02:00
2013-12-05 17:23:24 +02:00
public function update($publicId)
2013-12-01 22:58:25 +02:00
{
2013-12-05 17:23:24 +02:00
return $this->save($publicId);
}
2013-12-01 22:58:25 +02:00
2013-12-05 17:23:24 +02:00
private function save($publicId = null)
{
2014-01-16 21:12:46 +00:00
if ($errors = $this->paymentRepo->getErrors(Input::all()))
2014-01-06 20:03:00 +02:00
{
2013-12-05 17:23:24 +02:00
$url = $publicId ? 'payments/' . $publicId . '/edit' : 'payments/create';
return Redirect::to($url)
2014-01-16 21:12:46 +00:00
->withErrors($errors)
2013-12-05 17:23:24 +02:00
->withInput();
2014-01-06 20:03:00 +02:00
}
else
{
$this->paymentRepo->save($publicId, Input::all());
2013-12-05 17:23:24 +02:00
$message = $publicId ? 'Successfully updated payment' : 'Successfully created payment';
Session::flash('message', $message);
2014-01-01 01:50:13 +02:00
return Redirect::to('clients/' . Input::get('client'));
2013-12-05 17:23:24 +02:00
}
2013-12-01 22:58:25 +02:00
}
2013-12-05 17:23:24 +02:00
public function bulk()
2013-12-01 22:58:25 +02:00
{
2013-12-05 17:23:24 +02:00
$action = Input::get('action');
2013-12-05 22:25:20 +02:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2014-01-06 20:03:00 +02:00
$count = $this->paymentRepo->bulk($ids, $action);
2013-12-01 22:58:25 +02:00
2014-01-12 18:55:33 +00:00
if ($count > 0)
{
2014-01-14 11:52:56 +00:00
$message = Utils::pluralize('Successfully '.$action.'d ? payment', $count);
2014-01-12 18:55:33 +00:00
Session::flash('message', $message);
}
2013-12-05 17:23:24 +02:00
return Redirect::to('payments');
2013-12-01 22:58:25 +02:00
}
2013-12-05 17:23:24 +02:00
2013-11-26 14:45:07 +02:00
}