<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<BOY_41_FUNCTIONS>
  <Code>UF-183</Code>
  <Name>Import Expenses (Item Type)</Name>
  <U_BOY_RMKS>1. Modify number of segments (Line 143)2. Modify "Work Folder" in Universal Function3. Edit columns as needed or match the template</U_BOY_RMKS>
  <U_BOY_CATE />
  <U_BOY_FUNC>00000011</U_BOY_FUNC>
  <U_BOY_FI_GUIDOFCO>using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Xml;
using Boyum.Utilities.FileHandler.Model;
using SAPbobsCOM;
using System.IO;
using SBO.DI;
using SBO.UI; // Added for UI


namespace UFFITemplate
{
	public class UffiRunner : UniversalFunctions.Public.FileImporter.IImporter
	{
		double totalTax;
		public string  ExecuteQueryString(string sQuery)
		{
			SAPbobsCOM.Recordset oRecordSet = (SAPbobsCOM.Recordset) SBO.DI.Connection.SboCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
			oRecordSet.DoQuery(sQuery);
			
			if(!oRecordSet.EoF)
			{
				return oRecordSet.Fields.Item(0).Value.ToString();	
			}
			
			return "ERROR_ExeQS";
			
		}

		public FileHandlerResultEntry Import(FileHandlerParameters parameters)
		{
			try
			{
				SortedList&lt;string,string&gt; poInvoices = new SortedList&lt;string,string&gt;();
				string fileName = parameters.File;
				
				bool first = true;
				
				using (var reader = new StreamReader(fileName))
				{
							
					while (!reader.EndOfStream)
					{
						var line = reader.ReadLine();
						var values = line.Split(',');
						string temp;
						
						if(values[0] != "#")
						{
							temp = "0000" + values[0];
							temp = temp.Substring(temp.Length - 4, 4);
							poInvoices.Add(values[2] + temp, line);		
						}
						
					}
					
					string currClient = "";

					var document = B1BusinessObjects.Documents.Drafts.TemplateObject;
					var documentLines = document.Lines;
							
					foreach(var line in poInvoices)
					{
						var values = line.Value.Split(',');
						
						if (currClient != values[2] &amp;&amp; !first)
						{
														
							if (document.Add() != 0)
							{
								return new FileHandlerResultEntry(FileHandlerResultStatus.Error, GetLastError.ErrorNumberAndDescription + " Vendor:" + currClient);	
							}
							else
							{
								first = true;	
							}
							
						}
						
						if(first)
						{
							currClient = values[2];		// Controls the grouping per document in this case CardCode
							totalTax = 0;
							//
							
							document = B1BusinessObjects.Documents.Drafts.TemplateObject;
							document.DocObjectCode = BoObjectTypes.oPurchaseOrders;
							document.DocType = BoDocumentTypes.dDocument_Items;

							// Document Header
							
							document.CardCode = values[2];	 // CardCode
							document.DocDate = DateTime.Parse(values[1]);  // Posting Date
							document.DocDueDate = DateTime.Parse(values[1]);  // Due Date
							document.NumAtCard = "Daily Production: UFFI";   // Ref No
							document.Comments = "Daily Production: UFFI";   // Comments
							
							documentLines = document.Lines;
							
							first = false;
						}
						else
						{
							documentLines.Add();	
						}
						
						// Document Lines
						
						documentLines.ItemCode = values[3].ToString();					// Item Code
						documentLines.Quantity = Double.Parse(values[4]);		// Discount
						documentLines.Price = Double.Parse(values[5]); 		// Line Total
						documentLines.TaxCode = values[6];						// Tax Code
						
						// SBO.UI.Connection.SboApplication.StatusBar.SetText("Some Line", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success);
						
					}
					
					if (documentLines.Count &gt;= 1)
					{

						if (document.Add() != 0)
						{
					
							//return new FileHandlerResultEntry(FileHandlerResultStatus.Error, " Vendor1234: " + currClient + " - " + GetLastError.ErrorNumberAndDescription);
							return new FileHandlerResultEntry(FileHandlerResultStatus.Error, GetLastError.ErrorNumberAndDescription);
								
						}
	
					}
					
					SBO.UI.Connection.SboApplication.StatusBar.SetText("Import Complete. (UF-183)", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success); // Remove for UI
					
					return new FileHandlerResultEntry(FileHandlerResultStatus.Success);

				}
                			
			}
			catch (Exception e)
			{
				return new FileHandlerResultEntry(FileHandlerResultStatus.Error, "Exception caught", e);
			}
		}
	}
}</U_BOY_FI_GUIDOFCO>
  <U_BOY_FI_WORK>\\mtaylor-p50\b1_shr\Expense Import</U_BOY_FI_WORK>
  <U_BOY_FI_SUCCESS>\\mtaylor-p50\b1_shr\Expense Import\Success</U_BOY_FI_SUCCESS>
  <U_BOY_FI_ERROR>\\mtaylor-p50\b1_shr\Expense Import\Error</U_BOY_FI_ERROR>
  <U_BOY_FI_LOG>\\mtaylor-p50\b1_shr\Expense Import\Log</U_BOY_FI_LOG>
  <U_BOY_FI_FFILTER>*.csv</U_BOY_FI_FFILTER>
  <U_BOY_FI_CODEREF>System.dll;System.Data.dll;System.Xml.dll;System.Xml.Linq.dll;System.Data.DataSetExtensions.dll;System.Core.dll;Microsoft.VisualBasic.dll;Microsoft.CSharp.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\Interop.SAPbobsCOM.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\Interop.SAPbouiCOM.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\SBO_Functions.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\UniversalFunctions.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\Boyum.Utilities.dll;C:\Program Files (x86)\SAP\SAP Business One\AddOns\BOY\B1 Usability Package\SBO.DataAccess.dll;</U_BOY_FI_CODEREF>
  <BOY_41_FUNC_L01>
    <Entry>
      <Code>UF-183</Code>
      <U_BOY_ITM />
      <U_BOY_COL />
      <U_BOY_DESC />
      <U_BOY_ILL>Y</U_BOY_ILL>
      <U_BOY_VV />
      <U_BOY_ETV />
      <U_BOY_VTYP>0</U_BOY_VTYP>
    </Entry>
  </BOY_41_FUNC_L01>
</BOY_41_FUNCTIONS>