App\Ninja\Repositories\InvoiceRepository::getInvoices PHP Method

getInvoices() public method

public getInvoices ( $accountId, $clientPublicId = false, $entityType = ENTITY_INVOICE, $filter = false )
    public function getInvoices($accountId, $clientPublicId = false, $entityType = ENTITY_INVOICE, $filter = false)
    {
        $query = DB::table('invoices')->join('accounts', 'accounts.id', '=', 'invoices.account_id')->join('clients', 'clients.id', '=', 'invoices.client_id')->join('invoice_statuses', 'invoice_statuses.id', '=', 'invoices.invoice_status_id')->join('contacts', 'contacts.client_id', '=', 'clients.id')->where('invoices.account_id', '=', $accountId)->where('clients.deleted_at', '=', null)->where('contacts.deleted_at', '=', null)->where('invoices.is_recurring', '=', false)->where('contacts.is_primary', '=', true)->select(DB::raw('COALESCE(clients.currency_id, accounts.currency_id) currency_id'), DB::raw('COALESCE(clients.country_id, accounts.country_id) country_id'), 'clients.public_id as client_public_id', 'clients.user_id as client_user_id', 'invoice_number', 'invoice_status_id', DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"), 'invoices.public_id', 'invoices.amount', 'invoices.balance', 'invoices.invoice_date', 'invoices.due_date', 'invoice_statuses.name as invoice_status_name', 'contacts.first_name', 'contacts.last_name', 'contacts.email', 'invoices.quote_id', 'invoices.quote_invoice_id', 'invoices.deleted_at', 'invoices.is_deleted', 'invoices.partial', 'invoices.user_id');
        if (!\Session::get('show_trash:' . $entityType)) {
            $query->where('invoices.deleted_at', '=', null);
        }
        if ($clientPublicId) {
            $query->where('clients.public_id', '=', $clientPublicId);
        }
        if ($filter) {
            $query->where(function ($query) use($filter) {
                $query->where('clients.name', 'like', '%' . $filter . '%')->orWhere('invoices.invoice_number', 'like', '%' . $filter . '%')->orWhere('invoice_statuses.name', 'like', '%' . $filter . '%')->orWhere('contacts.first_name', 'like', '%' . $filter . '%')->orWhere('contacts.last_name', 'like', '%' . $filter . '%')->orWhere('contacts.email', 'like', '%' . $filter . '%');
            });
        }
        return $query;
    }

Usage Example

 public function getDatatable($accountId, $clientPublicId = null, $entityType, $search)
 {
     $datatable = new InvoiceDatatable(!$clientPublicId, $clientPublicId);
     $datatable->entityType = $entityType;
     $query = $this->invoiceRepo->getInvoices($accountId, $clientPublicId, $entityType, $search)->where('invoices.invoice_type_id', '=', $entityType == ENTITY_QUOTE ? INVOICE_TYPE_QUOTE : INVOICE_TYPE_STANDARD);
     if (!Utils::hasPermission('view_all')) {
         $query->where('invoices.user_id', '=', Auth::user()->id);
     }
     return $this->datatableService->createDatatable($datatable, $query);
 }