public JsonResult ApplyDiscount(Opportunity opportunity, Guid voucherId)
        {
            var httpClient = GetHttpClient();
            var success    = OpportunityService.UpdateOpportunity(httpClient, opportunity);

            VoucherService.SetVoucherState(GetService(), voucherId);
            return(Json(success, JsonRequestBehavior.AllowGet));
        }
        public void UpdateOpportunity_Ok()
        {
            var jsonOpportunity = @"{      
                'opportunityId': 1,
		        'opportunityName': 'Squad Care alterado',
		        'opportunityRequirements': '.NET Core',
		        'desirableRequirements': 'Conhecimento em Kafka',
		        'dateRegister':'2021-05-05',
		        'closingDate':'2021-05-05',
		        'cancellationDate':'2021-05-05',
                'opportunityStatus': true
                }";

            var opportunity = JsonConvert.DeserializeObject <Opportunity>(jsonOpportunity);

            var service = new OpportunityService(_opportunityRepositoryMock.Object);

            service.UpdateOpportunity(opportunity);

            Assert.IsTrue(true);
        }
        public ActionResult ProceedWithCheckout(List <CombinedPropertyObject> combinedPropertyObjects, List <InspectionPortalQA> questions)
        {
            var httpClient = GetHttpClient();
            var service    = GetService();

            var orderId            = Guid.Empty;
            var propertyId         = Guid.Empty;
            var inspectionDetailId = Guid.Empty;

            #region Create Property
            var firstItem = combinedPropertyObjects[0];
            if (firstItem.DoCreateAnInspection)
            {
                if (propertyId == Guid.Empty)
                {
                    propertyId = InspectionService.CreateProperty(httpClient, firstItem.Property);
                    firstItem.Inspection.PropertyId = propertyId;
                }
                else
                {
                    firstItem.Inspection.PropertyId = propertyId;
                }
            }
            else
            {
                propertyId = firstItem.Property.Id;
                //firstItem.Inspection.PropertyId = propertyId;
            }
            #endregion

            /*Query order. If order does not exist, then conver opportunity to order*/
            var existingOrder = OrderService.QueryOrderByOpportunity(httpClient, combinedPropertyObjects[0].OpportunityId);
            if (existingOrder == null)
            {
                orderId = OpportunityService.ConvertOpportunityToSalesOrder(service, combinedPropertyObjects[0].OpportunityId);
            }
            else
            {
                orderId = existingOrder.Id;
            }

            #region Opportunity Update
            var opportunity = OpportunityService.QueryOpportunity(httpClient, firstItem.OpportunityId);
            if (opportunity != null)
            {
                opportunity.PropertyId = propertyId;
                OpportunityService.UpdateOpportunity(httpClient, opportunity);
                if (opportunity.StateCode == 0)
                {
                    OpportunityService.SetOpportunityState(service, firstItem.OpportunityId);
                }
            }
            #endregion

            #region Appointments, Inspection
            foreach (var cpo in combinedPropertyObjects)
            {
                if (cpo.Apppointment == null)
                {
                    continue;
                }
                var newAppointmentId = AppointmentService.CreateAppointment(httpClient, service, cpo.Apppointment);
                cpo.Inspection.AppointmentId = newAppointmentId;
                cpo.Inspection.OrderId       = orderId;
                cpo.Inspection.PropertyId    = propertyId;
                inspectionDetailId           = InspectionService.CreateInspection(httpClient, cpo.Inspection);
            }
            #endregion

            #region Order

            var newlyCreatedOrder = OrderService.QueryOrder(httpClient, orderId);
            if (newlyCreatedOrder == null)
            {
                return(Json("Error occured while retrieveing order with id: " + orderId, JsonRequestBehavior.AllowGet));
            }
            newlyCreatedOrder.PropertyId = propertyId;
            OrderService.UpdateOrder(httpClient, newlyCreatedOrder);
            #endregion

            #region Questions
            if (questions != null)
            {
                foreach (var question in questions)
                {
                    if (question == null)
                    {
                        continue;
                    }
                    question.InspectionId  = inspectionDetailId;
                    question.OrderId       = orderId;
                    question.PropertyId    = propertyId;
                    question.OpportunityId = opportunity.Id;
                    QuestionSetupService.CreateInspectionPortalQA(httpClient, question);
                    if (question.AdditionalProductId != Guid.Empty && (question.AnswerDataTypeText == "Boolean (Yes/No)" || question.AnswerDataTypeText == "Boolean (True/False)"))
                    {
                        if (question.Answer != "Yes")
                        {
                            continue;
                        }
                        var product = ProductService.QueryProduct(httpClient, question.AdditionalProductId);
                        if (product == null)
                        {
                            continue;
                        }
                        var orderProduct = new OrderProduct()
                        {
                            OrderId   = orderId,
                            Amount    = product.BuyerPays,
                            ProductId = product.Id,
                            UomId     = product.UomId
                        };
                        OrderService.CreateOrderProduct(httpClient, orderProduct);
                    }
                }
            }
            #endregion

            return(Json(newlyCreatedOrder, JsonRequestBehavior.AllowGet));
        }