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

176 lines
6.3 KiB
PHP
Raw Normal View History

2016-01-21 20:15:30 +01:00
<?php namespace app\Ninja\Repositories;
2016-01-06 20:52:09 +01:00
use DB;
use Utils;
use App\Models\Expense;
use App\Models\Vendor;
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
{
2016-01-07 16:14:11 +01:00
// Expenses
2016-01-06 20:52:09 +01:00
public function getClassName()
{
return 'App\Models\Expense';
}
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);
2016-01-19 20:35:15 +01:00
$accountid = \Auth::user()->account_id;
$query = DB::table('expenses')
->join('accounts', 'accounts.id', '=', 'expenses.account_id')
->where('expenses.account_id', '=', $accountid)
2016-01-26 21:22:33 +01:00
->where('expenses.vendor_id', '=', $vendorId)
2016-01-21 23:29:10 +01:00
->select(
'expenses.id',
'expenses.expense_date',
'expenses.amount',
'expenses.public_notes',
'expenses.public_id',
'expenses.deleted_at',
'expenses.should_be_invoiced',
2016-03-16 00:08:00 +01:00
'expenses.created_at',
'expenses.user_id'
2016-01-21 23:29:10 +01:00
);
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',
2016-01-23 22:36:31 +01:00
'clients.name as client_name',
'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)
{
$publicId = isset($input['public_id']) ? $input['public_id'] : false;
if ($publicId) {
$expense = Expense::scope($publicId)->firstOrFail();
} 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-01-06 20:52:09 +01:00
$expense->save();
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
}