2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Ninja\Repositories;
|
2015-12-07 23:41:48 +02:00
|
|
|
|
|
|
|
|
use App\Models\Contact;
|
|
|
|
|
|
|
|
|
|
class ContactRepository extends BaseRepository
|
|
|
|
|
{
|
2017-08-31 15:55:15 +03:00
|
|
|
public function all()
|
|
|
|
|
{
|
|
|
|
|
return Contact::scope()
|
|
|
|
|
->withTrashed()
|
|
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 11:36:48 +03:00
|
|
|
public function save($data, $contact = false)
|
2015-12-07 23:41:48 +02:00
|
|
|
{
|
|
|
|
|
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
|
|
|
|
|
|
2017-03-27 11:36:48 +03:00
|
|
|
if ($contact) {
|
|
|
|
|
// do nothing
|
2018-06-19 22:46:35 +03:00
|
|
|
} elseif (! $publicId || intval($publicId) < 0) {
|
2015-12-07 23:41:48 +02:00
|
|
|
$contact = Contact::createNew();
|
|
|
|
|
$contact->send_invoice = true;
|
|
|
|
|
$contact->client_id = $data['client_id'];
|
|
|
|
|
$contact->is_primary = Contact::scope()->where('client_id', '=', $contact->client_id)->count() == 0;
|
2017-04-02 20:46:01 +03:00
|
|
|
$contact->contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
|
2015-12-07 23:41:48 +02:00
|
|
|
} else {
|
|
|
|
|
$contact = Contact::scope($publicId)->firstOrFail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contact->fill($data);
|
|
|
|
|
$contact->save();
|
|
|
|
|
|
|
|
|
|
return $contact;
|
|
|
|
|
}
|
2017-01-30 18:05:31 +02:00
|
|
|
}
|