Esempio n. 1
0
        public LeadModel UpdateLeadStage(LeadModel leadModel)
        {
            //LeadModel leadModel = new LeadModel();
            int  leadId, stageId, modifyBy;
            bool isClosedWin;

            leadId      = leadModel.LeadId;
            stageId     = (int)leadModel.StageId;
            isClosedWin = (bool)leadModel.IsClosedWin;
            modifyBy    = (int)leadModel.ModifiedBy;
            RatingModel ratingModel = new RatingModel();
            Stage       stage       = new Stage();

            stage             = stageRepository.SingleOrDefault(r => r.StageId == stageId && r.RecordDeleted == false);
            leadModel.LeadId  = leadId;
            leadModel.StageId = stageId;
            int?ratingId = stage.DefaultRatingId;

            leadModel.RatingId = ratingId;
            ratingModel        = ratingBusiness.GetRatingByRatingId((int)(leadModel.RatingId == null ? 0 : leadModel.RatingId));
            bool?isLastStage = stage.IsLastStage == null ? false : stage.IsLastStage;

            leadModel        = Update(leadModel, (bool)isLastStage);
            leadModel.Rating = ratingModel;
            if (isLastStage == true)
            {
                Lead lead = leadRepository.SingleOrDefault(r => r.LeadId == leadModel.LeadId);

                if (isClosedWin == true && lead.AccountId == null)
                {//Code for Converting lead to Account as the lead is closed with win status
                    using (TransactionScope tscope = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        int     leadModuleId    = Convert.ToInt32(Utility.Enums.Module.Lead);
                        int     accountModuleId = Convert.ToInt32(Utility.Enums.Module.Account);
                        Account account         = new Account();
                        account.AccountName    = lead.LeadCompanyName ?? lead.Title;
                        account.AccountOwnerId = lead.LeadOwnerId.Value;
                        account.CompanyId      = lead.CompanyId;
                        accountRepository.Insert(account);
                        account.CreatedBy   = modifyBy;
                        account.CreatedDate = DateTime.UtcNow;
                        int accountId = account.AccountId;


                        //Code to Create Sale order automatically just after creating new order

                        SalesOrder saleaOrder = new SalesOrder();
                        saleaOrder.Subject      = lead.Title;
                        saleaOrder.AccountId    = account.AccountId;
                        saleaOrder.CompanyId    = account.CompanyId;
                        saleaOrder.OwnerId      = account.CreatedBy;
                        saleaOrder.OrderAmount  = lead.Amount;
                        saleaOrder.GrandTotal   = lead.Amount;
                        saleaOrder.CreatedBy    = account.CreatedBy;
                        saleaOrder.CreatedDate  = DateTime.UtcNow;
                        saleaOrder.ModifiedBy   = account.ModifiedBy;
                        saleaOrder.ModifiedDate = DateTime.UtcNow;
                        salesOrderRepository.Insert(saleaOrder);

                        //salesOrderRepository
                        //code for saving leadcontact to account contact
                        List <AccountContact>     accountContactList = new List <AccountContact>();
                        AccountContact            accountcontact     = new AccountContact();
                        ICollection <LeadContact> leadContacts       = new List <LeadContact>();
                        leadContacts = lead.LeadContacts;
                        foreach (var leadcontact in leadContacts)
                        {
                            accountcontact.AccountId   = accountId;
                            accountcontact.ContactId   = leadcontact.ContactId;
                            accountcontact.CreatedDate = DateTime.UtcNow;
                            accountcontact.CreatedBy   = modifyBy;
                            accountContactList.Add(accountcontact);
                        }

                        accountcontactRepository.InsertAll(accountContactList);
                        //code for saving leadDocument TO accountDocument
                        List <FileAttachment> fileAttacmentList = new List <FileAttachment>();
                        fileAttacmentList = fileAttachmentrepository.GetAll(r => r.LeadId == leadId).ToList();
                        foreach (FileAttachment file in fileAttacmentList)
                        {
                            file.AccountId = accountId;
                        }
                        fileAttachmentrepository.UpdateAll(fileAttacmentList);
                        //Code for Converting LeadActivity to account Activity
                        List <TaskItem> activityList = new List <TaskItem>();
                        activityList = taskItemRepository.GetAll(r => r.AssociatedModuleId == leadModuleId && r.AssociatedModuleValue == leadId).ToList();
                        List <TaskItem> listActivityToAdd = new List <TaskItem>();
                        foreach (TaskItem task in activityList)
                        {
                            TaskItem taskObj = new TaskItem();
                            taskObj.CompanyId             = task.CompanyId;
                            taskObj.AssociatedModuleId    = accountModuleId;
                            taskObj.AssociatedModuleValue = accountId;
                            taskObj.Description           = task.Description;
                            taskObj.DueDate     = task.DueDate;
                            taskObj.EndDate     = task.EndDate;
                            taskObj.CreatedDate = task.CreatedDate;
                            taskObj.CreatedBy   = task.CreatedBy;
                            taskObj.Subject     = task.Subject;
                            taskObj.Status      = task.Status;
                            taskObj.OwnerId     = task.OwnerId;
                            listActivityToAdd.Add(taskObj);
                        }
                        taskItemRepository.InsertAll(listActivityToAdd);
                        //Associating Lead TO NewLy created account
                        lead.AccountId   = accountId;
                        lead.ClosingDate = DateTime.UtcNow;
                        lead.IsLeadConvertedToAccount = true;
                        lead.RatingId     = ratingId;
                        lead.StageId      = stageId;
                        lead.ModifiedBy   = modifyBy;
                        lead.ModifiedDate = DateTime.UtcNow;
                        lead.IsClosed     = true;
                        leadRepository.Update(lead);
                        tscope.Complete();
                    }
                    leadModel.Rating.Icons = CommonFunctions.GetRatingImage("", true, true);
                }
                else
                {
                    leadModel.Rating.Icons        = CommonFunctions.GetRatingImage("", true, false);
                    lead.ClosingDate              = DateTime.UtcNow;
                    lead.IsLeadConvertedToAccount = false;
                    lead.RatingId     = ratingId;
                    lead.StageId      = stageId;
                    lead.ModifiedBy   = modifyBy;
                    lead.ModifiedDate = DateTime.UtcNow;
                    lead.LeadStatusId = (int?)Utility.Enums.LeadStatus.Lost;    //5 is set for lost  lead
                    lead.IsClosed     = true;
                    leadRepository.Update(lead);
                }
            }
            return(leadModel);
        }
        private bool DeleteAllAccountItems(int accountId, int userId)
        {
            //Attachements

            List <FileAttachment> accountFileAttachments = fileAttachmentRepository.GetAll(x => x.AccountId == accountId).ToList();

            for (int i = 0; i < accountFileAttachments.Count; i++)
            {
                accountFileAttachments[i].RecordDeleted = true;
            }


            //Activities
            List <TaskItem> accountTaskItems = taskItemRepository.GetAll(x => x.AssociatedModuleId == (int)Utility.Enums.Module.Account && x.AssociatedModuleValue == accountId).ToList();

            for (int i = 0; i < accountTaskItems.Count; i++)
            {
                accountTaskItems[i].RecordDeleted = true;
                accountTaskItems[i].ModifiedBy    = userId;
                accountTaskItems[i].ModifiedDate  = DateTime.UtcNow;
            }


            //SaleOrders
            List <SalesOrder> accountSalesOrders = salesOrderRepository.GetAll(x => x.AccountId == accountId).ToList();

            for (int i = 0; i < accountSalesOrders.Count; i++)
            {
                accountSalesOrders[i].RecordDeleted = true;
                accountSalesOrders[i].ModifiedBy    = userId;
                accountSalesOrders[i].ModifiedDate  = DateTime.UtcNow;
            }

            //Contacts

            List <Contact> accountContacts = contactRepository.GetAll(x => x.AccountId == accountId).ToList();

            for (int i = 0; i < accountContacts.Count; i++)
            {
                accountContacts[i].RecordDeleted = true;
                accountContacts[i].ModifiedBy    = userId;
                accountContacts[i].ModifiedDate  = DateTime.UtcNow;
            }


            //Cases
            List <AccountCase> accountCases = accountCaseRepository.GetAll(x => x.AccountId == accountId).ToList();

            for (int i = 0; i < accountCases.Count; i++)
            {
                accountCases[i].RecordDeleted = true;

                accountCases[i].ModifiedBy   = userId;
                accountCases[i].ModifiedDate = DateTime.UtcNow;
            }


            fileAttachmentRepository.UpdateAll(accountFileAttachments);
            taskItemRepository.UpdateAll(accountTaskItems);
            salesOrderRepository.UpdateAll(accountSalesOrders);
            contactRepository.UpdateAll(accountContacts);
            accountCaseRepository.UpdateAll(accountCases);

            return(true);
        }