Funcions incloses fins la versió 1.27 de factures emeses

//System.Collections.Generic.List<string> keys = new System.Collections.Generic.List<string>();
System.Collections.Generic.List<string> exentas = new System.Collections.Generic.List<string>();
System.Collections.Generic.Dictionary<string, bool> gMapOfTaxGroups = new System.Collections.Generic.Dictionary<string, bool>();
System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
bool gTaxGroupMapGenerated = false;
bool gTipoNoExentaWritten = false;
bool gDesGloseIVAWritten = false;

bool gIsCancelFlow = false;
int firstRef = 0;

public string externalRef(XPathNodeIterator pathNode)
{
    if (pathNode.MoveNext())
    {
        firstRef = 1;
        return pathNode.Current.Value;
    }
    return "";
}

public int IsFirstRef()
{
    return firstRef;
}

public string SetCancelFlow(XPathNodeIterator pathNode)
{
    if (pathNode.MoveNext())
    {
        gIsCancelFlow = pathNode.Current.Value == "C";
    }

    return "1.1";
}

public bool IsCancelFlow()
{
    return gIsCancelFlow;
}

public string FillListOfTaxGroups(XPathNodeIterator pathNode)
{
    if (gTaxGroupMapGenerated)
    {
        return "generated";
    }

    // just get all different tax groups
    //if (pathNode.MoveNext ())
    {
        XPathNavigator navi = pathNode.Current;
        string xPath = String.Format("//DataSource/TAX1/TaxCode");
        XPathNodeIterator node = navi.Select(xPath);

        string taxCode = "";
        while (node.MoveNext())
        {
            taxCode = node.Current.Value;
            if (taxCode != "" && !gMapOfTaxGroups.ContainsKey(taxCode))
            {
                gMapOfTaxGroups[taxCode] = false; // means it was not handled yet
            }
        }

        gTaxGroupMapGenerated = true;
        return "done";
    }

    return "problem";
}

public string IsTipoNoExentaWritten(XPathNodeIterator pathNode)
{
    if (gTipoNoExentaWritten)
    {
        return "true";
    }
    else
    {
        if (GetVtgTransactionTypeValue(pathNode).Length > 0)
        {
            gTipoNoExentaWritten = true;
        }
        else
        {
            return "true";
        }
    }

    return "false";
}

public string IsDesGloseIVAWritten(XPathNodeIterator pathNode)
{
    if (gDesGloseIVAWritten)
    {
        return "true";
    }
    else
    {
        if (GetVtgTransactionTypeValue(pathNode).Length > 0)
        {
            gDesGloseIVAWritten = true;
        }
        else
        {
            return "true";
        }
    }

    return "false";
}

public string HasSumOfBaseSum(XPathNodeIterator taxGroupNode)
{
    if (taxGroupNode.MoveNext())
    {
        if (!gTaxGroupMapGenerated)
        {
            FillListOfTaxGroups(taxGroupNode);
        }

        string taxCode = taxGroupNode.Current.Value;
        if (gMapOfTaxGroups.ContainsKey(taxCode) && !gMapOfTaxGroups[taxCode])
        {
            return "true";
        }
    }

    return "false";
}

public string MapContainsVatGroup(XPathNodeIterator taxGroupNode)
{
    string taxCode = "";
    if (taxGroupNode.MoveNext())
    {
        taxCode = taxGroupNode.Current.Value;
        XPathNavigator navi = taxGroupNode.Current;
        if (gMapOfTaxGroups.ContainsKey(taxCode))
        {
            return "true";
        }
    }

    return "false";
}

public string MapChangeVatGroup(XPathNodeIterator taxGroupNode)
{
    string taxCode = "";
    bool oldValue = false; ;
    if (taxGroupNode.MoveNext())
    {
        taxCode = taxGroupNode.Current.Value;
        XPathNavigator navi = taxGroupNode.Current;
        if (gMapOfTaxGroups.ContainsKey(taxCode))
        {
            oldValue = gMapOfTaxGroups[taxCode];
            gMapOfTaxGroups[taxCode] = true;
            oldValue = gMapOfTaxGroups[taxCode];
        }
    }

    if (oldValue)
    {
        return "true";
    }
    else
    {
        return "false";
    }
    return "false";
}

public decimal GetSumOfBaseSum(XPathNodeIterator taxGroupNode)
{
    string taxCode = "";
    decimal lbaseSum = 0;
    if (taxGroupNode.MoveNext())
    {
        taxCode = taxGroupNode.Current.Value;
        XPathNavigator navi = taxGroupNode.Current;
        if (gMapOfTaxGroups.ContainsKey(taxCode) && gMapOfTaxGroups[taxCode] != true)
        {
            gMapOfTaxGroups[taxCode] = true; // mark our gMap as already computed

            // the sum for given tax code was not yet computed
            XPathNodeIterator node = navi.Select(String.Format("//DataSource/TAX1/TaxCode[text()='{0}']/..", taxCode));
            nfi.NumberDecimalSeparator = ".";
            while (node.MoveNext())
            {
                lbaseSum += decimal.Parse(node.Current.SelectSingleNode("BaseSum").Value, nfi);
            }
        }
    }

    if (gIsCancelFlow)
    {
        lbaseSum *= -1;
    }
    return lbaseSum;
}

public decimal GetSumOfBaseSum2(XPathNodeIterator taxGroupNode, string taxCode)
{
    decimal lbaseSum = 0;
    if (taxGroupNode.MoveNext())
    {
        if (!gTaxGroupMapGenerated)
        {
            FillListOfTaxGroups(taxGroupNode);
        }

        XPathNavigator navi = taxGroupNode.Current;
        if (gMapOfTaxGroups.ContainsKey(taxCode) && gMapOfTaxGroups[taxCode] != true)
        {
            gMapOfTaxGroups[taxCode] = true; // mark our gMap as already computed

            // the sum for given tax code was not yet computed
            XPathNodeIterator node = navi.Select(String.Format("//DataSource/TAX1/TaxCode[text()='{0}']/..", taxCode));
            nfi.NumberDecimalSeparator = ".";
            while (node.MoveNext())
            {
                lbaseSum += decimal.Parse(node.Current.SelectSingleNode("BaseSum").Value, nfi);
            }
        }
    }

    if (gIsCancelFlow)
    {
        lbaseSum *= -1;
    }
    return lbaseSum;
}

public decimal GetSumOfVatSum(XPathNodeIterator taxGroupNode)
{
    string taxCode = "";
    decimal lvatSum = 0;
    if (taxGroupNode.MoveNext())
    {
        taxCode = taxGroupNode.Current.Value;
        XPathNavigator navi = taxGroupNode.Current;

        XPathNodeIterator node = navi.Select(String.Format("//DataSource/TAX1/TaxCode[text()='{0}']/..", taxCode));
        while (node.MoveNext())
        {
            lvatSum += decimal.Parse(node.Current.SelectSingleNode("VatSum").Value, nfi);
        }
    }

    if (gIsCancelFlow)
    {
        lvatSum *= -1;
    }
    return lvatSum;
}

public decimal GetSumOfEqSum(XPathNodeIterator taxGroupNode)
{
    string taxCode = "";
    decimal leqSum = 0;
    if (taxGroupNode.MoveNext())
    {
        taxCode = taxGroupNode.Current.Value;
        XPathNavigator navi = taxGroupNode.Current;

        XPathNodeIterator node = navi.Select(String.Format("//DataSource/TAX1/TaxCode[text()='{0}']/..", taxCode));
        while (node.MoveNext())
        {
            leqSum += decimal.Parse(node.Current.SelectSingleNode("EqSum").Value, nfi);
        }
    }

    if (gIsCancelFlow)
    {
        leqSum *= -1;
    }
    return leqSum;
}

public string HasEqSum(XPathNodeIterator taxGroupNode)
{
    if (GetSumOfEqSum(taxGroupNode) > 0)
    {
        return "true";
    }

    return "false";
}


public string GetSpecialRegimeValue(XPathNodeIterator specialRegimeCode)
{
    string specialRegimeVal = "";
    string oldSpecialRegimeVal = "";

    if (specialRegimeCode.MoveNext())
    {
        XPathNavigator navi = specialRegimeCode.Current;
        navi.MoveToRoot();

        string xPath = "DataSource/OVTG/U_B1SYS_SPEC_REGIME";
        XPathNodeIterator nodeRegimeCodesIter = navi.Select(xPath);
        while (nodeRegimeCodesIter.MoveNext())
        {
            specialRegimeVal = nodeRegimeCodesIter.Current.Value;
            if (specialRegimeVal != "")
            {
                if (oldSpecialRegimeVal == "")
                {
                    oldSpecialRegimeVal = specialRegimeVal;
                }
                else if (oldSpecialRegimeVal != specialRegimeVal)
                {
                    // there can't be more different values so return nothing to cause EFM error
                    specialRegimeVal = "-1";
                    break;
                }
            }
        }
    }

    return specialRegimeVal;
}


public string GetVtgTransactionTypeValue(XPathNodeIterator nodeTax1TaxCode)
{
    string taxCode = "";
    string transactionType = "";

    if (nodeTax1TaxCode.MoveNext())
    {
        taxCode = nodeTax1TaxCode.Current.Value;
        XPathNavigator navi = nodeTax1TaxCode.Current;
        //navi.MoveToRoot();

        XPathNavigator transactionTypeNavig = navi.SelectSingleNode(String.Format("//DataSource/OVTG/Code[text()='{0}']/../U_B1SYS_TRANS_TYPE", taxCode));
        if (transactionTypeNavig != null)
        {
            transactionType = transactionTypeNavig.Value;
        }

        return transactionType;
        //return taxCode;
    }
    return transactionType;
}

public string GetVtgExemptReasonValue(XPathNodeIterator nodeTax1TaxCode)
{
    string taxCode = "";
    string exemptReason = "";

    if (nodeTax1TaxCode.MoveNext())
    {
        taxCode = nodeTax1TaxCode.Current.Value;
    }

    XPathNavigator navi = nodeTax1TaxCode.Current;
    //navi.MoveToRoot();

    XPathNavigator exemptReasonNavig = navi.SelectSingleNode(String.Format("//DataSource/OVTG/Code[text()='{0}']/../U_B1SYS_EXEM_REASON", taxCode));
    if (exemptReasonNavig != null)
    {
        exemptReason = exemptReasonNavig.Value;
    }

    return exemptReason;
}



public int CheckExenta(XPathNodeIterator TransType, XPathNodeIterator ExemptReason)
{
    string exemptReason = "";
    string transType = "";

    
    if (TransType.MoveNext())
    {
        transType = TransType.Current.Value;
    }

    if (transType == "")
    {
        if (ExemptReason.MoveNext())
        {
            exemptReason = ExemptReason.Current.Value;
        }

        if (exentas.Contains(exemptReason))
        {
            return 1;
        }
        else
        {
            exentas.Add(exemptReason);
            return 0;
        }
    }

    return 1;   
}

public decimal GetSumOfBaseSum3(XPathNodeIterator taxGroupNode, XPathNodeIterator ExemptReason)
{
    decimal lbaseSum = 0;
    string exemptReason = "";
    if (ExemptReason.MoveNext())
    {
        exemptReason = ExemptReason.Current.Value;
    }

    if (exemptReason != "" && taxGroupNode.MoveNext())
    {

        XPathNavigator naviGroup = taxGroupNode.Current;
        XPathNavigator naviTax = ExemptReason.Current;

        XPathNodeIterator taxGroups = naviGroup.Select(String.Format("//DataSource/OVTG/U_B1SYS_EXEM_REASON[text()='{0}']/../Code", exemptReason));
        while (taxGroups.MoveNext())
        {
            string taxCode = taxGroups.Current.Value;
            XPathNodeIterator node = naviTax.Select(String.Format("//DataSource/TAX1/TaxCode[text()='{0}']/..", taxCode));
            nfi.NumberDecimalSeparator = ".";
            while (node.MoveNext())
            {
                lbaseSum += decimal.Parse(node.Current.SelectSingleNode("BaseSum").Value, nfi);
            }
        }

        if (gIsCancelFlow)
        {
            lbaseSum *= -1;
        }
    }
    return lbaseSum;
}

public string GetVtgExemptReasonValue2(XPathNodeIterator nodeTax1TaxCode, string taxCode)
{
    string exemptReason = "";

    if (nodeTax1TaxCode.MoveNext())
    {
        XPathNavigator navi = nodeTax1TaxCode.Current;

        XPathNavigator exemptReasonNavig = navi.SelectSingleNode(String.Format("//DataSource/OVTG/Code[text()='{0}']/../U_B1SYS_EXEM_REASON", taxCode));
        if (exemptReasonNavig != null)
        {
            exemptReason = exemptReasonNavig.Value;
        }
    }
    return exemptReason;
}

public string GetFirstValidTipoNoExenta(XPathNodeIterator specialRegimeCode)
{
    string specialRegimeVal = "";
    string oldSpecialRegimeVal = "";

    if (specialRegimeCode.MoveNext())
    {
        XPathNavigator navi = specialRegimeCode.Current;

        string xPath = "//DataSource/OVTG/U_B1SYS_TRANS_TYPE";
        XPathNodeIterator nodeRegimeCodesIter = navi.Select(xPath);
        while (nodeRegimeCodesIter.MoveNext())
        {
            specialRegimeVal = nodeRegimeCodesIter.Current.Value;
            if (specialRegimeVal != "")
            {
                if (oldSpecialRegimeVal == "")
                {
                    oldSpecialRegimeVal = specialRegimeVal;
                }
                else if (oldSpecialRegimeVal != specialRegimeVal)
                {
                    // there can't be more different values so return nothing to cause EFM error
                    specialRegimeVal = "-1";
                    break;
                }
            }
        }
    }

    return specialRegimeVal;
}

public string GetFirstExentaTaxCode(XPathNodeIterator pathName)
{
    string taxCode = "";
    string transType = "";

    if (pathName.MoveNext())
    {
        XPathNavigator navi = pathName.Current;

        string xPath = "//DataSource/OVTG";
        XPathNodeIterator nodesIter = navi.Select(xPath);
        while (nodesIter.MoveNext())
        {
            XPathNavigator transNode = nodesIter.Current.SelectSingleNode("U_B1SYS_TRANS_TYPE");
            if (transNode == null)
            {
                taxCode = nodesIter.Current.SelectSingleNode("Code").Value;
                return taxCode;
            }
            else
            {
                transType = transNode.Value;
                if (transType == "")
                {
                    taxCode = nodesIter.Current.SelectSingleNode("Code").Value;
                }
            }
        }
    }

    return taxCode;
}

public string ConvertEL2GR(string val)
{
    if (val == "EL")
    {
        return "GR";
    }

    return val;
}