2015-03-24 09:21:12 +01:00
< ? php namespace App\Ninja\Repositories ;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
use Utils ;
2015-03-31 11:38:24 +02:00
use App\Models\Credit ;
use App\Models\Client ;
2015-10-28 20:22:07 +01:00
use App\Ninja\Repositories\BaseRepository ;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
class CreditRepository extends BaseRepository
2015-03-16 22:45:25 +01:00
{
2015-10-28 20:22:07 +01:00
public function getClassName ()
{
return 'App\Models\Credit' ;
}
2015-03-16 22:45:25 +01:00
public function find ( $clientPublicId = null , $filter = null )
{
$query = \DB :: table ( 'credits' )
-> join ( 'clients' , 'clients.id' , '=' , 'credits.client_id' )
-> join ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
-> where ( 'clients.deleted_at' , '=' , null )
2015-11-29 17:00:50 +01:00
-> where ( 'contacts.deleted_at' , '=' , null )
2015-03-16 22:45:25 +01:00
-> where ( 'contacts.is_primary' , '=' , true )
-> select ( 'credits.public_id' , 'clients.name as client_name' , 'clients.public_id as client_public_id' , 'credits.amount' , 'credits.balance' , 'credits.credit_date' , 'clients.currency_id' , 'contacts.first_name' , 'contacts.last_name' , 'contacts.email' , 'credits.private_notes' , 'credits.deleted_at' , 'credits.is_deleted' );
if ( $clientPublicId ) {
$query -> where ( 'clients.public_id' , '=' , $clientPublicId );
}
if ( ! \Session :: get ( 'show_trash:credit' )) {
$query -> where ( 'credits.deleted_at' , '=' , null );
}
if ( $filter ) {
$query -> where ( function ( $query ) use ( $filter ) {
$query -> where ( 'clients.name' , 'like' , '%' . $filter . '%' );
});
}
return $query ;
}
2015-10-28 20:22:07 +01:00
public function save ( $input )
2015-03-16 22:45:25 +01:00
{
2015-10-28 20:22:07 +01:00
$publicId = isset ( $data [ 'public_id' ]) ? $data [ 'public_id' ] : false ;
2015-03-16 22:45:25 +01:00
if ( $publicId ) {
$credit = Credit :: scope ( $publicId ) -> firstOrFail ();
} else {
$credit = Credit :: createNew ();
}
$credit -> client_id = Client :: getPrivateId ( $input [ 'client' ]);
$credit -> credit_date = Utils :: toSqlDate ( $input [ 'credit_date' ]);
$credit -> amount = Utils :: parseFloat ( $input [ 'amount' ]);
$credit -> balance = Utils :: parseFloat ( $input [ 'amount' ]);
$credit -> private_notes = trim ( $input [ 'private_notes' ]);
$credit -> save ();
return $credit ;
}
}