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 21:25:20 +01:00
$query = DB :: table ( 'payments' )
-> join ( 'clients' , 'clients.id' , '=' , 'payments.client_id' )
-> leftJoin ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> where ( 'payments.account_id' , '=' , Auth :: user () -> account_id )
-> where ( 'payments.deleted_at' , '=' , null )
-> 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' );
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-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-05 21:25:20 +01:00
$table -> addColumn ( 'client_name' , function ( $model ) { return link_to ( 'clients/' . $model -> client_public_id , $model -> client_name ); });
2013-12-03 23:00:01 +01:00
}
2013-12-05 21:25:20 +01:00
return $table -> addColumn ( 'invoice_number' , function ( $model ) { return $model -> invoice_public_id ? link_to ( 'invoices/' . $model -> invoice_public_id . '/edit' , $model -> invoice_invoice_number ) : '' ; })
2013-12-05 16:23:24 +01:00
-> addColumn ( 'amount' , function ( $model ) { return '$' . $model -> amount ; })
2013-12-05 21:25:20 +01:00
-> addColumn ( 'payment_date' , function ( $model ) { return timestampToDateString ( $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 >
< 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-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
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' ));
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 ();
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
}