2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Repositories;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-02-16 16:30:09 +01:00
|
|
|
use App\Events\ClientWasCreated;
|
|
|
|
use App\Events\ClientWasUpdated;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Contact;
|
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use DB;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class ClientRepository extends BaseRepository
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Client';
|
|
|
|
}
|
|
|
|
|
2015-11-18 15:40:50 +01:00
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return Client::scope()
|
|
|
|
->with('user', 'contacts', 'country')
|
|
|
|
->withTrashed()
|
|
|
|
->where('is_deleted', '=', false)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
public function find($filter = null, $userId = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-12-07 14:34:55 +01:00
|
|
|
$query = DB::table('clients')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'clients.account_id')
|
2015-03-16 22:45:25 +01:00
|
|
|
->join('contacts', 'contacts.client_id', '=', 'clients.id')
|
|
|
|
->where('clients.account_id', '=', \Auth::user()->account_id)
|
|
|
|
->where('contacts.is_primary', '=', true)
|
|
|
|
->where('contacts.deleted_at', '=', null)
|
2016-10-20 17:14:54 +02:00
|
|
|
//->whereRaw('(clients.name != "" or contacts.first_name != "" or contacts.last_name != "" or contacts.email != "")') // filter out buy now invoices
|
2015-12-07 14:34:55 +01:00
|
|
|
->select(
|
|
|
|
DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'),
|
2016-11-27 10:46:32 +01:00
|
|
|
DB::raw("CONCAT(contacts.first_name, ' ', contacts.last_name) contact"),
|
2015-12-07 14:34:55 +01:00
|
|
|
'clients.public_id',
|
|
|
|
'clients.name',
|
|
|
|
'contacts.first_name',
|
|
|
|
'contacts.last_name',
|
|
|
|
'clients.balance',
|
|
|
|
'clients.last_login',
|
|
|
|
'clients.created_at',
|
2016-11-24 10:22:37 +01:00
|
|
|
'clients.created_at as client_created_at',
|
2015-12-07 14:34:55 +01:00
|
|
|
'clients.work_phone',
|
|
|
|
'contacts.email',
|
|
|
|
'clients.deleted_at',
|
2016-03-16 00:08:00 +01:00
|
|
|
'clients.is_deleted',
|
2017-02-28 19:24:30 +01:00
|
|
|
'clients.user_id',
|
|
|
|
'clients.id_number'
|
2015-12-07 14:34:55 +01:00
|
|
|
);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-11-18 14:31:43 +01:00
|
|
|
$this->applyFilters($query, ENTITY_CLIENT);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($filter) {
|
|
|
|
$query->where(function ($query) use ($filter) {
|
|
|
|
$query->where('clients.name', 'like', '%'.$filter.'%')
|
2017-04-27 10:06:09 +02:00
|
|
|
->orWhere('clients.id_number', 'like', '%'.$filter.'%')
|
2015-03-16 22:45:25 +01:00
|
|
|
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('contacts.email', 'like', '%'.$filter.'%');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-23 18:52:20 +02:00
|
|
|
if ($userId) {
|
|
|
|
$query->where('clients.user_id', '=', $userId);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return $query;
|
|
|
|
}
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public function save($data, $client = null)
|
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
|
|
|
|
2016-04-28 14:16:33 +02:00
|
|
|
if ($client) {
|
2017-01-30 17:05:31 +01:00
|
|
|
// do nothing
|
2017-01-30 20:40:43 +01:00
|
|
|
} elseif (! $publicId || $publicId == '-1') {
|
2015-03-16 22:45:25 +01:00
|
|
|
$client = Client::createNew();
|
|
|
|
} else {
|
|
|
|
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
|
|
|
|
}
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2017-03-29 12:22:09 +02:00
|
|
|
// auto-set the client id number
|
|
|
|
if (Auth::check() && Auth::user()->account->client_number_counter && !$client->id_number && empty($data['id_number'])) {
|
|
|
|
$data['id_number'] = Auth::user()->account->getNextNumber();
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:40:04 +02:00
|
|
|
if ($client->is_deleted) {
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:01:43 +01:00
|
|
|
// convert currency code to id
|
|
|
|
if (isset($data['currency_code'])) {
|
|
|
|
$currencyCode = strtolower($data['currency_code']);
|
2017-01-30 17:05:31 +01:00
|
|
|
$currency = Cache::get('currencies')->filter(function ($item) use ($currencyCode) {
|
2016-02-29 11:01:43 +01:00
|
|
|
return strtolower($item->code) == $currencyCode;
|
|
|
|
})->first();
|
|
|
|
if ($currency) {
|
|
|
|
$data['currency_id'] = $currency->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
$client->fill($data);
|
2015-03-16 22:45:25 +01:00
|
|
|
$client->save();
|
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
/*
|
|
|
|
if ( ! isset($data['contact']) && ! isset($data['contacts'])) {
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
$first = true;
|
2017-05-26 10:04:40 +02:00
|
|
|
$contacts = isset($data['contact']) ? [$data['contact']] : (isset($data['contacts']) ? $data['contacts'] : [[]]);
|
2015-03-16 22:45:25 +01:00
|
|
|
$contactIds = [];
|
2016-04-17 12:53:53 +02:00
|
|
|
|
2016-04-17 13:03:37 +02:00
|
|
|
// If the primary is set ensure it's listed first
|
2016-04-17 12:42:30 +02:00
|
|
|
usort($contacts, function ($left, $right) {
|
2016-11-04 10:48:27 +01:00
|
|
|
if (isset($right['is_primary']) && isset($left['is_primary'])) {
|
|
|
|
return $right['is_primary'] - $left['is_primary'];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-17 12:40:23 +02:00
|
|
|
});
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2015-11-17 09:46:14 +01:00
|
|
|
foreach ($contacts as $contact) {
|
2015-10-28 20:22:07 +01:00
|
|
|
$contact = $client->addContact($contact, $first);
|
|
|
|
$contactIds[] = $contact->public_id;
|
|
|
|
$first = false;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $client->wasRecentlyCreated) {
|
2016-06-02 21:03:59 +02:00
|
|
|
foreach ($client->contacts as $contact) {
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! in_array($contact->public_id, $contactIds)) {
|
2016-06-02 21:03:59 +02:00
|
|
|
$contact->delete();
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
if (! $publicId || $publicId == '-1') {
|
2016-02-16 16:30:09 +01:00
|
|
|
event(new ClientWasCreated($client));
|
|
|
|
} else {
|
|
|
|
event(new ClientWasUpdated($client));
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return $client;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2016-08-04 19:01:30 +02:00
|
|
|
|
|
|
|
public function findPhonetically($clientName)
|
|
|
|
{
|
|
|
|
$clientNameMeta = metaphone($clientName);
|
|
|
|
|
|
|
|
$map = [];
|
2016-08-13 21:19:37 +02:00
|
|
|
$max = SIMILAR_MIN_THRESHOLD;
|
2016-08-04 19:01:30 +02:00
|
|
|
$clientId = 0;
|
|
|
|
|
2016-08-08 16:45:37 +02:00
|
|
|
$clients = Client::scope()->get(['id', 'name', 'public_id']);
|
2016-08-04 19:01:30 +02:00
|
|
|
|
|
|
|
foreach ($clients as $client) {
|
2016-08-14 11:30:16 +02:00
|
|
|
$map[$client->id] = $client;
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $client->name) {
|
2016-08-04 19:01:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$similar = similar_text($clientNameMeta, metaphone($client->name), $percent);
|
|
|
|
|
|
|
|
if ($percent > $max) {
|
|
|
|
$clientId = $client->id;
|
|
|
|
$max = $percent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 16:45:37 +02:00
|
|
|
$contacts = Contact::scope()->get(['client_id', 'first_name', 'last_name', 'public_id']);
|
2016-08-04 19:01:30 +02:00
|
|
|
|
|
|
|
foreach ($contacts as $contact) {
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $contact->getFullName() || ! isset($map[$contact->client_id])) {
|
2016-08-04 19:01:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$similar = similar_text($clientNameMeta, metaphone($contact->getFullName()), $percent);
|
|
|
|
|
|
|
|
if ($percent > $max) {
|
|
|
|
$clientId = $contact->client_id;
|
|
|
|
$max = $percent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 21:19:37 +02:00
|
|
|
return ($clientId && isset($map[$clientId])) ? $map[$clientId] : null;
|
2016-08-04 19:01:30 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|