[X++] Get TaxCode from SalesLine

My requirement was to get the Text of the TaxExemptCode from a SalesLine in the Report SalesConfirm.
The DataProvider class SalesConfirmDP already has logic to initialize a TmpTaxWorkTrans buffer and therefore the TaxExemptTxt can be found by the following code:

Description           taxExemptTxt;
TaxExemptCode           taxExemptCode           = TaxGroupData::find(_tmpTaxWorkTrans.TaxGroup, _tmpTaxWorkTrans.TaxCode).TaxExemptCode;

taxExemptTxt = TaxExemptCodeTable::txt(taxExemptCode, languageId)

If you know a shorter or nicer way, let me know in the comments!

But then I was curious how to get the TaxCode starting from a given SalesLine buffer since SalesLine only has the fields TaxGroup and TaxItemGroup but no TaxCode.

The standard does this in the following method:
\Classes\Tax\insertIntersection

Adapting this code to a method, it could look something similar to this:

public static TaxCode taxCodeFromSalesLine(SalesLine   _salesLine)
{
    TaxGroup            taxGroup;
    TaxItemGroup        taxItemGroup;
    TaxGroupData        taxGroupData;
    TaxOnItem           taxOnItem;
    Set                 taxCodeCombination = new Set(Types::String);

    container           cachedResult;
    TaxCode             taxCode;
    
    taxGroup        = _salesLine.TaxGroup;
    taxItemGroup    = _salesLine.TaxItemGroup;
    
    // Pull the intersection information from cache
    cachedResult = TaxCache::getValue(
        TaxCacheScope::IntersectionData,
        [curext(), taxGroup, taxItemGroup]);
    if (cachedResult != conNull())
    {
        taxCode = any2str(conPeek(conPeek(cachedResult, 1), 2));
    }
    else
    {
        // Data not yet cached, look it up
        while select TaxGroup, TaxCode, ExemptTax, IntracomVAT from taxGroupData
            where taxGroupData.TaxGroup             == taxGroup
            join TaxItemGroup, TaxCode
                // 
                ,ExemptTax_BR
                // 
                from taxOnItem
            where taxOnItem.TaxItemGroup            == taxItemGroup     &&
                    taxOnItem.TaxCode               == taxGroupData.TaxCode
        {
            taxCode = taxGroupData.TaxCode;
        }
    }
    
    return taxCode;
}

Call to the method:

SalesLine sl;
    select firstOnly sl where sl.SalesId == "YOUR_SALES_ID";
    
    info(YOUR_CLASS::taxCodeFromSalesLine(sl));