1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Ninja/Repositories/ExpenseRepository.php

223 lines
8.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2016-01-06 20:52:09 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Ninja\Repositories;
use App\Models\Document;
2016-01-06 20:52:09 +01:00
use App\Models\Expense;
use App\Models\Vendor;
2017-01-30 20:40:43 +01:00
use Auth;
use DB;
use Utils;
2016-01-06 20:52:09 +01:00
class ExpenseRepository extends BaseRepository
{
protected $documentRepo;
2016-05-19 08:47:57 +02:00
2016-07-21 14:35:23 +02:00
// Expenses
2016-01-06 20:52:09 +01:00
public function getClassName()
{
return 'App\Models\Expense';
}
public function __construct(DocumentRepository $documentRepo)
{
$this->documentRepo = $documentRepo;
}
2016-05-19 08:47:57 +02:00
2016-01-07 12:04:01 +01:00
public function all()
2016-01-06 20:52:09 +01:00
{
2016-01-07 12:04:01 +01:00
return Expense::scope()
->with('user')
->withTrashed()
->where('is_deleted', '=', false)
->get();
}
2016-01-19 20:35:15 +01:00
public function findVendor($vendorPublicId)
{
2016-01-26 21:22:33 +01:00
$vendorId = Vendor::getPrivateId($vendorPublicId);
$query = $this->find()->where('expenses.vendor_id', '=', $vendorId);
2016-01-21 20:15:30 +01:00
return $query;
2016-01-19 20:35:15 +01:00
}
2016-01-07 12:04:01 +01:00
public function find($filter = null)
{
$accountid = \Auth::user()->account_id;
$query = DB::table('expenses')
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
2016-01-21 23:29:10 +01:00
->leftjoin('clients', 'clients.id', '=', 'expenses.client_id')
2016-01-23 22:36:31 +01:00
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
2016-01-21 23:29:10 +01:00
->leftjoin('vendors', 'vendors.id', '=', 'expenses.vendor_id')
->leftJoin('invoices', 'invoices.id', '=', 'expenses.invoice_id')
2016-07-06 20:35:16 +02:00
->leftJoin('expense_categories', 'expenses.expense_category_id', '=', 'expense_categories.id')
2016-01-07 12:04:01 +01:00
->where('expenses.account_id', '=', $accountid)
2016-01-23 22:36:31 +01:00
->where('contacts.deleted_at', '=', null)
->where('vendors.deleted_at', '=', null)
->where('clients.deleted_at', '=', null)
2016-11-29 18:47:26 +01:00
->where(function ($query) { // handle when client isn't set
2016-01-23 22:36:31 +01:00
$query->where('contacts.is_primary', '=', true)
2016-01-28 15:07:03 +01:00
->orWhere('contacts.is_primary', '=', null);
2016-01-23 22:36:31 +01:00
})
->select(
DB::raw('COALESCE(expenses.invoice_id, expenses.should_be_invoiced) status'),
2016-01-23 22:36:31 +01:00
'expenses.account_id',
2016-01-07 12:04:01 +01:00
'expenses.amount',
2016-01-08 19:01:00 +01:00
'expenses.deleted_at',
'expenses.exchange_rate',
2017-03-28 11:40:53 +02:00
'expenses.expense_date as expense_date_sql',
DB::raw("CONCAT(expenses.expense_date, expenses.created_at) as expense_date"),
2016-01-08 19:01:00 +01:00
'expenses.id',
'expenses.is_deleted',
'expenses.private_notes',
'expenses.public_id',
'expenses.invoice_id',
2016-01-07 12:04:01 +01:00
'expenses.public_notes',
2016-01-08 19:01:00 +01:00
'expenses.should_be_invoiced',
2016-01-09 06:24:43 +01:00
'expenses.vendor_id',
2016-02-01 23:07:09 +01:00
'expenses.expense_currency_id',
'expenses.invoice_currency_id',
'expenses.user_id',
2016-10-27 16:26:42 +02:00
'expenses.tax_rate1',
'expenses.tax_rate2',
2017-10-14 19:55:37 +02:00
'expenses.private_notes',
2017-04-19 16:18:24 +02:00
'expenses.payment_date',
2016-07-06 20:35:16 +02:00
'expense_categories.name as category',
2017-03-25 20:01:35 +01:00
'expense_categories.user_id as category_user_id',
'expense_categories.public_id as category_public_id',
2016-01-21 23:29:10 +01:00
'invoices.public_id as invoice_public_id',
'invoices.user_id as invoice_user_id',
2016-07-03 11:33:35 +02:00
'invoices.balance',
2016-01-23 22:36:31 +01:00
'vendors.name as vendor_name',
'vendors.public_id as vendor_public_id',
'vendors.user_id as vendor_user_id',
DB::raw("COALESCE(NULLIF(clients.name,''), NULLIF(CONCAT(contacts.first_name, ' ', contacts.last_name),''), NULLIF(contacts.email,'')) client_name"),
2016-01-23 22:36:31 +01:00
'clients.public_id as client_public_id',
'clients.user_id as client_user_id',
2016-01-23 22:36:31 +01:00
'contacts.first_name',
'contacts.email',
'contacts.last_name',
2016-01-21 23:29:10 +01:00
'clients.country_id as client_country_id'
);
2016-01-08 19:01:00 +01:00
2016-11-18 14:31:43 +01:00
$this->applyFilters($query, ENTITY_EXPENSE);
2016-11-20 15:08:36 +01:00
if ($statuses = session('entity_status_filter:' . ENTITY_EXPENSE)) {
$statuses = explode(',', $statuses);
2016-11-18 14:31:43 +01:00
$query->where(function ($query) use ($statuses) {
2016-11-20 15:08:36 +01:00
$query->whereNull('expenses.id');
2016-11-18 14:31:43 +01:00
if (in_array(EXPENSE_STATUS_LOGGED, $statuses)) {
$query->orWhere('expenses.invoice_id', '=', 0)
->orWhereNull('expenses.invoice_id');
}
if (in_array(EXPENSE_STATUS_INVOICED, $statuses)) {
$query->orWhere('expenses.invoice_id', '>', 0);
2017-04-19 16:18:24 +02:00
if (! in_array(EXPENSE_STATUS_BILLED, $statuses)) {
2016-11-18 14:31:43 +01:00
$query->where('invoices.balance', '>', 0);
}
}
2017-04-19 16:18:24 +02:00
if (in_array(EXPENSE_STATUS_BILLED, $statuses)) {
2016-11-18 14:31:43 +01:00
$query->orWhere('invoices.balance', '=', 0)
->where('expenses.invoice_id', '>', 0);
}
2017-04-19 16:18:24 +02:00
if (in_array(EXPENSE_STATUS_PAID, $statuses)) {
$query->orWhereNotNull('expenses.payment_date');
}
if (in_array(EXPENSE_STATUS_UNPAID, $statuses)) {
$query->orWhereNull('expenses.payment_date');
}
if (in_array(EXPENSE_STATUS_PENDING, $statuses)) {
$query->orWhere('expenses.should_be_invoiced', '=', 1)
->whereNull('expenses.invoice_id');
}
2016-11-18 14:31:43 +01:00
});
2016-01-09 06:24:43 +01:00
}
2016-01-19 20:35:15 +01:00
2016-01-06 20:52:09 +01:00
if ($filter) {
$query->where(function ($query) use ($filter) {
2016-01-28 15:07:03 +01:00
$query->where('expenses.public_notes', 'like', '%'.$filter.'%')
->orWhere('clients.name', 'like', '%'.$filter.'%')
2016-07-06 20:35:16 +02:00
->orWhere('vendors.name', 'like', '%'.$filter.'%')
2017-01-30 17:05:31 +01:00
->orWhere('expense_categories.name', 'like', '%'.$filter.'%');
;
2016-01-06 20:52:09 +01:00
});
}
2016-01-08 19:01:00 +01:00
2016-01-06 20:52:09 +01:00
return $query;
}
2016-07-21 14:35:23 +02:00
public function save($input, $expense = null)
2016-01-06 20:52:09 +01:00
{
$publicId = isset($input['public_id']) ? $input['public_id'] : false;
if ($expense) {
// do nothing
} elseif ($publicId) {
2016-01-06 20:52:09 +01:00
$expense = Expense::scope($publicId)->firstOrFail();
2016-10-10 10:40:04 +02:00
if (Utils::isNinjaDev()) {
\Log::warning('Entity not set in expense repo save');
}
2016-01-06 20:52:09 +01:00
} else {
$expense = Expense::createNew();
}
2016-10-10 10:40:04 +02:00
if ($expense->is_deleted) {
return $expense;
}
2016-01-08 19:01:00 +01:00
// First auto fill
2016-01-06 20:52:09 +01:00
$expense->fill($input);
2016-01-19 20:35:15 +01:00
2016-10-23 10:01:21 +02:00
if (isset($input['expense_date'])) {
$expense->expense_date = Utils::toSqlDate($input['expense_date']);
2016-01-26 21:22:33 +01:00
}
2017-04-19 16:18:24 +02:00
if (isset($input['payment_date'])) {
$expense->payment_date = Utils::toSqlDate($input['payment_date']);
}
2016-10-23 10:01:21 +02:00
2017-01-30 17:05:31 +01:00
if (! $expense->expense_currency_id) {
2016-02-01 23:07:09 +01:00
$expense->expense_currency_id = \Auth::user()->account->getCurrencyId();
}
2017-01-30 17:05:31 +01:00
if (! $expense->invoice_currency_id) {
2016-02-01 23:07:09 +01:00
$expense->invoice_currency_id = \Auth::user()->account->getCurrencyId();
2016-01-23 22:36:31 +01:00
}
2016-01-21 21:36:49 +01:00
$rate = isset($input['exchange_rate']) ? Utils::parseFloat($input['exchange_rate']) : 1;
$expense->exchange_rate = round($rate, 4);
2016-10-23 10:01:21 +02:00
if (isset($input['amount'])) {
$expense->amount = round(Utils::parseFloat($input['amount']), 2);
}
2016-05-19 08:47:57 +02:00
2016-03-25 00:55:56 +01:00
$expense->save();
2016-01-21 21:36:49 +01:00
// Documents
2017-01-30 20:40:43 +01:00
$document_ids = ! empty($input['document_ids']) ? array_map('intval', $input['document_ids']) : [];
2017-06-26 10:10:51 +02:00
2017-01-30 17:05:31 +01:00
foreach ($document_ids as $document_id) {
// check document completed upload before user submitted form
if ($document_id) {
$document = Document::scope($document_id)->first();
2017-01-30 17:05:31 +01:00
if ($document && Auth::user()->can('edit', $document)) {
$document->invoice_id = null;
$document->expense_id = $expense->id;
$document->save();
}
}
}
2016-07-03 11:33:35 +02:00
2016-05-19 08:47:57 +02:00
// prevent loading all of the documents if we don't have to
2017-01-30 17:05:31 +01:00
if (! $expense->wasRecentlyCreated) {
foreach ($expense->documents as $document) {
if (! in_array($document->public_id, $document_ids)) {
2016-05-19 08:47:57 +02:00
// Not checking permissions; deleting a document is just editing the invoice
$document->delete();
}
}
}
2016-01-06 20:52:09 +01:00
return $expense;
}
}