public function actionExamList() { if(Yii::$app->session->has('Event_Type') && Yii::$app->session->get('Event_Type') == '1') { $this->layout = '@app/views/layouts/main-event'; }else{ $this->layout = '@app/views/layouts/main-exam'; } $model = new Dashboard(); $CentreBillTbl = Yii::$app->params['CentreBillTbl']; $TATBL = Yii::$app->params['TABillHeaderTbl']; $OBSProfile = Yii::$app->params['ExamProfile']; $ExamDates = Yii::$app->params['ExamDates']; $ID = Yii::$app->user->identity->NTA_Unique_Admin_ID; $startDate = isset($_POST['startDate']) && !empty($_POST['startDate']) ? date('Y-m-d',strtotime($_POST['startDate'])) : null; $endDate = isset($_POST['endDate']) && !empty($_POST['endDate'])? date('Y-m-d',strtotime($_POST['endDate'])) : null; if(isset($_POST['reset'])) { //echo 'hiiiiiiiiiiiiii';die; $data['startDate']=''; $data['endDate']=''; $startDate = ''; $endDate = ''; }else{ $data['startDate']=$startDate; $data['endDate']=$endDate; } $dateConditions = []; if(!empty($startDate) && !empty($endDate)){ for ($i = 1; $i <= 12; $i++) { $dateConditions[] = "(c.Date_$i BETWEEN '$startDate' AND '$endDate')"; } $model->startDate = $startDate; $model->endDate = $endDate; }elseif(!empty($startDate)){ for ($i = 1; $i <= 12; $i++) { $dateConditions[] = "(c.Date_$i >= '$startDate')"; } }elseif(!empty($endDate)){ for ($i = 1; $i <= 12; $i++) { $dateConditions[] = "(c.Date_$i <= '$endDate')"; } } // Check if $dateConditions is empty if(empty($dateConditions)) { // If no date conditions were set, set a default condition to avoid SQL syntax errors $dateConditions[] = "(1=1)"; // You can use any condition that is always true } $dateConditionString = implode(' OR ', $dateConditions); // Define the limit clause $limitClause = (empty($startDate) && empty($endDate)) ? "TOP 10" : ""; $examLists = Yii::$app->db->createCommand(" SELECT b.[Observer Type] as ObType, c.[Exam Name] as ExamName, c.[Exam Type] as examType, c.Year, c.Exam_Duration, B.CENTNO, b.CITY_CD, b.TC_CODE FROM FRONT_ERP_USER_AdminUser a INNER JOIN $OBSProfile b ON b.ObserverID = a.NTA_Unique_Admin_ID INNER JOIN $ExamDates c ON c.[Exam Type] = b.ExamType WHERE a.NTA_Unique_Admin_ID = :ID AND ($dateConditionString) ") ->bindValue(':ID', $ID) ->queryAll(); // H::prd($examLists); $uniqueExamType = []; $uniqueExamTypes = []; foreach ($examLists as $exam) { $ObType = $exam['ObType']; $examType = $exam['examType']; $ExamName = $exam['ExamName']; $Year = $exam['Year']; $Exam_Duration = $exam['Exam_Duration']; $CENTNO = $exam['CENTNO']; $CITY_CD = $exam['CITY_CD']; $TC_CODE = $exam['TC_CODE']; if (!in_array($examType, $uniqueExamType)) { $uniqueExamType['ObType'] = $ObType; $uniqueExamType['examType'] = $examType; $uniqueExamType['ExamName'] = $ExamName; $uniqueExamType['Year'] = $Year; $uniqueExamType['Exam_Duration'] = $Exam_Duration; $uniqueExamType['CENTNO'] = $CENTNO; $uniqueExamType['CITY_CD'] = $CITY_CD; $uniqueExamType['TC_CODE'] = $TC_CODE; $uniqueExamTypes[] = $uniqueExamType; } } //H::prd($uniqueExamTypes); // echo '
'; print_r($examLists);die;

        // +++++++++++++++++++++++++++ CC Bills Section ++++++++++++++++++++++++++++++++++//
        $whr = '';
        if(isset($_GET['bill_status']) && !empty($_GET['bill_status'])){
            $whr .= " AND b.[Bill Status] = '{$_GET['bill_status']}'";
        }
        if(isset($_GET['exam_type']) && !empty($_GET['exam_type'])){
            $whr .= " AND e.[Exam Type Id] = '{$_GET['exam_type']}'";
        }
        if(isset($_GET['role']) && !empty($_GET['role'])){
            $whr .= " AND p.[Observer Type] = '{$_GET['role']}'";
        }

        // Properly concatenate with "AND"
        $whr .= " AND p.[ObserverID] = '$ID'";

        $sql = "SELECT p.CITY_CD, p.TC_CODE, p.ObserverID, b.[Reference No_] as ReferenceNo, p.CENTNO, p.CITY, p.NAME, b.[Superintendent Name] SuperintendentName, 
        b.[Created on] billDate, p.ExamType, e.[Exam Name] as examName, b.[Bill Status] as billStatus, e.[Exam Type Id] as examTypeId, b.[Total Expenditure Amount] as TotalAmount
        FROM $OBSProfile p
        JOIN $CentreBillTbl b ON b.[Observer ID] = p.ObserverID AND b.CenterNo = p.CENTNO
        JOIN $ExamDates e ON e.[Exam Type Id] = b.[Exam ID]
        WHERE 1=1 $whr
        ORDER BY p.ObserverID";


        $sql1 = "SELECT p.CITY_CD, p.TC_CODE, p.ObserverID, b.[Reference No_] as ReferenceNo, p.CENTNO, p.CITY, p.NAME, b.[Superintendent Name] SuperintendentName, 
        b.[Created on] billDate, p.ExamType, e.[Exam Name] as examName, b.[Bill Status] as billStatus, e.[Exam Type Id] as examTypeId, b.[Total Expenditure Amount] as TotalAmount
        FROM $OBSProfile p
        JOIN $CentreBillTbl b ON b.[Observer ID] = p.ObserverID AND b.CenterNo = p.CENTNO
        JOIN $ExamDates e ON e.[Exam Type Id] = b.[Exam ID]
        WHERE 1=1 $whr";

        // echo $sql; die;
        // Initialize variables
        $CCBills = [];
        $limit = 5;
        $page = Yii::$app->getRequest()->getQueryParam('page') ?: 1;
        //echo $page;die;
        $offset = ($page - 1) * $limit;

        // Fetch data for the current page
        $CCBills = Yii::$app->db->createCommand("$sql OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY")->queryAll();
        // echo '
'; print_r($CCBills);die;
        // Get total count for pagination
        $countQuery = "SELECT COUNT(*) FROM ($sql1) AS count_query";
        $count = Yii::$app->db->createCommand($countQuery)->queryScalar();

        // Create pagination object
        $pagination = new Pagination(['totalCount' => $count, 'defaultPageSize' => $limit, 'page' => $page]);

            // ++++++++++++++++++ Pagination +++++++++++++++++++//

        // +++++++++++++++++++++++++++ Exam Types List ++++++++++++++++++++++++++++++++++//
        $examTypeLists = Yii::$app->db->createCommand("SELECT [Exam Type Id] ExamId, [Exam Type] ExamType FROM $ExamDates ")
                                ->queryAll();
        //    echo '
'; print_r($examTypeLists);die;
        
        //        $NTA_Unique_Admin_ID = Yii::$app->user->identity->NTA_Unique_Admin_ID;
        //        $CandidateID = Yii::$app->user->identity->ID;
        //        $Descriptions = 'Login to DashBoard';
        //        $UserRole = '';
        //        $UserName = '';//Yii::$app->user->identity->UserName;
        //        $ModuleName = 'Login';
        //        $data = array("CandidateID" => $CandidateID, "NTA_Unique_Admin_ID" => $NTA_Unique_Admin_ID,"UserRole"=>$UserRole,"UserName"=>$UserName,"ModuleName"=>$ModuleName,"Descriptions"=>$Descriptions);
        //        Yii::$app->ActivityLog->addlogdata($data);
        

        $authUserData = Yii::$app->user->identity;


        //To display TABill listing and filter
        $getObserverType = Yii::$app->masterdata->getObserverType();
        
        $whrTaBill = '';
        if(isset($_GET['ta_bill_status']) && !empty($_GET['ta_bill_status'])){
            $whrTaBill .= " AND b.[Bill Status] = '{$_GET['ta_bill_status']}'";
        }
        if(isset($_GET['ta_exam_type']) && !empty($_GET['ta_exam_type'])){
            $whrTaBill .= " AND e.[Exam Type Id] = '{$_GET['ta_exam_type']}'";
        }
        if(isset($_GET['ta_role']) && !empty($_GET['ta_role'])){
            $whrTaBill .= " AND p.[Observer Type] = '{$_GET['ta_role']}'";
        }

        // Properly concatenate with "AND"
        $whrTaBill .= " AND p.[ObserverID] = '$ID'";

        $sqlTaBill = "SELECT p.ObserverID, b.[Reference No_] as ReferenceNo, p.CENTNO, p.CITY, p.NAME,p.BillType, b.[Exam_Mode],b.[Superintendent Name] SuperintendentName, 
        p.CITY_CD,p.TC_CODE,p.City_code,b.[CreatedDate] billDate, p.ExamType, e.[Exam Name] as examName,e.[Exam Type], b.[Bill Status] as billStatus, e.[Exam Type Id] as examTypeId, b.[Net Payable Amount] as TotalAmount
        FROM $OBSProfile p
        JOIN $TATBL b ON b.[Observer ID] = p.ObserverID AND p.CENTNO = b.[Centre No_]
        JOIN $ExamDates e ON e.[Exam Type Id] = b.[Exam_ID]
        WHERE 1=1 $whrTaBill
        ORDER BY p.ObserverID";


        $sqlTaBill1 = "SELECT p.ObserverID, b.[Reference No_] as ReferenceNo, p.CENTNO, p.CITY, p.NAME,p.BillType, b.[Superintendent Name] SuperintendentName, 
        p.CITY_CD,p.TC_CODE,p.City_code,b.[CreatedDate] billDate, p.ExamType, e.[Exam Name] as examName, b.[Bill Status] as billStatus, e.[Exam Type Id] as examTypeId, b.[Net Payable Amount] as TotalAmount
        FROM $OBSProfile p
        JOIN $TATBL b ON b.[Observer ID] = p.ObserverID AND p.CENTNO = b.[Centre No_]
        JOIN $ExamDates e ON e.[Exam Type Id] = b.[Exam_ID]
        WHERE 1=1 $whrTaBill";

        //echo $sql; die;
        // Initialize variables
        $TaBills = [];
        $Talimit = 5;
        $Tapage = Yii::$app->getRequest()->getQueryParam('page') ?: 1;
        //echo $page;die;
        $Taoffset = ($Tapage - 1) * $Talimit;

        // Fetch data for the current page
        $TaBills = Yii::$app->db->createCommand("$sqlTaBill OFFSET $Taoffset ROWS FETCH NEXT $Talimit ROWS ONLY")->queryAll();
        // echo '
'; print_r($TaBills);die;
        // Get total count for pagination
        $TacountQuery = "SELECT COUNT(*) FROM ($sqlTaBill1) AS count_query";
        $Tacount = Yii::$app->db->createCommand($TacountQuery)->queryScalar();

        // Create pagination object
        $Tapagination = new Pagination(['totalCount' => $Tacount, 'defaultPageSize' => $Talimit, 'page' => $Tapage]);

        // echo "
";
        // print_r($TaBills);die;


            
            //$Bills = Yii::$app->db->createCommand("SELECT * FROM $TATBL JOIN $OBSProfile ON $TATBL.[Observer ID] = $OBSProfile.ObserverID AND $TATBL.[Exam Code] = $OBSProfile.ExamType JOIN $ExamDates ON $TATBL.[Exam Code] = $ExamDates.[Exam Type] WHERE $OBSProfile.[ObserverID] = '$authUserData->NTA_Unique_Admin_ID' ")->queryAll();
        //     echo "
";
        // print_r($TaBills);die;

        return $this->render('index',[
            'examLists'=>$examLists,
            'cc_bills_data'=>$CCBills, 
            'examTypeLists'=>$examTypeLists, 
            'pagination' => $pagination ,
            'getObserverType' => $getObserverType,
            'tBillData'=> $TaBills,
            'Tapagination' => $Tapagination,
            'model' => $model,
            'uniqueExamTypes' => $uniqueExamTypes,
            'searchData'=>$data
        ]);
    
    }