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

170 lines
7.1 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-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
{
2013-12-05 21:25:20 +01:00
$query = DB::table('payments')
->join('clients', 'clients.id', '=','payments.client_id')
->leftJoin('invoices', 'invoices.id', '=','payments.invoice_id')
2013-12-30 21:17:45 +01:00
->join('contacts', 'contacts.client_id', '=', 'clients.id')
2013-12-05 21:25:20 +01:00
->where('payments.account_id', '=', Auth::user()->account_id)
->where('payments.deleted_at', '=', null)
2013-12-15 13:55:50 +01:00
->where('clients.deleted_at', '=', null)
2013-12-30 21:17:45 +01:00
->where('contacts.is_primary', '=', true)
->select('payments.public_id', 'payments.transaction_reference', 'clients.name as client_name', 'clients.public_id as client_public_id', 'payments.amount', 'payments.payment_date', 'invoices.public_id as invoice_public_id', 'invoices.invoice_number', 'payments.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email');
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if ($clientPublicId) {
2013-12-05 21:25:20 +01:00
$query->where('clients.public_id', '=', $clientPublicId);
2013-11-29 13:09:21 +01:00
}
2013-12-11 21:33:44 +01:00
$filter = Input::get('sSearch');
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
2013-12-05 21:25:20 +01:00
$table = Datatable::query($query);
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">
<li><a href="' . URL::to('payments/'.$model->public_id.'/edit') . '">Edit Payment</a></li>
<li class="divider"></li>
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-12-05 21:25:20 +01:00
->orderColumns('transaction_reference', 'client_name', 'invoice_number', 'amount', 'payment_date')
2013-11-26 13:45:07 +01:00
->make();
}
2013-12-04 17:20:14 +01:00
2013-12-09 10:38:49 +01:00
public function create($clientPublicId = 0)
2013-12-04 17:20:14 +01:00
{
$data = array(
2013-12-30 21:17:45 +01:00
'clientPublicId' => $clientPublicId,
2013-12-05 16:23:24 +01:00
'invoice' => null,
2013-12-30 21:17:45 +01:00
'invoices' => Invoice::scope()->with('client')->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-29 18:40:11 +01:00
'currencies' => Currency::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();
$data = array(
2013-12-05 16:23:24 +01:00
'client' => null,
'invoice' => null,
2013-12-30 21:17:45 +01:00
'invoices' => Invoice::scope()->with('client')->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-29 18:40:11 +01:00
'currencies' => Currency::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);
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;
2013-12-29 18:40:11 +01:00
$payment->currency_id = Input::get('currency_id') ? Input::get('currency_id') : null;
2013-12-07 21:33:07 +01:00
$payment->payment_date = Utils::toSqlDate(Input::get('payment_date'));
2013-12-07 19:45:00 +01:00
$payment->amount = floatval(Input::get('amount'));
2013-12-05 16:23:24 +01:00
$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');
2013-12-05 21:25:20 +01:00
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
2013-12-05 16:23:24 +01:00
$payments = Payment::scope($ids)->get();
2013-12-15 13:55:50 +01:00
foreach ($payments as $payment) {
if ($action == 'delete') {
$payment->is_deleted = true;
$payment->save();
2013-12-05 16:23:24 +01:00
}
2013-12-15 13:55:50 +01:00
$payment->delete();
2013-12-05 16:23:24 +01:00
}
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
}