1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Services/ExpenseService.php

119 lines
2.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Services;
2016-01-06 20:52:09 +01:00
use App\Models\Client;
use App\Models\Vendor;
2016-05-23 18:52:20 +02:00
use App\Ninja\Datatables\ExpenseDatatable;
2017-01-30 20:40:43 +01:00
use App\Ninja\Repositories\ExpenseRepository;
use Auth;
use Utils;
2016-01-07 12:04:01 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class ExpenseService.
*/
2016-01-06 20:52:09 +01:00
class ExpenseService extends BaseService
{
/**
* @var ExpenseRepository
*/
2016-01-06 20:52:09 +01:00
protected $expenseRepo;
/**
* @var DatatableService
*/
2016-01-06 20:52:09 +01:00
protected $datatableService;
/**
* ExpenseService constructor.
*
* @param ExpenseRepository $expenseRepo
2017-01-30 20:40:43 +01:00
* @param DatatableService $datatableService
*/
2016-01-06 20:52:09 +01:00
public function __construct(ExpenseRepository $expenseRepo, DatatableService $datatableService)
{
$this->expenseRepo = $expenseRepo;
$this->datatableService = $datatableService;
}
/**
* @return ExpenseRepository
*/
2016-01-06 20:52:09 +01:00
protected function getRepo()
{
return $this->expenseRepo;
}
/**
* @param $data
* @param null $expense
2017-01-30 20:40:43 +01:00
*
* @return mixed|null
*/
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
}
/**
* @param $search
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\JsonResponse
*/
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
if (! Utils::hasPermission('view_expense')) {
2016-03-16 00:08:00 +01:00
$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
}
/**
* @param $vendorPublicId
2017-01-30 20:40:43 +01:00
*
* @return \Illuminate\Http\JsonResponse
*/
2016-01-19 20:35:15 +01:00
public function getDatatableVendor($vendorPublicId)
{
2016-11-25 15:02:39 +01:00
$datatable = new ExpenseDatatable(true, true);
2016-01-19 20:35:15 +01:00
$query = $this->expenseRepo->findVendor($vendorPublicId);
if (! Utils::hasPermission('view_vendor')) {
$query->where('expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable($datatable, $query);
2016-01-19 20:35:15 +01:00
}
/**
* @param $clientPublicId
*
* @return \Illuminate\Http\JsonResponse
*/
public function getDatatableClient($clientPublicId)
{
$datatable = new ExpenseDatatable(true, true);
$query = $this->expenseRepo->findClient($clientPublicId);
if (! Utils::hasPermission('view_client')) {
$query->where('expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable($datatable, $query);
2016-01-19 20:35:15 +01:00
}
2016-01-19 20:35:15 +01:00
}