1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00
invoiceninja/app/Ninja/Repositories/ClientRepository.php

81 lines
2.6 KiB
PHP
Raw Normal View History

<?php namespace App\Ninja\Repositories;
2015-03-16 22:45:25 +01:00
2015-10-28 20:22:07 +01:00
use App\Ninja\Repositories\BaseRepository;
2015-03-31 11:38:24 +02:00
use App\Models\Client;
use App\Models\Contact;
use App\Models\Activity;
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();
}
2015-03-16 22:45:25 +01:00
public function find($filter = null)
{
$query = \DB::table('clients')
->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)
->select('clients.public_id', 'clients.name', 'contacts.first_name', 'contacts.last_name', 'clients.balance', 'clients.last_login', 'clients.created_at', 'clients.work_phone', 'contacts.email', 'clients.currency_id', 'clients.deleted_at', 'clients.is_deleted');
if (!\Session::get('show_trash:client')) {
$query->where('clients.deleted_at', '=', null);
}
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
->orWhere('contacts.email', 'like', '%'.$filter.'%');
});
}
return $query;
}
2015-10-28 20:22:07 +01:00
public function save($data)
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
2015-10-28 20:22:07 +01:00
if (!$publicId || $publicId == '-1') {
2015-03-16 22:45:25 +01:00
$client = Client::createNew();
} else {
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
}
2015-10-28 20:22:07 +01:00
$client->fill($data);
2015-03-16 22:45:25 +01:00
$client->save();
2015-10-28 20:22:07 +01:00
$first = true;
$contacts = isset($data['contact']) ? [$data['contact']] : $data['contacts'];
2015-03-16 22:45:25 +01:00
$contactIds = [];
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
}
2015-10-28 20:22:07 +01:00
foreach ($client->contacts as $contact) {
if (!in_array($contact->public_id, $contactIds)) {
$contact->delete();
2015-03-16 22:45:25 +01:00
}
}
2015-10-28 20:22:07 +01:00
return $client;
2015-03-16 22:45:25 +01:00
}
}