1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Services/ExpenseService.php

106 lines
2.7 KiB
PHP
Raw Normal View History

2016-01-06 20:52:09 +01:00
<?php namespace App\Services;
2016-03-16 00:08:00 +01:00
use Auth;
2016-01-08 19:01:00 +01:00
use DB;
2016-01-06 20:52:09 +01:00
use Utils;
use URL;
use App\Services\BaseService;
use App\Ninja\Repositories\ExpenseRepository;
2016-03-16 00:08:00 +01:00
use App\Models\Expense;
use App\Models\Invoice;
use App\Models\Client;
use App\Models\Vendor;
2016-05-23 18:52:20 +02:00
use App\Ninja\Datatables\ExpenseDatatable;
2016-01-07 12:04:01 +01:00
2016-01-06 20:52:09 +01:00
class ExpenseService extends BaseService
{
2016-01-07 16:14:11 +01:00
// Expenses
2016-01-06 20:52:09 +01:00
protected $expenseRepo;
protected $datatableService;
public function __construct(ExpenseRepository $expenseRepo, DatatableService $datatableService)
{
$this->expenseRepo = $expenseRepo;
$this->datatableService = $datatableService;
}
protected function getRepo()
{
return $this->expenseRepo;
}
public function save($data, $expense = null)
2016-01-06 20:52:09 +01:00
{
if (isset($data['client_id']) && $data['client_id']) {
$data['client_id'] = Client::getPrivateId($data['client_id']);
}
2016-03-02 14:36:42 +01:00
if (isset($data['vendor_id']) && $data['vendor_id']) {
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
}
2016-03-02 14:36:42 +01:00
return $this->expenseRepo->save($data, $expense);
2016-01-06 20:52:09 +01:00
}
2016-01-07 12:04:01 +01:00
public function getDatatable($search)
2016-01-06 20:52:09 +01:00
{
2016-01-07 12:04:01 +01:00
$query = $this->expenseRepo->find($search);
2016-01-06 20:52:09 +01:00
2016-03-16 00:08:00 +01:00
if(!Utils::hasPermission('view_all')){
$query->where('expenses.user_id', '=', Auth::user()->id);
}
2016-05-23 18:52:20 +02:00
return $this->datatableService->createDatatable(new ExpenseDatatable(), $query);
2016-01-06 20:52:09 +01:00
}
2016-01-19 20:35:15 +01:00
public function getDatatableVendor($vendorPublicId)
{
$datatable = new ExpenseDatatable(false, true);
2016-01-19 20:35:15 +01:00
$query = $this->expenseRepo->findVendor($vendorPublicId);
if(!Utils::hasPermission('view_all')){
$query->where('expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable($datatable, $query);
2016-01-19 20:35:15 +01:00
}
protected function getDatatableColumnsVendor($entityType, $hideClient)
{
return [
[
'expense_date',
function ($model) {
2016-01-28 15:07:03 +01:00
return Utils::dateToString($model->expense_date);
2016-01-19 20:35:15 +01:00
}
],
[
'amount',
function ($model) {
return Utils::formatMoney($model->amount, false, false);
}
],
[
'public_notes',
function ($model) {
return $model->public_notes != null ? $model->public_notes : '';
}
],
[
'invoice_id',
2016-01-19 20:35:15 +01:00
function ($model) {
return '';
2016-01-19 20:35:15 +01:00
}
],
2016-01-06 20:52:09 +01:00
];
}
2016-01-09 06:24:43 +01:00
2016-01-19 20:35:15 +01:00
protected function getDatatableActionsVendor($entityType)
{
return [];
}
2016-03-02 14:36:42 +01:00
2016-01-19 20:35:15 +01:00
}