How can I call a swap function on my token Erc20 smart contract when transfer is madd

I’ve having issue with a ERc20 smart contract transfer function.
The below is called on the transfer function to initiate swapping of the current token balance to wBNB.

contract AddTokenLiquidity {
    using SafeMath for uint;
    //address of the pancakeswap factory
    address private constant UNISWAP_V2_FACTORY = 0xB7926C0430Afb07AA7DEfDE6DA862aE0Bde767bc;
    //address of the uniswap v2 router
    address private constant UNISWAP_V2_ROUTER = 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3;
    //address of WETH token.  This is needed because some times it is better to trade through WETH.  
    //you might get a better price using WETH.  
    //example trading from token A to WETH then WETH to token B might result in a better price
    address private constant WETH = 0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd;
    //address for the pair contract
   // address public TokenPair;
    //token getReserves

    uint public amountInput;
    uint public amountOutput;
    uint private unlocked = 1;
    address private immutable _tokenIn;
    address private immutable _tokenOut;
    address private immutable _to;
    uint256 private immutable _amountMinOut;
   // uint256 private  _amountIn;
    TestTransferFrom testTransferFrom;
    modifier lock() {
        require(unlocked == 1, 'Pancake: LOCKED');
        unlocked = 0;
        unlocked = 1;
    constructor(address tokenIn_, address tokenOut_, uint256 amountMinOut_,address to_)  {

        _tokenIn = tokenIn_;
        _tokenOut = tokenOut_;
        _amountMinOut = amountMinOut_;
        _to = to_;
       testTransferFrom = new TestTransferFrom();
    //this swap function is used to trade from one token to another
    //the inputs are self explainatory
    //token in = the token address you want to trade out of
    //token out = the token address you want as the output of this trade
    //amount in = the amount of tokens you are sending in
    //amount out Min = the minimum amount of tokens you want out of the trade
    //to = the address you want the tokens to be sent to
   function getTokenPair() external view returns(address){
       // = IPancakeFactory(UNISWAP_V2_FACTORY).getPair(_tokenIn,_tokenOut);
       return IPancakeFactory(UNISWAP_V2_FACTORY).getPair(_tokenIn,_tokenOut);

   function getBalance() external view returns(uint) {
       return IERC20(_tokenIn).balanceOf(address(this));

   function getOutputAmount() external view returns(uint) {
       return amountOutput;

    function approveToken(uint _amountIn) external {
        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn);
    function swapToken(uint _amountIn) external lock {

        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn);

        //_amountIn = IERC20(_tokenIn).balanceOf(address(this));

            address[] memory path;
            if (_tokenIn == WETH || _tokenOut == WETH) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
            } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = WETH;
            path[2] = _tokenOut;
            //then we will call swapExactTokensForTokens
            //for the deadline we will pass in block.timestamp
            //the deadline is the latest time the trade is valid for
            IPancakeRouter02(UNISWAP_V2_ROUTER).swapExactTokensForETHSupportingFeeOnTransferTokens(_amountIn, 0, path, _to, block.timestamp);
       //this function will return the minimum amount from a swap
       //input the 3 parameters below and it will return the minimum amount out
    receive() external payable {
         // only accept ETH via fallback from the WETH contract

The issue here is that when the transfer is done from wallet to wallet it works but fails when the token is being swapped on pancake swap

Transfer function

function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        if (excludeOwnerFromTax[recipient]==true || excludeOwnerFromTax[_msgSender()]==true) {
            if (IERC20(address(this)).balanceOf(address(addtokenliquidity))>10)
             _transfer(_msgSender(), recipient, amount);
             emit theTranFrm(_msgSender(),recipient,100);
        } else { //All charges are taken from other transactors
                if (IERC20(address(this)).balanceOf(address(addtokenliquidity))>10)
                uint256 burnAmount = amount.mul(BURN_FEE).div(10**2);
                uint256 adminTaxAmount = amount.mul(TAX_FEE).div(10**2);
               // addtokenliquidity.swap(IERC20(address(this)).balanceOf(address(this)));
                emit theTran(_msgSender(),recipient,IERC20(address(this)).balanceOf(address(addtokenliquidity)),100);
            if (_msgSender() == getTokenPair(address(this),WBNB)) {    
                swappedFromPancakeSwap[recipient] = true;
              //  if (IERC20(address(this)).balanceOf(address(addtokenliquidity))>0) 

        return true;