using Fab2ApprovalSystem.DMO;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Fab2ApprovalSystem.Models;
using Fab2ApprovalSystem.Misc;
using System.IO;
using System.Reflection;
using System.Configuration;
using Fab2ApprovalSystem.ViewModels;



namespace Fab2ApprovalSystem.Controllers
{
    [Authorize]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    [SessionExpireFilter]
    public class LotDispositionController : Controller
    {
       
        
        
        
        // GET: /LotDisposition/
        LotDispositionDMO ldDMO = new LotDispositionDMO();
        WorkflowDMO wfDMO = new WorkflowDMO();
        
        
        // GET: /LotDisposition/Create
        public ActionResult Create()
        {
            //excel.FileName = "";
            //var lot = 
            LotDisposition lotDispo = new LotDisposition();
            try
            {
                // insert a records to get the issueID
                
                lotDispo.OriginatorID = (int)Session[GlobalVars.SESSION_USERID];
                ldDMO.InsertLotDisposition(lotDispo);
                return RedirectToAction("Edit", new { issueID = lotDispo.IssueID.ToString() });
            }

            catch(Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\nCreat Lot Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = lotDispo.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                return View("Error");
            }
        }

       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lot"></param>
        /// <returns></returns>
        public JsonResult AddLot(Lot lot)
        {
            Lot l = lot;
            MRB_DMO mrbDMO = new MRB_DMO();
            IssueWithExistingLotsViewModel issueWEL = new IssueWithExistingLotsViewModel();
            try
            {
                
                lot.LotStatusOption.LotStatusOptionID = l.LotStatusOptionID;
                lot.LotStatusOption.LotStatusOption = l.LotStatusOptionName;
                ldDMO.InsertLot(lot, false);

                //if (!mrbDMO.DoesMRBLotExist(lot.LotNumber))
                //{
                    //get All the MRBs associated to the Parent lot     
                    //insert the lot into the MRBChildLotNotInMRB table and NOT in the MRB Lot table for each MRB
                    //ldDMO.InsertChildLot_NotInTheMRB(lot.LotNumber);

               //}
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + lot.IssueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Add Lot Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = l.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                throw new Exception(e.Message);
            }
            return Json(l, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Add array of lots
        /// </summary>
        /// <param name="lot"></param>
        /// <returns></returns>
        //public void AddLots(string lotNumbers, int issueID)
        //{

        //    try
        //    {
        //        if (lotNumbers.Length > 0)
        //        {
        //            string[] tempLots = lotNumbers.Split(new char[] { '~' });
        //            foreach (string lotNumber in tempLots)
        //            {
        //                Lot l = new Lot();
        //                l.LotNumber = lotNumber;
        //                l.IssueID = issueID;
        //                l.LotStatusOption.LotStatusOptionID = (int)GlobalVars.LotStatusOption.NotAvailable;
        //                ldDMO.InsertLot(l, true);
        //            }
        //        }

        //    }
        //    catch (Exception e)
        //    {
        //        string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + issueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
        //        Functions.WriteEvent(@User.Identity.Name + "\r\n AddLots Disposition\r\n" + e.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
        //        EventLogDMO.Add(new WinEventLog() { UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
        //        throw new Exception(e.Message);

        //    }
        //}


        public JsonResult AddLots(string lotNumbers, int issueID)
        {
            MRB_DMO mrbDMO = new MRB_DMO();
            bool existingLotUpdated;
            IssueWithExistingLotsViewModel issueWEL = new IssueWithExistingLotsViewModel();
            try
            {
                if (lotNumbers.Length > 0)
                {
                    string[] tempLots = lotNumbers.Split(new char[] { '~' });
                    foreach (string lotNumber in tempLots)
                    {
                        Lot l = new Lot();
                        l.LotNumber = lotNumber;
                        l.IssueID = issueID;
                        l.LotStatusOption.LotStatusOptionID = (int)GlobalVars.LotStatusOption.NotAvailable;
                        ldDMO.InsertLot(l, true);
                        if (l.OpenIssueWithExistingLots != 0)
                        {
                            issueWEL.LotList += l.LotNumber + "~";
                            issueWEL.IssuesList += l.OpenIssueWithExistingLots + "~";
                        }

                        
                    }

                    //foreach (string lotNumber in tempLots)
                    //{
                        //===================================================================
                        // ONLY DO THIS PART IF THE LOT DOES NOT EXIST IN THE 
                        //"MRBLot" TABLE AND THE "MRBChildLotNotInMRB" TABLE
                        //===================================================================
 
                        //if (!mrbDMO.DoesMRBLotExist(lotNumber))
                        //{
                        //    //get All the MRBs associated to the Parent lot     
                        //    //insert the lot into the MRBChildLotNotInMRB table and NOT in the MRB Lot table for each MRB
                        //    ldDMO.InsertChildLot_NotInTheMRB(lotNumber);
                            
                        //}
                        
                   // }


                }

            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + issueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AddLots Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                throw new Exception(e.Message);

            }

            return Json(issueWEL, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /LotDisposition/Edit/5
        public ActionResult Edit(int issueID)
        {
            int isITARCompliant = 1;
            try
            {
                LotDisposition lotDispo = new LotDisposition();
                lotDispo = ldDMO.GetLotDispositionItemForRead(issueID, out isITARCompliant, (int) Session[GlobalVars.SESSION_USERID]);

                if (isITARCompliant == 0) // not ITAR Compliant
                {
                    return View("UnAuthorizedAccess");
                }
                else
                {
                    if (lotDispo.CloseDate != null)
                    {
                        return RedirectToAction("ReadOnly", new { issueID = issueID });
                    }
                    if (lotDispo.CurrentStep == 1)
                    {
                        List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(issueID, lotDispo.CurrentStep, (int)GlobalVars.DocumentType.LotDisposition);
                        //ApproversListViewModel approver = userList.Find(delegate(ApproversListViewModel al) { return al.UserID == GlobalVars.USER_ID; });
                        ApproversListViewModel approver = userList.Find(delegate(ApproversListViewModel al) { return al.UserID == (int)Session[GlobalVars.SESSION_USERID]; });
                        if (approver == null)
                            ViewBag.IsApprover = "false";
                        else
                            ViewBag.IsApprover = "true";

                        if (approver == null && (!(bool)Session[GlobalVars.IS_ADMIN]) && (int)Session[GlobalVars.SESSION_USERID] != lotDispo.OriginatorID )
                        {

                            //return RedirectToAction("ReadOnly", lotDispo);
                            return RedirectToAction("ReadOnly", new { issueID = issueID });
                        }
                        else
                        {
                            if ((ViewBag.IsApprover == "true" || (bool)Session[GlobalVars.IS_ADMIN])
                                || (int)Session[GlobalVars.SESSION_USERID] == lotDispo.OriginatorID)
                            {
                                lotDispo = ldDMO.GetLotDispositionItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                                if (lotDispo.RecordLockIndicator && lotDispo.RecordLockedBy == (int)Session[GlobalVars.SESSION_USERID])
                                {
                                    PopulateLotStatusOptions();
                                    ViewBag.OriginatorList = ldDMO.GetUserList();
                                    ViewBag.deps = ldDMO.GetDepartments();
                                    ViewBag.ResponsibilityList = ldDMO.GetResponsibilityList();
                                    ViewBag.ResponsibilityIssueList = ldDMO.GetResponsibilityIssueList(lotDispo.ResponsibilityID);

                                    return View(lotDispo);
                                }
                                else
                                {
                                    return RedirectToAction("ReadOnly", new { issueID = issueID });
                                }
                            }
                            else
                            {
                                return RedirectToAction("ReadOnly", new { issueID = issueID });
                            }                           
                        }
                    }
                    else if (lotDispo.CurrentStep > 1)
                    {

                        List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(issueID, lotDispo.CurrentStep, (int)GlobalVars.DocumentType.LotDisposition);
                        //ApproversListViewModel approver = userList.Find(delegate(ApproversListViewModel al) { return al.UserID == GlobalVars.USER_ID; });
                        ApproversListViewModel approver = userList.Find(delegate(ApproversListViewModel al) { return al.UserID == (int)Session[GlobalVars.SESSION_USERID]; }); 
                        if (approver == null)
                        {
                            ViewBag.IsApprover = "false";
                            
                        }
                        else
                        {
                            ViewBag.IsApprover = "true";
                            
                        }

                        if (approver == null && (!(bool)Session[GlobalVars.IS_ADMIN]) && (int)Session[GlobalVars.SESSION_USERID] != lotDispo.OriginatorID)
                        {
                            return RedirectToAction("ReadOnly", new { issueID = issueID });
                        }
                        else
                        {
                           
                            //// Check the record Lock indicator
                            //if ((lotDispo.RecordLockIndicator && lotDispo.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]) || (ViewBag.IsApprover == "false"))
                            //{
                            //    return RedirectToAction("ReadOnly", new { issueID = issueID });
                            //}
                            //else
                            //    return RedirectToAction("EditStep", new { issueID = issueID });


                            if ((ViewBag.IsApprover == "true" || (bool)Session[GlobalVars.IS_ADMIN])
                               || (int)Session[GlobalVars.SESSION_USERID] == lotDispo.OriginatorID)
                            {
                                return RedirectToAction("EditStep", new { issueID = issueID });

                            }
                            else
                            {
                                return RedirectToAction("ReadOnly", new { issueID = issueID });
                            }

                        }
                          
                    }
                    else
                    {
                        // TODO
                        // Check the recordlock indicator
                        if ((lotDispo.RecordLockIndicator && lotDispo.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]) || (ViewBag.IsApprover == "false"))
                        {
                            return RedirectToAction("ReadOnly", new { issueID = issueID });
                        }
                        else
                        {
                            lotDispo = ldDMO.GetLotDispositionItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                            PopulateLotStatusOptions();
                            ViewBag.OriginatorList = ldDMO.GetUserList();
                            ViewBag.deps = ldDMO.GetDepartments();
                            ViewBag.ResponsibilityList = ldDMO.GetResponsibilityList();
                            ViewBag.ResponsibilityIssueList = ldDMO.GetResponsibilityIssueList(lotDispo.ResponsibilityID);

                            return View(lotDispo);
                        }
                    }
                }
            }

            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\nEdit Lot Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                return View("Error");
            }
        
        }


        //
        // POST: /LotDisposition/Edit/5
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lotDispo"></param>
        [HttpPost]
        public void Edit(LotDisposition lotDispo)
        {
            try
            {
                ldDMO.UpdateLotDisposition(lotDispo);
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + lotDispo.IssueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n UpdatEdit Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = lotDispo.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                throw new Exception(e.Message);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lotDispo"></param>
        /// <returns></returns>
        public ActionResult ReadOnly(int issueID)
        {
            int isITARCompliant = 1;
            try
            {
                LotDisposition lotDispo = new LotDisposition();
                lotDispo = ldDMO.GetLotDispositionItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                PopulateLotStatusOptions();
                ViewBag.OriginatorList = ldDMO.GetUserList();
                ViewBag.deps = ldDMO.GetDepartments();
                ViewBag.ResponsibilityList = ldDMO.GetResponsibilityList();
                ViewBag.ResponsibilityIssueList = ldDMO.GetResponsibilityIssueList(lotDispo.ResponsibilityID);


                return View(lotDispo);
            }

            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + issueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n ReadOnly Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                throw new Exception(e.Message);

            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult EditStep(int issueID)
        {
            int isITARCompliant = 1;
            try
            {
               


                LotDisposition lotDispo = new LotDisposition();
                lotDispo = ldDMO.GetLotDispositionItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(issueID, lotDispo.CurrentStep, (int)GlobalVars.DocumentType.LotDisposition);
                ApproversListViewModel appUser = userList.Find(delegate(ApproversListViewModel al) { return al.UserID == (int)Session[GlobalVars.SESSION_USERID]; });
                if (appUser == null)
                    ViewBag.IsApprover = "false";
                else
                    ViewBag.IsApprover = "true";

                
                // Check the recordlock indicator

                //if ((lotDispo.RecordLockIndicator && lotDispo.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]))
                //{
                //    return RedirectToAction("ReadOnly", new { issueID = issueID });
                //}
                //else
                //{
                //    PopulateLotStatusOptions();
                //    ViewBag.OriginatorList = ldDMO.GetUserList();
                //    ViewBag.deps = ldDMO.GetDepartments();
                //    ViewBag.ResponsibilityList = ldDMO.GetResponsibilityList();
                //    ViewBag.ResponsibilityIssueList = ldDMO.GetResponsibilityIssueList(lotDispo.ResponsibilityID);
                //}


                if ((ViewBag.IsApprover == "true" || (bool)Session[GlobalVars.IS_ADMIN]))
                {
                    lotDispo = ldDMO.GetLotDispositionItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                    if (lotDispo.RecordLockIndicator && lotDispo.RecordLockedBy == (int)Session[GlobalVars.SESSION_USERID])
                    {
                        PopulateLotStatusOptions();
                        ViewBag.OriginatorList = ldDMO.GetUserList();
                        ViewBag.deps = ldDMO.GetDepartments();
                        ViewBag.ResponsibilityList = ldDMO.GetResponsibilityList();
                        ViewBag.ResponsibilityIssueList = ldDMO.GetResponsibilityIssueList(lotDispo.ResponsibilityID);

                        return View(lotDispo);
                    }
                    else
                    {
                        return RedirectToAction("ReadOnly", new { issueID = issueID });
                    }
                }
                else
                {
                    return RedirectToAction("ReadOnly", new { issueID = issueID });
                }       

            

                //return View(lotDispo);
            }
            
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "IssueID=" + issueID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n EditStep Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = exceptionString });
                throw new Exception(e.Message);

            }

           
        }



       

        //
        // GET: /LotDisposition/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="responsibilityID"></param>
        /// <returns></returns>
        public JsonResult GetResponsibilityIssue(int responsibilityID)
        {
            List<ResponsibilityIssue> respIssue = ldDMO.GetResponsibilityIssueList(responsibilityID);

            var data = respIssue.Select(s => new SelectListItem{ Value = s.ResponsibilityIssueID.ToString(), Text = s.Issue});
            return Json(data, JsonRequestBehavior.AllowGet);

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult GetDepartments([DataSourceRequest] DataSourceRequest request)
        {

            var departments = ldDMO.GetDepartments();
            return Json(departments, JsonRequestBehavior.AllowGet);

        }


        public void PopulateLotStatusOptions()
        {
            var lotStatusOptions = ldDMO.GetLotStatusOptions();
            ViewData["LotStatusOptions"] = lotStatusOptions;
            //ViewData["DefaultLotStatusOptions"] = lotStatusOptions.First();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult EditingCustom_Read([DataSourceRequest] DataSourceRequest request, int issueID)
        {
            return Json(ldDMO.GetLotDispositionLots(issueID).ToDataSourceResult(request));
        }


        /// <summary>
        /// Updates the lot tables
        /// </summary>
        /// <param name="request"></param>
        /// <param name="lot"></param>
        /// <returns></returns>
            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult EditingCustom_Update([DataSourceRequest] DataSourceRequest request, Lot lot)
            {
                
                if (lot != null && ModelState.IsValid)
                {
                    ldDMO.UpdateLotDispoLot(lot);
                }

                lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
                return Json(new[] { lot }.ToDataSourceResult(request, ModelState));
            }


        /// <summary>
        /// Deletes record from the lot table
        /// </summary>
        /// <param name="request"></param>
        /// <param name="lot"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingCustom_Destroy([DataSourceRequest] DataSourceRequest request, Lot lot)
        {
            try
            {
                if (lot != null && ModelState.IsValid)
                {
                    ldDMO.DeleteLotDispoLot(lot.LotID);

                }
            }
            catch (Exception ex)
            {
                // TODO
                throw new Exception(ex.Message);
            }


            return Json(new[] { lot }.ToDataSourceResult(request, ModelState));
        }

        
        // REGION ATTACHMENT
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult Attachment_Read([DataSourceRequest] DataSourceRequest request, int issueID)
        {
            return Json(ldDMO.GetLotDispoAttachments(issueID).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="attachment"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Attachment_Destroy([DataSourceRequest] DataSourceRequest request, Attachment attachment)
        {
            try
            {
                if (attachment != null && ModelState.IsValid)
                {
                    ldDMO.DeleteLotDispoAttachment(attachment.AttachmentID);

                }
            }
            catch (Exception e)
            {
                //string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Delete =" + attachmentID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                //Functions.WriteEvent(@User.Identity.Name + "\r\n AttachmentID Disposition\r\n" + e.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
                //EventLogDMO.Add(new WinEventLog() { UserID = @User.Identity.Name, OperationType = "Error", Comments = exceptionString });
                //throw new Exception(e.Message);

            }


            return Json(new[] { attachment }.ToDataSourceResult(request, ModelState));
        }

        
        //[AcceptVerbs(HttpVerbs.Post)]
        /// <summary>
        /// 
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <param name="fileName"></param>
        [HttpPost]        
        public void DeleteAttachment(int attachmentID, string fileName)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ldDMO.DeleteLotDispoAttachment(attachmentID);
                    //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);                    
                    var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + "LotDisposition", fileName);

                    FileInfo f = new FileInfo(physicalPath);

                    if (f.Exists)
                        f.Delete();

                }
            }
             catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "AttachmentID=" + attachmentID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AttachmentID Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = 999, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "AttachmentID Disposition " + exceptionString });
                throw new Exception(e.Message);

            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public JsonResult GetLotDispoAttachments(int issueID)
        {

            var model = ldDMO.GetLotDispoAttachments(issueID);
            return Json(model, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public ActionResult AttachSave(IEnumerable<HttpPostedFileBase> files, int issueID )
        {
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    // Some browsers send file names with full path.
                    // We are only interested in the file name.
                    var fileName = Path.GetFileName(file.FileName);
                    //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);                   
                    var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + "LotDisposition", fileName);
                   
                    file.SaveAs(physicalPath);
                    Attachment attach = new Attachment()
                    {
                        IssueID = issueID,
                        FileName = fileName,
                        UserID = (int)Session[GlobalVars.SESSION_USERID],
                    };
                    ldDMO.InsertLotDispositionAttachment(attach);
                }
            }

            //var model = ldDMO.GetLotDispoAttachments(issueID);

            
            return Content("");
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchText"></param>
        /// <param name="searchBy"></param>
        /// <returns></returns>
        public JsonResult SearchLots(string searchText, string searchBy)
        {
            List<String> lotlist = MiscDMO.SearchLots(searchText, searchBy).Select(x => x.LotNumber).ToList<String>();
            
            //IEnumerable<Lot> lotlist = MiscDMO.SearchLots(searchText, searchBy);

            return Json(lotlist, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        public void DeleteAllLots(int issueID)
        {
            // trap the error on then client side
            ldDMO.DeleteAllLotDispoLot(issueID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="lotNumber"></param>
        /// <returns></returns>
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult LotStatusDetail(int issueID, string lotNumber, bool readonlyView, string lotLocation = "")
        {
            ViewBag.LotLocation = lotLocation;
            var model = new ScrapLot();
            var data = ldDMO.GetLotStausDetail(issueID, lotNumber);
            if (data != null)
                model = data;

            model.IssueID = issueID;
            model.LotNo = lotNumber;

            if (lotLocation.Equals("QDB", StringComparison.OrdinalIgnoreCase) ||
                lotLocation.Equals("EDB", StringComparison.OrdinalIgnoreCase))
            {
                model.WaferCount = 50;
            }

            if (readonlyView)
                return PartialView("_ScrapReleaseReadOnly", model);
            else
                return PartialView("_ScrapRelease", model);
        }


       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public void UpdateLotScrapReleaseStatus(ScrapLot sl)
        {
            int scrapCount = 0;
            int releaseCount = 0;
            int splitOfHoldCount = 0;
            int closeToQDBCount = 0;
            try
            {
                foreach (PropertyInfo pi in sl.GetType().GetProperties())
                {
                    if (pi.Name.ToLower().StartsWith("lot") && pi.Name.ToLower().EndsWith("state"))
                    {
                        byte currentValue = (byte)pi.GetValue(sl, null);
                        if (currentValue == (int)GlobalVars.LotStatusOption.Release || currentValue == (int)GlobalVars.LotStatusOption.CloseToQDB)
                        {
                            releaseCount++;
                            //required to update the lot status option
                            if (currentValue == (int)GlobalVars.LotStatusOption.CloseToQDB)
                                closeToQDBCount++;
                        }
                        else if (currentValue == (int)GlobalVars.LotStatusOption.Scrap)
                            scrapCount++;
                        else if (currentValue == (int)GlobalVars.LotStatusOption.SplitOffHold)
                            splitOfHoldCount++;
                    }
                }

                sl.ScrapCount = scrapCount;
                sl.ReleaseCount = releaseCount;
                sl.SplitOfHoldCount = splitOfHoldCount;
                sl.CloseToQDBCount = closeToQDBCount;
               
                ldDMO.UpdateLotScrapReleaseStatus(sl);
                ldDMO.UpdateLotStatus(sl);
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + sl.IssueID.ToString() + " LotNo:" + sl.LotNo.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Scrap Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = sl.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "UpdateLotScrapReleaseStatus - " + exceptionString });
                throw new Exception(e.Message);

            }
        }


        /// <summary>
        /// Update the all the lots to "RELEASE" Status for a given Lot disposition document
        /// </summary>
        /// <param name="issueID"></param>
        public void ReleaseAll(int issueID)
        {

            ScrapLot sl = new ScrapLot();
            try
            {
                sl.IssueID = issueID;
                foreach (PropertyInfo pi in sl.GetType().GetProperties())
                {
                    if (pi.Name.ToLower().StartsWith("lot") && pi.Name.ToLower().EndsWith("state"))
                    {
                        //byte currentValue = (byte)pi.GetValue(sl, null);
                        //if (currentValue != 2)
                        pi.SetValue(sl, (byte)1, null);
                    }
                }

                ldDMO.UpdateLotStatusAll(sl, (int)GlobalVars.LotStatusOption.Release);
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + sl.IssueID.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Release All Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = sl.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "ReleaseAll - " + exceptionString });
                throw new Exception(e.Message);

            }
        }

        /// <summary>
        /// Update the all the lots to "SCRAP" Status for a given Lot disposition document
        /// </summary>
        /// <param name="issueID"></param>
        public void ScrapAll(int issueID)
        {
            ScrapLot sl = new ScrapLot();
            try
            {
                sl.IssueID = issueID;
                foreach (PropertyInfo pi in sl.GetType().GetProperties())
                {
                    if (pi.Name.ToLower().StartsWith("lot") && pi.Name.ToLower().EndsWith("state"))
                    {
                        //byte currentValue = (byte)pi.GetValue(sl, null);
                        //if (currentValue != 2)
                        pi.SetValue(sl, (byte)2, null);
                    }
                }

                ldDMO.UpdateLotStatusAll(sl, (int)GlobalVars.LotStatusOption.Scrap);
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + sl.IssueID.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Scrap All Disposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = sl.IssueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "ScrapAll - " + exceptionString });
                throw new Exception(e.Message);

            }


        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public JsonResult GetLotDispositionLotSummary(int issueID)
        {
            return Json(ldDMO.GetLotDispositionLotSummary(issueID), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="peRequired"></param>
        /// 
        public ActionResult SubmitDocument(int issueID, bool peRequired, bool mrbRequired)
        {
            try
            {
                int appoverCount = ldDMO.SubmitDocument(issueID, peRequired, mrbRequired, (int)Session[GlobalVars.SESSION_USERID]);
                if (appoverCount > 0)
                    NotifyApprovers(issueID, (byte)GlobalVars.WorkFLowStepNumber.Step1);
                else 
                {
                    // automaically approve current step (Step 1) beacuase there are no approvers in step 1 and move to the next step of approval
                    Approve(issueID, (byte)GlobalVars.WorkFLowStepNumber.Step1, ""); // this is the Submit Level Approval
                   
                }


                if (Request.IsAjaxRequest())
                {
                    return Content("Redirect");
                }
                else
                    return Content("Invalid");
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n SubmitDocument\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "SubmitDocument - " + exceptionString });


                Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                return Json(new { result = "Error", issueID = issueID, detail = e.Message }, JsonRequestBehavior.AllowGet);
            }          
        }

        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="currentStep"></param>
        /// <param name="comments"></param>
        public  void Approve(int issueID, byte currentStep, string comments)
        {
            int isITARCompliant = 1;
            try
            {
                bool lastStep = false;
                LotDisposition ltDispo = ldDMO.GetLotDispositionItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                bool lastApprover = wfDMO.Approve(issueID, currentStep, comments, out lastStep, (int)Session[GlobalVars.SESSION_USERID], (int)GlobalVars.DocumentType.LotDisposition, ltDispo.WorkFlowNumber);

                while (lastApprover && !lastStep)
                {
                    currentStep++;
                    lastApprover = wfDMO.Approve(issueID, currentStep, comments, out lastStep, (int)Session[GlobalVars.SESSION_USERID],(int)GlobalVars.DocumentType.LotDisposition, ltDispo.WorkFlowNumber);
                    NotifyApprovers(issueID, currentStep);
                }
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + currentStep + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n " + "Approve\r\n" + issueID.ToString() + "\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "Approve - " + exceptionString });
                throw new Exception(e.Message);
            }   

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="currentStep"></param>
        /// <param name="comments"></param>
        public void Reject(int issueID, byte currentStep, string comments)
        {
            try
            {
                if (Session[GlobalVars.SESSION_USERID] != null)
                {
                    wfDMO.Reject(issueID, currentStep, comments, (int)Session[GlobalVars.SESSION_USERID], (int) GlobalVars.DocumentType.LotDisposition);
                    NotifyRejectionToOrginator(issueID);
                }
                else
                {
                    
                    Response.Redirect("~/Account/Login");
                }
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + currentStep + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Reject\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "Reject - " + exceptionString });
                throw new Exception(e.Message);

            }  

        }

        /// <summary>
        /// Get a list of Approvers and the status
        /// </summary>
        /// <param name="request"></param>
        /// <param name="issueID"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        public ActionResult GetApproversList([DataSourceRequest] DataSourceRequest request, int issueID, byte step)
        {
            //return Json(ldDMO.GetApproversList(issueID, step).ToDataSourceResult(request));
            return Json(MiscDMO.GetApproversListByDocument(issueID, step, (int)GlobalVars.DocumentType.LotDisposition).ToDataSourceResult(request));
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="currentStep"></param>
        /// <returns></returns>
        public JsonResult GetAllUsersList()
        {
            //var userList = ldDMO.GetApprovedApproversList(issueID, currentStep);
            //return Json(userList, JsonRequestBehavior.AllowGet);

            UserAccountDMO userDMO = new UserAccountDMO();
            IEnumerable<LoginModel> userlist = userDMO.GetAllUsers();
            return Json(userlist, JsonRequestBehavior.AllowGet);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="userIDs"></param>
        /// <param name="step"></param>
        public void ReAssignApproval(int issueID, int userIDs, byte step)
        {
            var email = "";
            try
            {
                email = wfDMO.ReAssignApproval(issueID, (int)Session[GlobalVars.SESSION_USERID], userIDs, step, (int)GlobalVars.DocumentType.LotDisposition);

            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + step + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n ReAssignApproval\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "ReAssignApproval - " + exceptionString });
                throw new Exception(e.Message);

            }  


            string emailTemplate = "LotDispositionReAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Lot Disposition Re-Assignment";
            string senderName = "LotDisposition";
            
            subject = "Lot Disposition Re-Assignment" + " - Email would be sent to " + email;
            EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
            string[] emailparams = new string[3];
            emailparams[0] = issueID.ToString();
            emailparams[1] = issueID.ToString();
            emailparams[2] = GlobalVars.hostURL;
            userEmail = email;
//#if(DEBUG)
//            userEmail = "rkotian1@irf.com";
//#endif

            en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Email", Comments = "ReAssign Approver: " + email });
            }
            catch { }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="reAssignApproverFrom"></param>
        /// <param name="reAssignApproverTo"></param>
        /// <param name="step"></param>
        public void ReAssignApproverByAdmin(int issueID, int reAssignApproverFrom, int reAssignApproverTo, byte step)
        {
            var email = "";
            try
            {
                email = wfDMO.ReAssignApproval(issueID, reAssignApproverFrom, reAssignApproverTo, step, (int)GlobalVars.DocumentType.LotDisposition);

            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + step + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n ReAssignApproval\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "ReAssignApproval - " + exceptionString });
                throw new Exception(e.Message);

            }


            string emailTemplate = "LotDispositionReAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Lot Disposition Re-Assignment";
            string senderName = "LotDisposition";

            subject = "Lot Disposition Re-Assignment" + " - Email would be sent to " + email;
            EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
            string[] emailparams = new string[3];
            emailparams[0] = issueID.ToString();
            emailparams[1] = issueID.ToString();
            emailparams[2] = GlobalVars.hostURL;
            userEmail = email;
            //#if(DEBUG)
            //            userEmail = "rkotian1@irf.com";
            //#endif

            en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Email", Comments = "ReAssign Approver: " + email });
            }
            catch { }

        }

        #region Additional Approvers

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public   JsonResult GetUserListForAdditionalAppprrovers()
        {
            UserAccountDMO userDMO = new UserAccountDMO();
            IEnumerable<LoginModel> userlist = userDMO.GetAllUsers();
            return Json(userlist, JsonRequestBehavior.AllowGet);

        }

        public void AddAdditionalApproval(int issueID, byte step, string userIDs)
        {
            string emailSentList = "";
            var emailArray = "";
            try
            {
                emailArray = wfDMO.AddAdditionalApproval(issueID, userIDs, step, (int) GlobalVars.DocumentType.LotDisposition);
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + step + " " + " Userid:" + userIDs + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AddAdditionalApproval\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "AddAdditionalApproval - " + exceptionString });
                throw new Exception(e.Message);
            } 

            string emailTemplate = "LotDispositionAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Lot Disposition Assignment";
            string senderName = "LotDisposition";

            string[] emailIst = emailArray.Split(new char[] { '~' });
            foreach (string email in emailIst)
            {
                if (email.Length > 0)
                {
                    //subject = "Lot Disposition Assignment" + " - Email would be sent to " + email;
                    subject = "Lot Disposition Assignment";
                    EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                    string[] emailparams = new string[3];
                    emailparams[0] = issueID.ToString();
                    emailparams[1] = issueID.ToString();
                    emailparams[2] = GlobalVars.hostURL;
                    userEmail =email;
                    //#if(DEBUG)
                    //    userEmail = "rkotian1@irf.com";
                    //#endif

                    en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
                    emailSentList += email + ",";

                }
               
            }
            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Email", Comments = "Additonal Approver: " + emailSentList });
            }
            catch { }
        }

        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="currentStep"></param>
        public void NotifyApprovers(int issueID, byte currentStep)
        {
            try
            {
                string emailSentList = "";

                //List<string> emailIst = ldDMO.GetApproverEmailList(@issueID, currentStep).Distinct().ToList();
                List<string> emailIst = MiscDMO.GetApproverEmailListByDocument(@issueID, currentStep, (int)GlobalVars.DocumentType.LotDisposition).Distinct().ToList();

                string emailTemplate = "LotDispositionAssigned.txt";
                string userEmail = string.Empty;
                string subject = "Lot Disposition Assignment";
                string senderName = "LotDisposition";

                foreach (string email in emailIst)
                {
                    subject = "Lot Disposition Assignment";
                    EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                    string[] emailparams = new string[3];
                    emailparams[0] = issueID.ToString();
                    emailparams[1] = issueID.ToString();
                    emailparams[2] = GlobalVars.hostURL;
                    userEmail = email;
                    //#if(DEBUG)
                    //    userEmail = "rkotian1@irf.com";
                    //#endif

                    en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
                    emailSentList += email + ",";
                }

                try
                {

                    EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Email", Comments = "Approvers for Step " + currentStep.ToString() + ":" + emailSentList });
                }
                catch { }
            }
            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + issueID.ToString() + " Step:" + currentStep + " "  + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Lot Dispo - NotifyApprovers\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "AddAdditionalApproval - " + exceptionString });
                throw e;


            }
            //EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
            //string[] emailparams = new string[4];
            //emailparams[0] = issueID.ToString();
            //emailparams[1] = issueID.ToString();
            //emailparams[2] = GlobalVars.hostURL;
            //emailparams[3] =Session[GlobalVars.SESSION_USERNAME].ToString();
            //en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, emailIst, null, subject, emailparams);

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        public void NotifyRejectionToOrginator(int issueID)
        {
            
            List<string> emailIst = ldDMO.GetRejectionOrginatorEmailList(@issueID).Distinct().ToList();

            string emailTemplate = "LotDispositionReject.txt";
            string userEmail = string.Empty;
            string subject = "Lot Disposition Rejection";
            string senderName = "LotDisposition";

            foreach (string email in emailIst)
            {
                subject = "Lot Disposition Rejection";
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = issueID.ToString();
                emailparams[1] = issueID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = Session[GlobalVars.SESSION_USERNAME].ToString();
                userEmail = email;
                //#if(DEBUG)
                //    userEmail = "rkotian1@irf.com";
                //#endif

                en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Email", Comments = "Rejection: " + userEmail });
            }
            catch { }
           
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="reasonForDisposition"></param>
        public void UpdateReasonForDisposition(int issueID, string reasonForDisposition)
        {
            try
            {
                ldDMO.UpdateReasonForDisposition(issueID, reasonForDisposition);
            }

            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim();
                Functions.WriteEvent(@User.Identity.Name + "\r\n UpdateReasonForDisposition\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "UpdateReasonForDisposition - " + exceptionString });
                throw new Exception(e.Message);

            }

        }


        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult GetComments([DataSourceRequest] DataSourceRequest request, int issueID)
        {
            //return Json(ldDMO.GetApproversList(issueID, step).ToDataSourceResult(request));
            return Json(ldDMO.GetComments(issueID).ToDataSourceResult(request));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="Comments"></param>
        public void InsertComments(int issueID, string Comments)
        {
            try
            {
                ldDMO.InsertComments(issueID, Comments, (int)Session[GlobalVars.SESSION_USERID]);
            }

            catch (Exception e)
            {
                string detailedException = "";
                try
                {
                    detailedException = e.InnerException.ToString();
                }
                catch
                {
                    detailedException = e.Message;
                }
                string exceptionString = e.Message.ToString().Trim();
                Functions.WriteEvent(@User.Identity.Name + "\r\n UpdateCopmments\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Disposition", OperationType = "Error", Comments = "UpdateCopmments - " + exceptionString });
                throw new Exception(e.Message);

            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Lotfile"></param>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult ExcelLotOpen(IEnumerable<HttpPostedFileBase> Lotfile, int issueID)
        {
            MRB_DMO mrbDMO = new MRB_DMO();
            var physicalPath = "";
            try
            {
                foreach (var file in Lotfile)
                {
                    //string guid = "";
                    
                    string fName = User.Identity.Name + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    //physicalPath = Path.Combine(@"C:\Websites\FabApprovalTempPipeLine", fName + "." + Path.GetExtension(file.FileName));\
                    physicalPath = Path.Combine(ConfigurationManager.AppSettings["LotTempPipeLine"].ToString(), fName + "." + Path.GetExtension(file.FileName));
                    file.SaveAs(physicalPath);
                    ExcelData x = new ExcelData(physicalPath);
                    var lotNumbers = x.ReadData();

                    foreach (var lotInfo in lotNumbers)
                    {
                        Lot l = new Lot();
                        l.LotNumber = lotInfo.LotNo;
                        l.IssueID = issueID;
                        if (l.LotStatusOptionID == 0)
                            l.LotStatusOption.LotStatusOptionID = (int)GlobalVars.LotStatusOption.Release;

                        ldDMO.InsertLot(l, true);
                        //if (!mrbDMO.DoesMRBLotExist(lotInfo.LotNo))
                        //{
                        //    //get All the MRBs associated to the Parent lot     
                        //    //insert the lot into the MRBChildLotNotInMRB table and NOT in the MRB Lot table for each MRB
                        //    ldDMO.InsertChildLot_NotInTheMRB(lotInfo.LotNo);

                        //}
                    }

                    FileInfo f = new FileInfo(physicalPath);
                    if (f.Exists)
                        f.Delete();

                }

                return Content("");
            }
            catch
            {
                FileInfo f = new FileInfo(physicalPath);
                if (f.Exists)
                    f.Delete();

                return Content("Incorrect File Format");
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public void ReleaseLockOnDocument(int issueID)
        {
            try
            {
                ldDMO.ReleaseLockOnDocument((int)Session[GlobalVars.SESSION_USERID], issueID);
            }
            catch (Exception e)
            {

                try
                {
                    Functions.WriteEvent(@User.Identity.Name + "\r\n ReleaseLockOnDocument LD\r\n" + issueID.ToString() + "\r\n" + e.Message, System.Diagnostics.EventLogEntryType.Error);
                }
                catch { }
                ldDMO.ReleaseLockOnDocument(-1, issueID);

            }
        }


        public ActionResult GetFileMetaData(){

            return Content("");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public FileResult DownloadFile(string attachmentID)
        {
            //fileName = "ECNForm_71132.pdf";           

            string fileName = ldDMO.GetFileName(attachmentID);

            var sDocument = Path.Combine(Functions.GetAttachmentFolder() + "LotDisposition", fileName);
            var FDir_AppData = Functions.GetAttachmentFolder();
            if (!sDocument.StartsWith(FDir_AppData))
            {
                // Ensure that we are serving file only inside the Fab2ApprovalAttachments folder
                // and block requests outside like "../web.config"
                throw new HttpException(403, "Forbidden");
            }

            if (!System.IO.File.Exists(sDocument))
            {
                return null;
                //throw new Exception("File not found");
            }

            return File(sDocument, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
        }
    }
}