Per saber si una factura conté una línia de serveis amb una compta que comenci per 701:

Torna true si hi ha alguna compta 701, o false en cas contrari

Exemple: user:PortaCompta701(%AcctCode)

public string PortaCompta701(XPathNodeIterator DOC1AcctCode)
{
  string resultat = "false";
  if (DOC1AcctCode.MoveNext()) 
  {
    XPathNavigator navegador = DOC1AcctCode.Current;
    XPathNodeIterator iterador = navegador.Select("//DataSource/DOC1/AcctCode[starts-with(text(),'701')]/..");
    if (iterador.MoveNext())
      resultat="true";  
  }
  return resultat;
}

Per saber si una factura porta desglòs per factura o per operació:

Torna O (Operació) o F (Factura)

Exemple: user:obtenirTipusDesglos(%DocType,%OCRD_ResidenNum,%LicTradNum,%U_B1SYS_INV_TYPE)

// Obtenir el tipus de desglos (O=Operacio F=Factura)
// Obligatoriament seran amb desglos les factures a clients extrangers o que tinguin un NIF que comenci amb N
// i que no suguin factures simplificades o assentaments resum (F2 o F4)
public string obtenirTipusDesglos(String docType,int OCRD_ResidenNum,String licTradNum, String U_B1SYS_INV_TYPE)
{
  String resultat="F";
  if ((OCRD_ResidenNum>2 | licTradNum.Substring(0,3)=="ESN") & U_B1SYS_INV_TYPE!="F2" & U_B1SYS_INV_TYPE!="F4")
    resultat="O";
  return resultat;
}

Per saber si un desglòs per operació es de tipus entrega o prestació de serveis a les factures de vendes (a les de compres es igual pero tenint en compte el grup 6):

Torna E (Entrega) o S (Prestació de serveis)

Exemple: user:obtenirNaturaTransaccio(%AcctCode)

public string obtenirNaturaTransaccio(XPathNodeIterator DOC1AcctCode)
{
  string resultat="E";
  int g70=0;
  string valor="";
  if (DOC1AcctCode.MoveNext()) 
  {
    XPathNavigator navegador = DOC1AcctCode.Current;
    XPathNodeIterator iterador = navegador.Select("//DataSource/DOC1/AcctCode");
    while (iterador.MoveNext())
	{
	  valor=iterador.Current.Value;
	  if (valor.StartsWith("70"))
	    g70=g70+1;
	}
	if (g70==0)
	  resultat="S";
  }
  return resultat;
}

Per confeccionar el concepte de les factures de vendes a partir de les comptes comptables (les de compres seríen igual però considerant el grup 6):

Torna el concepte que s’informarà al SII

Exemple: user:revisarConcepte(%AcctCode)

public string obtenirConcepte(XPathNodeIterator DOC1AcctCode)
{
  string resultat="";
  int g70=0;
  string valor="";
  if (DOC1AcctCode.MoveNext()) 
  {
    XPathNavigator navegador = DOC1AcctCode.Current;
    XPathNodeIterator iterador = navegador.Select("//DataSource/DOC1/AcctCode");
    while (iterador.MoveNext())
    {
      valor=iterador.Current.Value;
      if (valor.StartsWith("70"))
        g70=g70+1;
    }
    if (g70>0)
      resultat="Venta de mercancias";
    else
      resultat="Prestación de servicios";	
  }
  return resultat;
}

Versions del SAP Business One

9.20.180
9.20.190
9.20.210
9.30.130
9.30.140
9.30.141
9.30.150
9.30.160

Funcions SAP

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 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;
}