1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Ninja/Repositories/ExpenseRepository.php

198 lines
7.1 KiB
PHP
Raw Normal View History

2016-05-01 13:31:10 +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
use App\Ninja\Repositories\BaseRepository;
2016-01-08 19:01:00 +01:00
use Session;
2016-01-06 20:52:09 +01:00
class ExpenseRepository extends BaseRepository
{
protected $documentRepo;
2016-05-19 08:47:57 +02:00
2016-01-07 16:14:11 +01: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-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)
->where(function ($query) {
$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(
2016-02-17 14:55:31 +01:00
DB::raw('COALESCE(expenses.invoice_id, expenses.should_be_invoiced) expense_status_id'),
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-01-21 23:29:10 +01:00
'invoices.public_id as invoice_public_id',
'invoices.user_id as invoice_user_id',
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-01-09 06:24:43 +01:00
$showTrashed = \Session::get('show_trash:expense');
2016-01-19 20:35:15 +01:00
2016-01-09 06:24:43 +01:00
if (!$showTrashed) {
$query->where('expenses.deleted_at', '=', null);
}
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.'%')
->orWhere('vendors.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;
}
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();
\Log::warning('Entity not set in expense repo save');
2016-01-06 20:52:09 +01:00
} else {
$expense = Expense::createNew();
}
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-01-06 20:52:09 +01:00
$expense->expense_date = Utils::toSqlDate($input['expense_date']);
2016-01-26 21:22:33 +01:00
if (isset($input['private_notes'])) {
$expense->private_notes = trim($input['private_notes']);
}
2016-01-08 19:01:00 +01:00
$expense->public_notes = trim($input['public_notes']);
2016-01-21 20:15:30 +01:00
$expense->should_be_invoiced = isset($input['should_be_invoiced']) || $expense->client_id ? true : false;
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);
$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']):array();;
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-05-30 10:51:41 +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;
}
2016-01-19 20:35:15 +01:00
2016-01-09 09:08:24 +01:00
public function bulk($ids, $action)
{
$expenses = Expense::withTrashed()->scope($ids)->get();
foreach ($expenses as $expense) {
if ($action == 'restore') {
$expense->restore();
$expense->is_deleted = false;
$expense->save();
} else {
if ($action == 'delete') {
$expense->is_deleted = true;
$expense->save();
}
$expense->delete();
}
}
return count($tasks);
}
2016-01-06 20:52:09 +01:00
}