Files
Global-Jain/app/Repositories/Backend/ChaturmasDate/ChaturmasDateService.php
2025-11-05 10:37:10 +05:30

226 lines
7.7 KiB
PHP

<?php
namespace App\Repositories\Backend\ChaturmasDate;
use App\Constant\Constant;
use App\Exceptions\GeneralException;
use App\Models\ChaturmasDate;
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 ChaturmasDateService extends BaseService
{
/**
* Associated Service Model.
*/
protected const MODEL = ChaturmasDate::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()->select(
'id',
'year',
'from',
'to',
'created_at',
);
$columns = ['', 'id', 'year', 'from', 'to', 'created_at'];
if (isset($request['year']) && $request['year'] != '') {
$searchQuery = $searchQuery->where('year', 'like', '%' . $request['year'] . '%');
}
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 ($chaturmas_date) {
return $chaturmas_date->chaturmas_date_checkbox_action;
}
)
->addIndexColumn()
->addColumn(
'year',
function ($chaturmas_date) {
return (($chaturmas_date->year) ? $chaturmas_date->year : '-');
}
)
->addColumn(
'from',
function ($chaturmas_date) {
return (($chaturmas_date->from) ? defaultDateTimeFormat($chaturmas_date->from) : '-');
}
)
->addColumn(
'to',
function ($chaturmas_date) {
return (($chaturmas_date->to) ? defaultDateTimeFormat($chaturmas_date->to) : '-');
}
)
->addColumn(
'created_at',
function ($chaturmas_date) {
return (($chaturmas_date->created_at) ? defaultDateTimeFormat($chaturmas_date->created_at) : '-');
}
)
->addColumn(
'action',
function ($chaturmas_date) {
if (
!empty($chaturmas_date->roles[0]['name']) != config('access.users.admin_role')
|| !empty($chaturmas_date->roles[0]['name']) != config('access.users.super_admin_role')
|| loggedInUser()->isSuperAdmin()
|| loggedInUser()->isAdmin()
) {
return $chaturmas_date->chaturmas_date_action_buttons;
}
return "";
}
)
->setRowId(
function ($chaturmas_date) {
return 'recordRow-' . $chaturmas_date->id;
}
)
->rawColumns(['status', 'action', 'checkbox_action'])
->make(true);
} catch (Exception $ex) {
Log::error($ex->getMessage());
}
return $response;
}
/**
* This function is for the create chaturmas_date
*
* @param array $request
* @return ChaturmasDate
* @throws GeneralException
*/
public function create(array $request): ChaturmasDate
{
try {
return DB::transaction(
function () use ($request) {
$chaturmas_date = $this->query()->create(
[
'year' => $request['year'],
'from' => !empty($request['from']) ? $request['from'] : Constant::STATUS_ZERO,
'to' => !empty($request['to']) ? $request['to'] : Constant::STATUS_ZERO,
'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL
]
);
return $chaturmas_date;
}
);
} catch (Exception $ex) {
Log::error($ex->getMessage());
throw new GeneralException(__('message.create_chaturmas_date_error'));
}
}
/**
* This function is for the update chaturmas_date
*
* @param object $chaturmas_date
* @param array $request
* @return ChaturmasDate
* @throws GeneralException
*/
public function update(object $chaturmas_date, array $request): ChaturmasDate
{
try {
return DB::transaction(
function () use ($chaturmas_date, $request) {
$this->query()->where('id', $chaturmas_date->id)->update(
[
'year' => !empty($request['year']) ? $request['year'] : $chaturmas_date->year,
'from' => !empty($request['from']) ? $request['from'] : $chaturmas_date->from,
'to' => !empty($request['to']) ? $request['to'] : $chaturmas_date->to,
'updated_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : $chaturmas_date->updated_by,
]
);
return $chaturmas_date;
}
);
} catch (Exception $ex) {
Log::error($ex->getMessage());
throw new GeneralException(__('message.update_chaturmas_date_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_chaturmas_date_error'));
}
}
}