245 lines
8.1 KiB
PHP
245 lines
8.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Repositories\Backend\Sampraday;
|
||
|
|
|
||
|
|
use App\Constant\Constant;
|
||
|
|
use App\Exceptions\GeneralException;
|
||
|
|
use App\Models\Dharma;
|
||
|
|
use App\Models\Sampraday;
|
||
|
|
use App\Services\BaseService;
|
||
|
|
use Exception;
|
||
|
|
use Illuminate\Database\Eloquent\Builder;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Yajra\DataTables\Facades\DataTables;
|
||
|
|
|
||
|
|
class SampradayService extends BaseService
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Associated Service Model.
|
||
|
|
*/
|
||
|
|
protected const MODEL = Sampraday::class;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is use for search query in Datatable.
|
||
|
|
* Also for sorting function we are using join query for appropriate sorting result
|
||
|
|
* @param $request
|
||
|
|
* @return $this|array|Builder
|
||
|
|
*/
|
||
|
|
public function prepareSearchQuery($request)
|
||
|
|
{
|
||
|
|
$searchQuery = [];
|
||
|
|
try {
|
||
|
|
$searchQuery = $this->query()->with('dharma')->select(
|
||
|
|
'id',
|
||
|
|
'name',
|
||
|
|
'dharma_id',
|
||
|
|
'status',
|
||
|
|
'created_at',
|
||
|
|
);
|
||
|
|
|
||
|
|
$columns = ['', 'id', 'name', 'dharma_id', 'status', 'created_at'];
|
||
|
|
|
||
|
|
if (isset($request['status']) && $request['status'] != '') {
|
||
|
|
$searchQuery = $searchQuery->where('status', $request['status']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($request['name']) && $request['name'] != '') {
|
||
|
|
$searchQuery = $searchQuery->where('name', 'like', '%' . $request['name'] . '%');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($request['order']) && $request['order'] != "") {
|
||
|
|
$searchQuery = $searchQuery->orderBy(
|
||
|
|
$columns[$request['order'][0]['column']],
|
||
|
|
$request['order'][0]['dir']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
return $searchQuery;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the format the Datatable columns
|
||
|
|
*
|
||
|
|
* @param $request
|
||
|
|
* @return object
|
||
|
|
*/
|
||
|
|
public function getForDataTable($request): object
|
||
|
|
{
|
||
|
|
$response = (object)[];
|
||
|
|
try {
|
||
|
|
$dataTableQuery = $this->prepareSearchQuery($request);
|
||
|
|
// ->orderBy('id', 'DESC');
|
||
|
|
|
||
|
|
$response = Datatables::of($dataTableQuery)
|
||
|
|
->addColumn(
|
||
|
|
'checkbox_action',
|
||
|
|
function ($sampraday) {
|
||
|
|
return $sampraday->sampraday_checkbox_action;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addIndexColumn()
|
||
|
|
->addColumn(
|
||
|
|
'dharma_id',
|
||
|
|
function ($jati) {
|
||
|
|
return $jati->dharma->name ?? '-';
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'status',
|
||
|
|
function ($sampraday) {
|
||
|
|
return $sampraday->sampraday_status_action;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'created_at',
|
||
|
|
function ($sampraday) {
|
||
|
|
return (($sampraday->created_at) ? defaultDateTimeFormat($sampraday->created_at) : '-');
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'action',
|
||
|
|
function ($sampraday) {
|
||
|
|
if (
|
||
|
|
!empty($sampraday->roles[0]['name']) != config('access.users.admin_role')
|
||
|
|
|| !empty($sampraday->roles[0]['name']) != config('access.users.super_admin_role')
|
||
|
|
|| loggedInUser()->isSuperAdmin()
|
||
|
|
|| loggedInUser()->isAdmin()
|
||
|
|
) {
|
||
|
|
return $sampraday->sampraday_action_buttons;
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->setRowId(
|
||
|
|
function ($sampraday) {
|
||
|
|
return 'recordRow-' . $sampraday->id;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->rawColumns(['status', 'action', 'checkbox_action'])
|
||
|
|
->make(true);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the create sampraday
|
||
|
|
*
|
||
|
|
* @param array $request
|
||
|
|
* @return Sampraday
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function create(array $request): Sampraday
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
return DB::transaction(
|
||
|
|
function () use ($request) {
|
||
|
|
|
||
|
|
$sampraday = $this->query()->create(
|
||
|
|
[
|
||
|
|
'name' => $request['name'],
|
||
|
|
'status' => !empty($request['status']) ? Constant::STATUS_ONE : Constant::STATUS_ONE,
|
||
|
|
'sant_id' => $request['sant_id'] ?? Constant::NULL,
|
||
|
|
'dharma_id' => $request['dharma_id'] ?? Constant::NULL,
|
||
|
|
'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return $sampraday;
|
||
|
|
}
|
||
|
|
);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.create_sampraday_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the update sampraday
|
||
|
|
*
|
||
|
|
* @param object $sampraday
|
||
|
|
* @param array $request
|
||
|
|
* @return Sampraday
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function update(object $sampraday, array $request): Sampraday
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
return DB::transaction(
|
||
|
|
function () use ($sampraday, $request) {
|
||
|
|
|
||
|
|
$this->query()->where('id', $sampraday->id)->update(
|
||
|
|
[
|
||
|
|
'name' => !empty($request['name']) ? $request['name'] : $sampraday->name,
|
||
|
|
'sant_id' => $request['sant_id'] ?? $sampraday->sant_id,
|
||
|
|
'dharma_id' => $request['dharma_id'] ?? $sampraday->dharma_id,
|
||
|
|
'status' => isset($request['status']) ? Constant::STATUS_ONE : Constant::STATUS_ZERO,
|
||
|
|
'updated_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : $sampraday->updated_by,
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
return $sampraday;
|
||
|
|
}
|
||
|
|
);
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.update_sampraday_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is for the multiple records action from the grid
|
||
|
|
*
|
||
|
|
* @param string $actionType
|
||
|
|
* @param array $data
|
||
|
|
* @return bool
|
||
|
|
* @throws GeneralException
|
||
|
|
*/
|
||
|
|
public function gridActions(string $actionType, array $data): bool
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
switch ($actionType) {
|
||
|
|
case Constant::STATUS_ACTIVE:
|
||
|
|
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ONE]);
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
case Constant::STATUS_INACTIVE:
|
||
|
|
$this->query()->whereIn('id', $data)->update(['status' => Constant::STATUS_ZERO]);
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
case Constant::STATUS_DELETE:
|
||
|
|
$this->query()->whereIn('id', $data)->delete();
|
||
|
|
return Constant::STATUS_TRUE;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
throw new GeneralException(__('message.delete_sampraday_error'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This function is use for get gachadhi pati by dharma.
|
||
|
|
*
|
||
|
|
* @param $request
|
||
|
|
* @return $this|array|Builder
|
||
|
|
*/
|
||
|
|
public function getGachadhiPatiByDharma($data)
|
||
|
|
{
|
||
|
|
$gachadhiPaties = [];
|
||
|
|
try {
|
||
|
|
$dharma = Dharma::find($data['dharma_id']);
|
||
|
|
$gachadhiPaties = $dharma->sants()->where('dharma_id', $data['dharma_id'])->get();
|
||
|
|
return $gachadhiPaties;
|
||
|
|
} catch (Exception $ex) {
|
||
|
|
Log::error($ex->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|