Exemple #1
0
        /// <summary>
        /// This method add the current payment in the database and update the amount of each accounts.
        /// </summary>
        /// <param name="activeAccount">This is active account</param>
        /// <param name="idAccountRecipient">This is the id of the Recipient's account</param>
        /// <param name="datePayment">This is date of the payment</param>
        /// <param name="amount">This is the amount of the payment</param>
        /// <param name="informationSent">This is the information transmitted by the customer to the other one </param>
        /// <param name="personalInformation">This is the personal information that is only see by the sender</param>
        /// <returns></returns>
        public bool addPayment(Account activeAccount, int idAccountRecipient, DateTime datePayment, decimal amount, string informationSent, string personalInformation)
        {
            ApplicationSettings settings    = JsonDataSaverReader.ReadAppSettings();
            DbConnector         dbConnector = new DbConnector(settings.ConnectionString);
            string querySelect = "SELECT ID from accounts WHERE AccountNumber = " + "'" + this.accountRecipient + "'";

            List <List <object> > queryResultSelect = dbConnector.Select(querySelect);

            if (queryResultSelect.Count == 1)
            {
                this.idAccountRecipient = Convert.ToInt32(queryResultSelect[0][0]);
                string query       = "INSERT INTO payments(`FkIDAccountOwner`,`FkIDAccountRecipient`,`Amount`,`DatePay`,`InformationTransmitted`,`PersonalInformation`) VALUES ('" + activeAccount.IdAccount.ToString() + "', '" + this.idAccountRecipient + "','" + amount + "','" + datePayment.ToString("yyyy-MM-dd-HH-mm-ss") + "','" + informationSent + "','" + personalInformation + "');";
                bool   queryResult = dbConnector.Insert(query);
                if (queryResult == false)
                {
                    return(false);
                }
                else
                {
                    string querySelectOwner = "SELECT Amount from accounts WHERE AccountNumber = " + "'" + activeAccount.AccountNumber + "'";
                    List <List <object> > queryResultSelectOwner = dbConnector.Select(querySelectOwner);
                    if (queryResultSelectOwner.Count == 1)
                    {
                        decimal amountFinalOwner  = Convert.ToDecimal(queryResultSelectOwner[0][0]) - amount;
                        string  queryUpdate       = "UPDATE accounts SET Amount = " + amountFinalOwner + " WHERE ID =" + activeAccount.IdAccount.ToString();
                        bool    queryResultUpdate = dbConnector.Update(queryUpdate);
                        if (queryResultUpdate == true)
                        {
                            string querySelectRecipient = "SELECT Amount from accounts WHERE AccountNumber = " + "'" + accountRecipient + "'";
                            List <List <object> > queryResultSelecRecipient = dbConnector.Select(querySelectRecipient);
                            if (queryResultSelecRecipient.Count == 1)
                            {
                                decimal amountFinalRecipient       = Convert.ToDecimal(queryResultSelecRecipient[0][0]) + amount;
                                string  queryUpdateRecipient       = "UPDATE accounts SET Amount = " + amountFinalRecipient + " WHERE ID =" + this.idAccountRecipient;
                                bool    queryResultUpdateRecipient = dbConnector.Update(queryUpdateRecipient);
                                if (queryResultUpdateRecipient == true)
                                {
                                    return(true);
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }
        }