protected ScanViewModel ScanModelPrinter(ScanViewModel model)
 {
     if (model == null)
     {
         model = new ScanViewModel();
     }
     LoadPrinterInfo(model);
     return model;
 }
        public ActionResult Scan(ScanViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            try
            {
                ProductionScan newScan;

                ProductionJob scanJob = (from j in PrdnDBContext.ProductionJobs.Include("Product").Include("Priority").Include("Run.PrdnType").Include("Run.PrdnOrder")
                                         where j.SerialNo == model.SerialNo
                                         select j).FirstOrDefault();

                if (scanJob == null)
                {
                    //string msg = SystemExtensions.Sentence(LocalStr.SerialNo, model.SerialNo, LocalStr.verbIsNot, LocalStr.valid);
                    newScan = AddScan(model.SerialNo, ScanResult.InvalidScanValue);
                    if (newScan != null)
                    {
                        ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), ScanViewModel.ResultMessage(newScan).SafeSub(0, 200));
                    }
                    return LoadedScanView(model);
                }

                CompleteJobResult result = CompleteJob(scanJob);

                if (result.Type == ActionUpdateType.Success)
                {
                    model = ScanModelPrinter(model);
                    if (model.IsNotNull(m => m.LabelPrinterID))
                    {
                        try
                        {
                            PrintJobLabel(model, scanJob);

                            newScan = AddScan(model.SerialNo, result.PrintedScanResult(), scanJob.Status);
                        }
                        catch (Exception ex)
                        {
                            string msg = (ex.GetType().Name + " - " + ex.Message).SafeSub(0, 450);
                            if (AddScan(model.SerialNo, result.NotPrintedScanResult(), scanJob.Status, msg) != null)
                            {
                                ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), msg.SafeSub(0, 200));
                            }
                        }
                    }
                    else
                    {
                        newScan = AddScan(model.SerialNo, result.NotPrintedScanResult(), scanJob.Status, SystemExtensions.Sentence(LocalStr.No, LocalStr.ScanPrinter, LocalStr.Assigned));
                    }
                }
                else if (result.Type == ActionUpdateType.Invalid)
                {
                    CompleteCstJobResult cstResult = result as CompleteCstJobResult;
                    if (cstResult.IfNotNull(r => r.InvItemID) != null)      // cst job result with an item id means the item already exists
                    {
                        newScan = AddScan(model.SerialNo, ScanResult.ItemExists, scanJob.Status);
                        if (newScan != null)
                        {
                            ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), ScanViewModel.ResultMessage(newScan).SafeSub(0, 200));
                        }
                        return LoadedScanView(model);
                    }
                    else
                    {
                        newScan = AddScan(model.SerialNo, ScanResult.InvalidStatus, scanJob.Status);
                        if (newScan != null)
                        {
                            ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), ScanViewModel.ResultMessage(newScan).SafeSub(0, 200));
                        }
                        return LoadedScanView(model);
                    }
                }
                else if (result.Type == ActionUpdateType.Exception)
                {
                    //string msg = SystemExtensions.Sentence(result.Message.SafeSub(0, 450));
                    newScan = AddScan(model.SerialNo, ScanResult.ScanException, scanJob.Status);
                    if (newScan != null)
                    {
                        ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), ScanViewModel.ResultMessage(newScan).SafeSub(0, 200));
                    }
                    return LoadedScanView(model);
                }
            }
            catch (Exception ex)
            {
                string msg = (ex.GetType().Name +" - "+ ex.Message).SafeSub(0, 450);
                if (AddScan(model.SerialNo, ScanResult.ScanException, null, msg) != null)
                {
                    ModelState.AddModelError(model.FullPropertyName(s => s.SerialNo), msg.SafeSub(0, 200));
                }
                return LoadedScanView(model);
            }

            model.SerialNo = null;
            ModelState.Remove(model.FullPropertyName(m => m.SerialNo));

            return LoadedScanView(model);
        }
 protected ActionResult LoadedScanView(ScanViewModel model)
 {
     model = ScanModelPrinter(model);
     model.Scans = ScanList(fromDt: DateTime.Today.Date, take: 30);
     return View(model);
 }
 public ActionResult Scan()
 {
     ScanViewModel model = new ScanViewModel();
     return LoadedScanView(model);
 }