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

203 lines
7.9 KiB
PHP
Raw Normal View History

2016-07-21 14:35:23 +02:00
<?php namespace App\Ninja\Repositories;
2016-01-06 20:52:09 +01:00
use DB;
use Utils;
2016-04-26 03:53:39 +02:00
use Auth;
2016-01-06 20:52:09 +01:00
use App\Models\Expense;
use App\Models\Vendor;
use App\Models\Document;
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',
2016-01-07 12:04:01 +01:00
'expenses.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',
2016-07-06 20:35:16 +02:00
'expense_categories.name as category',
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);
if ( ! in_array(EXPENSE_STATUS_PAID, $statuses)) {
$query->where('invoices.balance', '>', 0);
}
}
if (in_array(EXPENSE_STATUS_PAID, $statuses)) {
$query->orWhere('invoices.balance', '=', 0)
->where('expenses.invoice_id', '>', 0);
}
});
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.'%')
->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
}
2016-10-23 10:01:21 +02:00
2016-07-04 11:41:44 +02:00
$expense->should_be_invoiced = isset($input['should_be_invoiced']) && floatval($input['should_be_invoiced']) || $expense->client_id ? true : false;
2016-01-21 20:15:30 +01:00
2016-02-01 23:07:09 +01:00
if ( ! $expense->expense_currency_id) {
$expense->expense_currency_id = \Auth::user()->account->getCurrencyId();
}
if ( ! $expense->invoice_currency_id) {
$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
$document_ids = !empty($input['document_ids'])?array_map('intval', $input['document_ids']):[];;
foreach ($document_ids as $document_id){
// check document completed upload before user submitted form
if ($document_id) {
$document = Document::scope($document_id)->first();
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
if ( ! $expense->wasRecentlyCreated) {
foreach ($expense->documents as $document){
if ( ! in_array($document->public_id, $document_ids)){
// Not checking permissions; deleting a document is just editing the invoice
$document->delete();
}
}
}
2016-01-06 20:52:09 +01:00
return $expense;
}
}