/// <summary>
 /// deletes records from udic
 /// </summary>
 /// <param name="udicName"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public VisionMessage DeleteUDIC(string udicName, XElement data)
 {
     try
     {
         string _strData    = data.ToString().Replace("xmlns=\"\"", "");
         string _strMessage = service.DeleteUDIC(GetConnectionInfo(), udicName, _strData);
         return(VisionMessage.FromXML(_strMessage));
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"DeleteUDIC() failed");
         return(VisionMessage.FromException(ex));
     }
 }
 public VisionMessage DeleteRecords(string data)
 {
     try
     {
         string _strData    = data.Replace("xmlns=\"\"", "");
         string _strMessage = service.DeleteRecords(GetConnectionInfo(), _strData);
         return(VisionMessage.FromXML(_strMessage));
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"DeleteRecords() failed");
         return(VisionMessage.FromException(ex));
     }
 }
 public VisionMessage SendDataToDeltekVision(string infoCenter, string data)
 {
     try
     {
         string _strData    = data.Replace("xmlns=\"\"", "");
         string _strMessage = service.SendDataToDeltekVision(infoCenter, GetConnectionInfo(), _strData);
         return(VisionMessage.FromXML(_strMessage));
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"SendDataToDeltekVision() failed");
         return(VisionMessage.FromException(ex));
     }
 }
        /// <summary>
        /// executes a stored procedure in Vision DB
        /// </summary>
        /// <param name="sprocName">stored procedures must be named "DeltekStoredProc_XYZ", only the XYZ portion must be provided!</param>
        /// <param name="parameters">a key/value pair of parameters</param>
        /// <returns></returns>
        public VisionMessage ExecuteStoredProcedure(string sprocName, Dictionary <string, object> parameters = null)
        {
            try
            {
                var _parameterXML = ToParameterXML(parameters);
                var _retval       = service.ExecuteStoredProcedure(GetConnectionInfo(), sprocName, _parameterXML.ToString());

                return(VisionMessage.FromXML(_retval));
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"ExecuteStoredProcedure({sprocName}) failed");
                return(VisionMessage.FromException(ex));
            }
        }
        /// <summary>
        /// get records by query
        /// </summary>
        /// <param name="infoCenter"></param>
        /// <param name="query"></param>
        /// <param name="recordDetail"></param>
        /// <param name="rowAccess"></param>
        /// <param name="chunkSize"></param>
        /// <param name="tableInfo"></param>
        /// <returns></returns>
        public VisionMessage GetRecordsByQuery(string infoCenter,
                                               string query,
                                               RecordDetail recordDetail = RecordDetail.Empty,
                                               bool rowAccess            = false,
                                               int chunkSize             = 100,
                                               XElement tableInfo        = null)
        {
            try
            {
                VisionMessage _retval    = new VisionMessage();
                int           _nextChunk = 1;
                bool          _lastChunk = false;
                string        _sessionId = "";
                string        _connInfo  = GetConnectionInfo();
                XElement      _xQuery    = new XElement("Queries", new XElement("Query", query, new XAttribute("ID", 1)));

                do
                {
                    string _xmlretval = service.GetRecordsByQuery(
                        _connInfo,
                        GetInfoCenterXML(infoCenter, rowAccess, _nextChunk, chunkSize, tableInfo),
                        _xQuery.ToString(),
                        recordDetail.GetValueName()
                        );

                    //read loop moved into separate method...
                    _lastChunk = ReadResult(ref _sessionId, _xmlretval, ref _retval);
                    if (string.IsNullOrEmpty(_sessionId) == false)
                    {
                        _connInfo = GetVisionConnInfoXML(database, username, password, _sessionId);
                    }
                    _nextChunk += 1;
                } while (_lastChunk == false);

                return(_retval);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"GetRecordsByQuery() with query [{query}] call failed");
                return(VisionMessage.FromException(ex));
            }
        }
        /// <summary>
        /// posts one or more batches into vision
        /// </summary>
        /// <returns></returns>
        public VisionMessage PostTransaction(TransactionType transType, string batchList, int period)
        {
            try
            {
                string _message = service.PostTransaction(GetConnectionInfo(), transType.GetValueName(), batchList, period);

                //catch special case when the return value does not contain any xml
                if (_message.Contains("<") == false)
                {
                    return(new VisionMessage("1", "Posting Successful", _message));
                }
                else
                {
                    return(VisionMessage.FromXML(_message));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"PostTransaction() failed for Transaction Type {transType.GetValueName()}");
                return(VisionMessage.FromException(ex));
            }
        }
        /// <summary>
        /// retrieves record by UID
        /// </summary>
        /// <param name="udicName"></param>
        /// <param name="id"></param>
        /// <param name="recordDetail"></param>
        /// <returns></returns>
        public VisionMessage GetUDICRecordsByKey(string udicName, string id, RecordDetail recordDetail = RecordDetail.Empty)
        {
            try
            {
                VisionMessage _retval    = new VisionMessage();
                int           _nextChunk = 1;
                bool          _lastChunk = false;
                string        _sessionId = "";
                string        _connInfo  = GetConnectionInfo();

                do
                {
                    string _xmlretval = service.GetUDICByKey(
                        _connInfo,
                        udicName,
                        id,
                        recordDetail.GetValueName()
                        );

                    //read loop moved into separate method...
                    _lastChunk = ReadResult(ref _sessionId, _xmlretval, ref _retval);
                    if (string.IsNullOrEmpty(_sessionId) == false)
                    {
                        _connInfo = GetVisionConnInfoXML(database, username, password, _sessionId);
                    }
                    _nextChunk += 1;
                } while (_lastChunk == false);

                return(_retval);
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"GetUDICRecordsByKey() for {udicName} with key [{id}] call failed");
                return(VisionMessage.FromException(ex));
            }
        }
        /// <summary>
        /// available transaction types
        /// </summary>



        public VisionMessage AddTransaction(TransactionType transType, XElement data)
        {
            try
            {
                string _connInfo = GetConnectionInfo();
                string _strData  = data.ToString().Replace("xmlns=\"\"", "");
                string _message  = "";

                switch (transType)
                {
                case TransactionType.AP:
                    _message = service.AddAPVouchersTransaction(_connInfo, _strData);
                    break;

                case TransactionType.CD:
                    _message = service.AddCashDisbTransaction(_connInfo, _strData);
                    break;

                case TransactionType.CR:
                    _message = service.AddCashReceiptsTransaction(_connInfo, _strData);
                    break;

                case TransactionType.CV:
                    _message = service.AddAPDisbursementsTransaction(_connInfo, _strData);
                    break;

                case TransactionType.ER:
                    _message = service.AddEmpRepaymentTransaction(_connInfo, _strData);
                    break;

                case TransactionType.EX:
                    _message = service.AddEmpExpenseTransaction(_connInfo, _strData);
                    break;

                case TransactionType.IN:
                    _message = service.AddInvoiceTransaction(_connInfo, _strData);
                    break;

                case TransactionType.JE:
                    _message = service.AddJournalEntryTransaction(_connInfo, _strData);
                    break;

                case TransactionType.LA:
                    _message = service.AddLaborAdjustTransaction(_connInfo, _strData);
                    break;

                case TransactionType.MI:
                    _message = service.AddMiscTransaction(_connInfo, _strData);
                    break;

                case TransactionType.PR:
                    _message = service.AddPrintsReproTransaction(_connInfo, _strData);
                    break;

                case TransactionType.TS:
                    _message = service.AddTimesheetTransaction(_connInfo, _strData);
                    break;

                case TransactionType.UN:
                    _message = service.AddUnitTransaction(_connInfo, _strData);
                    break;

                case TransactionType.UP:
                    _message = service.AddUnitByProjectTransaction(_connInfo, _strData);
                    break;

                default:
                    break;
                }

                return(VisionMessage.FromXML(_message));
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"AddTransaction() failed for Transaction Type {transType.GetValueName()}");
                return(VisionMessage.FromException(ex));
            }
        }