Inheritance: System.Data.Objects.DataClasses.EntityObject
 /// <summary>
 /// Deprecated Method for adding a new object to the RTCCs1 EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToRTCCs1(RTCC rTCC)
 {
     base.AddObject("RTCCs1", rTCC);
 }
示例#2
0
        /// <summary>
        /// This method attempts to reserve a the initial amount of credit for a call.
        /// </summary>
        /// <param name="accountCode">The accountCode the credit should be reserved against.</param>
        /// <param name="amount">The amount of credit to reserve.</param>
        /// <param name="rate">The rate for the call destination and the values that will be used for subsequent credit reservations.</param>
        /// <param name="initialSeconds">IF the reservation is successful this parameter will hold the number of seconds that were reserved for the initial reservation.</param>
        /// <returns>True if there was enough credit for the reservation otherwise false.</returns>
        public decimal ReserveInitialCredit(string accountCode, string rateID, SIPCDR cdr, out int initialSeconds)
        {
            try
            {
                logger.Debug("ReserveInitialCredit for " + accountCode + " and rate ID " + rateID + ".");

                initialSeconds = 0;

                using (var db = new SIPSorceryEntities())
                {
                    using (var trans = new TransactionScope())
                    {
                        var rate = db.Rates.Where(x => x.ID == rateID).SingleOrDefault();

                        if (accountCode.IsNullOrBlank() || (rate == null || rate.Rate1 <= 0))
                        {
                            return(Decimal.MinusOne);
                        }

                        logger.Debug("ReserveInitialCredit for " + accountCode + ", rate " + rate.Rate1 + ", setup cost " + rate.SetupCost + ", increment seconds " + rate.IncrementSeconds + ".");

                        var customerAccount = db.CustomerAccounts.Where(x => x.AccountCode == accountCode).SingleOrDefault();

                        if (customerAccount == null)
                        {
                            logger.Debug("The initial reservation for " + accountCode + " failed due to the no matching accountcode.");
                            return(Decimal.MinusOne);
                        }

                        // Get the owning customer's RTCC billing increment.
                        //int rtccIncrement = (from cust in db.Customers where cust.Name.ToLower() == customerAccount.Owner.ToLower() select cust.RTCCBillingIncrement).Single();

                        initialSeconds = (rate.IncrementSeconds > MINIMUM_INITIAL_RESERVATION_SECONDS) ? rate.IncrementSeconds : MINIMUM_INITIAL_RESERVATION_SECONDS;

                        decimal reservationCost = ((decimal)initialSeconds / (decimal)60 * rate.Rate1) + rate.SetupCost;

                        if (customerAccount.Credit < reservationCost)
                        {
                            logger.Debug("The initial reservation for " + accountCode + ", duration " + initialSeconds + "s and " + reservationCost.ToString("0.#####") + " failed due to lack of credit.");
                            return(Decimal.MinusOne);
                        }
                        else
                        {
                            var rtccRecord = new RTCC()
                            {
                                ID               = Guid.NewGuid().ToString(),
                                CDRID            = cdr.CDRId.ToString(),
                                AccountCode      = accountCode,
                                SecondsReserved  = initialSeconds,
                                Cost             = reservationCost,
                                Rate             = rate.Rate1,
                                SetupCost        = rate.SetupCost,
                                IncrementSeconds = rate.IncrementSeconds,
                                Inserted         = DateTime.UtcNow
                            };

                            db.RTCCs1.AddObject(rtccRecord);
                            //var callCDR = (from cdr in db.CDRs where cdr.ID == cdrID select cdr).SingleOrDefault();

                            //if (callCDR == null)
                            //{
                            //    logger.Debug("The initial reservation for " + accountCode + " and " + reservationCost.ToString("0.#####") + " failed due to the no matching CDR for " + cdrID + ".");
                            //    return false;
                            //}
                            //else if (callCDR.HungupTime != null)
                            //{
                            //    logger.Debug("The initial reservation for " + accountCode + " and " + reservationCost.ToString("0.#####") + " failed due to the CDR already being hungup.");
                            //    return false;
                            //}

                            // The credit is available deduct it from the customer account balance and place it on the CDR.
                            customerAccount.Credit = customerAccount.Credit - reservationCost;

                            // Set the fields on the CDR.
                            //callCDR.Rate = rate;
                            //callCDR.SecondsReserved = seconds;
                            //callCDR.AccountCode = accountCode;
                            //callCDR.Cost = reservationCost;

                            //db.CDRs.AddObject(cdr);

                            db.SaveChanges();

                            trans.Complete();

                            logger.Debug("The initial reservation for " + accountCode + ", duration " + initialSeconds + "s and " + reservationCost.ToString("0.#####") + " was successful.");

                            return(reservationCost);
                        }
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception ReserveInitialCredit. " + excp);
                throw;
            }
        }
 /// <summary>
 /// Create a new RTCC object.
 /// </summary>
 /// <param name="id">Initial value of the ID property.</param>
 /// <param name="cDRID">Initial value of the CDRID property.</param>
 /// <param name="isHangingUp">Initial value of the IsHangingUp property.</param>
 /// <param name="setupCost">Initial value of the SetupCost property.</param>
 /// <param name="incrementSeconds">Initial value of the IncrementSeconds property.</param>
 /// <param name="inserted">Initial value of the Inserted property.</param>
 public static RTCC CreateRTCC(global::System.String id, global::System.String cDRID, global::System.Boolean isHangingUp, global::System.Decimal setupCost, global::System.Int32 incrementSeconds, global::System.DateTime inserted)
 {
     RTCC rTCC = new RTCC();
     rTCC.ID = id;
     rTCC.CDRID = cDRID;
     rTCC.IsHangingUp = isHangingUp;
     rTCC.SetupCost = setupCost;
     rTCC.IncrementSeconds = incrementSeconds;
     rTCC.Inserted = inserted;
     return rTCC;
 }
        /// <summary>
        /// This method attempts to reserve a the initial amount of credit for a call.
        /// </summary>
        /// <param name="accountCode">The accountCode the credit should be reserved against.</param>
        /// <param name="amount">The amount of credit to reserve.</param>
        /// <param name="rate">The rate for the call destination and the values that will be used for subsequent credit reservations.</param>
        /// <param name="initialSeconds">IF the reservation is successful this parameter will hold the number of seconds that were reserved for the initial reservation.</param>
        /// <returns>True if there was enough credit for the reservation otherwise false.</returns>
        public decimal ReserveInitialCredit(string accountCode, string rateID, SIPCDR cdr, out int initialSeconds)
        {
            try
            {
                logger.Debug("ReserveInitialCredit for " + accountCode + " and rate ID " + rateID + ".");

                initialSeconds = 0;

                using (var db = new SIPSorceryEntities())
                {
                    using (var trans = new TransactionScope())
                    {
                        var rate = db.Rates.Where(x => x.ID == rateID).SingleOrDefault();

                        if (accountCode.IsNullOrBlank() || (rate == null || rate.Rate1 <= 0))
                        {
                            return Decimal.MinusOne;
                        }

                        logger.Debug("ReserveInitialCredit for " + accountCode + ", rate " + rate.Rate1 + ", setup cost " + rate.SetupCost + ", increment seconds " + rate.IncrementSeconds + ".");

                        var customerAccount = db.CustomerAccounts.Where(x => x.AccountCode == accountCode).SingleOrDefault();

                        if (customerAccount == null)
                        {
                            logger.Debug("The initial reservation for " + accountCode + " failed due to the no matching accountcode.");
                            return Decimal.MinusOne;
                        }

                        // Get the owning customer's RTCC billing increment.
                        //int rtccIncrement = (from cust in db.Customers where cust.Name.ToLower() == customerAccount.Owner.ToLower() select cust.RTCCBillingIncrement).Single();

                        initialSeconds = (rate.IncrementSeconds > MINIMUM_INITIAL_RESERVATION_SECONDS) ? rate.IncrementSeconds : MINIMUM_INITIAL_RESERVATION_SECONDS;

                        decimal reservationCost = ((decimal)initialSeconds / (decimal)60 * rate.Rate1) + rate.SetupCost;

                        if (customerAccount.Credit < reservationCost)
                        {
                            logger.Debug("The initial reservation for " + accountCode + ", duration " + initialSeconds + "s and " + reservationCost.ToString("0.#####") + " failed due to lack of credit.");
                            return Decimal.MinusOne;
                        }
                        else
                        {
                            var rtccRecord = new RTCC()
                            {
                                ID = Guid.NewGuid().ToString(),
                                CDRID = cdr.CDRId.ToString(),
                                AccountCode = accountCode,
                                SecondsReserved = initialSeconds,
                                Cost = reservationCost,
                                Rate = rate.Rate1,
                                SetupCost = rate.SetupCost,
                                IncrementSeconds = rate.IncrementSeconds,
                                Inserted = DateTime.UtcNow
                            };

                            db.RTCCs1.AddObject(rtccRecord);
                            //var callCDR = (from cdr in db.CDRs where cdr.ID == cdrID select cdr).SingleOrDefault();

                            //if (callCDR == null)
                            //{
                            //    logger.Debug("The initial reservation for " + accountCode + " and " + reservationCost.ToString("0.#####") + " failed due to the no matching CDR for " + cdrID + ".");
                            //    return false;
                            //}
                            //else if (callCDR.HungupTime != null)
                            //{
                            //    logger.Debug("The initial reservation for " + accountCode + " and " + reservationCost.ToString("0.#####") + " failed due to the CDR already being hungup.");
                            //    return false;
                            //}

                            // The credit is available deduct it from the customer account balance and place it on the CDR.
                            customerAccount.Credit = customerAccount.Credit - reservationCost;

                            // Set the fields on the CDR.
                            //callCDR.Rate = rate;
                            //callCDR.SecondsReserved = seconds;
                            //callCDR.AccountCode = accountCode;
                            //callCDR.Cost = reservationCost;

                            //db.CDRs.AddObject(cdr);

                            db.SaveChanges();

                            trans.Complete();

                            logger.Debug("The initial reservation for " + accountCode + ", duration " + initialSeconds + "s and " + reservationCost.ToString("0.#####") + " was successful.");

                            return reservationCost;
                        }
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception ReserveInitialCredit. " + excp);
                throw;
            }
        }