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

390 lines
16 KiB
PHP

<?php
namespace App\Repositories\Backend\Chaturmas;
use Exception;
use App\Models\Sant;
use App\Models\User;
use App\Models\Chaturmas;
use App\Constant\Constant;
use App\Models\ThanaMember;
use App\Services\BaseService;
use App\Models\UserDeviceToken;
use Illuminate\Support\Facades\Log;
use App\Exceptions\GeneralException;
use App\Jobs\Notifications\Chaturmas\SendApproveChaturmas;
use App\Traits\PushNotificationTraits;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Database\Eloquent\Builder;
use App\Jobs\Notifications\Chaturmas\SendNewChaturmas;
use App\Jobs\Notifications\Chaturmas\SendRejectChaturmas;
use App\Jobs\Notifications\Chaturmas\SendUpdateChaturmas;
class ChaturmasService extends BaseService
{
use PushNotificationTraits;
/**
* Associated Service Model.
*/
protected const MODEL = Chaturmas::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, $sant)
{
$searchQuery = [];
try {
$searchQuery = $this->query()->with('chaturmasInfo')->select(
'id',
'chaturmas_date_id',
'place',
'sangh_id',
'sant_id',
'is_approved',
'created_by',
'updated_by',
'created_at'
)->where('sant_id', $sant->id);
$columns = ['', 'id', 'place', 'sant_id', 'updated_by','created_at'];
if (isset($request['name']) && $request['name'] != '') {
$searchQuery = $searchQuery->where('place', '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, $sant)
{
$response = (object)[];
try {
$dataTableQuery = $this->prepareSearchQuery($request, $sant);
// ->orderBy('id', 'DESC');
//return $dataTableQuery;
$response = Datatables::of($dataTableQuery)
->addColumn(
'checkbox_action',
function ($chaturmas) {
return $chaturmas->chaturmas_date_checkbox_action;
}
)
->addIndexColumn()
->addColumn(
'place',
function($chaturmas) {
if (!empty($chaturmas->place)) {
$place = $chaturmas->place;
} else if (!empty($chaturmas->sangh_id)) {
$place = $chaturmas->sangh->name;
} else {
$place = "-";
}
return $place;
}
)
->editColumn(
'chaturmas_date_id',
function($chaturmas) {
return ($chaturmas->chaturmasInfo->year) ? $chaturmas->chaturmasInfo->year : '-';
}
)
// ->addColumn(
// 'from',
// function($chaturmas) {
// return defaultDateTimeFormat($chaturmas->chaturmas_info->from);
// }
// )
// ->addColumn(
// 'to',
// function($chaturmas) {
// return defaultDateTimeFormat($chaturmas->chaturmas_info->to);
// }
// )
->addColumn(
'updated_by',
function($chaturmas) {
return $chaturmas->updatedBy->name ?? '-';
}
)
->addColumn(
'status',
function ($chaturmas) {
if ($chaturmas->is_approved === Constant::STATUS_ONE) {
return '<span class="label label-lg label-light-success label-inline">' . __('label.approved') . '</span>';
} else {
return '<span class="label label-lg label-light-warning label-inline">' . __('label.in_review') . '</span>';
}
}
)
->addColumn(
'created_at',
function($chaturmas) {
return defaultDateTimeFormat($chaturmas->created_at);
}
)
->addColumn(
'action',
function ($vihar) {
if (
!empty($vihar->roles[0]['name']) != config('access.users.admin_role')
|| !empty($vihar->roles[0]['name']) != config('access.users.super_admin_role')
|| loggedInUser()->isSuperAdmin()
|| loggedInUser()->isAdmin()
) {
return $vihar->chaturmas_date_action_buttons;
}
return "";
}
)
->setRowId(
function ($chaturmas) {
return 'recordRow-' . $chaturmas->id;
}
)
->rawColumns(['checkbox_action', 'status','action'])
->make(true);
} catch (Exception $ex) {
Log::error($ex->getMessage());
}
return $response;
}
/**
* This function is for the create chaturmas
*
* @param array $request
* @return Chaturmas
* @throws GeneralException
*/
public function create(array $request, $sant)
{
try {
$user = loggedInUser();
$thanaID = ThanaMember::where('sant_id', $sant->id)->value('thana_id');
$thanaSantID = ThanaMember::where('thana_id', $thanaID)->pluck('sant_id');
if (count($thanaSantID) > 0) {
foreach ($thanaSantID as $santID) {
$chaturmas = $this->query()->create(
[
'chaturmas_date_id' => $request['chaturmas_date_id'],
'place' => $request['place'],
'latitude' => $request['latitude'],
'longitude' => $request['longitude'],
'sant_id' => $santID,
'thana_sant_id' => $thanaID,
'is_approved' => $request['is_approved'] ?? Constant::NULL,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL
]
);
$sant = Sant::find($santID);
//Push Notification to followers of sant
dispatch(new SendNewChaturmas($sant, $user));
}
} else {
$chaturmas = $this->query()->create(
[
'chaturmas_date_id' => $request['chaturmas_date_id'],
'place' => $request['place'],
'latitude' => $request['latitude'],
'longitude' => $request['longitude'],
'sant_id' => $sant->id,
'thana_sant_id' => Constant::NULL,
'is_approved' => $request['is_approved'] ?? Constant::NULL,
'created_by' => loggedInUser()->id,
'updated_by' => Constant::NULL
]
);
$sant = Sant::find($sant->id);
//Push Notification to followers of sant
dispatch(new SendNewChaturmas($sant, $user));
}
return $chaturmas;
} catch (Exception $ex) {
Log::error($ex->getMessage());
throw new GeneralException(__('message.create_chaturmas_error'));
}
}
/**
* This function is for the update chaturmas
*
* @param object $chaturmas
* @param array $request
* @return Chaturmas
* @throws GeneralException
*/
public function update(object $chaturmas, array $request, $sant)
{
try {
$user = loggedInUser();
$chaturmasYear = $chaturmas->chaturmas_date_id ?? "";
$chaturmasID = Chaturmas::where('thana_sant_id', $chaturmas->thana_sant_id)
->where('thana_sant_id', '!=', null)
->where('chaturmas_date_id', $chaturmasYear)
->pluck('id');
$chaturmasData = Chaturmas::find($chaturmas->id);
if (count($chaturmasID) > 0) {
foreach ($chaturmasID as $id) {
$chaturmasDetails = Chaturmas::find($id);
$chaturmas = $this->query()->where('id', $id)->update(
[
'chaturmas_date_id' => $request['chaturmas_date_id'],
'place' => $request['place'],
'latitude' => $request['latitude'],
'longitude' => $request['longitude'],
'sant_id' => $chaturmasDetails->sant_id,
'thana_sant_id' => $chaturmasDetails->thana_sant_id ?? Constant::NULL,
'updated_by' => !empty($chaturmasDetails->updated_by) ? $chaturmasDetails->updated_by : Constant::NULL,
'is_approved' => isset($request['is_approved']) ? Constant::STATUS_ONE : Constant::STATUS_ZERO,
]
);
$sant = Sant::find($chaturmasDetails->sant_id);
dispatch(new SendUpdateChaturmas($sant, $user, $chaturmas));
if (isset($request['is_approved']) && $request['is_approved'] == Constant::STATUS_ZERO) {
$chaturmasDetails->is_approved = Constant::STATUS_ONE;
$chaturmasDetails->save();
$chaturmasData->addKarmaPoints($chaturmasData, $chaturmasData->created_by, config('config-variables.karma_points_message.add_chaturmas'), config('config-variables.karma_points.add_chaturmas'), config('config-variables.karma_points_key.add_chaturmas'), []);
//Push Notification to user who added chaturmas
dispatch(new SendApproveChaturmas($sant, $user, $chaturmasData));
//Push Notification to followers of sant
dispatch(new SendNewChaturmas($sant, $user));
}
}
} else {
$chaturmas = $this->query()->where('id', $chaturmas->id)->update(
[
'chaturmas_date_id' => $request['chaturmas_date_id'],
'place' => $request['place'],
'latitude' => $request['latitude'],
'longitude' => $request['longitude'],
'sant_id' => $chaturmasData->sant_id,
'thana_sant_id' => $chaturmasData->thana_sant_id ?? Constant::NULL,
'updated_by' => !empty($chaturmasData->updated_by) ? $chaturmasData->updated_by : Constant::NULL,
'is_approved' => isset($request['is_approved']) ? Constant::STATUS_ONE : Constant::STATUS_ZERO,
]
);
$sant = Sant::find($chaturmasData->sant_id);
dispatch(new SendUpdateChaturmas($sant, $user, $chaturmas));
if (isset($request['is_approved']) && $request['is_approved'] == Constant::STATUS_ZERO) {
$chaturmasData->is_approved = Constant::STATUS_ONE;
$chaturmasData->save();
$chaturmasData->addKarmaPoints($chaturmasData, $chaturmasData->created_by, config('config-variables.karma_points_message.add_chaturmas'), config('config-variables.karma_points.add_chaturmas'), config('config-variables.karma_points_key.add_chaturmas'), []);
//Push Notification to user who added chaturmas
dispatch(new SendApproveChaturmas($sant, $user, $chaturmasData));
//Push Notification to followers of sant
dispatch(new SendNewChaturmas($sant, $user));
}
}
// Add karma points on chaturmas approve
// if (isset($chaturmasData->is_approved) && $chaturmasData->is_approved == Constant::STATUS_ONE) {
// $chaturmasData->addKarmaPoints($chaturmasData, $chaturmasData->created_by, config('config-variables.karma_points_message.add_chaturmas'), config('config-variables.karma_points.add_chaturmas'), config('config-variables.karma_points_key.add_chaturmas'), []);
// }
if (!empty($chaturmas)) {
return $chaturmas;
}
} catch (Exception $ex) {
Log::error($ex->getMessage());
throw new GeneralException(__('message.update__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:
$chaturmasDetails = Chaturmas::whereIn('id', $data)->where('thana_sant_id', '!=', null)->get();
if (count($chaturmasDetails) > 0) {
foreach ($chaturmasDetails as $chaturmasDetail) {
$chaturmasSantThana = Chaturmas::where('id', $chaturmasDetail->id)->pluck('thana_sant_id');
$chaturmasYear = Chaturmas::where('id', $chaturmasDetail->id)->pluck('chaturmas_date_id');
$chaturmas = $this->query()->whereIn('thana_sant_id', $chaturmasSantThana)
->where('thana_sant_id', '!=', null)
->where('chaturmas_date_id', $chaturmasYear)
->get();
if (count($chaturmas) > 0) {
foreach ($chaturmas as $data) {
$data->delete();
}
}
}
$user = User::where('id', $chaturmasDetail->created_by)->first() ?? "";
$sant = Sant::where('id', $chaturmasDetail->sant_id)->first() ?? "";
//Delete Chaturmas Activity
$chaturmasDetail->activity()->whereJsonContains('properties->attributes->chaturmas_information->id', $chaturmasDetail->id)->delete();
//Push Notification
dispatch(new SendRejectChaturmas($sant, $user));
} else {
$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_error'));
}
}
}