protected void loadViewJobForPost(EditPrdnJobViewModel viewJob)
 {
     LoadViewJobProdLists(viewJob);
     AssignViewJobLookupListsFuncs(viewJob);
     if (viewJob.IsNotNull(j => j.EditWorksheet))
     {
         string modKey = viewJob.FullPropertyName(m => m.EditWorksheet.Editable);
         ModelState.Remove(modKey);
         viewJob.EditWorksheet.Editable = false;
     }
     ProcessAttacherForPost(viewJob.Attacher);
 }
        protected bool JobModelValidateOtherJobs(EditPrdnJobViewModel viewJob)
        {
            bool isValid = true;
            string dbCancStr = PrdnJobStatus.Canceled.DbValStr();

            using (PrdnEntities PrdnDBContext = new PrdnEntities())
            {
                if (!String.IsNullOrEmpty(viewJob.CstRequestID))
                {
                    var jobReqQry = from j in PrdnDBContext.ProductionJobs
                                    where j.CstRequestID == viewJob.CstRequestID
                                    && j.CustID == viewJob.CustID
                                    && j.StatusStr != dbCancStr
                                    select j;

                    if (viewJob.ID != null)
                    {
                        jobReqQry = from j in jobReqQry
                                    where j.ID != viewJob.ID
                                    select j;
                    }

                    var reqJob = (from j in jobReqQry
                                       select new
                                       {
                                           j.ID,
                                           j.SerialNo,
                                           j.Run.PrdnOrderNo,
                                           j.Run.PrdnType.Code,
                                           j.StatusStr,
                                       }).FirstOrDefault();

                    if (reqJob != null)
                    {
                        ModelState.AddModelError(viewJob.FullPropertyName(j => j.CstRequestID),
                            SystemExtensions.Sentence(LocalStr.Job, reqJob.PrdnOrderNo + reqJob.Code, LocalStr.JobID, reqJob.ID.ToString(), LocalStr.SerialNo, reqJob.SerialNo, LocalStr.isAlreadyAssignedTo, LocalStr.RequestID, viewJob.CstRequestID));
                        isValid = false;
                    }
                }

                if (isValid && !String.IsNullOrEmpty(viewJob.OrderNo) && (viewJob.OrderLine != null) && !String.IsNullOrEmpty(viewJob.ProdCD))
                {
                    var jobOrdQry = from j in PrdnDBContext.ProductionJobs
                                    where j.OrderNo == viewJob.OrderNo
                                    && j.CustID == viewJob.CustID
                                    && j.OrderLine == viewJob.OrderLine
                                    && j.ProdCD == viewJob.ProdCD
                                    && j.StatusStr != dbCancStr
                                    select j;

                    if (viewJob.ID != null)
                    {
                        jobOrdQry = from j in jobOrdQry
                                    where j.ID != viewJob.ID
                                    select j;
                    }

                    var ordJob = (from j in jobOrdQry
                                       select new
                                       {
                                           j.ID,
                                           j.SerialNo,
                                           j.Run.PrdnOrderNo,
                                           j.Run.PrdnType.Code,
                                           j.StatusStr,
                                       }).FirstOrDefault();

                    if (ordJob != null)
                    {
                        ModelState.AddModelError(viewJob.FullPropertyName(j => j.CstRequestID),
                            SystemExtensions.Sentence(LocalStr.Job, ordJob.PrdnOrderNo + ordJob.Code, LocalStr.JobID, ordJob.ID.ToString(), LocalStr.SerialNo, ordJob.SerialNo,
                                LocalStr.isAlreadyAssignedTo, LocalStr.OrderNo, viewJob.OrderNo, LocalStr.OrderLine, viewJob.OrderLineInt.ToString(), LocalStr.ProductCD, viewJob.ProdCD));
                        isValid = false;
                    }
                }
            }
            return isValid;
        }
        protected bool JobModelValidate(EditPrdnJobViewModel viewJob)
        {
            bool isValid = (ModelState.IsValid);
            if (isValid)
            {
                if (!String.IsNullOrEmpty(viewJob.ProdCD))
                {
                    var prodObj = (from p in PrdnDBContext.Products
                                   where (p.ProdTypeCD == viewJob.ProdTypeCD && p.ProdCD == viewJob.ProdCD)
                                   select new { p.ProdCD })
                                   .FirstOrDefault();
                    if (prodObj == null)
                    {
                        ModelState.AddModelError(viewJob.FullPropertyName(j => j.ProdCD), SystemExtensions.Sentence(viewJob.ProdCD, LocalStr.verbIsNot, LocalStr.valid));
                        isValid = false;
                    }
                }
            }
            if (isValid)
            {
                if ((viewJob.CustID == @PrdnDataHelper.PrdnCustIDCST) && (!String.IsNullOrEmpty(viewJob.OrderNo)))
                {
                    isValid = JobModelValidateCstOrder(viewJob);
                }
            }
            if (isValid)
            {
                isValid = JobModelValidateOtherJobs(viewJob);
            }

            return isValid;
        }
        protected bool JobModelValidateCstOrder(EditPrdnJobViewModel viewJob)
        {
            var lnQry = from l in PrdnDBContext.CstOrderLines
                        where l.OrderLine == viewJob.OrderLine
                        select new
                        {
                            l.OrderNo,
                            l.OrderLine,
                            l.OrderLineID,
                            l.ProdCD
                        };

            var ordMatch = (from o in PrdnDBContext.CstOrders
                            where o.OrderNo == viewJob.OrderNo
                            join l in lnQry on o.OrderNo equals l.OrderNo into temp
                            from ln in temp.DefaultIfEmpty()
                            select new
                            {
                                o.OrderNo,
                                line = ln
                            }).FirstOrDefault();

            bool isValid = false;
            if (ordMatch == null)
            {
                ModelState.AddModelError(viewJob.FullPropertyName(j => j.OrderNo), SystemExtensions.Sentence(LocalStr.OrderNo, viewJob.OrderNo, LocalStr.verbIsNot, LocalStr.valid));
            }
            else if (ordMatch.line == null)
            {
                ModelState.AddModelError(viewJob.FullPropertyName(j => j.OrderLineInt), SystemExtensions.Sentence(LocalStr.OrderNo, viewJob.OrderNo, LocalStr.OrderLine, viewJob.OrderLineInt.ToString(), LocalStr.verbIsNot, LocalStr.valid));
            }
            else if (ordMatch.line.ProdCD != viewJob.ProdCD)
            {
                ModelState.AddModelError(viewJob.FullPropertyName(j => j.ProdCD), SystemExtensions.Sentence(LocalStr.OrderNo, viewJob.OrderNo, LocalStr.OrderLine, viewJob.OrderLineInt.ToString(), LocalStr.ProductCD, viewJob.ProdCD, LocalStr.doesNotMatch, ordMatch.line.ProdCD));
            }
            else
            {
                isValid = true;
                viewJob.OrderLineID = ordMatch.line.OrderLineID;
            }
            return isValid;
        }
        public ActionResult Edit(EditPrdnJobViewModel editViewJob, IEnumerable<HttpPostedFileBase> uploadedFiles)
        {
            ProcessAttacherForSave(editViewJob.Attacher, uploadedFiles);

            ProductionJob editJob = (from j in PrdnDBContext.ProductionJobs.Include("PrdnInvItem")
                                     where j.ID == editViewJob.ID
                                     select j).FirstOrDefault();

            if (editJob == null)
            {
                return ErrMsgView(SystemExtensions.Sentence("Error Saving Job: ", LocalStr.JobID, editViewJob.IfNotNull(j => j.ID).ToString(), LocalStr.verbIsNot, LocalStr.valid));
            }

            if (editViewJob.EditStatus == PrdnJobStatus.New)
            {
                editViewJob.EditStatus = editJob.Status;
            }
            else
            if ((editJob.Status != editViewJob.EditStatus) && (!EditPrdnJobViewModel.FromStatusTo(editJob.Status, editViewJob.EditStatus)))
            {
                ModelState.AddModelError(editViewJob.FullPropertyName(j => j.EditStatus),
                    SystemExtensions.Sentence(LocalStr.Status, LocalStr.Change, LocalStr.to, editViewJob.EditStatus.Description(),
                    LocalStr.verbIsNot, LocalStr.valid));
            }

            JobModelValidate(editViewJob);

            if (ModelState.IsValid)
            {
                if (editJob.IsNotNull(j => j.PrdnInvItem))
                {
                    return ErrMsgView(SystemExtensions.Sentence("Error Saving Job: ", editJob.PrdnInvItem.SerialNo, " exists in inventory."));
                }
                EnsurePrdnDBContextOpen();
                using (var transaction = PrdnDBContext.Connection.BeginTransaction())
                {
                    try
                    {
                        Mapper.Map<EditPrdnJobViewModel, ProductionJob>(editViewJob, editJob);

                        if (editJob.Status != editViewJob.EditStatus)
                        {
                            editJob.UpdateStatus((PrdnJobStatus)editViewJob.EditStatus, GetCurrentUserID());
                        }

                        UpdateJobWorksheet(editJob, editViewJob);
                        UpdateJobAttachments(editJob, editViewJob.Attacher);
                        PrdnDBContext.SaveChanges();

                        if ((editViewJob.EditRunSeqNo != null) && (editJob.RunSeqNo != editViewJob.EditRunSeqNo))
                        {
                            InsertRunSeqs(PrdnDBContext, editJob, (int)editViewJob.EditRunSeqNo);
                            ReSequenceRun(editJob.RunID);
                            PrdnDBContext.SaveChanges();
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return ErrMsgView("Error Saving Job: " + ex.Message);
                    }
                }

                return RedirectIfLocal(editViewJob.UrlReturn,
                    () => RedirectToAction(actionName: "Jobs", controllerName: "Schedule", routeValues: new { id = editViewJob.RunID }));
                //if (this.IsLocalUrl(editViewJob.UrlReturn)) { return Redirect(editViewJob.UrlReturn);} else {return RedirectToAction(actionName: "Jobs", controllerName: "Schedule", routeValues: new { RunID = editViewJob.RunID });}
            }
            else
            {
                //assignViewJobLookupListsFuncs(editViewJob);    //ProcessAttacherForPost(editViewJob.Attacher);
                loadViewJobForPost(editViewJob);
                return View(editViewJob);
            }
        }