Ejemplo n.º 1
0
        public static void Save(this Config config)
        {
            using (var db = new BeverageManagementEntities())
            {
                db.Entry(config).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
            var mailServer = new CustomMailServer(config.SiteName, config.ServerEmailSender, config.ServerEmailSenderPassword, config.ServerSmtpHost, config.ServerSmtpPort);

            DevMvcComponent.Mvc.Setup(config.SiteName, config.DevelopersEmails, System.Reflection.Assembly.GetExecutingAssembly(), mailServer);
            GC.Collect();
        }
 public ActionResult Edit(Employee employee)
 {
     try
     {
         if (ModelState.IsValid)
         {
             db.Entry(employee).State = System.Data.Entity.EntityState.Modified;
             db.SaveChanges(); // update
             return(RedirectToAction("Index"));
         }
     }
     catch
     {
         throw new Exception("We can't save the modified data.");
     }
     return(View(employee));
 }
        public ActionResult ProcessPayment(EmailDetailViewModel emailInfo)
        {
            var selectedEmployeesForPayment = (List <Employee>)TempData["SelectedEmployees"];

            emailInfo.EmailBody = emailInfo.EmailBody.Replace("$amount", AppConfig.Config.DefaultBeveragePrice.ToString());

            foreach (var employee in selectedEmployeesForPayment)
            {
                var dbEmployee = db.Employees.FirstOrDefault(e => e.EmployeeID == employee.EmployeeID);
                if (dbEmployee == null)
                {
                    continue;
                }
                dbEmployee.Cycle           = employee.Cycle + 1;
                dbEmployee.LastPaymentDate = DateTime.Now;
                if (ModelState.IsValid)
                {
                    db.Entry(dbEmployee).State = System.Data.Entity.EntityState.Modified;
                    History history = new History();
                    history.EmployeeID = employee.EmployeeID;
                    history.Dated      = DateTime.Now;
                    history.WeekNumber = AppConfig.Config.CurrentRunningCycle;
                    history.Amount     = (int)AppConfig.Config.DefaultBeveragePrice;
                    db.Histories.Add(history);
                }
            }
            try {
                db.SaveChanges();
            } catch {
                throw new Exception("We can't save the modified data.");
            }
            string timeStamp  = DateTime.Now.ToString("dd_MMM_yy_h_mm_ss_tt");
            string folderPath = DirectoryExtension.GetBaseOrAppDirectory() + "ExcelFiles\\";
            var    attachmentFilePathAndName = folderPath + timeStamp + ".xls";
            var    lastTwoYearsHistories     = _logic.GetLastTwoYearsHistories(DateTime.Now);

            ExcelFileCreation(lastTwoYearsHistories, attachmentFilePathAndName);

            #region Thread for mailing and excel deletion
            var thread = new Thread(() => {
                List <Attachment> attachments = new List <Attachment>()
                {
                    new Attachment(attachmentFilePathAndName)
                };
                attachments[0].Name            = AppConfig.Config.EmailAttachmentName + ".xls";
                string[] employeeEmails        = new string[1];
                employeeEmails[0]              = selectedEmployeesForPayment.FirstOrDefault().Email;
                MailSendingWrapper mailWrapper = Mvc.Mailer.GetMailSendingWrapper(employeeEmails, emailInfo.EmailSubject, emailInfo.EmailBody, null, attachments, MailingType.MailBlindCarbonCopy);
                try
                {
                    foreach (var employee in selectedEmployeesForPayment)
                    {
                        employeeEmails[0] = employee.Email;
                        mailWrapper       = Mvc.Mailer.GetMailSendingWrapper(employeeEmails, emailInfo.EmailSubject, emailInfo.EmailBody.Replace("$name", employee.Name), null, attachments,
                                                                             MailingType.MailBlindCarbonCopy);
                        Mvc.Mailer.SendMail(mailWrapper, false);
                    }
                }
                catch (Exception ex)
                {}
                finally
                {
                    mailWrapper.MailMessage.Dispose();
                    mailWrapper.MailServer.Dispose();
                    attachments[0] = null;
                    attachments    = null;
                    GC.Collect();
                    System.IO.File.Delete(attachmentFilePathAndName);
                }
            });
            #endregion

            thread.Start();
            return(RedirectToAction("Index"));
        }