1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/controllers/PaymentController.php

151 lines
5.6 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
class PaymentController extends \BaseController
{
public function index()
{
2013-12-03 23:00:01 +01:00
return View::make('list', array(
'entityType'=>ENTITY_PAYMENT,
'title' => '- Payments',
2013-12-05 16:23:24 +01:00
'columns'=>['checkbox', 'Transaction Reference', 'Client', 'Invoice', 'Amount', 'Payment Date', 'Action']
2013-12-03 23:00:01 +01:00
));
2013-11-26 13:45:07 +01:00
}
2013-12-04 17:20:14 +01:00
public function getDatatable($clientPublicId = null)
2013-11-26 13:45:07 +01:00
{
2013-12-05 16:23:24 +01:00
$collection = Payment::scope()->with('invoice', 'client');
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
$clientId = Client::getPrivateId($clientPublicId);
2013-11-29 13:09:21 +01:00
$collection->where('client_id','=',$clientId);
}
$table = Datatable::collection($collection->get());
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
2013-11-29 13:09:21 +01:00
}
2013-12-05 16:23:24 +01:00
$table->addColumn('transaction_reference', function($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; });
2013-12-03 23:00:01 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
$table->addColumn('client', function($model) { return link_to('clients/' . $model->client->public_id, $model->client->name); });
2013-12-03 23:00:01 +01:00
}
2013-12-05 16:23:24 +01:00
return $table->addColumn('invoice_number', function($model) { return $model->invoice ? link_to('invoices/' . $model->invoice->public_id . '/edit', $model->invoice->invoice_number) : ''; })
->addColumn('amount', function($model) { return '$' . $model->amount; })
2013-12-03 23:00:01 +01:00
->addColumn('date', function($model) { return timestampToDateTimeString($model->created_at); })
2013-12-05 16:23:24 +01:00
->addColumn('dropdown', function($model)
{
return '<div class="btn-group tr-action" style="display:none">
<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">
<li><a href="' . URL::to('payments/'.$model->public_id.'/edit') . '">Edit Payment</a></li>
<li class="divider"></li>
<li><a href="' . URL::to('payments/'.$model->public_id.'/archive') . '">Archive Payment</a></li>
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Payment</a></li>
</ul>
</div>';
})
2013-11-26 13:45:07 +01:00
->orderColumns('client')
->make();
}
2013-12-04 17:20:14 +01:00
public function create()
{
$data = array(
2013-12-05 16:23:24 +01:00
'client' => null,
'invoice' => null,
'invoices' => Invoice::with('client')->scope()->orderBy('invoice_number')->get(),
2013-12-04 17:20:14 +01:00
'payment' => null,
'method' => 'POST',
'url' => 'payments',
2013-12-05 16:23:24 +01:00
'title' => '- New Payment',
'clients' => Client::scope()->orderBy('name')->get());
2013-12-04 17:20:14 +01:00
return View::make('payments.edit', $data);
}
public function edit($publicId)
{
$payment = Payment::scope($publicId)->firstOrFail();
$data = array(
2013-12-05 16:23:24 +01:00
'client' => null,
'invoice' => null,
'invoices' => Invoice::scope()->orderBy('invoice_number')->get(array('public_id','invoice_number')),
2013-12-04 17:20:14 +01:00
'payment' => $payment,
'method' => 'PUT',
'url' => 'payments/' . $publicId,
2013-12-05 16:23:24 +01:00
'title' => '- Edit Payment',
'clients' => Client::scope()->orderBy('name')->get());
2013-12-04 17:20:14 +01:00
return View::make('payments.edit', $data);
}
2013-12-05 16:23:24 +01:00
public function store()
{
return $this->save();
}
2013-12-04 17:20:14 +01:00
2013-12-05 16:23:24 +01:00
public function update($publicId)
2013-12-01 21:58:25 +01:00
{
2013-12-05 16:23:24 +01:00
return $this->save($publicId);
}
2013-12-01 21:58:25 +01:00
2013-12-05 16:23:24 +01:00
private function save($publicId = null)
{
$rules = array(
'client' => 'required',
'amount' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
$url = $publicId ? 'payments/' . $publicId . '/edit' : 'payments/create';
return Redirect::to($url)
->withErrors($validator)
->withInput();
} else {
if ($publicId) {
$payment = Payment::scope($publicId)->firstOrFail();
} else {
$payment = Payment::createNew();
}
$invoiceId = Input::get('invoice') && Input::get('invoice') != "-1" ? Input::get('invoice') : null;
$payment->client_id = Input::get('client');
$payment->invoice_id = $invoiceId;
$payment->payment_date = toSqlDate(Input::get('payment_date'));
$payment->amount = Input::get('amount');
$payment->save();
$message = $publicId ? 'Successfully updated payment' : 'Successfully created payment';
Session::flash('message', $message);
return Redirect::to('clients/' . $payment->client_id);
}
2013-12-01 21:58:25 +01:00
}
2013-12-05 16:23:24 +01:00
public function bulk()
2013-12-01 21:58:25 +01:00
{
2013-12-05 16:23:24 +01:00
$action = Input::get('action');
$ids = Input::get('ids');
$payments = Payment::scope($ids)->get();
foreach ($payments as $payment) {
if ($action == 'archive') {
$payment->delete();
} else if ($action == 'delete') {
$payment->forceDelete();
}
}
2013-12-01 21:58:25 +01:00
2013-12-05 16:23:24 +01:00
$message = pluralize('Successfully '.$action.'d ? payment', count($ids));
Session::flash('message', $message);
return Redirect::to('payments');
2013-12-01 21:58:25 +01:00
}
2013-12-05 16:23:24 +01:00
2013-11-26 13:45:07 +01:00
}