1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Ninja/Repositories/ProposalRepository.php

95 lines
3.4 KiB
PHP
Raw Normal View History

2018-01-31 10:10:15 +01:00
<?php
namespace App\Ninja\Repositories;
use App\Models\Proposal;
2018-02-01 07:47:17 +01:00
use App\Models\Invoice;
2018-02-04 17:42:13 +01:00
use App\Models\ProposalTemplate;
2018-01-31 10:10:15 +01:00
use Auth;
use DB;
use Utils;
class ProposalRepository extends BaseRepository
{
public function getClassName()
{
return 'App\Models\Proposal';
}
public function all()
{
return Proposal::scope()->get();
}
public function find($filter = null, $userId = false)
{
$query = DB::table('proposals')
->where('proposals.account_id', '=', Auth::user()->account_id)
2018-02-07 15:16:31 +01:00
->leftjoin('invoices', 'invoices.id', '=', 'proposals.invoice_id')
2018-01-31 10:10:15 +01:00
->leftjoin('clients', 'clients.id', '=', 'invoices.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
2018-02-04 17:42:13 +01:00
->leftJoin('proposal_templates', 'proposal_templates.id', '=', 'proposals.proposal_template_id')
2018-01-31 10:10:15 +01:00
->where('clients.deleted_at', '=', null)
->where('contacts.deleted_at', '=', null)
->where('contacts.is_primary', '=', true)
->select(
'proposals.public_id',
'proposals.user_id',
'proposals.deleted_at',
2018-02-04 17:42:13 +01:00
'proposals.created_at',
2018-01-31 10:10:15 +01:00
'proposals.is_deleted',
'proposals.private_notes',
2018-02-04 22:50:57 +01:00
'proposals.html as content',
2018-01-31 10:10:15 +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',
2018-02-04 17:42:13 +01:00
'invoices.invoice_number as quote',
2018-02-07 15:16:31 +01:00
'invoices.invoice_number as invoice_number',
'invoices.public_id as invoice_public_id',
'invoices.user_id as invoice_user_id',
2018-02-04 17:42:13 +01:00
'proposal_templates.name as template',
'proposal_templates.public_id as template_public_id',
'proposal_templates.user_id as template_user_id'
2018-01-31 10:10:15 +01:00
);
$this->applyFilters($query, ENTITY_PROPOSAL);
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('invoices.invoice_number', 'like', '%'.$filter.'%');
});
}
if ($userId) {
$query->where('proposals.user_id', '=', $userId);
}
return $query;
}
public function save($input, $proposal = false)
{
if (! $proposal) {
$proposal = Proposal::createNew();
}
$proposal->fill($input);
2018-02-01 07:47:17 +01:00
2018-02-07 15:16:31 +01:00
if (isset($input['invoice_id'])) {
$proposal->invoice_id = $input['invoice_id'] ? Invoice::getPrivateId($input['invoice_id']) : null;
2018-02-01 07:47:17 +01:00
}
2018-02-04 17:42:13 +01:00
if (isset($input['proposal_template_id'])) {
$proposal->proposal_template_id = $input['proposal_template_id'] ? ProposalTemplate::getPrivateId($input['proposal_template_id']) : null;
}
2018-01-31 10:10:15 +01:00
$proposal->save();
return $proposal;
}
}