1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Ninja/Repositories/ProjectRepository.php

88 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Repositories;
2016-11-29 18:47:26 +01:00
use App\Models\Project;
2017-01-30 20:40:43 +01:00
use Auth;
use DB;
2017-12-24 16:13:47 +01:00
use Utils;
2016-11-29 18:47:26 +01:00
class ProjectRepository extends BaseRepository
{
public function getClassName()
{
return 'App\Models\Project';
}
public function all()
{
return Project::scope()->get();
}
public function find($filter = null, $userId = false)
{
$query = DB::table('projects')
->where('projects.account_id', '=', Auth::user()->account_id)
->leftjoin('clients', 'clients.id', '=', 'projects.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->where('contacts.deleted_at', '=', null)
->where('clients.deleted_at', '=', null)
->where(function ($query) { // handle when client isn't set
$query->where('contacts.is_primary', '=', true)
->orWhere('contacts.is_primary', '=', null);
})
->select(
'projects.name as project',
'projects.public_id',
'projects.user_id',
'projects.deleted_at',
'projects.task_rate',
2016-11-29 18:47:26 +01:00
'projects.is_deleted',
2017-12-24 16:23:43 +01:00
'projects.due_date',
'projects.budgeted_hours',
'projects.private_notes',
2016-11-29 18:47:26 +01:00
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
'clients.user_id as client_user_id',
'clients.public_id as client_public_id'
);
$this->applyFilters($query, ENTITY_PROJECT);
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.'%')
->orWhere('projects.name', 'like', '%'.$filter.'%');
});
}
if ($userId) {
$query->where('projects.user_id', '=', $userId);
}
return $query;
}
public function save($input, $project = false)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
2017-01-30 17:05:31 +01:00
if (! $project) {
2016-11-29 18:47:26 +01:00
$project = Project::createNew();
2016-12-15 13:17:10 +01:00
$project['client_id'] = $input['client_id'];
2016-11-29 18:47:26 +01:00
}
$project->fill($input);
2017-12-24 16:13:47 +01:00
if (isset($input['due_date'])) {
$project->due_date = Utils::toSqlDate($input['due_date']);
}
2016-11-29 18:47:26 +01:00
$project->save();
return $project;
}
}