예제 #1
0
        public ActionResult EditAsset(string tag)
        {
            AssetProcess assetProcess = new AssetProcess();
            GenericProcess <Location>   locationProcess = new GenericProcess <Location>();
            GenericProcess <Issuer>     issuerProcess   = new GenericProcess <Issuer>();
            GenericProcess <AssetType>  typeProcess     = new GenericProcess <AssetType>();
            GenericProcess <AssetStatu> statusProcess   = new GenericProcess <AssetStatu>();
            GenericProcess <AssetClass> classProcess    = new GenericProcess <AssetClass>();


            string assetTag = tag;

            vwFixAssetList assetToEdit = assetProcess.GetFixAssetByAssetTag(assetTag);

            FixAssetViewModel assetView = new FixAssetViewModel();

            assetView.AssetTag      = assetToEdit.AssetTag;
            assetView.FixAssetID    = assetToEdit.FixAssetID;
            assetView.SerialNumber  = assetToEdit.SerialNumber;
            assetView.Model         = assetToEdit.Model;
            assetView.Brand         = assetToEdit.Brand;
            assetView.AssetClassID  = assetToEdit.AssetClassID;
            assetView.AssetStatusID = assetToEdit.AssetStatusID;
            assetView.AssetTypeID   = assetToEdit.AssetTypeID;
            assetView.IssuerID      = assetToEdit.IssuerID;
            assetView.LocationID    = assetToEdit.LocationID;
            assetView.Remarks       = assetToEdit.Remarks;

            if (assetToEdit.AcquisitionDate != null)
            {
                assetView.AcquisitionDate = assetToEdit.AcquisitionDate;
            }


            if (assetToEdit.ExpiryDate != null)
            {
                assetView.ExpiryDate = assetToEdit.ExpiryDate;
            }

            ViewBag.Location    = locationProcess.GetAll().ToList();
            ViewBag.Issuer      = issuerProcess.GetAll().ToList();
            ViewBag.AssetType   = typeProcess.GetAll().ToList();
            ViewBag.AssetStatus = statusProcess.GetAll().ToList();
            ViewBag.AssetClass  = classProcess.GetAll().ToList();

            return(View(assetView));
        }
        public ActionResult UploadFile()
        {
            ExcelHelper excelHelp = new ExcelHelper();
            List <AssetAssignmentUploadModel> assignments = new List <AssetAssignmentUploadModel>();
            BulkUpload             currentUpload          = new Common.BulkUpload();
            BulkUploadProcess      bulkProcess            = new BulkUploadProcess();
            AssetProcess           assetProcess           = new AssetProcess();
            EmployeeProcess        employeeProcess        = new EmployeeProcess();
            Employee               sender        = new Employee();
            ConfigurationProcess   configProcess = new ConfigurationProcess();
            FastEmailConfiguration emailConfig   = configProcess.GetEmailConfiguration();
            AssignmentProcess      assignProcess = new AssignmentProcess();

            StringBuilder uploadLog = new StringBuilder();

            string filename         = string.Empty;
            string completeFileName = string.Empty;

            //This is for the logging
            bulkProcess.UserID     = User.Identity.Name.ToInteger();
            configProcess.UserID   = User.Identity.Name.ToInteger();
            employeeProcess.UserID = User.Identity.Name.ToInteger();
            assetProcess.UserID    = User.Identity.Name.ToInteger();
            assignProcess.UserID   = User.Identity.Name.ToInteger();

            try
            {
                #region Get Request Files
                foreach (string upload in Request.Files)
                {
                    if (!(Request.Files[upload] != null && Request.Files[upload].ContentLength > 0))
                    {
                        continue;
                    }
                    string path = HttpContext.Server.MapPath("\\App_Data\\BulkUploads");
                    filename = Path.GetFileName(Request.Files[upload].FileName);

                    //check the filename and ensure its an xlsx file
                    if (String.Compare(filename.Substring(filename.Length - 4), "xlsx", true) != 0)
                    {
                        throw new Exception("Invalid file extension.");
                    }

                    //add the current time as unique indicator
                    filename = DateTime.Now.ToFileTime().ToString() + "_" + filename;

                    // If Upload folder is not yet existing, this code will create that directory.
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    completeFileName = Path.Combine(path, filename);
                    Request.Files[upload].SaveAs(completeFileName);
                }
                #endregion

                BulkUpload newFile = new BulkUpload()
                {
                    EmployeeID   = User.Identity.Name.ToInteger(),
                    FilePath     = filename,
                    TotalRecords = 0,
                    TotalInserts = 0,
                    RequestDate  = DateTime.Now,
                    Type         = FASTConstant.BULKIPLOAD_TYPE_ASSIGNMENT
                };

                if (bulkProcess.Add(newFile) == FASTConstant.RETURN_VAL_SUCCESS)
                {
                    //get the current upload
                    currentUpload = bulkProcess.GetCurrentUpload(newFile.FilePath, newFile.EmployeeID);
                }

                if (currentUpload != null)
                {
                    #region Process the excel file
                    //Success! Lets process the file.
                    System.Data.DataTable assignmentTable = new DataTable();

                    assignmentTable = excelHelp.GetExcelDataTable(completeFileName, "SELECT * FROM [AssetAssignment$]");

                    if (assignmentTable == null)
                    {
                        throw new Exception("The upload file contains null data.");
                    }

                    assignments = assignmentTable.ToList <Models.AssetAssignmentUploadModel>();
                    sender      = employeeProcess.GetEmployeeByID(currentUpload.EmployeeID);

                    if (assignments.Count > 0)
                    {
                        int totalInserts = 0;
                        currentUpload.TotalRecords = assignments.Count;

                        bulkProcess.UpdateProcessingStep(currentUpload, FASTConstant.BULKUPLOAD_STATUS_INPROGRESS);

                        foreach (AssetAssignmentUploadModel assign in assignments)
                        {
                            //Get the Fix Asset to be added
                            Common.vwFixAssetList newAssignedAsset = assetProcess.GetFixAssetByAssetTag(assign.AssetTag);

                            AssetAssignment tempAssignment = new AssetAssignment()
                            {
                                EmployeeID         = assign.EmployeeID,
                                AssignmentStatusID = FASTConstant.ASSIGNMENT_STATUS_WT_ACCEPTANCE,
                                DateAssigned       = DateTime.Now,
                                FixAssetID         = newAssignedAsset.FixAssetID
                            };

                            if (String.Compare(newAssignedAsset.SerialNumber, assign.SerialNumber, true) == 0)
                            {
                                //totalInserts++;
                                //Try Assigning the asset to the Employee


                                //Add delay to allow sending of email
                                System.Threading.Thread.Sleep(2000);
                                uploadLog.AppendLine(String.Format("<p>{0} : {1} assigned to {2}.</p>",
                                                                   FASTConstant.SUCCESSFUL, assign.AssetTag, assign.EmployeeID.ToString()));
                            }
                            else
                            {
                                uploadLog.AppendLine(String.Format("<p>{0} : {1} not assigned to {2}. Serial Number Mismatch.</p>",
                                                                   FASTConstant.FAILURE, assign.AssetTag, assign.EmployeeID.ToString()));
                            }
                        }

                        currentUpload.TotalInserts = totalInserts;
                        bulkProcess.UpdateProcessingStep(currentUpload, FASTConstant.BULKUPLOAD_STATUS_DONE);


                        //Send email to the requestor
                        FastEmail email = new FastEmail();
                        email.Receipients = new List <string>()
                        {
                            sender.EmailAddress
                        };
                        email.Subject  = FASTConstant.EMAIL_SIMPLE_SUBJECT.Replace("[XXX]", "Fix Asset Bulk Upload Result");
                        email.HTMLBody = FASTProcess.Helper.EmailHelper.GenerateHTMLBody(FASTProcess.Helper.EmailHelper.EmailType.BULK_UPLOAD);

                        email.HTMLBody = email.HTMLBody.Replace(FASTConstant.EMAIL_RECEIPIENT_NAME, sender.FirstName + " " + sender.LastName);
                        email.HTMLBody = email.HTMLBody.Replace(FASTConstant.EMAIL_BULKUPLOAD_INFO, bulkProcess.GenerateUploadinformationHTML(currentUpload));
                        email.HTMLBody = email.HTMLBody.Replace(FASTConstant.EMAIL_BULKUPLOAD_LOG, uploadLog.ToString());
                        email.HTMLBody = email.HTMLBody.Replace(FASTConstant.EMAIL_BULKUPLOAD_SUMMARY, bulkProcess.GenerateSummaryinformationHTML(currentUpload));

                        SMTPEmail emailSender = new SMTPEmail(emailConfig, email);
                        emailSender.SendEmail();
                    }
                    else
                    {
                        bulkProcess.UpdateProcessingStep(currentUpload, FASTConstant.BULKUPLOAD_STATUS_DONE);
                    }
                    #endregion
                }

                TempData["Result"]       = "SUCCESSFUL";
                TempData["Source"]       = "File Upload";
                TempData["ExtraMessage"] = "An email will be sent to you containing the results of the upload process.";
                TempData["Action"]       = "Index";
                TempData["Controller"]   = "FixAsset";

                return(View("~/Views/Shared/Result.cshtml"));
            }
            catch (Exception ex)
            {
                TempData["Result"]       = "FAILURE";
                TempData["Source"]       = "Fix Asset Bulk Upload";
                TempData["ExtraMessage"] = ex.Message;
                return(View("~/Views/Shared/Result.cshtml"));
            }
        }