2013-12-26 01:36:34 +01:00
< ? php namespace ninja\repositories ;
use Client ;
use Contact ;
class AccountRepository
{
public function getSearchData ()
{
$clients = \DB :: table ( 'clients' )
-> where ( 'clients.deleted_at' , '=' , null )
2014-01-09 22:38:18 +01:00
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
2013-12-30 21:17:45 +01:00
-> whereRaw ( " clients.name <> '' " )
2013-12-27 10:10:32 +01:00
-> select ( \DB :: raw ( " 'Clients' as type, clients.public_id, clients.name, '' as token " ));
2013-12-26 01:36:34 +01:00
$contacts = \DB :: table ( 'clients' )
-> join ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'clients.deleted_at' , '=' , null )
2014-01-09 22:38:18 +01:00
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
2013-12-30 21:17:45 +01:00
-> whereRaw ( " CONCAT(contacts.first_name, contacts.last_name, contacts.email) <> '' " )
-> select ( \DB :: raw ( " 'Contacts' as type, clients.public_id, CONCAT(contacts.first_name, ' ', contacts.last_name, ' ', contacts.email) as name, '' as token " ));
2013-12-26 01:36:34 +01:00
$invoices = \DB :: table ( 'clients' )
-> join ( 'invoices' , 'invoices.client_id' , '=' , 'clients.id' )
2014-01-09 22:38:18 +01:00
-> where ( 'clients.account_id' , '=' , \Auth :: user () -> account_id )
2013-12-26 01:36:34 +01:00
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'invoices.deleted_at' , '=' , null )
2013-12-27 10:10:32 +01:00
-> select ( \DB :: raw ( " 'Invoices' as type, invoices.public_id, CONCAT(invoices.invoice_number, ': ', clients.name) as name, invoices.invoice_number as token " ));
2013-12-26 01:36:34 +01:00
$data = [];
foreach ( $clients -> union ( $contacts ) -> union ( $invoices ) -> get () as $row )
{
2013-12-27 10:10:32 +01:00
$type = $row -> type ;
if ( ! isset ( $data [ $type ]))
{
$data [ $type ] = [];
}
$tokens = explode ( ' ' , $row -> name );
$tokens [] = $type ;
if ( $type == 'Invoices' )
2013-12-26 01:36:34 +01:00
{
2013-12-27 10:10:32 +01:00
$tokens [] = intVal ( $row -> token ) . '' ;
2013-12-26 01:36:34 +01:00
}
2013-12-27 10:10:32 +01:00
$data [ $type ][] = [
2013-12-26 01:36:34 +01:00
'value' => $row -> name ,
2013-12-27 10:10:32 +01:00
'public_id' => $row -> public_id ,
'tokens' => $tokens
2013-12-26 01:36:34 +01:00
];
}
2013-12-27 10:10:32 +01:00
2013-12-26 01:36:34 +01:00
return $data ;
}
}