|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/paras.theinteractive.in/app/Helpers/ |
Upload File : |
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Request;
// use App\Models\LogActivity as LogActivityModel;
use carbon\carbon;
use App\Models\{
Testresult,
Classes,
School,
Section,
Sms_cdr,
User,
Users_admission
};
class Frontend
{
public static function getPromoteStudent($user_unique_id,$class_id)
{
$user = User::where('user_unique_id', $user_unique_id)->first();
$user_id = $user->id;
$duplicates = DB::table('users')
->join('users_admissions', 'users.user_unique_id', '=', 'users_admissions.user_unique_id')
->join('users_classes_sections', 'users_classes_sections.user_id', '=', 'users.id')
->join('classes', 'classes.id', '=', 'users_classes_sections.classes_id')
->join('sections', 'sections.id', '=', 'users_classes_sections.section_id')
->where('users_classes_sections.user_id', $user_id)
->where('users_classes_sections.classes_id', $class_id)
->orderBy('users_classes_sections.id', 'desc')
->limit(1)
->first([ 'classes.class','sections.section']);
// dd($duplicates);
return $duplicates;
}
public static function getUserUniquidId($user_id)
{
$user = User::where('id', $user_id)->first();
$user_id = $user->user_unique_id;
return $user_id;
}
public static function getFinancialYearFromDate($dt)
{
$date = Carbon::parse($dt); // Convert to Carbon instance
$year = $date->year;
$startYear = ($date->month >= 4) ? $year : $year - 1;
$endYear = $startYear + 1;
return "$startYear-$endYear";
}
public static function getInvoicePrefix($financialYear)
{
$baseYear = 2024; // Year when "G" starts
$basePrefix = 'G'; // Initial prefix
// Extract the starting year of financial year (e.g., "2024-2025" → 2024)
$startYear = (int) explode('-', $financialYear)[0];
// Calculate character shift (A=65 in ASCII, G=71)
$prefixOffset = $startYear - $baseYear;
$prefix = chr(ord($basePrefix) + $prefixOffset);
return $prefix;
}
public static function get_invoice_number($fees)
{
$invoice_number = $fees->full_invoice_no;
$dt = $fees->created_at;
$date = Carbon::parse($dt); // Convert to Carbon instance
$year = $date->year;
$startYear = ($date->month >= 4) ? $year : $year - 1;
$endYear = $startYear + 1;
$finYear = "$startYear-$endYear";
$finYear = self::getFinancialYearFromDate($date);
$char = self::getInvoicePrefix($finYear);
if (empty($invoice_number)) {
$no = $fees->id;
// For existing records in current financial year:
if (!empty($fees->invoice_no) && $fees->invoice_no > 0) {
$no = $fees->invoice_no;
}
$invoice_number = $finYear . '/' . $char . '-' . $no;
}
return $invoice_number;
}
public static function numberToWord($num = '')
{
$num = (string) ((int) $num);
if ((int) ($num) && ctype_digit($num)) {
$words = array();
$num = str_replace(array(',', ' '), '', trim($num));
$list1 = array(
'',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen'
);
$list2 = array(
'',
'ten',
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety',
'hundred'
);
$list3 = array(
'',
'thousand',
'million',
'billion',
'trillion',
'quadrillion',
'quintillion',
'sextillion',
'septillion',
'octillion',
'nonillion',
'decillion',
'undecillion',
'duodecillion',
'tredecillion',
'quattuordecillion',
'quindecillion',
'sexdecillion',
'septendecillion',
'octodecillion',
'novemdecillion',
'vigintillion'
);
$num_length = strlen($num);
$levels = (int) (($num_length + 2) / 3);
$max_length = $levels * 3;
$num = substr('00' . $num, -$max_length);
$num_levels = str_split($num, 3);
foreach ($num_levels as $num_part) {
$levels--;
$hundreds = (int) ($num_part / 100);
$hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ($hundreds == 1 ? '' : 's') . ' ' : '');
$tens = (int) ($num_part % 100);
$singles = '';
if ($tens < 20) {
$tens = ($tens ? ' ' . $list1[$tens] . ' ' : '');
} else {
$tens = (int) ($tens / 10);
$tens = ' ' . $list2[$tens] . ' ';
$singles = (int) ($num_part % 10);
$singles = ' ' . $list1[$singles] . ' ';
}
$words[] = $hundreds . $tens . $singles . (($levels && (int) ($num_part)) ? ' ' . $list3[$levels] . ' ' : '');
}
$commas = count($words);
if ($commas > 1) {
$commas = $commas - 1;
}
$words = implode(', ', $words);
$words = trim(str_replace(' ,', ',', ucwords($words)), ', ');
if ($commas) {
$words = str_replace(',', ' and', $words);
}
return $words;
} else if (!((int) $num)) {
return 'Zero';
}
return '';
}
public static function getTotalTest($admission_id, $testcode)
{
$res = Testresult::where(['users_admission_id' => $admission_id, 'testcode' => $testcode])->where('obtained_marks', '!=', 'N')->first();
return $res->obtained_marks ?? '---';
}
public static function getUserMarks($admission_id, $testcode)
{
$res = Testresult::where(['users_admission_id' => $admission_id, 'testcode' => $testcode])->first();
return $res->obtained_marks ?? '---';
}
public static function getUserTotalMarks($admission_id, $testcode)
{
$res = Testresult::where(['users_admission_id' => $admission_id, 'testcode' => $testcode])->where('obtained_marks', '!=', 'N')->where('obtained_marks', '!=', 'A')->first();
return $res->max_marks ?? '---';
}
public static function getUserAbsent($admission_id, $testcode)
{
$res = Testresult::where(['users_admission_id' => $admission_id, 'testcode' => $testcode, 'obtained_marks' => 'A'])->count();
return $res ?? '---';
}
public static function getUserPresent($admission_id, $testcode)
{
$res = Testresult::where(['users_admission_id' => $admission_id, 'testcode' => $testcode])
->where('obtained_marks', '!=', 0.00)
->first();
return $res ?? '---';
}
public static function getClass($class_id)
{
$res = Classes::where(['id' => $class_id])->first('class');
return $res;
}
public static function getSection($section_id)
{
$res = Section::where(['id' => $section_id])->first('section');
return $res;
}
public static function getSections($section_id)
{
$res = Section::whereIn('id', $section_id)->get(['section']);
return $res;
}
// Schools
public static function get_schools()
{
$schools = School::get();
$_schools = array();
foreach ($schools as $school) {
if (Auth::user()->can(strtolower($school->code))) {
array_push($_schools, strtolower($school->id));
}
}
return $_schools;
}
// MARKS SMS API ========================
public static function send_marks_sms($user_id, $date, $ob_marks, $max_marks, $highest_marks, $testcode)
{
$carbon = new Carbon($date);
$date = $carbon->format('Y-m-d');
$user_admission = Users_admission::where('id', $user_id)->first();
if (!empty($user_admission)) {
// Send SMS When User Absent
$user = User::where('user_unique_id', $user_admission->user_unique_id)->first();
$parent = $user->guardian()->first();
if (!empty($parent)) {
$phone = $user->guardian()->first()->phone;
$school = $user->school()->first();
$branch = $school->school;
$name = $user_admission->first_name . ' ' . $user_admission->last_name;
// Start
$message_content = urlencode("Respected parents Your ward $name got $ob_marks marks out of $max_marks and highest marks are $highest_marks in test of $testcode held on $date Paras Institute of Commerce Pvt. Ltd. $branch");
//$message_content = urlencode("$name got $ob_marks marks out of $max_marks and highest marks is $highest_marks dated on $date Paras Institute $branch");
$smsLoad = array(
"username" => "paraserp",
"password" => "paraserp",
"from" => "PICHSR",
"pe_id" => "1701158079503322375",
"template_id" => "1707172102939233616",
"to" => ["$phone"],
"text" => $message_content,
"coding" => "0"
);
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => 'https://web.smsgw.in/smsapi/jsonapi.jsp',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($smsLoad),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
)
);
$response = curl_exec($curl);
curl_close($curl);
$header = json_decode($response);
// dd($header);
// failed =================================
if (isset($header->Error->ErrorCode)) {
$log = array(
'to' => $phone,
'text' => urldecode($message_content),
'status' => 0,
'sender_id' => 'PICHSR',
'template_id' => '1707168905104451631',
'created_at' => date('Y-m-d H:i:s')
);
Sms_cdr::create($log);
return false;
}
// success ====================
$log = array(
'to' => $phone,
'text' => urldecode($message_content),
'status' => 1,
'message_id' => $header->data->msgid,
'sender_id' => 'PICHSR',
'template_id' => '1707168905104451631',
'created_at' => date('Y-m-d H:i:s')
);
Sms_cdr::create($log);
return true;
// End
}
}
}
// ===================================
public static function send_absent_sms($mobile, $branch, $date)
{
$message_content = urlencode("Your Son/Daughter is absent on $date Paras Institute $branch");
$smsLoad = array(
"username" => "paraserp",
"password" => "paraserp",
"from" => "PICHSR",
"pe_id" => "1701158079503322375",
"template_id" => "1707168905081731755",
"to" => ["$mobile"],
"text" => $message_content,
"coding" => "0"
);
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => 'https://web.smsgw.in/smsapi/jsonapi.jsp',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($smsLoad),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
)
);
$response = curl_exec($curl);
// dd($response);
curl_close($curl);
$header = json_decode($response);
// failed =================================
if (isset($header->Error->ErrorCode)) {
$resPonse = array(
"status" => false,
"code" => 204,
"message" => "Something wen wrong, please try again!",
);
echo json_encode($resPonse);
$log = array(
'to' => $mobile,
'text' => urldecode($message_content),
'status' => 0,
'sender_id' => 'PICHSR',
'template_id' => '1707168905081731755',
'created_at' => date('Y-m-d H:i:s')
);
Sms_cdr::create($log);
return false;
}
else
{
// success ====================
$log = array(
'to' => $mobile,
'text' => urldecode($message_content),
'status' => 1,
'message_id' => $header->data->msgid,
'sender_id' => 'PICHSR',
'template_id' => '1707168905081731755',
'created_at' => date('Y-m-d H:i:s')
);
Sms_cdr::create($log);
return true;
}
}
}