Esempio n. 1
0
        protected void AddNewPlan(object sender, EventArgs e)
        {
            // Add new
            var plan = new Plan
            {
                Name = NewPlanDetail.Name,
                Duration = NewPlanDetail.Duration,
                FlagFall = NewPlanDetail.Flagfall,
                FreeCallAmount = NewPlanDetail.FreeCallAmount,
                NetworkId = NetworkId,
                PlanAmount = NewPlanDetail.PlanAmount,
                UnitOfTime = NewPlanDetail.UnitOfTime
            };

            var request = new InsertPlanRequest
            {
                Plan = plan,
                DefaultNetworkTariffId = NewPlanDetail.DefaultNetworkTariffId,
                User = Users.Current
            };

            var response = PlanService.InsertPlan(request);
            if (response.IsSuccessful)
            {
                UserMessage.MessageType = "Success";
                UserMessage.Message = "New Plan has been added.";
                NewPlanButton.Visible = false;
            }
            else
            {
                UserMessage.MessageType = "Failure";
                UserMessage.Message = response.Message;
            }
        }
Esempio n. 2
0
        public InsertPlanResponse InsertPlan(InsertPlanRequest request)
        {
            var response = new InsertPlanResponse();
            System.Threading.Thread.CurrentPrincipal = request.User.GetUserPrincipal();

            #region Validation

            if (!PermitOnlyI())
            {
                response.Message = Constants.Messages.NO_PERMISSIONS;
                return response;
            }

            // Only allow inserting of plans by I personnel
            var validationErrors = ValidateNewPlan(request.Plan);
            if (validationErrors.Count > 0)
            {
                response.Message = validationErrors[0];
                return response;
            }

            #endregion

            // Creating a new plan involves:
            // 1. Creating the new plan.
            // 2. Create a plan tariff for each network tariff that exists for the network of this plan
            // 3. Setting the default tariff for the plan.
            using (var ts = new TransactionScope())
            {
                _planRepository.InsertPlan(request.Plan);
                if (request.Plan.Id == null)
                {
                    response.IsSuccessful = false;
                    return response;
                }

                var networkTariffs = _networkTariffRepository.GetNetworkTariffsByNetworkId(request.Plan.NetworkId);
                foreach (var netTariff in networkTariffs)
                {
                    var tariff = new PlanTariff(netTariff.Id.Value)
                    {
                        UnitCost = 0,
                        IsCountedInFreeCall = netTariff.IsCountedInFreeCall,
                        PlanId = request.Plan.Id.Value
                    };
                    _planTariffRepository.InsertPlanTariff(tariff);
                    if (tariff.Id == null)
                    {
                        response.IsSuccessful = false;
                        return response;
                    }

                    if (tariff.NetworkTariffId != request.DefaultNetworkTariffId) continue;
                    request.Plan.DefaultTariffId = tariff.Id.Value;

                    if (!request.Plan.IsValid)
                    {
                        response.Message = request.Plan.GetRuleViolations().First().ErrorMessage;
                        return response;
                    }

                    if (_planRepository.UpdatePlan(request.Plan)) continue;
                    response.IsSuccessful = false;
                    return response;
                }

                ts.Complete();
                response.IsSuccessful = true;
            }

            return response;
        }