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

109 lines
2.8 KiB
PHP

<?php namespace App\Services;
use Auth;
use Utils;
use App\Ninja\Repositories\ExpenseRepository;
use App\Models\Client;
use App\Models\Vendor;
use App\Models\ExpenseCategory;
use App\Ninja\Datatables\ExpenseDatatable;
/**
* Class ExpenseService
*/
class ExpenseService extends BaseService
{
/**
* @var ExpenseRepository
*/
protected $expenseRepo;
/**
* @var DatatableService
*/
protected $datatableService;
/**
* ExpenseService constructor.
*
* @param ExpenseRepository $expenseRepo
* @param DatatableService $datatableService
*/
public function __construct(ExpenseRepository $expenseRepo, DatatableService $datatableService)
{
$this->expenseRepo = $expenseRepo;
$this->datatableService = $datatableService;
}
/**
* @return ExpenseRepository
*/
protected function getRepo()
{
return $this->expenseRepo;
}
/**
* @param $data
* @param null $expense
* @return mixed|null
*/
public function save($data, $expense = null)
{
if (isset($data['client_id']) && $data['client_id']) {
$data['client_id'] = Client::getPrivateId($data['client_id']);
}
if (isset($data['vendor_id']) && $data['vendor_id']) {
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
}
if ( ! empty($data['category'])) {
$name = trim($data['category']);
$category = ExpenseCategory::scope()->whereName($name)->first();
if ( ! $category) {
$category = ExpenseCategory::createNew();
$category->name = $name;
$category->save();
}
$data['expense_category_id'] = $category->id;
} elseif (isset($data['category'])) {
$data['expense_category_id'] = null;
}
return $this->expenseRepo->save($data, $expense);
}
/**
* @param $search
* @return \Illuminate\Http\JsonResponse
*/
public function getDatatable($search)
{
$query = $this->expenseRepo->find($search);
if(!Utils::hasPermission('view_all')){
$query->where('expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable(new ExpenseDatatable(), $query);
}
/**
* @param $vendorPublicId
* @return \Illuminate\Http\JsonResponse
*/
public function getDatatableVendor($vendorPublicId)
{
$datatable = new ExpenseDatatable(false, true);
$query = $this->expenseRepo->findVendor($vendorPublicId);
if(!Utils::hasPermission('view_all')){
$query->where('expenses.user_id', '=', Auth::user()->id);
}
return $this->datatableService->createDatatable($datatable, $query);
}
}