New-OMIS/app/Http/Controllers/RegistrationsController.php

531 lines
25 KiB
PHP
Raw Normal View History

2024-04-05 05:22:15 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\CustomMailer;
use App\Models\Agents;
use App\Models\Branches;
use App\Models\Campaigns;
use App\Models\Countries;
use App\Models\Leadcategories;
use App\Models\Qrscans;
use Illuminate\Http\Request;
use App\Models\Registrations;
use App\Models\Sources;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\CommonModelService;
use Log;
use Exception;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use LMS;
use NewRegistrationAdminNotification;
class RegistrationsController extends Controller
{
protected $modelService;
public function __construct(Registrations $model)
{
$this->modelService = new CommonModelService($model);
}
public function index(Request $request)
{
$data = $this->filterRegistrations($request);
return view("crud.generated.registrations.index", compact('data', 'request'));
}
public function bycampaign(Request $request, $CampaignAlias)
{
$Campaign = Campaigns::where("status", 1)->where("alias", $CampaignAlias)->first();
$title = "Registrations By Campaign : " . $Campaign->title;
$data = $this->filterRegistrations($request, $Campaign);
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
public function bycategory(Request $request, $CategoryAlias)
{
$LeadCategory = Leadcategories::where("alias", $CategoryAlias)->first();
$title = "Registrations By Lead Category : " . $LeadCategory->title;
$data = $this->filterRegistrations($request, null, $LeadCategory);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
public function bybranch(Request $request, $BranchAlias)
{
$Branch = Branches::where("alias", $BranchAlias)->first();
$title = "Registrations By Branch : " . $Branch->title;
$data = $this->filterRegistrations($request, null,null,null,null,null, $Branch);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
public function bysource(Request $request, $SourceAlias)
{
$Source = Sources::where("alias", $SourceAlias)->first();
$title = "Registrations By Source : " . $Source->title;
$data = $this->filterRegistrations($request, null, null, $Source);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
public function bycountry(Request $request, $CountryAlias)
{
$Country = Countries::where("alias", $CountryAlias)->first();
$title = "Registrations By Country : " . $Country->title;
$data = $this->filterRegistrations($request, null, null, null, $Country);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
public function byagent(Request $request, $AgentAlias)
{
$Agent = Agents::where("alias", $AgentAlias)->first();
$title = "Registrations By Agency : " . $Agent->title;
$data = $this->filterRegistrations($request, null, null, null, null, $Agent);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
private function filterRegistrations($request, $Campaign = null, $LeadCategory = null, $Source = null, $Country = null, $Agent = null, $Branch=null)
{
DB::enableQueryLog();
$requestData = $request->all();
// dd($requestData);
$data = Registrations::where('status', '<>', -1);
if (isset($requestData['from']) && $requestData['from']) {
$data = $data->whereDate("created_at", ">=", $requestData['from']);
}
if (isset($requestData['to'])) {
$data = $data->whereDate("created_at", "<", $requestData['to']);
}
if (isset($requestData['source_id']) && $requestData['source_id'] != 0) {
$data = $data->where("sources_id", "=", $requestData['source_id']);
}
if (isset($requestData['country_id']) && $requestData['country_id'] != 0) {
$data = $data->where("countries_id", "=", $requestData['country_id']);
}
if (isset($requestData['leadcategory_id']) && $requestData['leadcategory_id']) {
$data = $data->where("leadcategories_id", "=", $requestData['leadcategory_id']);
}
if (isset($requestData['branch_id']) && $requestData['branch_id']) {
$data = $data->where("branches_id", "=", $requestData['branch_id']);
}
if ($Campaign !== null) {
$data = $data->where("campaigns_id", $Campaign->campaign_id);
}
if ($LeadCategory !== null) {
$data = $data->where("leadcategories_id", $LeadCategory->leadcategory_id);
}
if ($Source !== null) {
$data = $data->where("sources_id", $Source->source_id);
}
if ($Country !== null) {
$data = $data->where("countries_id", $Country->country_id);
}
if ($Agent !== null) {
$data = $data->where("agents_id", $Agent->agent_id);
}
if ($Branch !== null) {
$data = $data->where("branches_id", $Branch->branch_id);
}
$data = $data->orderBy('display_order');
$sqlQuery = $data->toSql();
$data = $data->get();
$data->RawQuery = DB::getQueryLog();
return $data;
}
public function create(Request $request)
{
createActivityLog(RegistrationsController::class, 'create', ' Registrations create');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
return view("crud.generated.registrations.create", compact('TableData'));
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
// 'mobile' => 'required|numeric|unique:registrations,mobile', // Assuming 'users' is your table name and 'mobile' is the column
'mobile' => 'required|numeric:registrations,mobile', // Assuming 'users' is your table name and 'mobile' is the column
]);
$Campaign = Campaigns::findOrFail($request->campaigns_id);
if ($validator->fails()) {
$existingRegistration = Registrations::where('mobile', $request->mobile)->first();
if ($existingRegistration) {
//$qr_code = LMS::getLeadQR($existingRegistration->registration_id);
return response()->json([
'status' => false,
'message' => 'Mobile number already exists. Returning existing data.',
// 'qr_code' => site_url(str_replace(public_path(), '', $qr_code)),
'registration' => $existingRegistration,
'registration_id' => $existingRegistration->registration_id,
], 200);
}
return response()->json([
'status' => false,
'errors' => $validator->errors(),
'message' => 'Form validation failed.',
], 200); // 422 Unprocessable Entity
}
$request->request->add(['alias' => slugify($request->title)]);
$request->request->add(['display_order' => getDisplayOrder('tbl_registrations')]);
$request->request->add(['created_at' => date("Y-m-d h:i:s")]);
$request->request->add(['updated_at' => date("Y-m-d h:i:s")]);
$request->request->add(['highest_qualification' => $request->qualification]);
$request->request->add(['highest_year' => ($request->highest_year) ? $request->highest_year : 0]);
$request->request->add(['highest_college' => $request->board]);
$request->request->add(['intrested_for_country' => ($request->preferred_destination) ? $request->preferred_destination : SITEVARS->Countries[0]->country_id]);
$request->request->add(['sources_id' => ($request->sources_id) ? $request->sources_id : 2]);
$request->request->add(['campaigns_id' => ($request->campaigns_id) ? $request->campaigns_id : SITEVARS->Campaigns[0]->campaign_id]);
$request->request->add(['countries_id' => SITEVARS->Countries[0]->country_id]);
$request->request->add(['remarks' => $request->message]);
$requestData = $request->all();
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL') . '/', '', $value);
});
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL'), '', $value);
});
DB::beginTransaction();
try {
$operationNumber = getOperationNumber();
$registration = $this->modelService->create($operationNumber, $operationNumber, null, $requestData);
} catch (\Exception $e) {
dd($e->getMessage());
DB::rollBack();
}
DB::commit();
$mailer = new CustomMailer($requestData);
if (env('SEND_EMAIL') == true) {
// Mail::bcc("prajwalbro@hotmail.com")->send($mailer->build());
Mail::to($request['email'])->send($mailer->build());
$notify_emails = isset($Campaign->notify_emails) ? explode(",", $Campaign->notify_emails) : "prajwalbro@hotmail.com";
Mail::cc($notify_emails)->send($mailer->registration_completed());
}
$msg = $Campaign->sms_message;
$success_messsage = $Campaign->success_messsage;
$success_messsage = str_replace('%name%', $request['name'], $success_messsage);
// dd($Campaign);
if (env('SEND_SMS') == true) {
$msg = str_replace('%name%', $request['name'], $msg);
LMS::sendSMSWithCurl($requestData['mobile'], $msg);
}
LMS::SaveInGoogleSheet($requestData);
return response()->json([
'status' => true,
'qr_code' => false,
'message' => ($success_messsage) ? $success_messsage : 'The Registration Created Successfully.',
'registration_id' => $registration->registration_id,
], 200);
}
public function saveform(Request $request)
{
$validator = Validator::make($request->all(), [
'mobile' => 'required|numeric|unique:registrations,mobile', // Assuming 'users' is your table name and 'mobile' is the column
]);
$Campaign = Campaigns::findOrFail($request->campaigns_id);
if ($validator->fails()) {
$existingRegistration = Registrations::where('mobile', $request->mobile)->first();
if ($existingRegistration) {
return response()->json([
'status' => false,
'message' => 'Mobile number already exists',
'registration' => $existingRegistration,
'registration_id' => $existingRegistration->registration_id,
], 200);
}
return response()->json([
'status' => false,
'errors' => $validator->errors(),
'message' => 'Form validation failed.',
], 200); // 422 Unprocessable Entity
}
$request->request->add(['alias' => slugify($request->title)]);
$request->request->add(['display_order' => getDisplayOrder('tbl_registrations')]);
$request->request->add(['created_at' => date("Y-m-d h:i:s")]);
$request->request->add(['updated_at' => date("Y-m-d h:i:s")]);
$request->request->add(['highest_qualification' => $request->qualification]);
$request->request->add(['highest_year' => ($request->highest_year) ? $request->highest_year : 0]);
$request->request->add(['highest_college' => $request->board]);
$request->request->add(['intrested_for_country' => $request->preferred_destination]);
$request->request->add(['sources_id' => ($request->sources_id) ? $request->sources_id : 2]);
$request->request->add(['campaigns_id' => ($request->campaigns_id) ? $request->campaigns_id : SITEVARS->Campaigns[0]->campaign_id]);
$request->request->add(['countries_id' => SITEVARS->Countries[0]->country_id]);
$request->request->add(['remarks' => $request->message]);
$requestData = $request->all();
DB::beginTransaction();
try {
$operationNumber = getOperationNumber();
$registration = $this->modelService->create($operationNumber, $operationNumber, null, $requestData);
if (env('SEND_EMAIL') == true) {
$notify_emails = (isset($Campaign->notify_emails) && ($Campaign->notify_emails != "")) ? explode(",", $Campaign->notify_emails) : "prajwalbro@hotmail.com";
$mailer = new CustomMailer($requestData);
Mail::to($request['email'])->send($mailer->build());
Mail::cc($notify_emails)->send($mailer->build());
Mail::bcc("prajwalbro@hotmail.com")->send($mailer->build());
}
$success_messsage = $Campaign->success_messsage;
if (isset($Campaign->sms_message) && ($Campaign->sms_message != "")) {
$msg = $Campaign->sms_message;
$success_messsage = str_replace('%name%', $request['name'], $success_messsage);
$msg = str_replace('%name%', $request['name'], $msg);
if (env('SEND_SMS') == true) LMS::sendSMSWithCurl($requestData['mobile'], $msg);
}
} catch (\Exception $e) {
dd($e->getMessage());
DB::rollBack();
}
DB::commit();
return response()->json([
'status' => true,
'message' => ($success_messsage) ? $success_messsage : 'The Registration Created Successfully.',
'registration_id' => $registration->registration_id,
], 200);
}
function sendbulksms(Request $request)
{
$validator = Validator::make($request->all(), [
'message' => 'required', // Assuming 'users' is your table name and 'mobile' is the column
]);
if ($validator->fails()) {
return response()->json([
'status' => false,
'errors' => $validator->errors(),
'message' => 'Form validation failed.',
], 200); // 422 Unprocessable Entity
}
$mobile_nos = explode(",", $request->mobile);
//dd($mobile_nos);
$msg = $request->message;
foreach ($mobile_nos as $mobile) {
LMS::sendSMSWithCurl($mobile, $msg);
}
}
public function home(Request $request, $campaign_alias = null)
{
$alias = str_replace("_", "-", $campaign_alias);
$Campaign = Campaigns::where("status", "1")->where("alias", $alias)->first();
if ($Campaign) {
$viewPath = env("CLIENT_PATH") . ".landing.$campaign_alias";
// echo $viewPath;die;
if (view()->exists($viewPath))
return view($viewPath, compact("Campaign"));
else
return view(env("CLIENT_PATH") . '.home', compact("Campaign"));
}
switch ($campaign_alias) {
case 'office': // Student/Visitor office visit gareko bela, MOBILE bata QR Scan garera fill garne
return view("access.office-visits");
case 'spin_the_wheel': // Student/Visitor office visit gareko bela, MOBILE bata QR Scan garera fill garne
return view("games.spin_the_wheel.game");
case 'direct': //Office ko Reception Bata directly fill-in garne form
return view("backend.reception");
case 'online': //Adv etc run garda online default landing bata fill-in garne form
return view(env("CLIENT_PATH") . ".home");
default:
$Campaign = LMS::getActiveCampaign();
if ($Campaign) {
$viewPath = env("CLIENT_PATH") . ".landing.$Campaign->alias";
// echo $viewPath;die;
if (view()->exists($viewPath))
return view($viewPath, compact("Campaign"));
else
return view(env("CLIENT_PATH") . '.home', compact("Campaign"));
}
// $viewPath = env("CLIENT_PATH") . ".landing";
// if (view()->exists($viewPath))
// return view($viewPath, compact("Campaign"));
// else
// return view(env("CLIENT_PATH") . '.home', compact("Campaign"));
}
}
public function zapierrequest(Request $request)
{
//Office ko Reception Bata directly fill-in garne form
//return view("backend.reception");
$requiredFields = ['name', 'email', 'phone', 'qualification', 'passed', 'interest', 'source'];
if ($this->checkFieldsPresence($request, $requiredFields)) {
$campaign = Campaigns::orderBy("display_order", "ASC")->first();
$source = Sources::orderBy("display_order", "ASC")->first();
$data = [
'name' => $request->query('name'),
'email' => $request->query('email'),
'phone' => $request->query('phone'),
'mobile' => $request->query('phone'),
'highest_qualification' => $request->query('qualification'),
'highest_year' => $request->query('passed'),
'intrested_course' => $request->query('interest'),
// 'intrested_country' => $request->query('interest'),
'leadcategories_id' => 0,
'campaigns_id' => $campaign ? $campaign->campaign_id : null,
'sourcess_id' => $source ? $source->source_id : null,
'remarks' => $request->query('source')
];
Registrations::create($data);
return response()->json(['message' => 'Registration successful'], Response::HTTP_OK);
} else {
return response()->json(['message' => 'Registration Failed. Missing required fields.'], Response::HTTP_BAD_REQUEST);
}
}
private function checkFieldsPresence(Request $request, array $fields)
{
foreach ($fields as $field) {
if (!$request->query($field)) {
return false;
}
}
return true;
}
public function reception(Request $request)
{
//Office ko Reception Bata directly fill-in garne form
return view("backend.reception");
}
public function reception_registrations(Request $request)
{
$data = $this->filterRegistrations($request);
//Office ko Reception Bata directly fill-in garne form
return view("backend.reception.registrations", compact('data'));
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Registrations::find($id);
$companyArticle->display_order = $index + 1;
$companyArticle->save();
}
return response()->json(['status' => true, 'content' => 'The articles sorted successfully.'], 200);
}
public function updatealias(Request $request)
{
$articleId = $request->input('articleId');
$newAlias = $request->input('newAlias');
$companyArticle = Registrations::find($articleId);
if (!$companyArticle) {
return response()->json(['status' => false, 'content' => 'Company article not found.'], 404);
}
$companyArticle->alias = $newAlias;
$companyArticle->save();
return response()->json(['status' => true, 'content' => 'Alias updated successfully.'], 200);
}
public function view(Request $request, $id)
{
// $data = '';
if (Auth::check()) {
$data = Registrations::where(DB::raw('md5(registration_id)'), $id)->firstOrFail();
if ($data) {
$qrdata = [
'registrations_id' => $data->registration_id,
'qrscan_type' => ($request->qrscan_type) ? $request->qrscan_type : 'mannual scan',
'qrscan_location' => ($request->qrscan_type) ? $request->qrscan_type : 'office visit',
// Add other columns as needed
];
Qrscans::create($qrdata);
return view("access.steps.scanqr", compact('data'));
}
return "Sorry!!! It seems QR is INVALID";
} else {
// Redirect the user to the login page or handle the case where the user is not logged in
return redirect()->route('login'); // Adjust 'login' to the actual login route in your application
}
}
public function confirmation($id)
{
$data = '';
$data = Registrations::where(DB::raw('md5(registration_id)'), $id)->firstOrFail();
return view("lms.confirmation", compact('data'));
}
public function show(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'show', ' Registrations show');
$data = Registrations::findOrFail($id);
return view("crud.generated.registrations.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'edit', ' Registrations edit');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
$data = Registrations::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.registrations.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.registrations.edit", compact('data', 'TableData'));
}
public function ajaxedit(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'edit', ' Registrations edit');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
$data = Registrations::findOrFail($id);
return view("crud.generated.registrations.ajaxedit", compact('data', 'TableData'));
}
public function update(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'update', ' Registrations update');
$validator = Validator::make($request->all(), [
//ADD VALIDATION FOR REQIRED FIELDS
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
$requestData = $request->all();
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL') . '/', '', $value);
});
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL'), '', $value);
});
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->update($OperationNumber, $OperationNumber, null, $requestData, $request->input('registration_id'));
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(RegistrationsController::class, 'update', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Registrations updated Successfully.'], 200);
}
// return redirect()->route('registrations.index')->with('success','The Registrations updated Successfully.');
return redirect()->back()->with('success', 'The Registrations updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'destroy', ' Registrations destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(RegistrationsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Registrations Deleted Successfully.'], 200);
}
public function toggle(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'destroy', ' Registrations destroy');
$data = Registrations::findOrFail($id);
$requestData = ['status' => ($data->status == 1) ? 0 : 1];
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->update($OperationNumber, $OperationNumber, null, $requestData, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(RegistrationsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Registrations Deleted Successfully.'], 200);
}
}