contract S1ProcessFee is LoanProcess{
    
    SELLSZO  public sellSZO;
    SZO     public szoToken;
  
    
    mapping(string=>uint256) public szoFee;
    
//================ HELP FUNCTION ==============
    function toUPPER(string memory source) public pure returns (string memory result) {
        bytes memory bufSrc = bytes(source);
        if (bufSrc.length == 0) {
            return "";
        }

        for(uint256 i=0;i<bufSrc.length;i++){
            uint8 test = uint8(bufSrc[i]);
            if(test>=97 && test<= 122)
                bufSrc[i] = byte(test - 32);
        }
        
        return string(bufSrc);

    }
    
    constructor() public{
        szoToken = SZO(0x6086b52Cab4522b4B0E8aF9C3b2c5b8994C36ba6);
        sellSZO = SELLSZO(0x0D80089B5E171eaC7b0CdC7afe6bC353B71832d1);
    }
    function setSellSZO(address _addr) public onlyPermits returns(bool){
        sellSZO = SELLSZO(_addr);
        return true;
    }
    
    function setSZOToken(address _addr)public onlyPermits returns(bool){
        szoToken = SZO(_addr);
        return true;
    }

    
    function payFee(address _from,uint256 amount,address _tokenAddr) internal returns(bool){
        // check have enounht szo or not if have use it if not buy it
        // if(szoToken.balanceOf(_from)>= amount)
        //     return sellSZO.useAndBurn(_from,amount);
        // else
            return sellSZO.buyUseAndBurn(_tokenAddr,_from,amount);
    }
    
    function mintDocFee(uint256 _tokenID,uint256 _docID,uint256 _docType,address _owner,string memory _ipfs,uint256 _fee,address _tokenAddr) public onlyPermits returns(bool){
         if(payFee(_owner,_fee,_tokenAddr) == true)
             return mintDocToken(_tokenID,_docID,_docType,_owner,_ipfs);
    }
    
    function mintLoanTokenFee(uint256 _tokenID,uint256 _docID,uint256 _contractID,address _borrow,uint256 _amount,uint256 _intCom,uint256 _intLen,uint256 _intGua,string calldata _currency,uint256 _fee,address _tokenAddr) external onlyPermits returns(bool){
         if(payFee(_borrow,_fee,_tokenAddr) == true)
            return mintLoanToken(_tokenID,_docID,_contractID, _borrow,_amount, _intCom, _intLen, _intGua,_currency);
    }
    
    function makeSpecialLoan(uint256 _contractID,uint256 _termpay,uint256 expirationTime,address lender) external onlySuperAdmin returns(string memory result){
        POOLS  pool = POOLS(lender);
        uint256 interest = getLoanInterest(_contractID);
        uint256 poolInt =  pool.borrowInterest();
        
        if(interest < poolInt){
            pool.setBorrowInterest(interest);
            result = activeContract(_contractID,_termpay,expirationTime,lender);
            pool.setBorrowInterest(poolInt);
          
        }
        else
        {
            result =  activeContract(_contractID,_termpay,expirationTime,lender);
        }
        
        
    }
    
    function setLegalApproveFee(address _legal,uint256 _tokenID,uint256 _fee,address _tokenAdd) external onlyPermits returns(bool){
        if(payFee(_legal,_fee,_tokenAdd) == true)
            return setLegalApprove(_legal,_tokenID);
    }
    
    function setAuditApproveAndCreditFee(address _audit,uint256 _tokenID,uint256 _credit,uint256 _score,uint256 _fee,address _tokenAdd) external onlyPermits returns(bool){
        if(payFee(_audit,_fee,_tokenAdd) == true)
            return setAuditApproveAndCredit(_audit,_tokenID,_credit,_score);
    }
    
    function paidContractFee(uint256 _conID,uint256 _amount,address _tokenAddr,address _from,uint256 _fee) external onlyPermits returns(bool){
        if(payFee(_from,_fee,_tokenAddr) == true)
            return paidContract(_conID,_amount,_tokenAddr,_from);
    }

    
    function mintCATTokenFee(uint256 _tokenID,address _from,uint256 _fee,address _tokenAddr) external onlyPermits returns(string memory){
        if(payFee(_from,_fee,_tokenAddr) == true)
            return mintCATToken(_tokenID);
    }
    
    function activeContractFree(uint256 _contractID,uint256 _termpay,uint256 expirationTime,address lender,address _from,uint256 _fee,address _tokenAddr) external onlyPermits  returns(string memory result){
        if(payFee(_from,_fee,_tokenAddr) == true)
           return activeContract(_contractID,_termpay,expirationTime,lender);
    }
  
    
}
function toUPPER(string memory source) public pure returns (string memory result) {
        bytes memory bufSrc = bytes(source);
        if (bufSrc.length == 0) {
            return "";
        }

        for(uint256 i=0;i<bufSrc.length;i++){
            uint8 test = uint8(bufSrc[i]);
            if(test>=97 && test<= 122)
                bufSrc[i] = byte(test - 32);
        }
        
        return string(bufSrc);

    }
//================ HELP FUNCTION ==============