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

namespace Fab2ApprovalSystem.Controllers
{
    [Authorize]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    [SessionExpireFilter]
    public class LotTravelerController : PdfViewController
    {
        //
        string docTypeString = "LotTraveler";
        LotTravelerDMO LotTravDMO = new LotTravelerDMO();
        WorkflowDMO wfDMO = new WorkflowDMO();
       
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateWorkRequest()
        {
            LTWorkRequest workRequest = new LTWorkRequest();
            try
            {
                workRequest.OriginatorID = (int)Session[GlobalVars.SESSION_USERID];
                LotTravDMO.InsertWorkRequest(workRequest);
                return RedirectToAction("Edit", new { issueID = workRequest.ID });
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + workRequest.SWRNumber.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n CreateWorkRequest - LotTraveler\r\n" + e.InnerException == null? e.Message : e.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = workRequest.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "CreateWorkRequest - " + exceptionString });
                throw new Exception(e.Message);

            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult Edit(int issueID)
        {
                int isITARCompliant = 1;
                Session["CreateNewRevision"] = "false";
                ViewBag.NewRevision = "false";


                LTWorkRequest workRequest = new LTWorkRequest();
                try
                {
                    workRequest = LotTravDMO.GetLTWorkRequestItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                    if (isITARCompliant == 0) // not ITAR Compliant
                    {
                        return View("UnAuthorizedAccess");
                    }
                    if ((int)Session[GlobalVars.SESSION_USERID] == workRequest.OriginatorID)
                        ViewBag.IsOriginator = "true";
                    else
                        ViewBag.IsOriginator = "false";


                    List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(issueID, workRequest.CurrentStep, (int)GlobalVars.DocumentType.LotTraveler);
                    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 (workRequest.CloseDate != null)
                    {
                        // LATER ON SHOULD CALL THE LOT TRAVLELER VIEW
                        return RedirectToAction("WorkRequestReadOnly", new { issueID = issueID });
                    }

                    // open the view based on the Workflow step
                    if (workRequest.CurrentStep == 0)
                    {
                        if ((workRequest.RecordLockIndicator && workRequest.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]))
                        {
                            return RedirectToAction("WorkRequestReadOnly", new { issueID = issueID });
                        }


                    }
                    else if (workRequest.CurrentStep >= 1 && workRequest.CurrentStep < 3 && workRequest.CloseDate == null) // Before
                    {
                        if ((ViewBag.IsApprover == "true") || ((bool)Session[GlobalVars.IS_ADMIN]))
                            return RedirectToAction("WorkRequestApproval", new { issueID = issueID });
                        else
                            return RedirectToAction("WorkRequestReadOnly", new { issueID = issueID });
                    }
                    else if (workRequest.CurrentStep == 3)
                    {
                        if ((workRequest.RecordLockIndicator && workRequest.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]) || (ViewBag.IsApprover == "false"))
                        {
                            return RedirectToAction("WorkRequestReadOnly", new { issueID = issueID });
                        }
                        else
                        {
                            Session["CreateNewRevision"] = "true";
                            ViewBag.NewRevision = "true";
                            workRequest = LotTravDMO.GetLTWorkRequestItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                            return RedirectToAction("WorkRequestRevision", new { workRequestID = issueID });
                        }

                        // Start the versioning of the documents with the Edit Feature
                        // Call the Edit Form



                    }
                    workRequest = LotTravDMO.GetLTWorkRequestItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                    // else do this part
                    ViewBag.Departments = MiscDMO.GetDepartments();
                    ViewBag.AffectedModules = MiscDMO.GetModules();
                    ViewBag.WorkReqRevisionList = LotTravDMO.GetWorkReqRevisions(workRequest.SWRNumber);
                    return View(workRequest);
                }
                catch (Exception e)
                {
                    string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + workRequest.SWRNumber.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                    Functions.WriteEvent(@User.Identity.Name + "\r\n Edit WorkRequest - LotTraveler\r\n" + e.InnerException == null? e.Message : e.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                    EventLogDMO.Add(new WinEventLog() { IssueID = workRequest.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Edit WorkRequest - " + exceptionString });
                    throw new Exception(e.Message);

                }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveRevision(LTWorkRequest model)
        {
            try
            {
                var data = model;
                if (Session["CreateNewRevision"].ToString() == "true")
                {
                    ViewBag.NewRevision = "false";
                    Session["CreateNewRevision"] = "false";
                    //Session["CurrentlyOnSamePage"] = "true";
                    model.OriginatorID = (int)Session[GlobalVars.SESSION_USERID];
                    int newRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                    // TODO
                    // Send email to the Originator and Group of People
                    NotifyfWorkRequestRevisionChange(newRequestID);

                    return Content(newRequestID.ToString());
                }
                else
                {
                    // model.OriginatorID = (int)Session[GlobalVars.SESSION_USERID];
                    LotTravDMO.UpdateWorkRequest(model, (int)Session[GlobalVars.SESSION_USERID]);
                    return Content("");
                }
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + model.SWRNumber.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n SaveRevision - LotTraveler\r\n" + e.InnerException == null? e.Message : e.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = model.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "SaveRevision - " + exceptionString });
                throw new Exception(e.Message);

            }



           
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult WorkRequestReadOnly(int issueID)
        {
            int isITARCompliant = 1;
            LTWorkRequest workRequest = new LTWorkRequest();
            try
            {
                workRequest = LotTravDMO.GetLTWorkRequestItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);


                ViewBag.Departments = MiscDMO.GetDepartments();
                ViewBag.AffectedModules = MiscDMO.GetModules();
                ViewBag.WorkReqRevisionList = LotTravDMO.GetWorkReqRevisions(workRequest.SWRNumber);

                return View(workRequest);
            }
            catch (Exception e)
            {
                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 WorkRequestReadOnly - LotTraveler\r\n" + e.InnerException == null? e.Message : e.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "WorkRequestReadOnly - " + exceptionString });
                throw new Exception(e.Message);

            }

            
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult WorkRequestApproval(int issueID)
        {
            int isITARCompliant = 1;
            LTWorkRequest workRequest = new LTWorkRequest();
            try
            {
                workRequest = LotTravDMO.GetLTWorkRequestItemForRead(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(issueID, workRequest.CurrentStep, (int)GlobalVars.DocumentType.LotTraveler);

                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 ((workRequest.RecordLockIndicator && workRequest.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID]) || (!((bool)Session[GlobalVars.IS_ADMIN]) && (ViewBag.IsApprover == "false")) )
                if (workRequest.RecordLockIndicator && workRequest.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID])
                {
                    return RedirectToAction("WorkRequestReadOnly", new { issueID = issueID });
                }


                workRequest = LotTravDMO.GetLTWorkRequestItem(issueID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                ViewBag.Departments = MiscDMO.GetDepartments();
                ViewBag.AffectedModules = MiscDMO.GetModules();
                ViewBag.WorkReqRevisionList = LotTravDMO.GetWorkReqRevisions(workRequest.SWRNumber);

                return View(workRequest);
            }
            catch (Exception e)
            {
                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 WorkRequestApproval - LotTraveler\r\n" + e.InnerException == null? e.Message : e.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = issueID, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "WorkRequestApproval - " + exceptionString });
                throw new Exception(e.Message);

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public ActionResult GetWorkRequestRevision(int workRequestID)
        {
            int isITARCompliant = 1;
            LTWorkRequest workRequest = new LTWorkRequest();
            workRequest = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

            //if (workRequest.IsCurrentRevision)
            //{
            //    return RedirectToAction("Edit", new { issueID = workRequestID });
            //}
            return Content("");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public ActionResult DisplayWorkRequestPDF(int workRequestID)
        {

            WorkRequestPdf workRequest = new WorkRequestPdf();
            try
            {

                workRequest = LotTravDMO.GetLTWorkRequestItemPDF(workRequestID);

                // To render a PDF instead of an HTML, all we need to do is call ViewPdf instead of View. This
                // requires the controller to be inherited from MyController instead of MVC's Controller.
                return this.ViewPdf("", "WorkRequestPDF", workRequest);

            }
            catch (Exception ex)
            {
                Functions.WriteEvent(@User.Identity.Name + "\r\n DisplayWorkRequestPDF - LotTraveler\r\n" + ex.InnerException.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = workRequest.SWRNumber, UserID = @User.Identity.Name, DocumentType = "LotTravler", OperationType = "Generate PDF", Comments = ex.Message });
                workRequest = null;
                return Content("");

            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <returns></returns>
        public ActionResult WorkRequestRevision(int workRequestID)
        {
            int isITARCompliant = 1;
            LTWorkRequest workRequest = new LTWorkRequest();
            workRequest = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(workRequestID, workRequest.CurrentStep, (int)GlobalVars.DocumentType.LotTraveler);
            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 (
                (workRequest.RecordLockIndicator && workRequest.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID] 
                || (ViewBag.IsApprover == "false"))
                || (!workRequest.IsCurrentRevision) 
                || (workRequest.CloseDate != null) 
                )
            {
                return RedirectToAction("WorkRequestReadOnly", new { issueID = workRequestID });
            }

            workRequest = LotTravDMO.GetLTWorkRequestItem(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            

            ViewBag.Departments = MiscDMO.GetDepartments();
            ViewBag.AffectedModules = MiscDMO.GetModules();
            ViewBag.WorkReqRevisionList = LotTravDMO.GetWorkReqRevisions(workRequest.SWRNumber);

            return View(workRequest);

        }

        [HttpPost]
        public ActionResult Edit(LTWorkRequest model)
        {
            try
            {
                Session["CreateNewRevision"] = "false";
                var data = model;
                LotTravDMO.UpdateWorkRequest(model, (int)Session[GlobalVars.SESSION_USERID]);
            }
            catch(Exception ex) 
            {
                return Content(ex.Message);
            }

            return Content("Successfully Saved");
        }
       /// <summary>
       /// 
       /// </summary>
       /// <param name="request"></param>
       /// <param name="workRequestID"></param>
       /// <returns></returns>
        public JsonResult GetBaseFlowLocations(string baseFlow)
        {
            List<BaseFlowLocation> loclist = LotTravDMO.GetBaseFlowLocations(baseFlow);
            return Json(loclist, JsonRequestBehavior.AllowGet);
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="materialID"></param>
        /// <returns></returns>
        public JsonResult GetMaterialRecord(int materialID)
        {
            LTMaterial ltMaterial = LotTravDMO.GetMaterialRecord(materialID);
            return Json(ltMaterial, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseFlow"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public JsonResult GetBaseFlowOperations(string baseFlow, string location)
        {
            List<BaseFlowOperations> operationslist = LotTravDMO.GetBaseFlowOperations(baseFlow, location);
            return Json(operationslist, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="workRequestID"></param>
        /// <returns></returns>       
        public ActionResult GetMaterialDetails([DataSourceRequest] DataSourceRequest request, int workRequestID)
        {
            return Json(LotTravDMO.GetMaterialDetails(workRequestID).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPartNumbers()
        {
            List<PartNumberAttrib> operationslist = LotTravDMO.GetPartNumbers();
            return Json(operationslist, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertMaterialDetail([DataSourceRequest] DataSourceRequest request, LTMaterialViewModel model)
        {
            try
            {
                string s = Session["CreateNewRevision"].ToString();
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.InsertMaterialDetail(model, (int)Session[GlobalVars.SESSION_USERID]);
                }

                return Json(new[] { model }.ToDataSourceResult(request, ModelState));
            }
            catch(Exception ex)
            {
                //throw new Exception(ex.Message);
                return this.Json(new DataSourceResult
                {
                    Errors = ex.Message
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateMaterialDetail([DataSourceRequest] DataSourceRequest request, LTMaterialViewModel model)
        {
            try
            {
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.UpdateMaterialDetail(model, (int)Session[GlobalVars.SESSION_USERID]);
                }

                //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
                return Json(new[] { model }.ToDataSourceResult(request, ModelState));
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
                return this.Json(new DataSourceResult
                {
                    Errors = ex.Message
                });
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult AddMaterialDetailRevision(LTWorkRequest model)
        {
            var modelMaterialDetail = model.LTMaterial;
            int previousMaterialID = model.LTMaterial.ID;
            int newWorkRequestID = model.ID;
            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                modelMaterialDetail.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.InsertMaterialDetail(modelMaterialDetail, (int)Session[GlobalVars.SESSION_USERID]);
                // TODO
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (modelMaterialDetail != null && ModelState.IsValid)
                {
                    LotTravDMO.InsertMaterialDetail(modelMaterialDetail, (int)Session[GlobalVars.SESSION_USERID]);
                }
            }

            return Content(newWorkRequestID.ToString());
        }

        /// <summary>
        /// For the Revison
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        
        public ActionResult UpdateMaterialDetailRevision(LTWorkRequest model)
        {
            var modelMaterialDetail = model.LTMaterial;
            int previousMaterialID = model.LTMaterial.ID;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                modelMaterialDetail.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.UpdateMaterialDetailRevision(modelMaterialDetail, previousMaterialID);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (modelMaterialDetail != null && ModelState.IsValid)
                {
                    LotTravDMO.UpdateMaterialDetail(modelMaterialDetail, (int)Session[GlobalVars.SESSION_USERID]);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }

        public ActionResult DeleteMaterialDetail([DataSourceRequest] DataSourceRequest request, LTMaterialViewModel model)
        {
            try
            {
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteMaterialDetail(model.ID);

                }
            }
            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[] { model }.ToDataSourceResult(request, ModelState));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult DeleteMaterialDetailRevision(LTWorkRequest model)
        {
            var modelMaterialDetail = model.LTMaterial;            
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                modelMaterialDetail.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.DeleteMaterialDetailRevision(modelMaterialDetail.ID);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (modelMaterialDetail != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteMaterialDetail(modelMaterialDetail.ID);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="workRequestID"></param>
        /// <returns></returns>
        public ActionResult GetHoldSteps([DataSourceRequest] DataSourceRequest request, int workRequestID)
        {
            return Json(LotTravDMO.GetHoldSteps(workRequestID).ToDataSourceResult(request));
        }

        
        //public ActionResult InsertUpdateHoldStep([DataSourceRequest] DataSourceRequest request, LTHoldStep model)
        //{
        //    if (model != null && ModelState.IsValid)
        //    {
        //        LotTravDMO.InsertUpdateHoldStep(model);
        //    }


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


        //public ActionResult UpdateHoldStep([DataSourceRequest] DataSourceRequest request, LTHoldStep model)
        //{
        //    if (model != null && ModelState.IsValid)
        //    {
        //        LotTravDMO.UpdateHoldStep(model);
        //    }


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


        public ActionResult InsertHoldStep([DataSourceRequest] DataSourceRequest request, LTHoldStep model)
        {
            if (model != null && ModelState.IsValid)
            {
                model.UpdatedBy = (int)Session[GlobalVars.SESSION_USERID];
                LotTravDMO.InsertHoldStep(model);
            }


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


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult UpdateHoldStepRevision(LTWorkRequest model)
        {
            var holdStepModel = model.LTHoldStep;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                holdStepModel.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.UpdateHoldStepRevision(holdStepModel);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (holdStepModel != null && ModelState.IsValid)
                {
                    holdStepModel.UpdatedBy = (int)Session[GlobalVars.SESSION_USERID];
                    LotTravDMO.UpdateHoldStep(holdStepModel);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }


        public ActionResult InsertHoldStepRevision(LTWorkRequest model)
        {
            var holdStepModel = model.LTHoldStep;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                holdStepModel.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.InsertHoldStepRevision(holdStepModel);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (holdStepModel != null && ModelState.IsValid)
                {
                    holdStepModel.UpdatedBy = (int)Session[GlobalVars.SESSION_USERID];
                    LotTravDMO.InsertHoldStep(holdStepModel);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult DeleteHoldStepRevision(LTWorkRequest model)
        {
            var holdStepModel = model.LTHoldStep;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                holdStepModel.LTWorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.DeleteHoldStepRevision(holdStepModel.ID);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (holdStepModel != null && ModelState.IsValid)
                {
                    
                    LotTravDMO.DeleteHoldStep(holdStepModel.ID, (int)Session[GlobalVars.SESSION_USERID]);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        public void UpdateHoldStep(LTHoldStep model)
        {
            if (model != null && ModelState.IsValid)
            {
                LotTravDMO.UpdateHoldStep(model);
            }
        }


        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteHoldStep([DataSourceRequest] DataSourceRequest request, LTHoldStep model)
        {
            try
            {
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteHoldStep(model.ID, (int)Session[GlobalVars.SESSION_USERID]);

                }
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
                return this.Json(new DataSourceResult
                {
                    Errors = ex.Message
                });
            }


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

        /// <summary>
        /// 
        /// </summary>
        /// <param name="holdStepID"></param>
        /// <returns></returns>
        public ActionResult GetHoldStep(int holdStepID)
        {
            var model = new LTHoldStep();
            model = LotTravDMO.GetHoldStep(holdStepID);
            
            return PartialView("_HoldStepAttachments", model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="holdStepID"></param>
        /// <returns></returns>
        public ActionResult GetHoldStepRevision(int holdStepID)
        {
            var model = new LTHoldStep();
            model = LotTravDMO.GetHoldStep(holdStepID);

            return PartialView("_HoldStepAttachmentsRevision", model);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltHoldStepID"></param>
        /// <returns></returns>
        public ActionResult GetLotTravHoldStepRevision(int ltHoldStepID)
        {
            var model = new LTLotTravelerHoldSteps();
            model = LotTravDMO.GetLotTravHoldStep(ltHoldStepID);
            return PartialView("_LotTravHoldStepAttachRev", model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="holdStepID"></param>
        /// <returns></returns>
        public ActionResult GetHoldStepAttachments([DataSourceRequest] DataSourceRequest request, int holdStepID)
        {

            //var model = LotTravDMO.GetHoldStepAttachemnts(holdStepID);
            //return Json(new[] { model }.ToDataSourceResult(request, ModelState));

            return Json(LotTravDMO.GetHoldStepAttachemnts(holdStepID).ToDataSourceResult(request));
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="ltHoldStepID"></param>
        /// <returns></returns>
        public ActionResult GetLTHoldStepAttachments([DataSourceRequest] DataSourceRequest request, int ltHoldStepID)
        {

            //var model = LotTravDMO.GetHoldStepAttachemnts(holdStepID);
            //return Json(new[] { model }.ToDataSourceResult(request, ModelState));

            return Json(LotTravDMO.GetLotTravHoldStepAttachemnts(ltHoldStepID).ToDataSourceResult(request));

        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteHoldStepAttachment([DataSourceRequest] DataSourceRequest request, LTWorkRequestAttachment model)
        {
            try
            {
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteWorkRequestAttachment(model.ID);

                }
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Delete =" + model.ID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AttachmentID Work Request\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[] { model }.ToDataSourceResult(request, ModelState));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="workRequestID"></param>
        /// <returns></returns>
        public ActionResult GetWorkRequestAttachments([DataSourceRequest] DataSourceRequest request, int workRequestID)
        {
            return Json(LotTravDMO.GetWorkRequestAttachments(workRequestID).ToDataSourceResult(request));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateWorkRequestAttachment([DataSourceRequest] DataSourceRequest request, LTWorkRequestAttachment model)
        {

            if (model != null && ModelState.IsValid)
            {
                LotTravDMO.UpdateWorkRequestAttachment(model);
            }

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

       /// <summary>
       /// 
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
        public ActionResult UpdateWorkRequestAttachmentRevision(LTWorkRequest model)
        {
            var wrAttachmentDetail = model.WorkRequestAttachment;
            int previousWorkRequestAttachmentID = model.WorkRequestAttachment.ID;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                wrAttachmentDetail.WorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.UpdateWorkRequestAttachmentRevision(wrAttachmentDetail, previousWorkRequestAttachmentID);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (wrAttachmentDetail != null && ModelState.IsValid)
                {
                    LotTravDMO.UpdateWorkRequestAttachment(wrAttachmentDetail);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
           
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteWorkRequestAttachment([DataSourceRequest] DataSourceRequest request, LTWorkRequestAttachment model)
        {
            try
            {
                if (model != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteWorkRequestAttachment(model.ID);

                }
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Delete =" + model.ID.ToString() + "  " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AttachmentID WorkRequiest\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[] { model }.ToDataSourceResult(request, ModelState));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult DeleteWorkRequestAttachmentRevision( LTWorkRequest model)
        {
            var wrAttachmentDetail = model.WorkRequestAttachment;
            int previousWorkRequestAttachmentID = model.WorkRequestAttachment.ID;
            int newWorkRequestID = model.ID;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                Session["CreateNewRevision"] = "false";

                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                wrAttachmentDetail.WorkRequestID = newWorkRequestID;


                // Any update will be a new entry
                LotTravDMO.DeleteWorkRequestAttachmentRevision(wrAttachmentDetail.ID);
                // TODO
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            else
            {

                if (wrAttachmentDetail != null && ModelState.IsValid)
                {
                    LotTravDMO.DeleteWorkRequestAttachment(wrAttachmentDetail.ID);
                }
            }

            //lot.LotStatusOptionID = lot.LotStatusOption.LotStatusOptionID;
            //return Json(new[] { modelMaterialDetail }.ToDataSourceResult(request, ModelState));
            return Content(newWorkRequestID.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <param name="AttachSaveWorkRequest"></param>
        /// <param name="currentRevision"></param>
        /// <param name="swrNo"></param>
        /// <returns></returns>
        public ActionResult AttachSaveWorkRequest(IEnumerable<HttpPostedFileBase> files, int workRequestID, int currentRevision, int swrNo, string comments, string docType)
        {
            // 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.

                    // TODO
                    //int currentRevision = 1;
                    var fileName = Path.GetFileName(file.FileName);
                    var fileExtension = Path.GetExtension(file.FileName);
                    //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);       
                    DirectoryInfo di;
                    var SWRPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                    di = new DirectoryInfo(SWRPhysicalPath);
                    if (!di.Exists)
                        di.Create();

                    //var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision;
                    var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo ;
                    di = new DirectoryInfo(SWR_RevPhysicalPath);
                    if (!di.Exists)
                        di.Create();

                    var guid = Guid.NewGuid().ToString();
                    var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo  + @"\", guid + fileExtension);



                    file.SaveAs(physicalPath);
                    LTWorkRequestAttachment attach = new LTWorkRequestAttachment()
                    {
                        WorkRequestID = workRequestID,
                        FileGUID = guid,
                        LTHoldStepID = -1,
                        FileName = fileName,
                        UploadedByID = (int)Session[GlobalVars.SESSION_USERID],
                        DocType = docType,
                        Comments = comments
                        
                    };

                    
                    LotTravDMO.InsertWorkRequestAttachment(attach);
                }
            }
            return Content("");
        }


        public ActionResult AttachSaveWorkRequestRevision(IEnumerable<HttpPostedFileBase> files, int workRequestID, int currentRevision, int swrNo, string revComments, string docType, string attachComments)
        {

            bool newRevision = false;
            int newWorkRequestID = -1;
            int isITARCompliant = 1;
            LTWorkRequest model = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            model.RevisionComments = revComments;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                newRevision = true;
                Session["CreateNewRevision"] = "false";
                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
             
                // Send email to the Originator and Group of People
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }

            // 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.

                    // TODO
                    //int currentRevision = 1;
                    var fileName = Path.GetFileName(file.FileName);
                    var fileExtension = Path.GetExtension(file.FileName);
                    //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);       
                    DirectoryInfo di;
                    var SWRPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                    di = new DirectoryInfo(SWRPhysicalPath);
                    if (!di.Exists)
                        di.Create();

                    //var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision;
                    var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                    di = new DirectoryInfo(SWR_RevPhysicalPath);
                    if (!di.Exists)
                        di.Create();

                    var guid = Guid.NewGuid().ToString();
                    var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\", guid + fileExtension);



                    file.SaveAs(physicalPath);
                    LTWorkRequestAttachment attach = new LTWorkRequestAttachment()
                    {
                        WorkRequestID = workRequestID,
                        FileGUID = guid,
                        LTHoldStepID = -1,
                        FileName = fileName,
                        UploadedByID = (int)Session[GlobalVars.SESSION_USERID],
                        DocType = docType,
                        Comments = attachComments
                    };


                   // LotTravDMO.InsertWorkRequestAttachment(attach);
                    if (newRevision)
                        LotTravDMO.InsertWorkRequestAttachmentRevision(attach);
                    else
                        LotTravDMO.InsertWorkRequestAttachment(attach);
                }
            }
            return Content("");
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <param name="holdStepID"></param>
        /// <param name="currentRevision"></param>
        /// <param name="swrNo"></param>
        /// <param name="docType"></param>
        /// <returns></returns>
        public ActionResult HoldStepAttachSave(IEnumerable<HttpPostedFileBase> HoldStepAttachment, int holdStepID, int currentRevision, int swrNo, string docType, string comments)
        {
            // The Name of the Upload component is "files"
            try
            {
                if (HoldStepAttachment != null)
                {
                    foreach (var file in HoldStepAttachment)
                    {
                        // Some browsers send file names with full path.
                        // We are only interested in the file name.

                        // TODO
                        //int currentRevision = 1;
                        var fileName = Path.GetFileName(file.FileName);
                        var fileExtension = Path.GetExtension(file.FileName);
                        //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);       
                        DirectoryInfo di;
                        var SWRPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                        di = new DirectoryInfo(SWRPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        //var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision;
                        var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo ;
                        di = new DirectoryInfo(SWR_RevPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        var guid = Guid.NewGuid().ToString();
                        //var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision + @"\", guid + fileExtension);
                        var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\", guid + fileExtension);



                        file.SaveAs(physicalPath);
                        LTWorkRequestAttachment attach = new LTWorkRequestAttachment()
                        {
                            FileGUID = guid,
                            LTHoldStepID = holdStepID,
                            FileName = fileName,
                            UploadedByID = (int)Session[GlobalVars.SESSION_USERID],
                            DocType = docType,
                            Comments = comments
                        };

                        LotTravDMO.InsertLotHoldStepAttachment(attach);
                    }
                }
            }
            catch(Exception e)
            {

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

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


            return Content("");
        }


        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="HoldStepAttachment"></param>
        /// <param name="workRequestID"></param>
        /// <param name="holdStepID"></param>
        /// <param name="currentRevision"></param>
        /// <param name="swrNo"></param>
        /// <param name="docType"></param>
        /// <param name="revComments"></param>
        /// <returns></returns>
        public ActionResult HoldStepAttachSaveRev(IEnumerable<HttpPostedFileBase> HoldStepAttachment, int workRequestID, int holdStepID, int currentRevision, int swrNo, string docType, string comments, string revComments)
        {
            // The Name of the Upload component is "files"
            bool newRevision = false;
            int newWorkRequestID = -1;
            int isITARCompliant = 1;
            LTWorkRequest model = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            model.RevisionComments = revComments;

            if (Session["CreateNewRevision"].ToString() == "true")
            {
                newRevision = true;
                Session["CreateNewRevision"] = "false";
                newWorkRequestID = LotTravDMO.CreateWorkRequestRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                
                NotifyfWorkRequestRevisionChange(newWorkRequestID);
            }
            
            try
            {
                if (HoldStepAttachment != null)
                {
                    foreach (var file in HoldStepAttachment)
                    {
                        // Some browsers send file names with full path.
                        // We are only interested in the file name.

                        // TODO
                        //int currentRevision = 1;
                        var fileName = Path.GetFileName(file.FileName);
                        var fileExtension = Path.GetExtension(file.FileName);
                        //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);       
                        DirectoryInfo di;
                        var SWRPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                        di = new DirectoryInfo(SWRPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        //var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision;
                        var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                        di = new DirectoryInfo(SWR_RevPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        var guid = Guid.NewGuid().ToString();
                        //var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision + @"\", guid + fileExtension);
                        var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\", guid + fileExtension);



                        file.SaveAs(physicalPath);
                        LTWorkRequestAttachment attach = new LTWorkRequestAttachment()
                        {
                            FileGUID = guid,
                            LTHoldStepID = holdStepID,
                            FileName = fileName,
                            UploadedByID = (int)Session[GlobalVars.SESSION_USERID],
                            DocType = docType,
                            Comments = comments
                        };
                        if (newRevision)
                            LotTravDMO.InsertLotHoldStepAttachmentRevision(attach);
                        else
                            LotTravDMO.InsertLotHoldStepAttachment(attach);
                        
                    }
                }
            }
            catch (Exception e)
            {

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

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


            return Content("");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="prevHoldStepID"></param>
        /// <returns></returns>
        public int GetRevisedWrkReqIDFromHoldStepID(int prevHoldStepID)
        {
            return LotTravDMO.GetRevisedWrkReqIDFromHoldStepID(prevHoldStepID);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="prevWorkRequestID"></param>
        /// <returns></returns>
        public int GetRevisedWrkReqIDFromPreviousWrkReqID(int prevWorkRequestID)
        {
            return LotTravDMO.GetRevisedWrkReqIDFromPreviousWrkReqID(prevWorkRequestID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="wrAttachmentID"></param>
        /// <returns></returns>
        public JsonResult GetWorkRequestAttachDetail(int wrAttachmentID)
        {
            LTWorkRequestAttachment data = LotTravDMO.GetWorkRequestAttachDetail(wrAttachmentID);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <param name="documentType"></param>
        /// <returns></returns>
        public ActionResult SubmitDocument(int workRequestID, int documentType)
        {
            int isITARCompliant = 1;
            LTWorkRequest ltWR = new LTWorkRequest();
            try
            {

                int appoverCount = LotTravDMO.SubmitDocument(workRequestID, (int)Session[GlobalVars.SESSION_USERID], documentType, out isITARCompliant);
                if (isITARCompliant == 0) // not ITAR Compliant
                {
                    return View("UnAuthorizedAccess");
                }
                else
                {

                    if (appoverCount > 0)
                    {
                        NotifyApprovers(workRequestID, (byte)GlobalVars.WorkFLowStepNumber.Step1, documentType);
                    }
                    else
                    {
                        // automaically approve current step (Step 1) beacuase there are no approvers in step 1 and move to the next step of approval
                        Approve(workRequestID, (byte)GlobalVars.WorkFLowStepNumber.Step1, "", documentType); // this is the Submit Level Approval

                    }

                    ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

                    List<ApproversListViewModel> userList = MiscDMO.GetPendingApproversListByDocument(workRequestID, ltWR.CurrentStep, (int)documentType);
                    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 ((int)Session[GlobalVars.SESSION_USERID] == ltWR.OriginatorID)
                        ViewBag.IsOriginator = "true";
                    else
                        ViewBag.IsOriginator = "false";


                    if (Request.IsAjaxRequest())
                    {
                        // the content gets evaluated on the client side
                        return Content("Redirect");
                    }
                    else
                        return Content("Invalid");

                    //return View(ltWR);
                }
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + workRequestID.ToString() + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n SubmitDocument\r\n" + e.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = docTypeString, OperationType = "Error", Comments = "SubmitDocument - " + exceptionString });
                throw new Exception(e.Message);

            }
        }

                /// <summary>
        /// 
        /// </summary>
        /// <param name="ecnNumber"></param>
        /// <param name="currentStep"></param>
        /// <param name="comments"></param>
        public void Approve(int workRequestID, byte currentStep, string comments, int documentType)
        {
            LTWorkRequest ltWR = new LTWorkRequest();
            int isITAR = 1;
            ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITAR, (int)Session[GlobalVars.SESSION_USERID]);
            try
            {

                bool lastStep = false;
                bool lastApprover = wfDMO.Approve(workRequestID, currentStep, comments, out lastStep, (int)Session[GlobalVars.SESSION_USERID], documentType, ltWR.WorkFlowNumber);

                while (lastApprover && !lastStep)
                {
                    currentStep++;
                    lastApprover = wfDMO.Approve(workRequestID, currentStep, comments, out lastStep, (int)Session[GlobalVars.SESSION_USERID], documentType, ltWR.WorkFlowNumber);

                    //if (currentStep == 3 && (!lastStep)) // add orginator to the last step
                    //{
                    //    int isITARCompliant = 1;
                    //    ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
                    //    wfDMO.AddAdditionalApproval(workRequestID, ltWR.OriginatorID.ToString(), currentStep, documentType);
                    //}

                    NotifyApprovers(workRequestID, currentStep, documentType);
                }

                if (lastApprover && lastStep)
                {
                    // Send email to the Originator and different groups
                    NotifyApprovalOfWorkRequest(workRequestID);
                }

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

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <param name="currentStep"></param>
        /// <param name="comments"></param>
        /// <param name="docType"></param>
        public void Reject(int workRequestID, byte currentStep, string comments, int docType)
        {
            LTWorkRequest ltWR = new LTWorkRequest();
            int isITARCompliant = 1;
            ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            try
            {

                if (Session[GlobalVars.SESSION_USERID] != null)
                {
                    wfDMO.Reject(workRequestID, currentStep, comments, (int)Session[GlobalVars.SESSION_USERID], docType);
                    NotifyRejectionToOrginator(workRequestID);
                }
                else
                {

                    Response.Redirect("~/Account/Login");
                }
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + workRequestID.ToString() + " Step:" + currentStep + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n Reject\r\n" + e.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Reject - " + exceptionString });
                throw new Exception(e.Message);

            }

        }

        // TODO ======================================

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        public void NotifyRejectionToOrginator(int workRequestID)
        {

            List<string> emailIst = LotTravDMO.GetRejectionOrginatorEmailList(workRequestID).Distinct().ToList();
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

            string emailTemplate = "WorkRequestReject.txt";
            string userEmail = string.Empty;
            string subject = "Work Request Rejection";
            string senderName = "Work Request";

            foreach (string email in emailIst)
            {
                subject = "Work Request Rejection notice for Number " + ltWR.SWRNumber + ", - " + ltWR.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[5];
                emailparams[0] = ltWR.SWRNumber.ToString();
                emailparams[1] = workRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = Session[GlobalVars.SESSION_USERNAME].ToString();
                emailparams[4] = "Work Request";
                userEmail = email;
                //#if(DEBUG)
                //    userEmail = "rkotian1@irf.com";
                //#endif

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

                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Lot Traveler Notify Rejection:" + email });
                }
            }

            try
            {

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

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <param name="currentStep"></param>
        /// <param name="documentType"></param>
        /// <param name="ecnTypeString"></param>
        public void NotifyApprovers(int workRequestID, byte currentStep, int documentType)
        {
            string emailSentList = "";
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

            List<string> emailIst = MiscDMO.GetApproverEmailListByDocument(workRequestID, currentStep, documentType).Distinct().ToList();

            string emailTemplate = "WorkRequestAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Work Request Assignment";
            var senderName = "";


            foreach (string email in emailIst)
            {
                subject = "Work Request Assignment notice for Number " + ltWR.SWRNumber + ", - " + ltWR.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = ltWR.SWRNumber.ToString();
                emailparams[1] = workRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = "Work Request";
                userEmail = email;
#if(DEBUG)
                userEmail = "rkotian1@irf.com";
#endif

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

                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "WR Notify Approvers:" + email });
                }
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Work Request", OperationType = "Email", Comments = "Approvers for Step " + currentStep.ToString() + ":" + emailSentList });
            }
            catch { }

          

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <param name="currentStep"></param>
        /// <param name="documentType"></param>
        public void NotifyApprovalOfWorkRequest(int workRequestID)
        {
            string emailSentList = "";
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);

            List<string> emailIst = MiscDMO.GetWorkRequestApprovedNotifyList((int)GlobalVars.NotificationType.WorkRequest, workRequestID, (int)Session[GlobalVars.SESSION_USERID]).Distinct().ToList();
            
            string emailTemplate = "WorkRequestApproval.txt";
            string userEmail = string.Empty;
            string subject = "Work Request Approval";
            var senderName = "";

            
            foreach (string email in emailIst)
            {
                subject = "Work Request Approval notice for Number " + ltWR.SWRNumber + ", - " + ltWR.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = ltWR.SWRNumber.ToString();
                emailparams[1] = workRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = "Work Request";
                userEmail = email;
#if(DEBUG)
                userEmail = "rkotian1@irf.com";
#endif

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

                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Lot Traveler Approval Notification:" + email });
                }
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Work Request", OperationType = "Email", Comments = "Approvers for WorkRequest: " + emailSentList });
            }
            catch { }



        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public void NotifyfWorkRequestRevisionChange(int workRequestID)
        {
            string emailSentList = "";
            int isITARCompliant = 0;
            int currentUserID = (int)Session[GlobalVars.SESSION_USERID];
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, currentUserID);

            List<string> emailIst = MiscDMO.GetWorkRequestRevisionNotifyList((int)GlobalVars.NotificationType.WorkRequest, workRequestID, currentUserID).Distinct().ToList();

            string emailTemplate = "WorkRequestRevisionChange.txt";                                   
            string userEmail = string.Empty;
            string subject = "Work Request Revision Change";
            var senderName = "";


            foreach (string email in emailIst)
            {
                subject = "Work Request Revision Change notice for Number " + ltWR.SWRNumber + ", - " + ltWR.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = ltWR.SWRNumber.ToString();
                emailparams[1] = workRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = "Work Request";
                userEmail = email;
#if(DEBUG)
                userEmail = "rkotian1@irf.com";
#endif

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

                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "WR Revision Change Notification:" + email });
                }
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Work Request", OperationType = "Email", Comments = "Work Request Revision Change for  :" + emailSentList });
            }
            catch { }



        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public void NotifyLotTravelerRevisionChange(int ltLotID, int revisionNumber)
        {
            string emailSentList = "";
            var data = LotTravDMO.GetLotTravelerHeaderForReadOnly(ltLotID, revisionNumber);
            int currentUserID = (int)Session[GlobalVars.SESSION_USERID];
            List<string> emailIst = MiscDMO.GetLotTravelerCreationAndRevisionNotifyList(ltLotID, data.LTWorkRequestID, currentUserID).Distinct().ToList();
            
            string emailTemplate = "LotTravelerRevisionChange.txt";
            string userEmail = string.Empty;
            string subject = "Lot Traveler Revision Change";
            var senderName = "";


            foreach (string email in emailIst)
            {
                subject = "Lot Traveler Revision for SWR# " + data.SWRNumber + ", Lot# " + data.LotNumber + " - " + data.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = data.SWRNumber.ToString();
                emailparams[1] = data.LTWorkRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = data.LotNumber;
                userEmail = email;
#if(DEBUG)
                userEmail = "rkotian1@irf.com";
#endif

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

                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = data.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Lot Traveler Revision Notification:" + email });
                }
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = data.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Email", Comments = "Lot Traveler Revision Change Notification:" + emailSentList });
            }
            catch { }



        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <param name="revisionNumber"></param>
        public void NotifyLotTravelerCreation(int ltLotID, int revisionNumber)
        {
            string emailSentList = "";
            var data = LotTravDMO.GetLotTravelerHeaderForReadOnly(ltLotID, revisionNumber);
            int currentUserID = (int)Session[GlobalVars.SESSION_USERID];
            List<string> emailIst = MiscDMO.GetLotTravelerCreationAndRevisionNotifyList(ltLotID, data.LTWorkRequestID, currentUserID).Distinct().ToList();

            string emailTemplate = "LotTravelerCreation.txt";
            string userEmail = string.Empty;
            string subject = "Lot Traveler Revision Change";
            var senderName = "";


            foreach (string email in emailIst)
            {
                subject = "Lot Traveler created for SWR# " + data.SWRNumber + ", Lot# " + data.LotNumber + " - "  + data.Title;
                EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                string[] emailparams = new string[4];
                emailparams[0] = data.SWRNumber.ToString();
                emailparams[1] = data.LTWorkRequestID.ToString();
                emailparams[2] = GlobalVars.hostURL;
                emailparams[3] = data.LotNumber;
                userEmail = email;
#if(DEBUG)
                userEmail = "rkotian1@irf.com";
#endif
                try
                {
                    emailSentList += email + ",";
                    en.SendNotificationEmail(emailTemplate,  GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
                    
                }
                catch
                {
                    EventLogDMO.Add(new WinEventLog() { IssueID = data.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Lot Traveler Creation Email Notification:" + email });
                }
            }

            try
            {

                EventLogDMO.Add(new WinEventLog() { IssueID = data.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Email", Comments = "Lot Traveler Revision Change Notification:" + emailSentList });
            }
            catch { }



        }



       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="workRequestID"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        public ActionResult GetApproversList([DataSourceRequest] DataSourceRequest request, int workRequestID, byte step)
        {

            return Json(MiscDMO.GetApproversListByDocument(workRequestID, step, (int)GlobalVars.DocumentType.LotTraveler).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public void ReleaseLockOnDocument(int workRequestID)
        {
            LotTravDMO.ReleaseLockOnDocument((int)Session[GlobalVars.SESSION_USERID], workRequestID);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        public void ReleaseLockOnLotTravelerUpdateDoc(int lotID)
        {
            try
            {
                LotTravDMO.ReleaseLockOnLotTravelerUpdateDoc((int)Session[GlobalVars.SESSION_USERID], lotID);
            }
            catch
            {
                //TODO
                // unlock the current revision of the Lot traveler
                LotTravDMO.ReleaseLockOnLotTravelerUpdateDoc(-1, lotID);
            }

        }

        

        /// <summary>
        /// 
        /// </summary>
        /// <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="workRequestID"></param>
        /// <param name="reAssignApproverFrom"></param>
        /// <param name="reAssignApproverTo"></param>
        /// <param name="step"></param>
        /// <param name="docType"></param>
        public void ReAssignApproverByAdmin(int workRequestID, int reAssignApproverFrom, int reAssignApproverTo, byte step, int docType)
        {
            var email = "";
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            try
            {

                email = wfDMO.ReAssignApproval(workRequestID, reAssignApproverFrom, reAssignApproverTo, step, docType);

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

            }

         
            string emailTemplate = "WorkRequestReAssigned.txt";
            string userEmail = string.Empty;
            string subject;
            string senderName = "Work Request";

            subject = "Work Request Re-Assignment" + " - Email would be sent to " + email + " for Number " + ltWR.SWRNumber + ", - " + ltWR.Title; ;
            EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
            string[] emailparams = new string[4];
            emailparams[0] = ltWR.SWRNumber.ToString(); 
            emailparams[1] = workRequestID.ToString(); // goes into the link
            emailparams[2] = GlobalVars.hostURL;
            emailparams[3] = "Work Request";

            userEmail = email;
            //#if(DEBUG)
            //            userEmail = "rkotian1@irf.com";
            //#endif

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

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

            }
            catch
            {
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "ReAssign Approver Notification:" + email });
            }

            try
            {

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

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <param name="userIDs"></param>
        /// <param name="step"></param>
        /// <param name="docType"></param>
     
        public void ReAssignApproval(int workRequestID, int userIDs, byte step, int docType)
        {
            var email = "";
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            try
            {
                email = wfDMO.ReAssignApproval(workRequestID, (int)Session[GlobalVars.SESSION_USERID], userIDs, step, docType);

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

            }

          

            string emailTemplate = "WorkRequestReAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Work Request Re-Assignment";
            string senderName = "Work Request";

            subject = "Work Request Re-Assignment" + " - Email would be sent to " + email + " for Number " + ltWR.SWRNumber + ", - " + ltWR.Title; ;
            EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
            string[] emailparams = new string[4];
            emailparams[0] = ltWR.SWRNumber.ToString();
            emailparams[1] = workRequestID.ToString();
            emailparams[2] = GlobalVars.hostURL;
            emailparams[3] = "Work Request";
            userEmail = email;
            //#if(DEBUG)
            //            userEmail = "rkotian1@irf.com";
            //#endif

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

            }
            catch
            {
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "ReAssign Approval Notification:" + email });
            }
            try
            {

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

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="issueID"></param>
        /// <param name="step"></param>
        /// <param name="userIDs"></param>
        /// <param name="docType"></param>
        /// <param name="ecnTypeString"></param>
        public void AddAdditionalApproval(int workRequestID, byte step, string userIDs, int docType)
        {
            string emailSentList = "";
            var emailArray = "";
            int isITARCompliant = 0;
            LTWorkRequest ltWR = LotTravDMO.GetLTWorkRequestItemForRead(workRequestID, out isITARCompliant, (int)Session[GlobalVars.SESSION_USERID]);
            try
            {
                emailArray = wfDMO.AddAdditionalApproval(workRequestID, userIDs, step, docType);
            }
            catch (Exception e)
            {
                string exceptionString = e.Message.ToString().Trim().Length > 500 ? "Issue=" + workRequestID.ToString() + " Step:" + step + " " + " Userid:" + userIDs + " " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
                Functions.WriteEvent(@User.Identity.Name + "\r\n AddAdditionalApproval\r\n" + e.Message.ToString(), System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Work Request", OperationType = "Error", Comments = "AddAdditionalApproval - " + exceptionString });
                throw new Exception(e.Message);
            }
           
           

            string emailTemplate = "WorkRequestAssigned.txt";
            string userEmail = string.Empty;
            string subject = "Work Request Assignment";
            string senderName = "Work Request";


            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 = "Work Request Assignment notice for Number " + workRequestID + ", - " + ltWR.Title;
                    EmailNotification en = new EmailNotification(subject, ConfigurationManager.AppSettings["EmailTemplatesPath"]);
                    string[] emailparams = new string[4];
                    emailparams[0] = ltWR.SWRNumber.ToString();
                    emailparams[1] = workRequestID.ToString();
                    emailparams[2] = GlobalVars.hostURL;
                    emailparams[3] = "Work Request";

                    userEmail = email;
                    //#if(DEBUG)
                    //    userEmail = "rkotian1@irf.com";
                    //#endif

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

                    }
                    catch
                    {
                        EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "Addtional Approver Notification:" + email });
                    }

                }

            }
            try
            {
                EventLogDMO.Add(new WinEventLog() { IssueID = ltWR.SWRNumber, UserID = @User.Identity.Name, DocumentType = "Work Request", OperationType = "Email", Comments = "Additonal Approver: " + emailSentList });
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lotNumbers"></param>
        /// <param name="issueID"></param>
        /// <returns></returns>
        public JsonResult AddLots(string lotNumbers, int workRequestID)
        {
            IssueWithExistingLotsViewModel issueWEL = new IssueWithExistingLotsViewModel();
            try
            {
                if (lotNumbers.Length > 0)
                {
                    string[] tempLots = lotNumbers.Split(new char[] { '~' });
                    foreach (string lotNumber in tempLots)
                    {
                        LTLot l = new LTLot();
                        l.LotNumber = lotNumber;
                        l.WorkRequestID = workRequestID;
                        l.LotUploadedBy = (int)Session[GlobalVars.SESSION_USERID];
                        LotTravDMO.InsertLot(l);
                        if (l.WRWithExistingLot != 0)
                        {
                            issueWEL.LotList += l.LotNumber + "~";
                            issueWEL.IssuesList += l.WRWithExistingLot.ToString() + "~";
                        }
                    }
                }

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

            }

            return Json(issueWEL, JsonRequestBehavior.AllowGet);
        }

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

            //IEnumerable<Lot> lotlist = MiscDMO.SearchLots(searchText, searchBy);

            return Json(lotlist, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public JsonResult SearchParts(string searchText)
        {
            List<String> partList = MiscDMO.SearchLTParts(searchText.Trim()).Select(x => x.WIPPartData).ToList<String>();
            return Json(partList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="workRequestID"></param>
        /// <returns></returns>
        public ActionResult GetLotList([DataSourceRequest] DataSourceRequest request,int workRequestID)
        {
            return Json(LotTravDMO.GetLotList(workRequestID).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <param name="workRequestID"></param>
        public void CreateTaveler(int ltLotID, int workRequestID)
        {
            try
            {
                LotTravDMO.CreateTraveler(ltLotID, workRequestID, (int)Session[GlobalVars.SESSION_USERID]);
                
               // Thread.Sleep(10000);
            NotifyLotTravelerCreation(ltLotID, -1);

            }
            catch(Exception ex)
            {
                EventLogDMO.Add(new WinEventLog() { IssueID = workRequestID, UserID = @User.Identity.Name, DocumentType = "LotTraveler", OperationType = "CreateTaveler", Comments = ex.Message });
                throw new Exception(ex.Message);
            }


        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public JsonResult GetLotTravelerHeader(int ltLotID)
        {
            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForUpdate(ltLotID, (int)Session[GlobalVars.SESSION_USERID]);
            //data.Revisions = LotTravDMO.GetLotTravRevisions(ltLotID).Select(s => new SelectListItem { Value = s.ID.ToString(), Text = s.RevisionNumber.ToString() }).ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public ActionResult GetLotTravRevisions(int ltLotID)
        {
            //return Json(LotTravDMO.GetLotTravRevisions(ltLotID).ToDataSourceResult(request));
            return Json(LotTravDMO.GetLotTravRevisions(ltLotID), JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public ActionResult DisplayLotTraveler(int ltLotID)
        {
            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForUpdate(ltLotID, (int)Session[GlobalVars.SESSION_USERID]);
            
            ViewBag.LotTravRevisionList = LotTravDMO.GetLotTravRevisions(ltLotID);
            ViewBag.LotList = LotTravDMO.GetLotsWithTraveler(data.LTWorkRequestID);

            if (data.RecordLockIndicator && data.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID])
            {
                // redirect to a readonly Lot Travler
                return Content("Readonly" + "~" + data.LotTravCurrentRevision.ToString());
            }
            else
                return Content("Update");

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public ActionResult LotTravelerUpdate(int ltLotID)
        {
            
            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForUpdate(ltLotID, (int)Session[GlobalVars.SESSION_USERID]);

            if (data.TravelerClosedDate != null)
            {
                return RedirectToAction("LotTravelerReadOnly", new { ltLotID = ltLotID, revisionNumber = -1 });
            }

            ViewBag.LotTravRevisionList = LotTravDMO.GetLotTravRevisions(ltLotID);
            ViewBag.LotList = LotTravDMO.GetLotsWithTraveler(data.LTWorkRequestID);

            Session["CreateLotTravNewRevision"] = "true";
            return View(data);

        }

        /// <summary>
        /// /
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <param name="revisionNumber"></param>
        /// <returns></returns>
        public ActionResult LotTravelerReadonly(int ltLotID, int revisionNumber)
        {
            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForReadOnly(ltLotID, revisionNumber);

            if (data.IsCurrentRevision && (!data.RecordLockIndicator))
            {
                return RedirectToAction("LotTravelerUpdate", new { ltLotID = ltLotID });
            }

            ViewBag.LotTravRevisionList = LotTravDMO.GetLotTravRevisions(ltLotID);
            ViewBag.LotList = LotTravDMO.GetLotsWithTraveler(data.LTWorkRequestID);
            return View(data);

        }



        public ActionResult DisplayLotTravelerForExecute(int ltLotID)
        {
            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForUpdate(ltLotID, (int)Session[GlobalVars.SESSION_USERID]);
            ViewBag.LotTravRevisionList = LotTravDMO.GetLotTravRevisions(ltLotID);
            ViewBag.LotList = LotTravDMO.GetLotsWithTraveler(data.LTWorkRequestID);

            if (data.RecordLockIndicator && data.RecordLockedBy != (int)Session[GlobalVars.SESSION_USERID])
            {
                // redirect to a readonly Lot Travler
                return Content("Readonly" + "~" + data.LotTravCurrentRevision.ToString());
            }
            else
                return Content("Execute");

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public ActionResult LotTravelerExecute(int ltLotID)
        {

            LTLotTravelerHeaderViewModel data = new LTLotTravelerHeaderViewModel();
            data = LotTravDMO.GetLotTravelerHeaderForUpdate(ltLotID, (int)Session[GlobalVars.SESSION_USERID]);
            ViewBag.LotTravRevisionList = LotTravDMO.GetLotTravRevisions(ltLotID);
            ViewBag.LotList = LotTravDMO.GetLotsWithTraveler(data.LTWorkRequestID);


            return View(data);

        }


        public ActionResult GetLotTravHoldSteps([DataSourceRequest] DataSourceRequest request, int LotID)
        {
            return Json(LotTravDMO.GetLotTravHoldSteps(LotID).ToDataSourceResult(request));
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="LotID"></param>
        /// <returns></returns>
        public ActionResult GetLotTravHoldStepsPending([DataSourceRequest] DataSourceRequest request, int LotID)
        {
            return Json(LotTravDMO.GetLotTravHoldStepsPending(LotID).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="LotID"></param>
        /// <returns></returns>
        public ActionResult GetLotTravHoldStepsCompleted([DataSourceRequest] DataSourceRequest request, int LotID)
        {
            return Json(LotTravDMO.GetLotTravHoldStepsCompleted(LotID).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentHoldStepID"></param>
        /// <param name="taskComments"></param>
        /// <param name="taskcompleted"></param>
        public void UpdateLotTravlerExecution(int currentHoldStepID, string taskComments, bool taskcompleted)
        {
            LotTravDMO.UpdateLotTravlerExecution(currentHoldStepID, taskComments, taskcompleted, (int)Session[GlobalVars.SESSION_USERID]);

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="LotID"></param>
        /// <param name="revisionNumber"></param>
        /// <returns></returns>
        public ActionResult GetLotTravHoldStepsByRevision([DataSourceRequest] DataSourceRequest request, int LotID, int revisionNumber)
        {
            return Json(LotTravDMO.GetLotTravelerHolStepsByRevision(LotID, revisionNumber).ToDataSourceResult(request));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltHoldStep"></param>
        public ActionResult UpdateLotTravHoldStepRevision(LTLotTravelerHoldSteps ltHoldStepObj)
        {
            int prevLotTravRevID = ltHoldStepObj.LotTravelerRevisionID;
            int newLotTravRevID = 1;
            try
            {
                if (Session["CreateLotTravNewRevision"].ToString() == "true")
                {
                    // Create a new Revision
                    Session["CreateLotTravNewRevision"] = "false";

                    //int result = LotTravDMO.CanAddLocationOperation(ltHoldStepObj);
                    //if (result == -1)
                    //{
                    //    Session["CreateLotTravNewRevision"] = "true";
                    //    throw new Exception("Cannot set to this Hold Step as the step has already been passed in the Mfg Process.");
                    //}

                    newLotTravRevID = LotTravDMO.CreateLotTravelerRevision(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);

                    try
                    {
                        
                        LotTravDMO.UpdateRevisedLotTravelerHoldStep(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);                        
                    }
                    catch
                    {
                        
                        LotTravDMO.RestoreLotTravToPrevRevision(prevLotTravRevID, newLotTravRevID);
                        throw new Exception("There was a problem while creating the revision, Please try againm,. \n If the problem persist please contact the Site Administrator");
                    }

                    // send a notification
                    // - 1 indicates to return the current revision
                    NotifyLotTravelerRevisionChange(ltHoldStepObj.LTLotID, -1);
                }
                else
                {
                    int result = LotTravDMO.UpdateLotTravelerHoldStep(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);
                    if (result == -1)
                    {
                        throw new Exception("Cannot add the Hold Step as the step has already been passed in the Mfg Process.");
                    }
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }

            return Content(GlobalVars.SUCCESS);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltHoldStepObj"></param>
        public ActionResult InsertLotTravHoldStepRevision(LTLotTravelerHoldSteps ltHoldStepObj)
        {
            int prevLotTravRevID = ltHoldStepObj.LotTravelerRevisionID;
            int newLotTravRevID = -1;
            try
            {
                if (Session["CreateLotTravNewRevision"].ToString() == "true")
                {
                    // Create a new Revision
                    Session["CreateLotTravNewRevision"] = "false";

                    int result = LotTravDMO.CanAddLocationOperation(ltHoldStepObj);
                    if (result == -1)
                    {
                        Session["CreateLotTravNewRevision"] = "true";
                        throw new Exception("Cannot add the Hold Step as the step has already been passed in the Mfg Process.");
                    }
                    newLotTravRevID = LotTravDMO.CreateLotTravelerRevision(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);
                    ltHoldStepObj.LotTravelerRevisionID = newLotTravRevID;
                    
                    try
                    {
                        
                        LotTravDMO.InsertLotTravelerHoldStep(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);
                    }
                    catch
                    {
                        //roll back the revision creation
                        LotTravDMO.RestoreLotTravToPrevRevision(prevLotTravRevID, newLotTravRevID);
                        throw new Exception("There was a problem while creating the revision, Please logout and log back and then retry. \n If the problem persist please contact the Site Administrator");
                    }


                    // send a notification
                    // - 1 indicates to return the current revision
                    NotifyLotTravelerRevisionChange(ltHoldStepObj.LTLotID, -1);


                }
                else
                {
                    int result = LotTravDMO.InsertLotTravelerHoldStep(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);
                    if (result == -1)
                    {
                        throw new Exception("Cannot add the Hold Step as the step has already been passed in the Mfg Process.");
                    }
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }

            return Content(GlobalVars.SUCCESS);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltHoldStepObj"></param>
        public ActionResult DeleteLotTravHoldStepRevision(LTLotTravelerHoldSteps ltHoldStepObj)
        {
            int prevLotTravRevID = ltHoldStepObj.LotTravelerRevisionID;
            int newLotTravRevID = -1;
            try
            {
                //int newLotTravRevisionAttribID = 0;
                if (Session["CreateLotTravNewRevision"].ToString() == "true")
                {
                    // Create a new Revision
                    Session["CreateLotTravNewRevision"] = "false";
                    newLotTravRevID = LotTravDMO.CreateLotTravelerRevision(ltHoldStepObj, (int)Session[GlobalVars.SESSION_USERID]);

                    try
                    {
                        LotTravDMO.DeleteLotTravHoldStepRevision(ltHoldStepObj.ID);
                    }
                    catch
                    {
                        //roll back the revision creation
                        LotTravDMO.RestoreLotTravToPrevRevision(prevLotTravRevID, newLotTravRevID);
                        throw new Exception("There was a problem while creating the revision, Please logout and log back and then retry. \n If the problem persist please contact the Site Administrator");
                    }

                    

                    // send a notification
                    // - 1 indicates to return the current revision
                    NotifyLotTravelerRevisionChange(ltHoldStepObj.LTLotID, -1);
                }
                else
                {
                    LotTravDMO.DeleteLotTravHoldStep(ltHoldStepObj.ID);
                }
            }
            catch(Exception ex)
            {
                return Content(ex.Message);
            }


            return Content(GlobalVars.SUCCESS);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        public void DeleteLot(int ltLotID)
        {
            LotTravDMO.DeleteLot(ltLotID);
        }

        ///
        public ActionResult DisplayLotTravlerPdf(int ltLotID, int revisionNumber)
        {
            //DateTime? expDt;
            LotTravelerPdf traveler = new LotTravelerPdf();
            try
            {

                traveler = LotTravDMO.GetLotTravlerPdf(ltLotID, revisionNumber);               
                
                // To render a PDF instead of an HTML, all we need to do is call ViewPdf instead of View. This
                // requires the controller to be inherited from MyController instead of MVC's Controller.
                return this.ViewPdf("", "LotTravelerPDF", traveler);
               
            }
            catch (Exception ex)
            {
                EventLogDMO.Add(new WinEventLog() { IssueID = traveler.SWRNumber, UserID = @User.Identity.Name, DocumentType = "LotTraveler", OperationType = "Generate PDF", Comments = ex.Message });
                traveler = null;
                return Content("");

            }

           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="swrNumber"></param>
        /// <returns></returns>
        public ActionResult ApprovalLogHistory_Read([DataSourceRequest] DataSourceRequest request, int swrNumber)
        {
            return Json(LotTravDMO.GetWorkReqApprovalLogHistory(swrNumber).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="swrNumber"></param>
        /// <returns></returns>
        public ActionResult GetWorkRequestRevisionHistory([DataSourceRequest] DataSourceRequest request, int swrNumber)
        {
            return Json(LotTravDMO.GetWorkReqRevisionHistory(swrNumber).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="ltLotID"></param>
        /// <returns></returns>
        public ActionResult GetLotTravelerRevisionHistory([DataSourceRequest] DataSourceRequest request, int lotID)
        {
            return Json(LotTravDMO.GetLotTravelerRevisionHistory(lotID).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }


        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LotTravHoldStepAttachment"></param>
        /// <param name="ltHoldStepID"></param>
        /// <param name="swrNo"></param>
        /// <param name="docType"></param>
        /// <param name="revComments"></param>
        /// <returns></returns>
        public ActionResult LotTravHoldStepAttachSaveRev(IEnumerable<HttpPostedFileBase> LotTravHoldStepAttachment, int ltHoldStepID, int swrNo, string docType, string revComments)
        {
            // The Name of the Upload component is "files"

            int prevLotTravRevID = -1 ;
            int newLotTravRevID = -1;

            bool newRevision = false;
            LTLotTravelerHoldSteps model = LotTravDMO.GetLotTravHoldStep(ltHoldStepID);
            prevLotTravRevID = model.LotTravelerRevisionID;

            model.RevisionComments = revComments;

            if (Session["CreateLotTravNewRevision"].ToString() == "true")
            {
                // Create a new Revision
                newRevision = true;
                Session["CreateLotTravNewRevision"] = "false";
                newLotTravRevID = LotTravDMO.CreateLotTravelerRevision(model, (int)Session[GlobalVars.SESSION_USERID]);
                model.LotTravelerRevisionID = newLotTravRevID;
               

            }


            try
            {
                if (LotTravHoldStepAttachment != null)
                {
                    foreach (var file in LotTravHoldStepAttachment)
                    {
                        // Some browsers send file names with full path.
                        // We are only interested in the file name.

                        // TODO
                        //int currentRevision = 1;
                        var fileName = Path.GetFileName(file.FileName);
                        var fileExtension = Path.GetExtension(file.FileName);
                        //var physicalPath = Path.Combine(Server.MapPath("~/UserUploads"), fileName);       
                        DirectoryInfo di;
                        var SWRPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                        di = new DirectoryInfo(SWRPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        //var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision;
                        var SWR_RevPhysicalPath = Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo;
                        di = new DirectoryInfo(SWR_RevPhysicalPath);
                        if (!di.Exists)
                            di.Create();

                        var guid = Guid.NewGuid().ToString();
                        //var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\Rev" + currentRevision + @"\", guid + fileExtension);
                        var physicalPath = Path.Combine(Functions.GetAttachmentFolder() + @"LotTraveler\" + swrNo + @"\", guid + fileExtension);



                        file.SaveAs(physicalPath);
                        LTLotTravAttachment attach = new LTLotTravAttachment()
                        {
                            FileGUID = guid,
                            LTLotTravHoldStepID = ltHoldStepID,
                            LotTravelerRevisionID = newLotTravRevID,
                            FileName = fileName,
                            UploadedByID = (int)Session[GlobalVars.SESSION_USERID],
                            DocType = docType
                        };
                        if (newRevision)
                        {
                            try
                            {
                                LotTravDMO.InsertLotTravLotHoldStepAttachmentRevision(attach);
                            }
                            catch
                            {
                                //roll back the revision creation
                                LotTravDMO.RestoreLotTravToPrevRevision(prevLotTravRevID, newLotTravRevID);
                                throw new Exception("There was a problem while creating the revision, Please logout and log back and then retry. \n If the problem persist please contact the Site Administrator");
                            }



                        }
                        else
                            LotTravDMO.InsertLotTravLotHoldStepAttachment(attach);

                    }

                    // send a notification
                    // - 1 indicates to return the current revision
                    if (newRevision)
                    {
                        NotifyLotTravelerRevisionChange(model.LTLotID, -1);
                    }
                }
            }
            catch (Exception e)
            {

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

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


            return Content("");
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="workRequestID"></param>
        /// <returns></returns>
        public bool IsWorkRequestDocLockedByUser(int workRequestID )
        {
            int result = LotTravDMO.IsWorkRequestDocLockedByUser(workRequestID, (int)Session[GlobalVars.SESSION_USERID]);
            if (result == 0)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ltLotID"></param>
        public ActionResult CloseTraveler(int ltLotID, string reason) 
        {
            int result = LotTravDMO.CloseTraveler(ltLotID, (int)Session[GlobalVars.SESSION_USERID], reason);

            if (result == 1)
                return Content("C");
            else
                return Content("O");
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="attachmentID"></param>
        /// <param name="ecnNumber"></param>
        /// <returns></returns>
        public FileResult DownloadFile(string fileGuid, string swrNumber, int typeOfDoc)
        {

            string fileName = LotTravDMO.GetFileName(fileGuid, typeOfDoc);

            string fileExtension = fileName.Substring(fileName.LastIndexOf("."), fileName.Length - fileName.LastIndexOf("."));

            string ecnFolderPath = Functions.GetAttachmentFolder() + "LotTraveler\\" + swrNumber.ToString();
            var sDocument = Path.Combine(ecnFolderPath, fileGuid + fileExtension);

            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);
        }

        [HttpPost]
        public void ReAssignOriginatorByAdmin(int workRequestID, string comments, int newOriginatorId)
        {
            if (Session[GlobalVars.IS_ADMIN] == null)
                throw new Exception("Permission denied");

            try
            {
                LotTravDMO.ReassignOriginator(workRequestID, newOriginatorId, 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();
                if (exceptionString.Length > 450) exceptionString = exceptionString.Substring(0, 450);
                Functions.WriteEvent(@User.Identity.Name + "\r\n ReAssignOriginatorByAdmin\r\n" + workRequestID.ToString() + "\r\n" + detailedException, System.Diagnostics.EventLogEntryType.Error);
                EventLogDMO.Add(new WinEventLog() { IssueID = workRequestID, UserID = @User.Identity.Name, DocumentType = "Lot Traveler", OperationType = "Error", Comments = "ReAssignOriginatorByAdmin - " + exceptionString });
                throw new Exception(e.Message);
            }
        }
    }
}