/// <summary>
        /// Validate if the Customer won a Promotion for calculating the VAlue awarded and save it into the DB
        /// </summary>
        /// <param name="promAdv">Reference value for warning the customer if he/she won an promotion</param>
        /// <param name="valueProm">Reference value for retrieving the value won.</param>
        /// <param name="p">Promotion Enum type</param>
        /// <param name="valueRecharge">Value Recharged by the Customer</param>
        /// <returns>true if the customer won a promotion, otherwise exit method.</returns>
        private bool saveGrantedBonus(ref string promAdv, ref decimal valueProm, Promotion p, decimal valueRecharge)
        {
            try
            {
                bool bonusGranted = (valueRecharge > 0) ? true : false;
                if (bonusGranted)
                {
                    //calcule bonus
                    valueProm       = BussinessOps.calculateBonus(valueRecharge, p.ValueType, p.Value);
                    rec.Value       = valueProm;
                    rec.Date        = DateTime.Now;
                    rec.Id          = cus.Id;
                    rec.PhoneNumber = cus.PhoneNumber;
                    rec.State       = 1;
                    int i = dataAccess.phoneRecharge(rec);

                    cusBonus               = new CustomerBonus();
                    cusBonus.Id            = rec.Id;
                    cusBonus.PhoneNumber   = rec.PhoneNumber;
                    cusBonus.PromotionId   = p.Id;
                    cusBonus.ActivationDay = DateTime.Now;
                    int res = dataAccess.customerBonus(cusBonus);
                    promAdv = "You have won a recharge bonus of " + rec.Value;
                }
                return(bonusGranted);
            }
            catch (Exception ex)
            {
                throw new Exception("saveGrantedBonus Error: " + ex.Message);
            }
        }
Example #2
0
        /// <summary>
        /// Execute an incoming Call Request. Calculating:
        /// 1.Call Duration.
        /// 2.Call Cost.
        /// 3.Minutes Used.
        /// 4.Minutes Balance.
        /// </summary>
        /// <param name="data">Stream with Json Data; Object: Call Entity</param>
        /// <returns>Stream with Json Data; Object: Response Entity</returns>
        public Stream startPhoneCall(Stream data)
        {
            try
            {
                decimal callLast = 0;
                call = SerializationHelpers.DeserializeJson <Call>(data);

                cus = dataAccess.getCustomerPerPhone(call.PhoneNumber);

                cusPhone = dataAccess.getBalance(cus.Id, cus.PhoneNumber);
                if (cusPhone != null)
                {
                    if (!transValidations.validateMinLeft(cusPhone.MinuteBalance))
                    {
                        resp.idResponse = 0;
                        resp.response   = "Customer does not have minutes for making this call.";
                        resp.exception  = null;
                        return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
                    }
                }
                else
                {
                    resp.idResponse = 0;
                    resp.response   = "Customer does not have minutes for making this call.";
                    resp.exception  = null;
                    return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
                }

                Price p = dataAccess.getPrice(Convert.ToInt32(2));

                //Call simulation
                var makecall = new CallSimulatorMock();
                var callreq  = new CallSimulatorRequest {
                    toPhoneNumber = cus.PhoneNumber, fromPhoneNumber = call.DestinationNumber, startCall = DateTime.Now, minutesLet = cusPhone.MinuteBalance
                };
                var callresp = new CallSimulatorResponse();
                callresp = makecall.StartPhoneCall(callreq);

                TimeSpan duCall = BussinessOps.GetDurationCall(callresp);
                if (Convert.ToDecimal(duCall.TotalSeconds) >= (cusPhone.MinuteBalance * 60))
                {
                    callLast = cusPhone.MinuteBalance;
                    cusPhone.MinuteBalance = 0;
                    cusPhone.MinutesUsed   = callLast;
                    dataAccess.updBalance(cusPhone);
                }
                else
                {
                    callLast = Convert.ToDecimal(duCall.TotalSeconds);
                    cusPhone.MinuteBalance -= callLast / 60;
                    cusPhone.MinutesUsed   += callLast / 60;
                    dataAccess.updBalance(cusPhone);
                }

                var callCost = BussinessOps.GetCallCost(p.Prices, callLast);
                var c        = new Call {
                    Id = cus.Id, PhoneNumber = cus.PhoneNumber, DestinationNumber = call.DestinationNumber, InitialDatetime = callresp.startCall, FinalDatetime = callresp.endCall, Duration = callLast, Cost = callCost, State = callresp.answerType
                };
                var res = dataAccess.initPhoneCall(c);

                resp.idResponse = 0;
                resp.response   = "From Phone Number: " + cus.PhoneNumber + " \nTo from Phone Number: " + call.DestinationNumber + " \nStart Call DateTime: " + c.InitialDatetime.ToString() + " \nEndCall DateTime : " + c.FinalDatetime.ToString() +
                                  " \nCall Duration: " + c.Duration.ToString() + "sec. \n Call Cost: " + c.Cost + " \n Call State: " + callresp.answerDesc;
                resp.exception = null;
                return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
            }
            catch (Exception ex)
            {
                resp.idResponse = 400;
                resp.response   = "Transaction: PhoneCall; Customer does not Exist.";
                resp.exception  = ex.Message;
                return(SerializationHelpers.GenerateStreamFromString(SerializationHelpers.SerializeJson <Response>(resp)));
            }
        }