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