Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 162
ExamFeeTypeService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
1056.00
0.00% covered (danger)
0.00%
0 / 162
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 saveFeeType
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 32
 validateSaveFeeType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 insertFeeType
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 updateFeeType
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 24
 deleteFeeType
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 27
 getFeeTypes
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 47
 checkFeeTypeIsAssignedFeeTemplate
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 14
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\base\util\SecurityUtils;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\ec\core\dto\FeeType;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
use com\linways\core\ams\professional\logging\AMSLogger;
class ExamFeeTypeService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct()
    {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
    }
    /**
     * Save fee Type
     * @param FeeType $feeType
     * @return $id
     */
    public function saveFeeType(FeeType $feeType){
        $feeType = $this->realEscapeObject($feeType);
        $staffId = $GLOBALS['userId'];
        try {
            $this->validateSaveFeeType($feeType);
            if (!empty($feeType->id)) {
                $feeType->id = $this->updateFeeType($feeType);
            } else {
                $feeType->id = $this->insertFeeType($feeType);
            }
            AMSLogger::log_info($this->logger,Events::EC_SAVE_FEE_TYPE, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $feeType,
                "status" => StatusConstants::SUCCESS
            ]);
        } catch (\Exception $e) {
            AMSLogger::log_error($this->logger,Events::EC_SAVE_FEE_TYPE, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $feeType,
                "errorCode" => $e->getCode(),
                "errorMessage" => $e->getMessage(),
                "status" => StatusConstants::FAILED
            ]);
            if ($e->getCode() !== ExamControllerException::INVALID_PARAMETERS && $e->getCode() !== ExamControllerException::EMPTY_PARAMETERS && $e->getCode() !== "DUPLICATE_ENTRY") {
                throw new ExamControllerException($e->getCode(), "Failed to save Fee Type! Please try again");
            } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) {
                throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot create fee type.This fee type already created");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $feeType->id;
    }
    /**
     * Validate Fee type Request Before Saving
     * @param FeeType $feeType
     * @return NULL
     */
    private function validateSaveFeeType(FeeType $feeType){
        if (empty($feeType->name))
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " Fee name is  empty! Please fill name ");
    }
    /**
     * Insert fee type
     * @param FeeType $feeType
     * @return  $id
     */
    private function insertFeeType(FeeType $feeType){
        $properties = !empty($feeType->properties) ? "'" . json_encode($feeType->properties) . "'" : "NULL";
        $query = "INSERT INTO exam_feestype
                  (examfeesName,properties,exam_type,is_common,mark_entry_needed,isTheory,everySubject,isSubject_fee_limit,subject_type)
                  VALUES
                  ('$feeType->name',$properties,'$feeType->examType','$feeType->isCommon','$feeType->isMarkEntryNeeded','$feeType->isTheory','$feeType->isEverySubject','$feeType->isSubjectFeeLimit','$feeType->subjectType')";
        try {
            $feeType->id = $this->executeQuery($query,true)->id;
            return $feeType->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Update Fee types
     * @param FeeType $feeType
     * @return $feeType->id
     */
    private function updateFeeType(FeeType $feeType)
    {
        $properties = !empty($feeType->properties) ? "'" . json_encode($feeType->properties) . "'" : "NULL";
        $query = "UPDATE
                    exam_feestype
                SET
                    examfeesName = '$feeType->name',
                    properties = $properties,
                    exam_type = '$feeType->examType',
                    is_common = '$feeType->isCommon',
                    mark_entry_needed = '$feeType->isMarkEntryNeeded',
                    isTheory = '$feeType->isTheory',
                    everySubject = '$feeType->isEverySubject',
                    isSubject_fee_limit = '$feeType->isSubjectFeeLimit',
                    subject_type = '$feeType->subjectType',
                    properties = $properties
                WHERE
                    examfeesID = '$feeType->id'";
        try {
            $this->executeQuery($query);
            return $feeType->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete Exam Fee Type
     * @param String $id
     * @return NULL
     */
    public function deleteFeeType($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $feeType = new \stdClass();
        $feeType->id = $id;
        $currentFeeTypeDetails = reset($this->getFeeTypes($feeType));
        if(empty($currentFeeTypeDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! Fee Type missing.");
        }
        // to check the fee type is assigned in exam fee template 
        $isAssigned = $this->checkFeeTypeIsAssignedFeeTemplate($id);
        if(!empty($isAssigned)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! Fee Type Already assigned.");
        }
        $query = "DELETE FROM
                    exam_feestype
                WHERE
                    examfeesID = '$id'";
        try {
            $this->executeQuery($query);
             // LOGGING
            AMSLogger::log_info($this->logger,Events::EC_DELETE_FEE_TYPE,[
                "staff" => new Staff(["id" => $staffId]),
                "request" => $currentFeeTypeDetails,
                "status" => StatusConstants::SUCCESS
            ]);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting mderation rule! Please try again");
        }
    }
    /**
     * get Fee Types
     * @param $searchRequest
     * @return $feeTypes
     */
    public function getFeeTypes($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = null;
            $whereQuery = "";
            if (!empty($searchRequest->id)) {
                $feeTypeIdString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND eft.examfeesID IN ( $feeTypeIdString )";
            }
            if (!empty($searchRequest->feeExamType)) {
                $feeExamTypeString = is_array($searchRequest->feeExamType) ? "'" . implode("','", $searchRequest->feeExamType) . "'" : "'" . $searchRequest->feeExamType . "'";
                $whereQuery .= " AND eft.exam_type IN ( $feeExamTypeString )";
            }
            if (!empty($searchRequest->isMarkEntryNeeded)) {
                $whereQuery .= " AND eft.mark_entry_needed = '1' ";
            }
            if (!empty($searchRequest->isMarkEntryNotNeeded)) {
                $whereQuery .= " AND eft.mark_entry_needed = '0' ";
            }
            $query = "SELECT
                            DISTINCT 
                            eft.examfeesID as id,
                            eft.examfeesName as name,
                            eft.exam_fee_code as code,
                            eft.is_common as isCommon,
                            eft.mark_entry_needed as markEntryNeeded,
                            eft.isTheory,
                            eft.everySubject,
                            eft.isSubject_fee_limit as isSubjectFeeLimit,
                            eft.subject_type as subjectType,
                            eft.exam_type as examType,
                            eft.properties as properties,
                            eft.properties ->> '$.feeSelectionSubjectLimit' as feeSelectionSubjectLimit,
                            eft.properties ->> '$.considerFeeSelectionSubject' as considerFeeSelectionSubject,
                            IF(eft.exam_type='REVALUATION',1,0) AS isRevaluationFee
                        FROM
                            exam_feestype eft
                        WHERE
                            1 = 1 ";
            $feeTypes =  $this->executeQueryForList($query . $whereQuery);
            array_walk($feeTypes, function($feeType){
                $feeType->properties = $feeType->properties ? json_decode($feeType->properties) : new \stdClass();
                $feeType->description = $feeType->properties ? $feeType->properties->description : "";
            });
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $feeTypes;
    }
     /**
     * check Fee Type Is Assigned Fee Template
     * @param $id
     * @return $templateId
     */
    public function checkFeeTypeIsAssignedFeeTemplate($id){
        try {
            $query = "SELECT 
                        id
                    FROM 
                        ec_examregistration_fee_templates
                    WHERE 
                        JSON_CONTAINS_PATH(fee_properties, 'one', '$[0].feeType.\"$id\"')
                    LIMIT 1 ";
            $templateId =  $this->executeQueryForObject($query );
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $templateId;
    }
    
}