Example #1
0
        private bool ParseMemberUploadbyAces(string file, CorporateAccount corporateAccount, List <long> customerList, IEnumerable <Language> languages, IEnumerable <Lab> labs)
        {
            try
            {
                _logger.Info("Start converting data from CSV to data table :" + DateTime.Now);

                var mediaLocation = _mediaRepository.GetMemberUploadbyAcesFolderLocation();
                var fileToParse   = GetFileName(file, mediaLocation);
                var memberData    = GetRows(fileToParse);

                var corportateUpload = _memberUploadByAcesHelper.Upload(fileToParse, corporateAccount);

                if (memberData == null)
                {
                    return(false);
                }

                var rowCountInFile = memberData.Rows.Count;

                _logger.Info("Data coverted from CSV to Data Table :" + DateTime.Now + " & Row count is : " + rowCountInFile);

                if (IsAnyColumnMissing(memberData))
                {
                    return(false);
                }

                //var failedCustomers = new List<MemberUploadbyAcesFailedCustomerModel>();
                var adjustOrderCustomer = new List <EventCusomerAdjustOrderViewModel>();

                var rows = memberData.AsEnumerable();

                var orgRoleUser = new OrganizationRoleUser()
                {
                    Id = OrgRoleUserId, RoleId = OrgRoleId
                };
                var corporateModel = new CorporateUploadEditModel {
                    Tag = corporateAccount.Tag, CorporateAccountId = corporateAccount.Id, UploadCorporateId = corportateUpload.Id
                };

                var accountAdditionalFields = _accountAdditionalFieldRepository.GetAccountAdditionalFieldsEditModelByAccountId(corporateAccount.Id);
                var index = 1;
                var customerIdWithSameAcesId = new List <CustomerWithDuplicateAcesModel>();

                foreach (DataRow row in rows)
                {
                    _logger.Info(index + " out of " + rowCountInFile + " processing");

                    var customerEditModel = CreateEditModel(row);

                    if (customerEditModel == null)
                    {
                        continue;
                    }
                    var sb = new StringBuilder();

                    try
                    {
                        _logger.Info("Record:  First Name :" + customerEditModel.FirstName + ", Last Name :" + customerEditModel.LastName + " and ACES Id :" + customerEditModel.AcesId);
                        var customerWithDuplicateAcesModel = new CustomerWithDuplicateAcesModel();
                        _corporateUploadService.CorporateUploadDataRow(customerEditModel, languages, labs, null, corporateModel, accountAdditionalFields,
                                                                       adjustOrderCustomer, orgRoleUser, OrgRoleId, customerList, (long)MemberUploadSource.Aces, sb, corportateUpload.Id, out customerWithDuplicateAcesModel);

                        if (customerWithDuplicateAcesModel != null && customerWithDuplicateAcesModel.CustomerId > 0)
                        {
                            customerIdWithSameAcesId.Add(customerWithDuplicateAcesModel);
                        }
                    }
                    catch (Exception ex)
                    {
                        customerEditModel.ErrorMessage = sb.ToString();
                        if (!string.IsNullOrEmpty(ex.Message))
                        {
                            customerEditModel.ErrorMessage += " Error Message: " + ex.Message;
                        }
                        _logger.Error("While Saving customer");
                        _logger.Error(customerEditModel.ErrorMessage);
                        //var model = _memberUploadbyAcesFailedCustomerFactory.Create(customerEditModel);
                        //failedCustomers.Add(model);
                    }
                    _logger.Info(index + " out of " + rowCountInFile + " completed");

                    var memberUploadParseDetail = _memberUploadParseDetailFactory.GetDomain(customerEditModel, corportateUpload.Id);
                    _memberUploadParseDetailRepository.Save(memberUploadParseDetail);

                    index++;
                }

                if (!customerIdWithSameAcesId.IsNullOrEmpty())
                {
                    _customerWithDuplicateAcesIdFileGenerator.GenerateCsvFile(corportateUpload.Id, customerIdWithSameAcesId);
                }

                var failedRecords   = _memberUploadParseDetailRepository.GetByCorporateUploadId(corportateUpload.Id, false);
                var failedCustomers = _memberUploadParseDetailFactory.GetMemberUploadbyAcesFailedCustomerListModel(failedRecords, corportateUpload.SourceId);

                CreateFailedUploadFile(file, mediaLocation, failedCustomers, corportateUpload);

                CreateAdjustOrderUploadFile(file, mediaLocation, adjustOrderCustomer, corportateUpload);

                corportateUpload.ParseStatus = (int)MemberUploadParseStatus.Start;
                corportateUpload             = _memberUploadByAcesHelper.UpdateCorporateUpload(corportateUpload, failedCustomers.Count(), rowCountInFile);

                if (corportateUpload.SuccessfullUploadCount > 0)
                {
                    _logger.Info("Set IsTermByAbsence is True");
                    corportateUpload.IsTermByAbsence = true;
                    _corporateUploadRepository.Save(corportateUpload);

                    _logger.Info("Publishing: Corporate Upload Id: " + corportateUpload.Id);
                    _memberTermByAbsencePublisher.PublishCorporateUpload(corportateUpload.Id);
                    _logger.Info("Publish Successfully: Corporate Upload Id: " + corportateUpload.Id);
                }

                return(failedCustomers.Any() || accountAdditionalFields.Any() || customerList.Any());
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Error occurred while processing the {0}.\nMessage: {1}\n\tStackTrace: {2}", file, ex.Message, ex.StackTrace));
                return(false);
            }
        }
        public JsonResult UploadCustomers(UploadCorporateCustomers model)
        {
            var fileName          = model.FileName;
            var pageSize          = model.PageSize;
            var tag               = model.Tag;
            var failedRecordsFile = model.FailedRecordsFile;
            var adjustOrderFile   = model.AdjustOrderRecordsFile;
            var pageNumber        = model.PageNumber;
            var customTags        = model.CustomTags;

            if (customTags != null)
            {
                customTags = customTags.Where(s => !string.IsNullOrEmpty(s)).Select(s => s).ToList();
            }
            var corporateModel = new CorporateUploadEditModel {
                Tag = tag, FileName = fileName, UploadCorporateId = model.UploadCorporateId
            };

            var mediaLocation = _mediaRepository.GetUploadCsvMediaFileLocation();
            var file          = mediaLocation.PhysicalPath + corporateModel.FileName;

            var csvReader = IoC.Resolve <ICsvReader>();

            var languages = _languageRepository.GetAll();
            var labs      = _labRepository.GetAll();

            var failureRecords         = mediaLocation.PhysicalPath + failedRecordsFile;
            var adjustOrderRecordsFile = mediaLocation.PhysicalPath + model.AdjustOrderRecordsFile;

            var customerTable        = csvReader.ReadWithTextQualifier(file);
            var createdByOrgRoleUser = Mapper.Map <OrganizationRoleUserModel, OrganizationRoleUser>(_sessionContext.UserSession.CurrentOrganizationRole);

            var customerIdWithSameAcesId = new List <CustomerWithDuplicateAcesModel>();

            try
            {
                //var failedCustomers = new List<CorporateCustomerEditModel>();
                var adjustOrderForCustomerEditModel = new List <EventCusomerAdjustOrderViewModel>();

                var query = customerTable.AsEnumerable();

                var rows                    = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize);
                var customerToRender        = rows.Count();
                var corporateAccount        = _corporateAccountRepository.GetByTag(tag);
                var accountAdditionalFields = _accountAdditionalFieldRepository.GetAccountAdditionalFieldsEditModelByAccountId(corporateAccount.Id);
                var customerEditModel       = new CorporateCustomerEditModel();

                foreach (DataRow row in rows)
                {
                    var sb = new StringBuilder();
                    try
                    {
                        customerEditModel = _corporateUploadHelper.GetCorporateCustomerEditModel(row);

                        var customerWithDuplicateAcesModel = new CustomerWithDuplicateAcesModel();
                        _corporateUploadService.CorporateUploadDataRow(customerEditModel, languages, labs, customTags, corporateModel,
                                                                       accountAdditionalFields, adjustOrderForCustomerEditModel, createdByOrgRoleUser,
                                                                       _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId, null,
                                                                       (long)MemberUploadSource.CorporateUplaod, sb, model.UploadCorporateId.Value, out customerWithDuplicateAcesModel);

                        if (customerWithDuplicateAcesModel != null && customerWithDuplicateAcesModel.CustomerId > 0)
                        {
                            customerIdWithSameAcesId.Add(customerWithDuplicateAcesModel);
                        }
                    }
                    catch (Exception ex)
                    {
                        customerEditModel.ErrorMessage = sb.ToString();
                        if (!string.IsNullOrEmpty(ex.Message))
                        {
                            customerEditModel.ErrorMessage += " Error Message: " + ex.Message;
                        }
                        //failedCustomers.Add(customerEditModel);
                    }

                    var memberUploadParseDetail = _memberUploadParseDetailFactory.GetDomain(customerEditModel, model.UploadCorporateId.Value);
                    _memberUploadParseDetailRepository.Save(memberUploadParseDetail);
                }

                if (!customerIdWithSameAcesId.IsNullOrEmpty())
                {
                    _customerWithDuplicateAcesIdFileGenerator.GenerateCsvFile(model.UploadCorporateId.Value, customerIdWithSameAcesId);
                }

                corporateModel.TotalCustomers = customerTable.Rows.Count;

                var totalPages      = corporateModel.TotalCustomers / pageSize + (corporateModel.TotalCustomers % pageSize != 0 ? 1 : 0);
                var failedCustomers = _memberUploadParseDetailRepository.GetByCorporateUploadId(model.UploadCorporateId.Value, false);
                corporateModel.FailedCustomers = failedCustomers != null?failedCustomers.Count() : 0;

                corporateModel.UploadedCustomers = customerToRender - corporateModel.FailedCustomers;
                corporateModel.IsParseSucceded   = totalPages == pageNumber;

                if (corporateModel.IsParseSucceded)
                {
                    var corporateUploadedBy = _sessionContext.UserSession.FullName;

                    string corporateName = string.Empty;
                    if (corporateAccount != null)
                    {
                        var organization = _organizationRepository.GetOrganizationbyId(corporateAccount.Id);
                        if (organization != null)
                        {
                            corporateName = organization.Name;
                        }
                    }

                    var corporateUploadNotificationModel = _emailNotificationModelsFactory.GetCorporateUploadNotificationViewModel(corporateName, corporateUploadedBy, corporateModel.TotalCustomers, (corporateModel.TotalCustomers - corporateModel.FailedCustomers), corporateModel.FailedCustomers);

                    _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.CorporateUploadNotification, EmailTemplateAlias.CorporateUploadNotification, corporateUploadNotificationModel, 0, 1, "System: Corporate Upload Notification");
                    _logger.Info("Corporate Upload Notification:Mail Send for upload corporate :" + tag + " UploadedBy:  " + corporateUploadedBy + " on : " + DateTime.Now);
                }

                corporateModel.FailedRecordsFile = failedRecordsFile;

                if (failedCustomers != null && failedCustomers.Any())
                {
                    var failedMember = _memberUploadParseDetailFactory.GetCorporateCustomerListModel(failedCustomers, model.UploadCorporateId.Value);
                    _corporateUploadHelper.UpdateFailedRecords(failureRecords, failedMember);

                    if (model.LogFileId < 1)
                    {
                        var fileInfo = new FileInfo(failureRecords);

                        var files = new Core.Application.Domain.File
                        {
                            Path       = fileInfo.Name,
                            FileSize   = fileInfo.Length,
                            Type       = FileType.Csv,
                            UploadedBy = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId),
                            UploadedOn = DateTime.Now
                        };

                        files = _fileRepository.Save(files);

                        model.LogFileId = files.Id;
                    }
                }

                if (adjustOrderForCustomerEditModel.Any())
                {
                    _corporateUploadHelper.UpdateAdjustOrderRecords(adjustOrderRecordsFile, adjustOrderForCustomerEditModel);
                    if (model.AdjustOrderLogFileId < 1)
                    {
                        var fileInfo = new FileInfo(adjustOrderRecordsFile);
                        var files    = new Core.Application.Domain.File
                        {
                            Path       = fileInfo.Name,
                            FileSize   = fileInfo.Length,
                            Type       = FileType.Csv,
                            UploadedBy = new OrganizationRoleUser(_sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId),
                            UploadedOn = DateTime.Now
                        };

                        files = _fileRepository.Save(files);

                        model.AdjustOrderLogFileId = files.Id;
                    }
                }

                if (model.UploadCorporateId.HasValue && corporateModel.IsParseSucceded)
                {
                    var count = _corporateUploadHelper.FailedCustomerCount(failedRecordsFile, mediaLocation);

                    var uploadCorporate = _corporateUploadRepository.GetById(model.UploadCorporateId.Value);
                    uploadCorporate.SuccessfullUploadCount = (corporateModel.TotalCustomers - count);
                    uploadCorporate.FailedUploadCount      = count;

                    if (model.LogFileId > 0)
                    {
                        uploadCorporate.LogFileId = model.LogFileId;
                    }

                    if (model.AdjustOrderLogFileId > 0)
                    {
                        uploadCorporate.AdjustOrderLogFileId = model.AdjustOrderLogFileId;
                    }

                    uploadCorporate.ParseStatus = (int)MemberUploadParseStatus.Start;
                    _corporateUploadRepository.Save(uploadCorporate);
                }

                corporateModel.FailedCustomersListPath = _corporateUploadHelper.CheckIsFileContainsRecord(corporateModel.IsParseSucceded, mediaLocation, failedRecordsFile);
                corporateModel.AdjustOrderRecordsFile  = _corporateUploadHelper.CheckIsFileContainsRecord(corporateModel.IsParseSucceded, mediaLocation, adjustOrderFile);
            }
            catch (Exception ex)
            {
                corporateModel.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage(ex.Message);
            }

            return(Json(new
            {
                corporateModel.FileName,
                corporateModel.UploadedCustomers,
                corporateModel.Tag,
                corporateModel.FailedCustomers,
                corporateModel.IsParseSucceded,
                PageNumber = pageNumber + 1,
                corporateModel.FailedCustomersListPath,
                LogFileId = model.LogFileId,
                corporateModel.AdjustOrderRecordsFile,
                model.AdjustOrderLogFileId
            }, JsonRequestBehavior.AllowGet));
        }