public async Task <ActionResult> DownloadInvoiceNoIntervalCsv(InvoiceNoViewModel viewModel)
        {
            ViewResult result = (ViewResult) await InquireInvoiceNoIntervalAsync(viewModel);

            return(View("~/Views/Invoice/Module/DownloadInvoiceNoIntervalCsv.ascx", result.Model));
        }
        public async Task <ActionResult> CommitInvoiceNoInterval(InvoiceNoViewModel viewModel)
        {
            ViewBag.ViewModel = viewModel;
            var profile = await HttpContext.GetUserAsync();

            if (!viewModel.BranchID.HasValue)
            {
                ModelState.AddModelError("BranchID", "請選擇分店!!");
            }

            viewModel.TrackCode = viewModel.TrackCode.GetEfficientString();
            if (viewModel.TrackCode == null || !Regex.IsMatch(viewModel.TrackCode, "[A-Z]{2}"))
            {
                ModelState.AddModelError("TrackCode", "字軌錯誤!!");
            }

            if (!viewModel.Year.HasValue)
            {
                ModelState.AddModelError("Year", "請選擇年份!!");
            }

            if (!viewModel.PeriodNo.HasValue)
            {
                ModelState.AddModelError("PeriodNo", "請選擇期別!!");
            }

            var trackCode = models.GetTable <InvoiceTrackCode>()
                            .Where(t => t.TrackCode == viewModel.TrackCode && t.Year == viewModel.Year && t.PeriodNo == viewModel.PeriodNo).FirstOrDefault();

            var table = models.GetTable <InvoiceNoInterval>();
            var item  = table.Where(i => i.IntervalID == viewModel.IntervalID).FirstOrDefault();

            if (!viewModel.StartNo.HasValue || !(viewModel.StartNo >= 0 && viewModel.StartNo < 100000000))
            {
                ModelState.AddModelError("StartNo", "起號非8位整數!!");
            }
            else if (!viewModel.EndNo.HasValue || !(viewModel.EndNo >= 0 && viewModel.EndNo < 100000000))
            {
                ModelState.AddModelError("EndNo", "迄號非8位整數!!");
            }
            else if (viewModel.EndNo <= viewModel.StartNo || ((viewModel.EndNo - viewModel.StartNo + 1) % 50 != 0))
            {
                ModelState.AddModelError("StartNo", "不符號碼大小順序與差距為50之倍數原則!!");
            }
            else
            {
                if (item != null)
                {
                    if (item.InvoiceNoAssignment.Count > 0)
                    {
                        ModelState.AddModelError("StartNo", "該區間之號碼已經被使用,不可修改!!!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0 &&
                                       t.SellerID == item.SellerID))
                    {
                        ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法修改!!");
                    }
                    else if (table.Any(t => t.IntervalID != item.IntervalID && t.TrackID == item.TrackID &&
                                       ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                    {
                        ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                    }
                }
                else
                {
                    if (trackCode != null)
                    {
                        if (table.Any(t => t.TrackID == trackCode.TrackID && t.StartNo >= viewModel.EndNo && t.InvoiceNoAssignment.Count > 0 &&
                                      t.SellerID == viewModel.BranchID))
                        {
                            ModelState.AddModelError("StartNo", "違反序時序號原則該區段無法新增!!");
                        }
                        else if (table.Any(t => t.TrackID == trackCode.TrackID &&
                                           ((t.EndNo <= viewModel.EndNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.StartNo >= viewModel.StartNo) || (t.StartNo <= viewModel.StartNo && t.EndNo >= viewModel.StartNo) || (t.StartNo <= viewModel.EndNo && t.EndNo >= viewModel.EndNo))))
                        {
                            ModelState.AddModelError("StartNo", "系統中已存在重疊的區段!!");
                        }
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                ViewBag.ModelState = ModelState;
                return(View("~/Views/Shared/ReportInputError.ascx"));
            }

            if (item == null)
            {
                if (trackCode == null)
                {
                    trackCode = new InvoiceTrackCode
                    {
                        TrackCode = viewModel.TrackCode,
                        PeriodNo  = viewModel.PeriodNo.Value,
                        Year      = viewModel.Year.Value
                    };
                    models.GetTable <InvoiceTrackCode>().InsertOnSubmit(trackCode);
                }

                var codeAssignment = trackCode.InvoiceTrackCodeAssignment.Where(t => t.SellerID == viewModel.BranchID).FirstOrDefault();
                if (codeAssignment == null)
                {
                    codeAssignment = new InvoiceTrackCodeAssignment
                    {
                        SellerID         = viewModel.BranchID.Value,
                        InvoiceTrackCode = trackCode
                    };

                    trackCode.InvoiceTrackCodeAssignment.Add(codeAssignment);
                }

                item = new InvoiceNoInterval {
                };
                codeAssignment.InvoiceNoInterval.Add(item);
            }

            item.StartNo = viewModel.StartNo.Value;
            item.EndNo   = viewModel.EndNo.Value;

            try
            {
                models.SubmitChanges();
                return(Json(new { result = true, item.IntervalID }));
            }
            catch (Exception ex)
            {
                ApplicationLogging.CreateLogger <InvoiceController>().LogError(ex, ex.Message);
                return(Json(new { result = false, message = ex.Message }));
            }
        }
        public ActionResult DownloadVacantNoCsv(InvoiceNoViewModel viewModel)
        {
            List <InquireVacantNoResult> items = models.DataContext.InquireVacantNo(viewModel.BranchID, viewModel.Year, viewModel.PeriodNo).ToList();

            return(View("~/Views/Invoice/Module/DownloadVacantNoCsv.ascx", items));
        }