public ActionResult Edit([Bind(Include = "CustomerId,LeaveAccId,EmployeeCode,LeaveTypeName,LeaveFromDate,LeaveToDate,NoOfDays,AppDate,IsApproved,IsDeleted,CreatedDate")] LeaveApplication leaveApplication)
        {
            try
            {
                decimal balanceLeaveM = 0;
                int     count         = (from a in db.LeaveLedgers where a.EmployeeCode == leaveApplication.EmployeeCode && a.LeaveType == leaveApplication.LeaveTypeName select a.BalanceLeaves).Count();
                var     balanceLeave  = (from a in db.LeaveLedgers where a.EmployeeCode == leaveApplication.EmployeeCode && a.LeaveType == leaveApplication.LeaveTypeName && a.BalanceLeaves >= leaveApplication.NoOfDays select a.BalanceLeaves).FirstOrDefault();


                var email = (from a in db.EmployeePIs where a.EmployeeCode == leaveApplication.EmployeeCode select a.EmpEmailId).FirstOrDefault();
                if (ModelState.IsValid)
                {
                    int lt        = Convert.ToInt32(leaveApplication.LeaveTypeName);
                    var leavetype = (from a in db.LeaveTypeMasters where a.LeaveTypeId == lt select a.LeaveTypeName).FirstOrDefault();
                    leaveApplication.LeaveTypeName = leavetype;

                    if (count == 0)
                    {
                        balanceLeaveM = (from a in db.LeaveMasters where a.LeaveTypeId == lt select a.LeaveCount).FirstOrDefault();
                        balanceLeave  = balanceLeaveM;
                    }
                    if (leaveApplication.IsApproved == true)
                    {
                        leaveApplication.Status = "Approved";
                    }
                    else
                    {
                        leaveApplication.Status = "Not Approved";
                    }
                    db.Entry(leaveApplication).State = EntityState.Modified;
                    db.SaveChanges();
                    /////////////////
                    LeaveLedger leaveledger = new Models.LeaveLedger();
                    leaveledger.EmployeeCode   = leaveApplication.EmployeeCode;
                    leaveledger.ConsumedLeaves = leaveApplication.NoOfDays;
                    leaveledger.BalanceLeaves  = balanceLeave - leaveApplication.NoOfDays;
                    leaveledger.LeaveType      = leaveApplication.LeaveTypeName;
                    DateTime appdt = Convert.ToDateTime(leaveApplication.AppDate);
                    leaveledger.FiscalYear = appdt.Year.ToString() + "-" + (appdt.Year + 1).ToString().Substring(2, 2);
                    db.LeaveLedgers.Add(leaveledger);
                    db.SaveChanges();

                    EmailModel model = new EmailModel();
                    model.Email    = "*****@*****.**";
                    model.Password = "******";
                    model.Subject  = "Leave Application Status";
                    model.Body     = "Your Leave Application is " + leaveApplication.Status;

                    model.To = email;

                    using (MailMessage mm = new MailMessage(model.Email, model.To))
                    {
                        mm.Subject = model.Subject;
                        mm.Body    = model.Body;

                        mm.IsBodyHtml = false;
                        using (SmtpClient smtp = new SmtpClient())
                        {
                            smtp.Host      = "smtp.gmail.com";
                            smtp.EnableSsl = true;

                            NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
                            smtp.UseDefaultCredentials = true;
                            smtp.Credentials           = NetworkCred;
                            smtp.Port = 587;
                            smtp.Send(mm);
                            ViewBag.Message = "Email sent.";
                        }
                    }
                    TempData["msg"] = "<script>alert('Mail is Successfully Send');</script>";
                    return(RedirectToAction("Index"));
                }
                return(View(leaveApplication));
            }
            catch (Exception ext)
            {
                TempData["msg"] = "<script>alert('Mail is failed due to Server error');</script>";
                return(RedirectToAction("Index"));
            }
        }
Пример #2
0
        public ActionResult Edit([Bind(Include = "LeaveAccId,EmployeeCode,LeaveTypeName,LeaveFromDate,LeaveToDate,NoOfDays,AppDate,IsApproved,IsDeleted,CreatedDate")] LeaveApplication leaveApplication)
        {
            try
            {
                decimal balanceLeaveM = 0;
                int     count         = (from a in db.LeaveLedgers where a.EmployeeCode == leaveApplication.EmployeeCode && a.LeaveType == leaveApplication.LeaveTypeName select a.BalanceLeaves).Count();
                var     balanceLeave  = (from a in db.LeaveLedgers where a.EmployeeCode == leaveApplication.EmployeeCode && a.LeaveType == leaveApplication.LeaveTypeName && a.BalanceLeaves >= leaveApplication.NoOfDays select a.BalanceLeaves).FirstOrDefault();


                var email = (from a in db.EmployeePIs where a.EmployeeCode == leaveApplication.EmployeeCode select a.EmpEmailId).FirstOrDefault();
                if (ModelState.IsValid)
                {
                    int lt        = Convert.ToInt32(leaveApplication.LeaveTypeName);
                    var leavetype = (from a in db.LeaveTypeMasters where a.LeaveTypeId == lt select a.LeaveTypeName).FirstOrDefault();
                    leaveApplication.LeaveTypeName = leavetype;

                    if (count == 0)
                    {
                        balanceLeaveM = (from a in db.LeaveMasters where a.LeaveTypeId == lt select a.LeaveCount).FirstOrDefault();
                        balanceLeave  = balanceLeaveM;
                    }
                    if (leaveApplication.IsApproved == true)
                    {
                        leaveApplication.Status = "Approved";
                    }
                    else
                    {
                        leaveApplication.Status = "Not Approved";
                    }
                    db.Entry(leaveApplication).State = EntityState.Modified;
                    db.SaveChanges();
                    /////////////////
                    LeaveLedger leaveledger = new Models.LeaveLedger();
                    leaveledger.EmployeeCode   = leaveApplication.EmployeeCode;
                    leaveledger.ConsumedLeaves = leaveApplication.NoOfDays;
                    leaveledger.BalanceLeaves  = balanceLeave - leaveApplication.NoOfDays;
                    leaveledger.LeaveType      = leaveApplication.LeaveTypeName;
                    DateTime appdt = Convert.ToDateTime(leaveApplication.AppDate);
                    leaveledger.FiscalYear = appdt.Year.ToString() + "-" + (appdt.Year + 1).ToString().Substring(2, 2);
                    db.LeaveLedgers.Add(leaveledger);
                    db.SaveChanges();
                    //////////////////
                    //MailMessage Msg = new MailMessage();
                    //// Sender e-mail address.
                    //Msg.From = new MailAddress(email);
                    //// Recipient e-mail address.
                    //Msg.To.Add(email);
                    //Msg.Subject = "Leave Application Status";
                    //Msg.Body = "Your Leave Application is " + leaveApplication.Status;//"some body message";
                    //SmtpClient smtp = new SmtpClient();
                    //smtp.Host = "relay-hosting.secureserver.net";// "smptout.secureserver.net";// "relay-hosting.secureserver.net";
                    //smtp.Send(Msg);

                    /////////////////New Code for Email/
                    EmailModel model = new EmailModel();
                    model.Email    = "*****@*****.**";
                    model.Password = "******";
                    model.Subject  = "Leave Application Status";
                    model.Body     = "Your Leave Application is " + leaveApplication.Status;

                    model.To = email;

                    using (MailMessage mm = new MailMessage(model.Email, model.To))
                    {
                        mm.Subject = model.Subject;
                        mm.Body    = model.Body;
                        //if (model.Attachment.ContentLength > 0)
                        //{
                        //    string fileName = Path.GetFileName(model.Attachment.FileName);
                        //    mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName));
                        //}
                        mm.IsBodyHtml = false;
                        using (SmtpClient smtp = new SmtpClient())
                        {
                            smtp.Host      = "smtp.gmail.com";
                            smtp.EnableSsl = true;

                            NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
                            smtp.UseDefaultCredentials = true;
                            smtp.Credentials           = NetworkCred;
                            smtp.Port = 587;
                            smtp.Send(mm);
                            ViewBag.Message = "Email sent.";
                        }
                    }


                    //////////////////
                    //SmtpClient smtpClient = new SmtpClient();
                    //var emailmessage = new System.Web.Mail.MailMessage()
                    //               {
                    //                   Subject = "Leave Application Status",
                    //                   Body ="Your Leave Application is "+ leaveApplication.Status,
                    //                   From = email,
                    //                   To = email,
                    //                   BodyFormat = System.Web.Mail.MailFormat.Text,
                    //                   Priority = System.Web.Mail.MailPriority.High
                    //               };

                    //smtpClient.Host = "smptout.secureserver.net";
                    //System.Web.Mail.SmtpMail.Send(emailmessage);
                    /////////////////
                    TempData["msg"] = "<script>alert('Mail is Successfully Send');</script>";



                    return(RedirectToAction("Index"));
                }


                /////////////////

                return(View(leaveApplication));
            }
            catch (Exception ext)
            {
                TempData["msg"] = "<script>alert('Mail is failed due to Server error');</script>";
                return(RedirectToAction("Index"));
            }
        }