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

93 lines
2.4 KiB
PHP
Raw Normal View History

2013-12-26 01:36:34 +01:00
<?php namespace ninja\repositories;
use Client;
use Contact;
2014-04-06 11:02:04 +02:00
use Account;
use Request;
use Session;
use Language;
use User;
2013-12-26 01:36:34 +01:00
class AccountRepository
{
2014-04-06 11:02:04 +02:00
public function create()
{
$account = new Account;
$account->ip = Request::getClientIp();
$account->account_key = str_random(RANDOM_KEY_LENGTH);
if (Session::has(SESSION_LOCALE))
{
$locale = Session::get(SESSION_LOCALE);
$language = Language::whereLocale($locale)->first();
if ($language)
{
$account->language_id = $language->id;
}
}
$account->save();
$random = str_random(RANDOM_KEY_LENGTH);
$user = new User;
$user->password = $random;
$user->password_confirmation = $random;
$user->username = $random;
$account->users()->save($user);
return $account;
}
2013-12-26 01:36:34 +01:00
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;
}
}