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

141 lines
5.4 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2014-01-06 19:03:00 +01:00
use ninja\repositories\PaymentRepository;
2013-11-26 13:45:07 +01:00
class PaymentController extends \BaseController
{
2014-01-06 19:03:00 +01:00
protected $creditRepo;
public function __construct(PaymentRepository $paymentRepo)
{
parent::__construct();
$this->paymentRepo = $paymentRepo;
}
2013-11-26 13:45:07 +01:00
public function index()
{
2013-12-03 23:00:01 +01:00
return View::make('list', array(
'entityType'=>ENTITY_PAYMENT,
'title' => '- Payments',
2013-12-11 21:33:44 +01:00
'columns'=>['checkbox', 'Transaction Reference', 'Client', 'Invoice', 'Payment 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
{
2014-01-06 19:03:00 +01:00
$payments = $this->paymentRepo->find($clientPublicId, Input::get('sSearch'));
$table = Datatable::query($payments);
2013-11-29 13:09:21 +01:00
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) {
2013-12-30 21:17:45 +01:00
$table->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
2013-12-03 23:00:01 +01:00
}
2013-12-08 14:32:49 +01:00
return $table->addColumn('invoice_number', function($model) { return $model->invoice_public_id ? link_to('invoices/' . $model->invoice_public_id . '/edit', $model->invoice_number) : ''; })
2013-12-29 18:40:11 +01:00
->addColumn('amount', function($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
2013-12-16 22:27:32 +01:00
->addColumn('payment_date', function($model) { return Utils::dateToString($model->payment_date); })
2013-12-05 16:23:24 +01:00
->addColumn('dropdown', function($model)
{
2013-12-05 21:25:20 +01:00
return '<div class="btn-group tr-action" style="visibility:hidden;">
2013-12-05 16:23:24 +01: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 13:55:50 +01:00
<li><a href="javascript:archiveEntity(' . $model->public_id. ')">Archive Payment</a></li>
2013-12-05 16:23:24 +01:00
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Payment</a></li>
</ul>
</div>';
})
2013-11-26 13:45:07 +01:00
->make();
}
2013-12-04 17:20:14 +01:00
2014-01-02 14:21:15 +01:00
public function create($clientPublicId = 0, $invoicePublicId = 0)
2013-12-04 17:20:14 +01:00
{
$data = array(
2013-12-30 21:17:45 +01:00
'clientPublicId' => $clientPublicId,
2014-01-02 14:21:15 +01:00
'invoicePublicId' => $invoicePublicId,
2013-12-05 16:23:24 +01:00
'invoice' => null,
2014-01-08 09:35:35 +01:00
'invoices' => Invoice::scope()->with('client', 'invoice_status')->where('balance','>',0)->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',
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-30 21:17:45 +01:00
'clients' => Client::scope()->with('contacts')->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();
2014-01-01 00:50:13 +01:00
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
2013-12-04 17:20:14 +01:00
$data = array(
2013-12-05 16:23:24 +01:00
'client' => null,
'invoice' => null,
2014-01-08 09:35:35 +01:00
'invoices' => Invoice::scope()->with('client', 'invoice_status')->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',
2013-12-31 00:19:17 +01:00
'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2013-12-30 21:17:45 +01:00
'clients' => Client::scope()->with('contacts')->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);
2014-01-06 19:03:00 +01:00
if ($validator->fails())
{
2013-12-05 16:23:24 +01:00
$url = $publicId ? 'payments/' . $publicId . '/edit' : 'payments/create';
return Redirect::to($url)
->withErrors($validator)
->withInput();
2014-01-06 19:03:00 +01:00
}
else
{
$this->paymentRepo->save($publicId, Input::all());
2013-12-05 16:23:24 +01:00
$message = $publicId ? 'Successfully updated payment' : 'Successfully created payment';
Session::flash('message', $message);
2014-01-01 00:50:13 +01:00
return Redirect::to('clients/' . Input::get('client'));
2013-12-05 16:23:24 +01:00
}
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');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2014-01-06 19:03:00 +01:00
$count = $this->paymentRepo->bulk($ids, $action);
2013-12-01 21:58:25 +01:00
2013-12-15 13:55:50 +01:00
$message = Utils::pluralize('Successfully '.$action.'d ? payment', count($payments));
2013-12-05 16:23:24 +01:00
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
}