Ejemplo n.º 1
0
        public JsonResult OpportunityDataGrid()
        {
            ///////////////////////////////////////////////////////////////////
            //First get users list
            var liUsers = new List <UserDto>();

            if (_authUser.ManagerUserID.HasValue && _authUser.ManagerUserID.Value != 0)
            {
                var userWManagers = _userBl.GetUsersWithManager(_authUser.ClientId);

                var tUser = userWManagers.SingleOrDefault(u => u.UserId == _authUser.UserId);
                liUsers.Add(tUser);

                var firstLevel = userWManagers.Where(u => u.ManagerUserID == _authUser.UserId).ToList();
                if (firstLevel.Count > 0)
                {
                    liUsers.AddRange(firstLevel);
                    // userWManagers.RemoveAll(x => !liUsers.Any(y => y.UserId == x.UserId));

                    Utils.GetUsers(userWManagers, liUsers);
                }
            }
            else
            {
                liUsers = _userBl.GetUsers(_authUser.ClientId);
            }
            ////////////////////////////////////////////////////////////////////


            Mapper.CreateMap <UserDto, UserModel>()
            .ForMember(dest => dest.ManagerUserID, opt => opt.MapFrom(src => src.ManagerUserID ?? 0))
            .ForMember(dest => dest.ManagerUserName, opt => opt.MapFrom(src => "-------"));
            var peopleVm = Mapper.Map <List <UserDto>, List <UserModel> >(liUsers);

            //Mapper.AssertConfigurationIsValid();

            foreach (var userModel in peopleVm.Where(userModel => userModel.ManagerUserID != null))
            {
                userModel.ManagerUserName =
                    liUsers.Where(u => u.UserId == userModel.ManagerUserID)
                    .Select(u => u.FirstName + " " + u.LastName).FirstOrDefault();
            }

            //Get opportunities for specific client
            var nList = _opportunityBl.GetOpportunities(_authUser.ClientId);

            //Get opportunity statuses for specific client
            var statuses = _utilityBL.GetStatuses(_authUser.ClientId);

            var displayOpps = (from opportunityDto in nList
                               join stat in statuses on opportunityDto.OppStatus ?? 0 equals stat.ID
                               join user in peopleVm on opportunityDto.OppOwner equals user.UserId
                               select new OpportunityModel
            {
                CRMOppID = opportunityDto.CRMOppID,
                ClientID = opportunityDto.ClientID,
                CloudLastUpdDT = opportunityDto.CloudLastUpdDT,
                CompanyName = opportunityDto.CompanyName,
                NumofQuotes = opportunityDto.NumofQuotes,
                OppCloseDate = opportunityDto.OppCloseDate,
                OppID = opportunityDto.OppID,
                OppName = opportunityDto.OppName,
                OppProbability = opportunityDto.OppProbability,
                OppStatusId = stat.ID,
                OppStatusName = stat.OppStatus,
                StageType = stat.StageType,
                SDALastUpdDT = opportunityDto.SDALastUpdDT,
                OppOwner = opportunityDto.OppOwner,
                OppOwnerName = user.FirstName + " " + user.LastName,
                Manager = user.ManagerUserName,
                QuotedAmount = opportunityDto.QuotedAmount,
                QuotedCost = opportunityDto.QuotedCost,
                QuotedMargin = opportunityDto.QuotedMargin,
                CreateDT = opportunityDto.CreateDT,
                ClientDefinedTotal1 = opportunityDto.ClientDefinedTotal1,
                ClientDefinedTotal2 = opportunityDto.ClientDefinedTotal2,
                ClientDefinedTotal3 = opportunityDto.ClientDefinedTotal3,
                ClientDefinedTotal4 = opportunityDto.ClientDefinedTotal4,
                ClientDefinedTotal5 = opportunityDto.ClientDefinedTotal5,
                ClientDefinedTotal6 = opportunityDto.ClientDefinedTotal6,
                ClientDefinedTotal7 = opportunityDto.ClientDefinedTotal7,
                ClientDefinedTotal8 = opportunityDto.ClientDefinedTotal8,
                ClientDefinedTotal9 = opportunityDto.ClientDefinedTotal9,
                ClientDefinedTotal10 = opportunityDto.ClientDefinedTotal10,
                ClientDefinedText1 = opportunityDto.ClientDefinedText1,
                ClientDefinedText2 = opportunityDto.ClientDefinedText2,
                ClientDefinedText3 = opportunityDto.ClientDefinedText3,
                ClientDefinedText4 = opportunityDto.ClientDefinedText4,
                ClientDefinedText5 = opportunityDto.ClientDefinedText5
            });

            return(Json(displayOpps, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        public Response RunUpdateCRMOpportunities(OpportunityDto opportunityDto)
        {
            // Initialise variables
            Response response = Authenticate(opportunityDto.LoginInfo);

            if (response.Errors.Count > 0)
            {
                return(response);
            }

            var opportunitiesToUpdate = opportunityDto.OpportunityTable;

            var xRefDef = opportunityDto.CRMXrefDefinition;

            CreateSqlAndMapper(xRefDef);

            try
            {
                var opportunityBL = new OpportunityBL();
                var utilityBl     = new UtilityBL();

                // Create a list of opportunities
                // mapping the values of the opportunity table with the ones on the Salesforce Opportunity object
                var recordsSdaUpdate = new List <OpportunityDto>();
                var recordsSdaAdd    = new List <OpportunityDto>();

                int crmClientId = -1;

                foreach (DataRow row in opportunitiesToUpdate.Rows)
                {
                    int clientID = -1;
                    if (row.Table.Columns.Contains("ClientID") && row["ClientID"] != DBNull.Value)
                    {
                        int.TryParse(row["ClientID"].ToString(), out clientID);
                        crmClientId = clientID;
                    }
                    else
                    {
                        response.Errors.Add("Invalid Client Id.");
                    }

                    var strCRMOppId = string.Empty;
                    if (row.Table.Columns.Contains("CRMOppID") && row["CRMOppID"] != DBNull.Value)
                    {
                        strCRMOppId = row["CRMOppID"].ToString();
                    }

                    List <OppStatusDto> lstat = utilityBl.GetStatuses(clientID);
                    bool hasStatus            = false;
                    if (row.Table.Columns.Contains("OppStatus") && row["OppStatus"] != DBNull.Value)
                    {
                        var strStatus = row["OppStatus"].ToString();
                        int statValue = 0;
                        foreach (var stat in lstat)
                        {
                            if (stat.OppStatus.ToLower().Trim().Equals(strStatus.ToLower().Trim()))
                            {
                                statValue = stat.ID;
                                break;
                            }
                        }

                        if (statValue > 0)
                        {
                            row["OppStatus"] = statValue;
                            hasStatus        = true;
                        }
                    }

                    if (!hasStatus)
                    {
                        if (lstat.Count > 0)
                        {
                            var defaultStatus = lstat.Where(s => s.Default.Equals("Y")).FirstOrDefault();
                            if (defaultStatus != null)
                            {
                                row["OppStatus"] = defaultStatus.ID;
                                hasStatus        = true;
                            }
                        }

                        if (!hasStatus)
                        {
                            response.Errors.Add("Invalid Opp Status.");
                        }
                    }


                    OpportunityDto opportunity = null;
                    if (!string.IsNullOrEmpty(strCRMOppId))
                    {
                        opportunity = opportunityBL.GetOpportunityByClientIDAndCRMOppID(clientID, strCRMOppId);
                    }

                    bool isNewOpportunity;
                    if (opportunity == null)
                    {
                        // Create a new opportunity
                        opportunity      = new OpportunityDto();
                        isNewOpportunity = true;
                    }
                    else
                    {
                        isNewOpportunity = false;
                    }

                    // Copy the fields to the opportunity
                    var type = opportunity.GetType();
                    //loop over the rows mapping the database field with the corresponding field on the CRM
                    foreach (var mappingObject in DatabaseToCRMMap)
                    {
                        var propertyInfo = type.GetProperty(mappingObject.CRMField);
                        if (propertyInfo != null)
                        {
                            if (row[mappingObject.SdaField] != DBNull.Value)
                            {
                                utilityBl.SetProperty(opportunity, propertyInfo,
                                                      row[mappingObject.SdaField]);
                            }
                        }
                    }

                    if (isNewOpportunity)
                    {
                        recordsSdaAdd.Add(opportunity);
                    }
                    else
                    {
                        recordsSdaUpdate.Add(opportunity);
                    }
                }

                // Update the list of opportunities on Salesforce
                response = new Response();
                Response responseUpdate = opportunityBL.UpdateSdaCloudOpportunity(recordsSdaUpdate);
                Response responseAdd    = opportunityBL.AddSdaCloudOpportunity(recordsSdaAdd);

                // Merge both results
                foreach (string result in responseUpdate.Results)
                {
                    response.Results.Add(result);
                }
                foreach (string result in responseAdd.Results)
                {
                    response.Results.Add(result);
                }
                //Merge both errors
                foreach (string error in responseUpdate.Errors)
                {
                    response.Errors.Add(error);
                }
                foreach (string error in responseAdd.Errors)
                {
                    response.Errors.Add(error);
                }
            }
            catch (Exception ex)
            {
                response.Errors.Add(ex.Message);
                if (ex.InnerException != null)
                {
                    response.Errors.Add(ex.InnerException.Message);
                }
            }
            return(response);
        }