Beispiel #1
0
        /// <summary>
        /// Creates a business object to use for the given module
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public IBusinessObject MakeBusinessObject(string module)
        {
            // get config for the given data source
            var config = new BusinessObjectConfig(module);

            // make the business object
            try
            {
                SetModule(config.Module);
                var taskId = (int)_oSS.InvokeMethod("nLookupTask", config.TaskName);
                _oSS.InvokeMethod("nSetProgram", taskId);
                var busObject =
                    new DispatchObject(_pvx.InvokeMethod("NewObject", config.BusObjectName, _oSS.GetObject()));

                if (config.IsDetails)
                {
                    var linesBusObject = new DispatchObject(busObject.GetProperty("oLines"));
                    return(new BusinessObject(this, linesBusObject));
                }

                return(new BusinessObject(this, busObject));
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error setting business service object");
                Logger.Error(e, e.Message);
                Logger.Error(e, GetError());
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a session service based on the provided settings
        /// </summary>
        /// <param name="settings"></param>
        public SessionService(Settings settings)
        {
            try
            {
                _pvx = new DispatchObject("ProvideX.Script");
                _pvx.InvokeMethod("Init", settings.HomePath);

                _oSS = new DispatchObject(_pvx.InvokeMethod("NewObject", "SY_Session"));
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                throw;
            }

            try
            {
                _oSS.InvokeMethod("nSetUser", settings.Username, settings.Password);
                _oSS.InvokeMethod("nSetCompany", settings.CompanyCode);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                Logger.Error(e, GetError());
                throw;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets all records from the table the business object is connected to
        /// </summary>
        /// <param name="busObject"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static List <Dictionary <string, dynamic> > GetAllRecords(IDispatchObject busObject, ISessionService session)
        {
            string[] columnsObject;
            object   recordCount;

            // get metadata
            try
            {
                var metadata = Metadata.Metadata.GetMetadata(busObject, session);
                columnsObject = metadata.columnsObject;
                recordCount   = metadata.recordCount;
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting meta data for all records");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }

            // init output list
            var outList = new List <Dictionary <string, dynamic> >();

            // get information from tables
            try
            {
                // return empty if no records
                if (recordCount.ToString() == "0")
                {
                    return(new List <Dictionary <string, dynamic> >());
                }

                // go to first record
                busObject.InvokeMethod("nMoveFirst");

                do
                {
                    // add record
                    outList.Add(GetRecord(busObject, session, columnsObject));

                    // move to next record
                    busObject.InvokeMethod("nMoveNext");

                    // keep going until no more records
                } while (busObject.GetProperty("nEOF").ToString() == "0");

                // return all records
                return(outList);
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting all records");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Checks if a record exists in Sage
        /// </summary>
        /// <param name="busObject"></param>
        /// <param name="session"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public static bool RecordExists(IDispatchObject busObject, ISessionService session, Record record)
        {
            Dictionary <string, dynamic> recordObject;

            string[] keyColumnsObject = GetKeys(busObject, session);

            // convert record json into object
            try
            {
                recordObject = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(record.DataJson);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                throw;
            }

            // set key column value to enable editing of record
            try
            {
                foreach (var key in keyColumnsObject)
                {
                    if (recordObject.ContainsKey(key))
                    {
                        var keyRecord = recordObject[key];
                        busObject.InvokeMethod("nSetKeyValue", key, keyRecord.ToString());
                    }
                }

                busObject.InvokeMethod("nSetKey");
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error finding single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                return(false);
            }

            try
            {
                var retVal = busObject.InvokeMethod("nFind").ToString();
                Logger.Info($"Find: {retVal}");
                return(retVal == "1");
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error finding single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                return(false);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Gets the table metadata that the business object is connected to
        /// </summary>
        /// <returns>the columns of the table and the number of records in the table</returns>
        public static (string[] columnsObject, object recordCount) GetMetadata(IDispatchObject busObject, ISessionService session)
        {
            // get metadata
            try
            {
                var dataSources       = busObject.InvokeMethod("sGetDataSources");
                var dataSourcesObject = dataSources.ToString().Split(System.Convert.ToChar(352));

                var columns       = busObject.InvokeMethod("sGetColumns", dataSourcesObject[0]);
                var columnsObject = columns.ToString().Split(System.Convert.ToChar(352));

                var recordCount = busObject.InvokeMethod("nGetRecordCount", dataSourcesObject[0]);

                return(columnsObject, recordCount);
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting metadata");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }
        }
Beispiel #6
0
 /// <summary>
 /// Gets the keys for the current business object
 /// </summary>
 /// <param name="busObject"></param>
 /// <param name="session"></param>
 /// <returns></returns>
 public static string[] GetKeys(IDispatchObject busObject, ISessionService session)
 {
     // get key columns for current business object
     try
     {
         var keyColumns       = busObject.InvokeMethod("sGetKeyColumns");
         var keyColumnsObject = keyColumns.ToString().Split(System.Convert.ToChar(352));
         return(keyColumnsObject);
     }
     catch (Exception e)
     {
         Logger.Error(e, "Error getting keys");
         Logger.Error(e, e.Message);
         Logger.Error(e, session.GetError());
         throw;
     }
 }
Beispiel #7
0
        /// <summary>
        /// Gets the first record from the table the business object is connected to
        /// </summary>
        /// <param name="busObject"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public static Dictionary <string, dynamic> GetSingleRecord(IDispatchObject busObject, ISessionService session)
        {
            string[] columnsObject;
            object   recordCount;

            // get metadata
            try
            {
                var metadata = Metadata.Metadata.GetMetadata(busObject, session);
                columnsObject = metadata.columnsObject;
                recordCount   = metadata.recordCount;
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting meta data for single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }

            // get information from tables
            try
            {
                // return empty if no records
                if (recordCount.ToString() == "0")
                {
                    return(new Dictionary <string, dynamic>());
                }

                // go to first record
                busObject.InvokeMethod("nMoveFirst");

                return(GetRecord(busObject, session, columnsObject));
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }
        }
Beispiel #8
0
        public static string SalesOrders(IDispatchObject busObject, ISessionService session, Record record)
        {
            string _command  = "";
            string _method   = "";
            string _value    = "";
            string _variable = "";

            void SetLogParams(string method, string command, string variable, string value)
            {
                _method   = method;
                _command  = command;
                _variable = variable;
                _value    = value;
            }

            string GetErrorMessage()
            {
                var sessionError = session.GetError();

                return
                    ($"Error: {sessionError}, Method: {_method}, Command: {_command}, Variable: {_variable}, Value: {_value}");
            }

            Dictionary <string, object> recordObject;

            string[] keyColumnsObject = Metadata.Metadata.GetKeys(busObject, session);

            // convert record json into object
            try
            {
                recordObject = JsonConvert.DeserializeObject <Dictionary <string, object> >(record.DataJson);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                throw;
            }

            // set key column value to enable editing of record
            try
            {
                SetLogParams("InsertSingleRecord", "nGetNextSalesOrderNo", "", "");
                var data = new object[] { "" };
                busObject.InvokeMethodByRef("nGetNextSalesOrderNo", data);
                var keyValue = data[0].ToString();
                var key      = keyColumnsObject[0];

                SetLogParams("InsertSingleRecord", "nSetKey", key, keyValue);
                busObject.InvokeMethod("nSetKey", keyValue);

                // remove key column as it is already set
                recordObject.Remove(key);
            }
            catch (Exception e)
            {
                var error = GetErrorMessage();
                Logger.Error(e, "Error inserting single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, error);
                return(error);
            }

            // set and validate all required properties
            var recordKey = "ARDivisionNo$";

            if (recordObject.ContainsKey(recordKey))
            {
                var recordValue = recordObject[recordKey];
                if (recordValue != null)
                {
                    try
                    {
                        SetLogParams("InsertSingleRecord", "nSetValue", recordKey, recordValue.ToString());
                        busObject.InvokeMethod("nSetValue", recordKey, recordValue.ToString());
                        recordObject.Remove(recordKey);
                    }
                    catch (Exception e)
                    {
                        var error = GetErrorMessage();
                        Logger.Error(e, "Error inserting single record");
                        Logger.Error(e, e.Message);
                        Logger.Error(e, error);
                        return(error);
                    }
                }
                else
                {
                    return($"{recordKey} was null");
                }
            }
            else
            {
                return($"{recordKey} must be set");
            }

            recordKey = "CustomerNo$";
            if (recordObject.ContainsKey(recordKey))
            {
                var recordValue = recordObject[recordKey];
                if (recordValue != null)
                {
                    try
                    {
                        SetLogParams("InsertSingleRecord", "nSetValue", recordKey, recordValue.ToString());
                        busObject.InvokeMethod("nSetValue", recordKey, recordValue.ToString());
                        recordObject.Remove(recordKey);
                    }
                    catch (Exception e)
                    {
                        var error = GetErrorMessage();
                        Logger.Error(e, "Error inserting single record");
                        Logger.Error(e, e.Message);
                        Logger.Error(e, error);
                        return(error);
                    }
                }
                else
                {
                    return($"{recordKey} was null");
                }
            }
            else
            {
                return($"{recordKey} must be set");
            }

            recordKey = "LineItems";
            if (recordObject.ContainsKey(recordKey))
            {
                var recordValue = recordObject[recordKey];
                if (recordValue != null)
                {
                    try
                    {
                        var linesBusObject = new DispatchObject(busObject.GetProperty("oLines"));
                        var lineItems      = JsonConvert.DeserializeObject <List <LineItem> >(JsonConvert.SerializeObject(recordValue));

                        foreach (var lineItem in lineItems)
                        {
                            SetLogParams("InsertSingleRecord", "nSetValue", recordKey, lineItem.ItemCode);
                            linesBusObject.InvokeMethod("nAddLine");
                            linesBusObject.InvokeMethod("nSetValue", "ItemCode$", lineItem.ItemCode);
                            linesBusObject.InvokeMethod("nSetValue", "QuantityOrdered", lineItem.QuantityOrdered.ToString());
                        }

                        recordObject.Remove(recordKey);
                    }
                    catch (Exception e)
                    {
                        var error = GetErrorMessage();
                        Logger.Error(e, "Error inserting single record");
                        Logger.Error(e, e.Message);
                        Logger.Error(e, error);
                        return(error);
                    }
                }
                else
                {
                    return($"{recordKey} was null");
                }
            }
            else
            {
                return($"{recordKey} must be set");
            }

            // write out all other columns
            try
            {
                foreach (var col in recordObject)
                {
                    if (col.Value != null)
                    {
                        SetLogParams("InsertSingleRecord", "nSetValue", col.Key, col.Value.ToString());
                        busObject.InvokeMethod("nSetValue", col.Key, col.Value.ToString());
                    }
                }

                SetLogParams("InsertSingleRecord", "nWrite", "", "");
                busObject.InvokeMethod("nWrite");
            }
            catch (Exception e)
            {
                var error = GetErrorMessage();
                Logger.Error(e, "Error inserting single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, error);
                return(error);
            }

            return("");
        }
Beispiel #9
0
        /// <summary>
        /// Checks if the source system has newer data than the requested write back
        /// </summary>
        /// <param name="busObject"></param>
        /// <param name="session"></param>
        /// <param name="record"></param>
        /// <param name="schema"></param>
        /// <returns></returns>
        public static bool IsSourceNewer(IDispatchObject busObject, ISessionService session, Record record, Schema schema)
        {
            Dictionary <string, dynamic> recordObject;

            string[] columnsObject;
            string[] keyColumnsObject = GetKeys(busObject, session);

            // convert record json into object
            try
            {
                recordObject = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(record.DataJson);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                throw;
            }

            // get metadata
            try
            {
                var metadata = GetMetadata(busObject, session);
                columnsObject = metadata.columnsObject;
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error getting meta data for record date check");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }

            // set key column value
            try
            {
                var key       = keyColumnsObject[0];
                var keyRecord = recordObject[key];
                busObject.InvokeMethod("nSetKeyValue", key, keyRecord.ToString());
                busObject.InvokeMethod("nSetKey");
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error setting key for record date check");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                throw;
            }

            // move pointer to records
            try
            {
//                // get source record
//                busObject.InvokeMethod("nFind");
//                var srcRecordObject = GetRecord(columnsObject);
//
//                // get modified key from schema
//                var modifiedKey = schema.Properties.First(x => x.IsUpdateCounter);
//
//                // if source is newer than request then exit
//                if (recordObject.ContainsKey(modifiedKey.Id) && srcRecordObject.ContainsKey(modifiedKey.Id))
//                {
//                    if (recordObject[modifiedKey.Id] != null && srcRecordObject[modifiedKey.Id] != null)
//                    {
//                        return DateTime.Parse((string) recordObject[modifiedKey.Id]) <=
//                               DateTime.Parse((string) srcRecordObject[modifiedKey.Id]);
//                    }
//                }

                return(false);
            }
            catch (Exception e)
            {
                Logger.Error(e, "Error checking date for record date check");
                Logger.Error(e, e.Message);
                Logger.Error(e, session.GetError());
                return(false);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Writes an updated record back to Sage
        /// </summary>
        /// <param name="busObject"></param>
        /// <param name="session"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public static string UpdateSingleRecord(IDispatchObject busObject, ISessionService session, Record record)
        {
            string _command  = "";
            string _method   = "";
            string _value    = "";
            string _variable = "";

            void SetLogParams(string method, string command, string variable, string value)
            {
                _method   = method;
                _command  = command;
                _variable = variable;
                _value    = value;
            }

            string GetErrorMessage()
            {
                var sessionError = session.GetError();

                return
                    ($"Error: {sessionError}, Method: {_method}, Command: {_command}, Variable: {_variable}, Value: {_value}");
            }

            Dictionary <string, dynamic> recordObject;

            string[] keyColumnsObject = Metadata.Metadata.GetKeys(busObject, session);

            // convert record json into object
            try
            {
                recordObject = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(record.DataJson);
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message);
                throw;
            }

            // set key column value to enable editing of record
            try
            {
                foreach (var key in keyColumnsObject)
                {
                    SetLogParams("UpdateSingleRecord", "nSetKeyValue", key, "");
                    var keyRecord = recordObject[key];
                    SetLogParams("UpdateSingleRecord", "nSetKeyValue", key, keyRecord.ToString());
                    busObject.InvokeMethod("nSetKeyValue", key, keyRecord.ToString());
                }

                SetLogParams("UpdateSingleRecord", "nSetKey", "", "");
                busObject.InvokeMethod("nSetKey");
            }
            catch (Exception e)
            {
                var error = GetErrorMessage();
                Logger.Error(e, "Error updating single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, error);
                return(error);
            }

            // write out all other columns
            try
            {
                // remove key column as it is already set
                recordObject.Remove(keyColumnsObject[0]);

                foreach (var col in recordObject)
                {
                    if (col.Value != null)
                    {
                        SetLogParams("UpdateSingleRecord", "nSetValue", col.Key, col.Value.ToString());
                        busObject.InvokeMethod("nSetValue", col.Key, col.Value.ToString());
                    }
                }

                SetLogParams("UpdateSingleRecord", "nWrite", "", "");
                busObject.InvokeMethod("nWrite");
            }
            catch (Exception e)
            {
                var error = GetErrorMessage();
                Logger.Error(e, "Error updating single record");
                Logger.Error(e, e.Message);
                Logger.Error(e, error);
                return(error);
            }

            return("");
        }