checkout – Magento 2.4.2: How to get order sub total in my custom module?

With the code below, I am trying to get the order sub total. Please note I am querying the table that I created in the Magento Database. $subtotal = $request->getOrderSubtotal(); doesn’t get the subtotal.

<?php

namespace PerfectmakeupmirrorsCustomShippingModelCarrier;

use MagentoQuoteModelQuoteAddressRateRequest;
use MagentoShippingModelCarrierAbstractCarrier;
use MagentoShippingModelCarrierCarrierInterface;
use PerfectmakeupmirrorsPmmFedexModelPmmFedexCarrier;
use MagentoFrameworkAppResourceConnection;

/**
 * Custom shipping model
 */
class Customshipping extends AbstractCarrier implements CarrierInterface
{
    const CAD_RATE = 1.26;
    const FEDEX_METHODS = (
        'EUROPE_FIRST_INTERNATIONAL_PRIORITY' => 'Europe First Priority',
        'FEDEX_1_DAY_FREIGHT' => '1 Day Freight',
        'FEDEX_2_DAY_FREIGHT' => '2 Day Freight',
        'FEDEX_2_DAY' => '2 Day',
        'FEDEX_2_DAY_AM' => '2 Day AM',
        'FEDEX_3_DAY_FREIGHT' => '3 Day Freight',
        'FEDEX_EXPRESS_SAVER' => 'Express Saver',
        'FEDEX_GROUND' => 'Ground',
        'FIRST_OVERNIGHT' => 'First Overnight',
        'GROUND_HOME_DELIVERY' => 'Home Delivery',
        'INTERNATIONAL_ECONOMY' => 'International Economy',
        'INTERNATIONAL_ECONOMY_FREIGHT' => 'Intl Economy Freight',
        'INTERNATIONAL_FIRST' => 'International First',
        'INTERNATIONAL_GROUND' => 'International Ground',
        'INTERNATIONAL_PRIORITY' => 'International Priority',
        'INTERNATIONAL_PRIORITY_FREIGHT' => 'Intl Priority Freight',
        'PRIORITY_OVERNIGHT' => 'Priority Overnight',
        'SMART_POST' => 'Smart Post',
        'STANDARD_OVERNIGHT' => 'Standard Overnight',
        'FEDEX_FREIGHT' => 'Freight',
        'FEDEX_NATIONAL_FREIGHT' => 'National Freight'
    );
    const SHIPPING_STANDARD = 'STD';
    const SHIPPING_2ND_DAY = '2DY';
    const SHIPPING_OVERNIGHT = 'ON';
    protected $_shipping_mode_strings = array(
        self::SHIPPING_STANDARD => 'Standard Ground',
        self::SHIPPING_2ND_DAY => 'Second Day',
        self::SHIPPING_OVERNIGHT => 'Next Day Air',
    );

    /**
     * @var string
     */
    protected $_code = 'customshipping';

    /**
     * @var bool
     */
    protected $_isFixed = true;

    /**
     * @var MagentoShippingModelRateResultFactory
     */
    private $rateResultFactory;

    /**
     * @var MagentoQuoteModelQuoteAddressRateResultMethodFactory
     */
    private $rateMethodFactory;

    /**
     * @var PerfectmakeupmirrorsCustomShippingHelperData
     */
    private $helper;

    /**
     * @var PsrLogLoggerInterface
     */
    protected $_logger;

    private $carrierFedex;

    protected $resource;

    /**
     * @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
     * @param MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory
     * @param PsrLogLoggerInterface $logger
     * @param MagentoShippingModelRateResultFactory $rateResultFactory
     * @param MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory
     * @param MagentoFrameworkAppResourceConnection $resource
     * @param array $data
     */
    public function __construct(
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoQuoteModelQuoteAddressRateResultErrorFactory $rateErrorFactory,
        PsrLogLoggerInterface $logger,
        MagentoShippingModelRateResultFactory $rateResultFactory,
        MagentoQuoteModelQuoteAddressRateResultMethodFactory $rateMethodFactory,
        PerfectmakeupmirrorsCustomShippingHelperData $helper,
        PerfectmakeupmirrorsPmmFedexModelPmmFedexCarrier $carrierFedex,
        MagentoFrameworkAppResourceConnection $resource,
        array $data = ()
    ) {
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data, $resource);

        $this->rateResultFactory = $rateResultFactory;
        $this->rateMethodFactory = $rateMethodFactory;
        $this->carrierFedex = $carrierFedex;
        $this->_logger = $logger;
        $this->helper = $helper;
        $this->resource = $resource;
    }

    /**
     * Custom Shipping Rates Collector
     *
     * @param RateRequest $request
     * @return MagentoShippingModelRateResult|bool
     */
    public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
        
        $this->_logger->info("Start of Custom Shipping collectRates");
        if ($request->getDestCountryId() == "CA") {
            $this->_logger->info("Country ID: " . $request->getDestCountryId());
            $fedex_results = $this->carrierFedex->collectRates($request);
            // 1. Get the array of method objects from $fedex_results object
            // 2. Loop through the array of method objects to set carrier.
            // 3. Set carrier to custom shipping. 
            // 4. Add brokerage to the shipping cost based on the table canada_brokerage_fees.

            // MagentoQuoteModelQuoteAddressRateResultMethod $methods
            $methods = $fedex_results->getAllRates();
            $this->_logger->info("At line 136");
            foreach ($methods as $method) {
                $this->_logger->info("At line 138");
                $method->setCarrier($this->_code);
                // Set new price with brokerage added to the shipping cost.
                // Get subtotal from the $request object.
                // Recalculate subtotal in CAD
                // Create table canada_brokerage_fee in Magento DB
                // Compute brokerage.
                $subtotal = $request->getOrderSubtotal();
                $this->_logger->info("Sub total in USD: " . $subtotal);

                // Convert $subtotal from USD to CAD
                // 1 USD = 1.26 CAD
                $subtotal_cad = ($subtotal * 1.26);
                $this->_logger->info("Sub total in CAD: " . $subtotal_cad);

                // Create DB connection
                // http://blog.chapagain.com.np/magento-2-run-custom-sql-query/
                $connection    = $this->resource->getConnection();
                $tableName     = $this->resource->getTableName('canada_brokerage_fees');
                $this->_logger->info("At line 157");

                // Fetch relevant fee from the table canada_brokerage_fees 
                // based on the merchandise total.
                $sql = "SELECT brokerage_cost_cad FROM " . $tableName .  
                       " WHERE max_merchandise_cost <=" . $subtotal_cad . " ORDER BY brokerage_cost_cad DESC LIMIT 1";
                $result = $connection->query($sql);   
                $brokerage_cost_cad = $result('brokerage_cost_cad');
                $this->_logger->info("At line 165");
                
                $this->_logger->info("brokerage in CAD: " . $brokerage_cost_cad);
                $method->setPrice($method->getPrice + $brokerage_cost_cad);
                $this->_logger->info("At line 169");
            }

            return $fedex_results;
        }

        /** @var MagentoShippingModelRateResult $result */
        $result = $this->rateResultFactory->create();

        /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
        $method = $this->rateMethodFactory->create();

        $method->setCarrier($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod($this->_code);
        $method->setMethodTitle($this->getConfigData('name'));

        $this->_logger->info(__FILE__ . ': At Start');

        // Get all the items.
        // NOTE: This getAllItems here is related to Quote and not the order.
        if ((!($items = $request->getAllItems())) or (count($items) == 0)) {
            return FALSE;
        }

        // $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        // $attributeSet = $objectManager->create('MagentoEavApiAttributeSetRepositoryInterface');
        // $attributeSetRepository = $attributeSet->get(9);
        // $attribute_set_name = $attributeSetRepository->getAttributeSetName();
        // $this->_logger->info(__FILE__ . ": Attribute Set Name = $attribute_set_name");

        //Standard code commented. Custom code added.
        //$shippingCost = (float)$this->getConfigData('shipping_cost');
        $shippingCost = (float)$this->helper->compute_standard_shipping_cost($items);

        $method->setPrice($shippingCost);
        $method->setCost($shippingCost);

        $result->append($method);

        return $result;
    }

    /**
     * @return array
     */
    public function getAllowedMethods()
    {
        $custom_shipping_methods = ($this->_code => $this->getConfigData('name'));
        $allowed_methods = array_merge($custom_shipping_methods, $this->FEDEX_METHODS);
        return $allowed_methods;
    }
}