1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 09:51:35 +02:00
invoiceninja/app/ninja/repositories/ClientRepository.php

206 lines
5.4 KiB
PHP
Raw Normal View History

2013-12-25 22:34:42 +01:00
<?php namespace ninja\repositories;
use Client;
use Contact;
class ClientRepository
{
2014-01-06 19:03:00 +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)
->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');
2014-07-30 09:08:01 +02:00
if (!\Session::get('show_trash:client'))
2014-02-18 22:56:18 +01:00
{
$query->where('clients.deleted_at', '=', null);
}
2014-01-06 19:03:00 +01:00
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;
}
2014-07-30 09:08:01 +02:00
public function getErrors($data)
2014-07-27 22:31:41 +02:00
{
$contact = isset($data['contacts']) ? (array)$data['contacts'][0] : (isset($data['contact']) ? $data['contact'] : []);
$validator = \Validator::make($contact, ['email' => 'required|email']);
if ($validator->fails()) {
2014-07-30 09:08:01 +02:00
return $validator->messages();
2014-07-27 22:31:41 +02:00
}
2014-07-30 09:08:01 +02:00
return false;
}
2014-07-27 22:31:41 +02:00
2014-07-30 09:08:01 +02:00
public function save($publicId, $data, $notify = true)
{
2014-07-27 22:31:41 +02:00
if (!$publicId || $publicId == "-1")
2013-12-25 22:34:42 +01:00
{
$client = Client::createNew();
2014-07-27 22:31:41 +02:00
$client->currency_id = 1;
2013-12-25 22:34:42 +01:00
$contact = Contact::createNew();
$contact->is_primary = true;
2014-07-27 22:31:41 +02:00
$contact->send_invoice = true;
2013-12-25 22:34:42 +01:00
}
else
{
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
$contact = $client->contacts()->where('is_primary', '=', true)->firstOrFail();
}
2014-07-27 22:31:41 +02:00
if (isset($data['name'])) {
$client->name = trim($data['name']);
}
if (isset($data['work_phone'])) {
$client->work_phone = trim($data['work_phone']);
}
if (isset($data['custom_value1'])) {
$client->custom_value1 = trim($data['custom_value1']);
}
if (isset($data['custom_value2'])) {
$client->custom_value2 = trim($data['custom_value2']);
}
if (isset($data['address1'])) {
$client->address1 = trim($data['address1']);
}
if (isset($data['address2'])) {
$client->address2 = trim($data['address2']);
}
if (isset($data['city'])) {
$client->city = trim($data['city']);
}
if (isset($data['state'])) {
$client->state = trim($data['state']);
}
if (isset($data['postal_code'])) {
$client->postal_code = trim($data['postal_code']);
}
if (isset($data['country_id'])) {
$client->country_id = $data['country_id'] ? $data['country_id'] : null;
}
if (isset($data['private_notes'])) {
$client->private_notes = trim($data['private_notes']);
}
if (isset($data['size_id'])) {
$client->size_id = $data['size_id'] ? $data['size_id'] : null;
}
if (isset($data['industry_id'])) {
$client->industry_id = $data['industry_id'] ? $data['industry_id'] : null;
}
if (isset($data['currency_id'])) {
$client->currency_id = $data['currency_id'] ? $data['currency_id'] : 1;
}
if (isset($data['payment_terms'])) {
$client->payment_terms = $data['payment_terms'];
}
if (isset($data['website'])) {
$client->website = trim($data['website']);
}
2014-04-18 10:57:31 +02:00
2013-12-25 22:34:42 +01:00
$client->save();
$isPrimary = true;
$contactIds = [];
2014-07-27 22:31:41 +02:00
if (isset($data['contact']))
2013-12-25 22:34:42 +01:00
{
2014-07-27 22:31:41 +02:00
$info = $data['contact'];
if (isset($info['email'])) {
$contact->email = trim(strtolower($info['email']));
2013-12-25 22:34:42 +01:00
}
2014-07-27 22:31:41 +02:00
if (isset($info['first_name'])) {
$contact->first_name = trim($info['first_name']);
2013-12-25 22:34:42 +01:00
}
2014-07-27 22:31:41 +02:00
if (isset($info['last_name'])) {
$contact->last_name = trim($info['last_name']);
}
if (isset($info['phone'])) {
$contact->phone = trim($info['phone']);
}
$contact->is_primary = true;
$contact->send_invoice = true;
2013-12-25 22:34:42 +01:00
$client->contacts()->save($contact);
}
2014-07-27 22:31:41 +02:00
else
2013-12-25 22:34:42 +01:00
{
2014-07-27 22:31:41 +02:00
foreach ($data['contacts'] as $record)
{
$record = (array) $record;
if ($publicId != "-1" && isset($record['public_id']) && $record['public_id'])
{
$contact = Contact::scope($record['public_id'])->firstOrFail();
}
else
{
$contact = Contact::createNew();
}
if (isset($record['email'])) {
$contact->email = trim(strtolower($record['email']));
}
if (isset($record['first_name'])) {
$contact->first_name = trim($record['first_name']);
}
if (isset($record['last_name'])) {
$contact->last_name = trim($record['last_name']);
}
if (isset($record['phone'])) {
$contact->phone = trim($record['phone']);
}
$contact->is_primary = $isPrimary;
$contact->send_invoice = isset($record['send_invoice']) ? $record['send_invoice'] : true;
$isPrimary = false;
$client->contacts()->save($contact);
$contactIds[] = $contact->public_id;
}
foreach ($client->contacts as $contact)
{
if (!in_array($contact->public_id, $contactIds))
{
$contact->delete();
}
2013-12-25 22:34:42 +01:00
}
}
2014-01-02 00:12:33 +01:00
$client->save();
2014-01-08 21:09:47 +01:00
2014-07-27 22:31:41 +02:00
if (!$publicId || $publicId == "-1")
2014-01-09 20:00:08 +01:00
{
2014-07-27 22:31:41 +02:00
\Activity::createClient($client, $notify);
2014-01-09 20:00:08 +01:00
}
2014-01-02 00:12:33 +01:00
2013-12-25 22:34:42 +01:00
return $client;
}
2014-01-08 00:59:06 +01:00
public function bulk($ids, $action)
{
$clients = Client::scope($ids)->get();
foreach ($clients as $client)
{
if ($action == 'delete')
{
$client->is_deleted = true;
$client->save();
}
$client->delete();
}
return count($clients);
}
2013-12-25 22:34:42 +01:00
}