JezK
Edit File: functions.php
<?php // Source - https://stackoverflow.com/a // Posted by Fancy John, modified by community. See post 'Timeline' for change history // Retrieved 2026-01-27, License - CC BY-SA 4.0 // ini_set('display_errors', '1'); // ini_set('display_startup_errors', '1'); // error_reporting(E_ALL); class Functions { function __construct() { require_once dirname(__FILE__) . '/dbconnect.php'; // opening db connection $db = new DbConnect(); $this->con = $db->connect(); } function getBaseURL() { $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = ($protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); $url1 = explode('BC-BF-Admin', $url); return $url1[0]; } function login($email, $password) { $query = "select * from bcAdmin where email = '" . $email . "' and password = '" . $password . "'"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { return mysqli_fetch_assoc($cmd); //success } else { return 0; //login failed } } function verifyOtp($userId, $otp) { // Prepare the SQL statement with placeholders $stmt = $this->con->prepare("SELECT * FROM bcAdmin WHERE id = ?"); if (!$stmt) { // Handle prepare error if needed return 0; } // Bind parameters (both are strings) $stmt->bind_param("i", $userId); // Execute the prepared statement $stmt->execute(); // Get the result set from the statement $result = $stmt->get_result(); // Fetch the associated row $data = $result->fetch_assoc(); if ($data['otp'] == $otp) { return 1; } else { return 0; } } function forgotPassword($phoneNo) { $query = "select * from bcAdmin where phone_no = '" . $phoneNo . "'"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { return mysqli_fetch_assoc($cmd); //success } else { return 0; //login failed } } function changePassword($password) { $query = "update bcAdmin set password = '" . $password . "' where id = 1"; $cmd = mysqli_query($this->con, $query); if ($cmd) { return 1; //success } else { return -3; //error } } function saveMonthlyReport($userID, $reportYear, $reportMonth, $totalSHG, $proposedSHG, $totalCollection, $totalRemit, $cashHolding, $boardMembersChange, $file, $shgMeetings, $shgPromoted, $shgAdopted, $leftedStaffName, $joinedStaffName) { $query = "insert into bcUserMonthlyReport(userID,reportYear,reportMonth,totalSHG,proposedSHG,totalCollection,totalRemit,cashHolding,boardMembersChange,file,shgMeetings,shgPromoted,shgAdopted,leftedStaffName,joinedStaffName) values('" . $userID . "','" . $reportYear . "','" . $reportMonth . "','" . $totalSHG . "','" . $proposedSHG . "','" . $totalCollection . "','" . $totalRemit . "','" . $cashHolding . "','" . $boardMembersChange . "','" . $file . "','" . $shgMeetings . "','" . $shgPromoted . "','" . $shgAdopted . "','" . $leftedStaffName . "','" . $joinedStaffName . "')"; $cmd = mysqli_query($this->con, $query); if ($cmd) { return mysqli_insert_id($this->con); //success } else { return -3; //error } } function saveBCUserStaffDetails($reportID, $bcUserID, $totalBCStaff, $totalNabfinsStaff, $totalNabfinsStaffIDCards, $totalNewStaffJoined, $totalNabfinsInvolvedStaff, $totalBCMobileCBS) { for ($i = 0; $i < count($totalBCStaff); $i++) { if ($totalBCStaff[$i] != "" || $totalNabfinsStaff[$i] != "" || $totalNabfinsStaffIDCards[$i] != "" || $totalNewStaffJoined[$i] != "" || $totalNabfinsInvolvedStaff[$i] != "" || $totalBCMobileCBS[$i] != "") { $query = "insert into bcUserStaffDetails(reportID,bcUserID,totalBCStaff,totalNabfinsStaff,totalNabfinsStaffIDCards,totalNewStaffJoined,totalNabfinsInvolvedStaff,totalBCMobileCBS) values('" . $reportID . "','" . $bcUserID . "','" . $totalBCStaff[$i] . "','" . $totalNabfinsStaff[$i] . "','" . $totalNabfinsStaffIDCards[$i] . "','" . $totalNewStaffJoined[$i] . "','" . $totalNabfinsInvolvedStaff[$i] . "','" . $totalBCMobileCBS[$i] . "')"; $cmd = mysqli_query($this->con, $query); if (!$cmd) { return -3; } } } return 1; } function getMonthlyReports($userID) { $query = "select * from bcUserMonthlyReport where userID = " . $userID . " order by id desc"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return 0; //no data found } } function isValidMonthReport($userID, $reportMonth, $reportYear) { $query = "select * from bcUserMonthlyReport where userID = " . $userID . " and reportMonth = '" . $reportMonth . "' and reportYear = '" . $reportYear . "' "; $cmd = mysqli_query($this->con, $query); return mysqli_num_rows($cmd); } function isValidUserReport($reportID, $userID) { $query = "select * from bcUserMonthlyReport where id = " . $reportID . " and userID = " . $userID; $cmd = mysqli_query($this->con, $query); return mysqli_num_rows($cmd); } function getBCStaffDetails($reportID) { $query = "select * from bcUserStaffDetails where reportID = " . $reportID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getStaffLeftDetails($reportID) { $query = "select * from staffLeft where reportID = " . $reportID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getStaffJoinedDetails($reportID) { $query = "select * from staffJoined where reportID = " . $reportID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getReports($reportID) { $query = "select * from bcUserMonthlyReport where id = " . $reportID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $staffLeftDetails = $this->getStaffLeftDetails($reportID); $staffJoinedDetails = $this->getStaffJoinedDetails($reportID); $staffDetails = $this->getBCStaffDetails($reportID); $data = array("reportDetails" => $row, "staffDetails" => $staffDetails, 'staffLeftDetails' => $staffLeftDetails, 'staffJoinedDetails' => $staffJoinedDetails); } return $data; } else { return array(); //no data found } } function getPendingApplications() { $query = "select * from bcUsers where submissionStatus = 1 and registrationStatus = 0 order by id desc"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getRejectedApplications() { $query = "select * from bcUsers where submissionStatus = 1 and registrationStatus = 2 order by id desc"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getSubmittedRenewalApplications() { $query = "select * from bcUsers where renewalStatus = -1 order by id desc"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); //no data found } } function getStates() { $query = "select * from states"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $states[] = $row; } return $states; } else { return ""; } } function getBCUserDetails($userID) { //retrieving bcUserDetails $query = "select * from bcUsers where id = " . $userID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { $userDetails = mysqli_fetch_assoc($cmd); //retrieving bcRegistrationDetails $registrationDetails = $this->getOtherTablesDetails($userID, 'bcRegistrationDetails'); //retrieving operations $operations = $this->getOtherTablesDetails($userID, 'operations'); //retrieving projects $projects = $this->getOtherTablesDetails($userID, 'projects'); //retrieving bcManagementDetails $bcManagementDetails = $this->getOtherTablesDetails($userID, 'bcManagementDetails'); //retrieving bcCeoDetails $bcCeoDetails = $this->getOtherTablesDetails($userID, 'bcCeoDetails'); //retrieving staffingDetails $staffingDetails = $this->getOtherTablesDetails($userID, 'staffingDetails'); //retrieving incomeDetails $incomeDetails = $this->getOtherTablesDetails($userID, 'incomeDetails'); //retrieving ceoProfile $ceoProfile = $this->getOtherTablesDetails($userID, 'ceoProfile'); //retrieving bcOtherDetails $bcOtherDetails = $this->getOtherTablesDetails($userID, 'bcOtherDetails'); //retrieving bcMFI //$bcMFIDetails = $this->getOtherTablesDetails($userID,'bcMFI'); //retrieving bcMFIStates $bcMFIStates = $this->getOtherTablesDetails($userID, 'bcMFIStates'); //retrieving partnerAddress //$partnerAddress = $this->getOtherTablesDetails($userID,'partnerAddress'); //retrieving bcPartners $bcPartners = $this->getOtherTablesDetails($userID, 'bcPartners'); //retrieving proposedDetails $proposedDetails = $this->getOtherTablesDetails($userID, 'proposedDetails'); //retrieving authorisedSignatures $authorisedSignatures = $this->getOtherTablesDetails($userID, 'authorisedSignatures'); //retrieving bcBankAccounts $bcBankAccounts = $this->getOtherTablesDetails($userID, 'bcBankAccounts'); return array("userDetails" => $userDetails, "registrationDetails" => $registrationDetails, "operations" => $operations, "projects" => $projects, "bcManagementDetails" => $bcManagementDetails, "bcCeoDetails" => $bcCeoDetails, "staffingDetails" => $staffingDetails, "incomeDetails" => $incomeDetails, "ceoProfile" => $ceoProfile, "bcMFIDetails" => array(), "bcMFIStates" => $bcMFIStates, "partnerAddress" => array(), "proposedDetails" => $proposedDetails, "authorisedSignatures" => $authorisedSignatures, "bcBankAccounts" => $bcBankAccounts, "bcOtherDetails" => $bcOtherDetails, "bcPartners" => $bcPartners); } else { return array(); } } function getOtherTablesDetails($userID, $tableName) { $query = "select * from " . $tableName . " where userID = " . $userID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $details[] = $row; } return $details; } else { return array(); } } function getStateName($stateID) { $query = "select * from states where id = " . $stateID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { $row = mysqli_fetch_assoc($cmd); return $row['name']; } else { return ""; } } function getDistrictName($districtID) { $query = "select * from districts where id = " . $districtID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { $row = mysqli_fetch_assoc($cmd); return $row['name']; } else { return ""; } } function getDistricts($stateID) { $query = "select * from districts where stateID = " . $stateID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $districts[] = $row; } return $districts; } else { return ""; } } function getPartnerOperations($partnerID, $userID) { $query = "select * from partnerOperations where userID = " . $userID . " and partnerID = " . $partnerID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $details[] = $row; } return $details; } else { return array(); } } function isUserAlreadyRegistered($userID) { $query = "select * from bcRegistrations where userID = " . $userID; $cmd = mysqli_query($this->con, $query); return mysqli_num_rows($cmd); } function registerUser($userID, $applicationType, $email, $formSubmissionID, $expiryDate, $bcCode, $password) { //checking if bcCode available or not $query = "select * from bcRegistrations where bcCode = '" . $bcCode . "'"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) == 0) //bcCode is available { //registering the user $query = "insert into bcRegistrations(userID,applicationType,formSubmissionID,expiryDate,bcCode,email,password) values(" . $userID . "," . $applicationType . ",'" . $formSubmissionID . "','" . $expiryDate . "','" . $bcCode . "','" . $email . "','" . $password . "')"; $cmd = mysqli_query($this->con, $query); if ($cmd) { //updating registrationStatus in bcUsers $query = "update bcUsers set registrationStatus = 1 where id = " . $userID; $cmd = mysqli_query($this->con, $query); return 1; //success } else { return -3; //error in insert } } else { return -2; //bcCode not available } } function rejectUser($userID, $applicationType, $email) { $query = "update bcUsers set registrationStatus = 2 where id = " . $userID; $cmd = mysqli_query($this->con, $query); if ($cmd) { return 1; //success } else { return -3; //error in insert } } function getRegisteredUsers() { $query = "select * from bcRegistrations order by id desc"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); } } function getRenewalUsers() { $query = "SELECT * FROM bcRegistrations WHERE DATE(expiryDate) <= DATE_SUB(NOW(), INTERVAL -3 MONTH) and Not Date(expiryDate) < date(NOW())"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); } } function getExpiredUsers() { $query = "SELECT * FROM bcRegistrations WHERE DATE(expiryDate) < date(NOW())"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $data[] = $row; } return $data; } else { return array(); } } function saveCommission($file, $commissionType, $year, $month, $data) { $query = "insert into commissionFiles(year,month,commissionType,file) values(" . $year . "," . $month . "," . $commissionType . ",'" . $file . "')"; $cmd = mysqli_query($this->con, $query); if ($cmd) { $commissionFileID = mysqli_insert_id($this->con); for ($i = 1; $i < count($data); $i++) { $bcCode = $data[$i][0]; $bcName = $data[$i][1]; $principalAmount = $data[$i][2]; $commissionPayable = $data[$i][3]; $tds = $data[$i][4]; $depositDeducted = $data[$i][5]; $allowanceDeducted = $data[$i][6]; $amountReleased = $data[$i][7]; $paidDate = $data[$i][8]; $amountHold = $data[$i][9]; $remarks = $data[$i][10]; $query = "insert into commissions(commissionFileID,commissionType,year,month,bcCode,bcName,principalAmount,commissionPayable,tds,depositDeducted,allowanceDeducted,amountReleased,paidDate,amountHold,remarks) values(" . $commissionFileID . "," . $commissionType . "," . $year . "," . $month . ",'" . $bcCode . "','" . $bcName . "','" . $principalAmount . "','" . $commissionPayable . "','" . $tds . "','" . $depositDeducted . "','" . $allowanceDeducted . "','" . $amountReleased . "','" . $paidDate . "','" . $amountHold . "','" . $remarks . "')"; $cmd = mysqli_query($this->con, $query); if (!$cmd) { return -3; //error } } return 1; } else { return -4; //error } } function getDashboardData() { $query = "select * from bcUsers where submissionStatus = 1 and registrationStatus = 0 order by id desc"; $cmd = mysqli_query($this->con, $query); $pendingApplications = mysqli_num_rows($cmd); $query = "SELECT * FROM bcRegistrations WHERE DATE(expiryDate) <= DATE_SUB(NOW(), INTERVAL -3 MONTH) and Not Date(expiryDate) < date(NOW())"; $cmd = mysqli_query($this->con, $query); $renewalApplications = mysqli_num_rows($cmd); $query = "select * from bcRegistrations where applicationType = 1"; $cmd = mysqli_query($this->con, $query); $totalFacilitators = mysqli_num_rows($cmd); $query = "select * from bcRegistrations where applicationType = 2"; $cmd = mysqli_query($this->con, $query); $totalCoordinators = mysqli_num_rows($cmd); return array("pendingApplications" => $pendingApplications, "renewalApplications" => $renewalApplications, "totalFacilitators" => $totalFacilitators, "totalCoordinators" => $totalCoordinators); } function getCommisionFiles() { $query = "select * from commissionFiles order by id desc"; $cmd = mysqli_query($this->con, $query); $commissionFiles = array(); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $commissionFiles[] = $row; } } return $commissionFiles; } function getCommisionByCommissionID($commissionID) { $query = "select * from commissions where id = " . $commissionID . " order by id desc"; $cmd = mysqli_query($this->con, $query); $commissions = array(); if (mysqli_num_rows($cmd) > 0) { return mysqli_fetch_assoc($cmd); } else { return $commissions; } } function getBcCOde($bcUserID) { $query = "select * from bcRegistrations where userID = " . $bcUserID; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { return mysqli_fetch_assoc($cmd); } else { return array(); } } function getCommisions($bcCode) { $query = "select * from commissions where bcCode = '" . $bcCode . "' order by id desc"; $cmd = mysqli_query($this->con, $query); $commissions = array(); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $commissions[] = $row; } } return $commissions; } function isUserExpired($userID) { $query = "SELECT * FROM bcRegistrations WHERE DATE(expiryDate) < date(NOW()) and userID = " . $userID; $cmd = mysqli_query($this->con, $query); return mysqli_num_rows($cmd); } function approveUserApplication($userID) { /*$registrationDetails = $this->getOtherTablesDetails($userID,'bcRegistrationDetails'); $validity = $registrationDetails[0]['validity']; if($validity == 2) //temporary { $query = "select * from bcUsers where id = ".$userID; $cmd = mysqli_query($this->con,$query); $userDetails = mysqli_fetch_assoc($cmd); $applicationType = $userDetails["applicationType"]; $expiry = ($applicationType == 1)?'+3 years':'+5 years'; /********** Note ******************/ //if (user already expired) //add expiryDate from currentDate; //else //add expiryDate from actual expiryDate /********** Note ******************/ /*$isUserExpired = $this->isUserExpired($userID); if($isUserExpired) { $expiryDate = date('Y-m-d H:i:s', strtotime($expiry, strtotime(Date('Y-m-d H:i:s')))); } else { $expiryDate = date('Y-m-d H:i:s', strtotime($expiry, strtotime($userDetails["expiryDate"]))); } $query = "update bcUsers set expiryDate = '".$expiryDate."' where id = ".$userID; $cmd = mysqli_query($this->con,$query); $query = "update bcRegistrations set expiryDate = '".$expiryDate."' where userID = ".$userID; $cmd = mysqli_query($this->con,$query); } else //permanant { $query = "update bcUsers set expiryDate = '' where id = ".$userID; $cmd = mysqli_query($this->con,$query); $query = "update bcRegistrations set expiryDate = '' where userID = ".$userID; $cmd = mysqli_query($this->con,$query); } if($cmd) { $query = "update bcUsers set renewalStatus = 1 where id = ".$userID; $cmd = mysqli_query($this->con,$query); if($cmd) { return 1; //success } else { return -3; //error } } else { return -4; //error in updating expiry date; }*/ /********** Note ******************/ //if (user already expired) //add expiryDate from currentDate; //else //add expiryDate from actual expiryDate /********** Note ******************/ $query = "select * from bcUsers where id = " . $userID; $cmd = mysqli_query($this->con, $query); $userDetails = mysqli_fetch_assoc($cmd); $applicationType = $userDetails["applicationType"]; $expiry = ($applicationType == 1) ? '+2 years' : '+3 years'; $isUserExpired = $this->isUserExpired($userID); if ($isUserExpired) { $expiryDate = date('Y-m-d H:i:s', strtotime($expiry, strtotime(Date('Y-m-d H:i:s')))); } else { $expiryDate = date('Y-m-d H:i:s', strtotime($expiry, strtotime($userDetails["expiryDate"]))); } $expiryDate = date('Y-m-d H:i:s', strtotime($expiry, strtotime(Date('Y-m-d H:i:s')))); $query = "update bcUsers set expiryDate = '" . $expiryDate . "',renewalStatus = 1 where id = " . $userID; $cmd = mysqli_query($this->con, $query); $query = "update bcRegistrations set expiryDate = '" . $expiryDate . "' where userID = " . $userID; $cmd = mysqli_query($this->con, $query); if ($cmd) { return 1; //success } else { return -3; //error } } function getUserIDFromBcCOde($bcCode) { $query = "select * from bcRegistrations where bcCode = '" . $bcCode . "'"; $cmd = mysqli_query($this->con, $query); if (mysqli_num_rows($cmd) > 0) { return mysqli_fetch_assoc($cmd); } else { return array(); } } function getAuthorisedSignatures($userID) { $query = "select * from bcManagementDetails where isAuthorized = 1 and userID = " . $userID; $cmd = mysqli_query($this->con, $query); $details = array(); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $details[] = $row; } } return $details; } function getSavedAuthorisedSignatures($userID) { $query = "select * from authorisedSignatures where status = 1 and userID = " . $userID; $cmd = mysqli_query($this->con, $query); $details = array(); if (mysqli_num_rows($cmd) > 0) { while ($row = mysqli_fetch_assoc($cmd)) { $details[] = $row; } } return $details; } function updateSignatureStatus($userID, $managementID, $status) { $query = "update authorisedSignatures set status = " . $status . " where userID = " . $userID . " and managementID = " . $managementID; $cmd = mysqli_query($this->con, $query); } function deleteOtherData($userID) { //$query = "delete othername,otherDescription,otherRemarks from bcUsers where id = ".$userID; $query = "update bcUsers set othername ='',otherDescription ='',otherRemarks='' where id =" . $userID; $cmd = mysqli_query($this->con, $query); } function sendSMS($mobile, $message, $template_id = "1707161726833412527") { $api_key = "C7U9UJtI"; $sender_id = "NABFIN"; $entity_id = "1701159306599716894"; // Build query string safely $query = http_build_query([ 'key' => $api_key, 'to' => $mobile, 'from' => $sender_id, 'body' => $message, 'entityid' => $entity_id, 'templateid' => $template_id ]); $url = "https://api.onex-aura.com/api/sms?" . $query; $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 30 ]); $response = curl_exec($ch); // Handle cURL error if ($response === false) { $error = curl_error($ch); curl_close($ch); return [ 'status' => 'error', 'message' => $error ]; } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return [ 'http_code' => $httpCode, 'response' => $response ]; } function randomOtp() { return rand(100000, 999999); } function updateOTP($id, $otp) { $stmt = $this->con->prepare( "UPDATE bcAdmin SET otp = ? WHERE id = ?" ); if (!$stmt) { return -3; // prepare failed } $stmt->bind_param("si", $otp, $id); $exec = $stmt->execute(); return $exec ? 1 : -3; } }