Esempio n. 1
0
 public static ForexTransactionModel ForexWriteTransaction(ForexTransactionModel transaction)
 {
     if (transaction != null)
     {
         fConnection.Open();
         var result = fConnection.ExecuteScalar <int>("INSERT INTO forex_transaction " +
                                                      "( account_year, forex_type, forex_oid, foreign_amount, exchange_rate, national_amount, journal_date_time, userID, isPOStransaction ) VALUES(" +
                                                      "@account_year," +
                                                      "@forex_type, " +
                                                      "@forex_oid, " +
                                                      "@foreign_amount, " +
                                                      "@exchange_rate, " +
                                                      "@national_amount, " +
                                                      "@journal_date_time, " +
                                                      "@UserID, " +
                                                      "@isPOStransaction); select LAST_INSERT_ID(); ", transaction);
         fConnection.Close();
         if (result <= 0)
         {
             throw new Exception("Nothing was written on ForexDB");
         }
         transaction.oid = result;
         return(transaction);
     }
     else
     {
         return(null);
     }
 }
Esempio n. 2
0
 public static string ForexWriteAccountData(ForexTransactionModel transaction)
 {
     fConnection.Open();
     using (var cmd = fConnection.CreateCommand())
     {
         var result = fConnection
                      .Query <ForexTransactionModel>("INSERT INTO forex_transaction(" +
                                                     " ", new { _transaction = transaction });
         fConnection.Close();
         return(result.ToString());
     }
 }
Esempio n. 3
0
        public static List <QueueModel> QueueGet()
        {
            string query = "" +
                           "SELECT " +
                           "QId," +
                           "IDU," +
                           "oid," +
                           "account_year," +
                           "forex_type," +
                           "forex_oid," +
                           "foreign_amount," +
                           "exchange_rate," +
                           "national_amount," +
                           "journal_date_time," +
                           "IFNULL(userID,'n/a')," +
                           "isPOStransaction " +
                           "FROM Queue ORDER BY Oid LIMIT 10";
            //var result = qConnection.Query<QueueModel>(query, new DynamicParameters()).ToList();
            var queue = new List <QueueModel>();

            using (var reader = qConnection.ExecuteReader(query))
            {
                //ForexTransactionModel transaction = null;
                while (reader.Read())
                {
                    ForexTransactionModel transaction = new ForexTransactionModel
                    {
                        oid               = reader.GetInt32(2),
                        account_year      = reader.GetInt32(3),
                        forex_type        = reader.GetInt32(4),
                        forex_oid         = reader.GetInt32(5),
                        foreign_amount    = reader.GetDecimal(6),
                        exchange_rate     = reader.GetDouble(7),
                        national_amount   = reader.GetDecimal(8),
                        journal_date_time = reader.GetDateTime(9),
                        userID            = reader.GetString(10),
                        isPOStransaction  = reader.GetInt32(11),
                    };

                    queue.Add(new QueueModel
                    {
                        QId         = reader.GetInt32(0),
                        IDU         = reader.GetString(1),
                        Transaction = transaction
                    });
                }
            }

            return(queue);
        }
Esempio n. 4
0
        private async void PullConsoleAction(object sender, ElapsedEventArgs e)
        {
            Log.Verbose("PullConsoleAction: scheduled start");
            try
            {
                //Pull actions from remote ConsoleAPI
                var responseBody = await ApiHelper.ActionsGet();

                var logmsg = $"PullConsoleAction: fetched {responseBody.Count} actions from console";
                if (responseBody.Count > 0)
                {
                    Log.Information(logmsg);
                }
                else
                {
                    Log.Debug(logmsg);
                }
                //iterate through actions
                foreach (ActionsGetReply res in responseBody)
                {
                    //parse params
                    Log.Debug($"PullConsoleAction: processing action {res.POSActionQueueId}");
                    ActionSendToPosParams resparams = JsonConvert.DeserializeObject <ActionSendToPosParams>(res.POSActionParams);
                    Log.Verbose($"PullConsoleAction: processing action {res.action} {res.POSActionQueueId}.Params: amount={resparams.amount},curr={resparams.currency},exch={resparams.exchangerate}");
                    ForexTransactionModel transaction = null;
                    switch (res.action)
                    {
                    case "sendtopos":
                        //write on ForexDB
                        transaction = new ForexTransactionModel {
                            account_year      = DateTime.Now.Year,
                            forex_type        = 3, //Deposit
                            forex_oid         = resparams.currency,
                            foreign_amount    = resparams.amount,
                            exchange_rate     = resparams.exchangerate,
                            national_amount   = 0,
                            journal_date_time = DateTime.Now,
                            userID            = "console",
                            isPOStransaction  = 0
                        };
                        break;

                    case "CHFtransfer":
                        transaction = new ForexTransactionModel {
                            account_year      = DateTime.Now.Year,
                            forex_type        = 2,                 //National currency IN
                            forex_oid         = 1,                 //always CHF for POS transactions
                            foreign_amount    = 0,                 //always 0
                            exchange_rate     = 0,                 //always 0
                            national_amount   = -resparams.amount, // negative amount is a workaround ! ;)
                            journal_date_time = DateTime.Now,
                            userID            = "console",
                            isPOStransaction  = 1
                        };
                        break;

                    default:
                        Log.Error($"PullConsoleAction: action {res.action} is unknown");
                        break;
                    }
                    if (transaction == null)
                    {
                        continue;
                    }
                    //Write transaction on ForexDB and then on local Queue to Sync remote DB.
                    //No need to check reply, this call raises an error in case http request fails
                    await ApiHelper.QueueAdd(new QueueModel { IDU = "I", Transaction = Store.ForexWriteTransaction(transaction) });

                    Log.Debug($"PullConsoleAction: Desposit transaction inserted in ForexDB and added to Queue");
                    //Feedback to console to remove Actions from remote queue
                    await ApiHelper.ActionAck(new ActionAck { POSActionQueueId = res.POSActionQueueId });

                    Log.Debug($"PullConsoleAction: action {res.POSActionQueueId} acknowledged to console");
                }
            }
            catch (HttpRequestException ex)
            {
                var    inner        = ex.InnerException;
                string StackMessage = "Error fetching actions from console: Api request error: " + ex.Message;
                while (inner != null)
                {
                    StackMessage += ": " + inner.Message;
                    inner         = inner.InnerException;
                }
                Log.Error(StackMessage);
            }
            catch (MySqlException ex)
            {
                string errmsg = "Error fetching actions from console: ForexDB access error. ";
                if (!Utils.MySQLIsRunning())
                {
                    errmsg += " MySQL service is not running, cannot write data to local Forex DB.";
                }
                else
                {
                    errmsg += ex.Message + ex.StackTrace;
                }
                Log.Error(errmsg);
            }
            catch (Exception ex) {
                var    inner      = ex.InnerException;
                string stacktrace = "Error fetching actions from console: " + ex.Message + ex.StackTrace;
                while (inner != null)
                {
                    stacktrace += inner.StackTrace;
                    inner       = inner.InnerException;
                }
                Log.Error(stacktrace);
            }
        }